2019-01-09 17:59:46 +00:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-06-20 02:07:01 +00:00
|
|
|
use crate::deno_error::err_check;
|
|
|
|
use crate::deno_error::DenoError;
|
2019-06-04 13:03:56 +00:00
|
|
|
use crate::diagnostics::Diagnostic;
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::msg;
|
|
|
|
use crate::resources;
|
2019-03-19 00:03:37 +00:00
|
|
|
use crate::startup_data;
|
2019-04-09 17:11:25 +00:00
|
|
|
use crate::state::*;
|
2019-04-05 04:04:06 +00:00
|
|
|
use crate::tokio_util;
|
2019-04-08 21:10:00 +00:00
|
|
|
use crate::worker::Worker;
|
2019-03-30 23:30:40 +00:00
|
|
|
use deno::Buf;
|
2019-01-09 17:59:46 +00:00
|
|
|
use futures::Future;
|
2019-04-04 09:33:32 +00:00
|
|
|
use futures::Stream;
|
2019-02-18 15:42:15 +00:00
|
|
|
use std::str;
|
2019-04-04 09:33:32 +00:00
|
|
|
use std::sync::atomic::Ordering;
|
2019-01-09 17:59:46 +00:00
|
|
|
|
|
|
|
// This corresponds to JS ModuleMetaData.
|
|
|
|
// TODO Rename one or the other so they correspond.
|
2019-04-01 19:09:59 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-02-18 15:42:15 +00:00
|
|
|
pub struct ModuleMetaData {
|
2019-01-09 17:59:46 +00:00
|
|
|
pub module_name: String,
|
2019-04-02 01:46:40 +00:00
|
|
|
pub module_redirect_source_name: Option<String>, // source of redirect
|
2019-01-09 17:59:46 +00:00
|
|
|
pub filename: String,
|
|
|
|
pub media_type: msg::MediaType,
|
2019-02-18 15:42:15 +00:00
|
|
|
pub source_code: Vec<u8>,
|
2019-02-02 06:28:31 +00:00
|
|
|
pub maybe_output_code_filename: Option<String>,
|
2019-02-18 15:42:15 +00:00
|
|
|
pub maybe_output_code: Option<Vec<u8>>,
|
2019-02-02 06:28:31 +00:00
|
|
|
pub maybe_source_map_filename: Option<String>,
|
2019-02-18 15:42:15 +00:00
|
|
|
pub maybe_source_map: Option<Vec<u8>>,
|
2019-01-09 17:59:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 15:42:15 +00:00
|
|
|
impl ModuleMetaData {
|
2019-03-28 20:05:41 +00:00
|
|
|
pub fn has_output_code_and_source_map(&self) -> bool {
|
|
|
|
self.maybe_output_code.is_some() && self.maybe_source_map.is_some()
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:06:25 +00:00
|
|
|
pub fn js_source(&self) -> String {
|
2019-01-09 17:59:46 +00:00
|
|
|
if self.media_type == msg::MediaType::Json {
|
2019-02-18 15:42:15 +00:00
|
|
|
return format!(
|
|
|
|
"export default {};",
|
|
|
|
str::from_utf8(&self.source_code).unwrap()
|
|
|
|
);
|
2019-01-09 17:59:46 +00:00
|
|
|
}
|
|
|
|
match self.maybe_output_code {
|
2019-02-18 15:42:15 +00:00
|
|
|
None => str::from_utf8(&self.source_code).unwrap().to_string(),
|
|
|
|
Some(ref output_code) => str::from_utf8(output_code).unwrap().to_string(),
|
2019-01-09 17:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:06:57 +00:00
|
|
|
type CompilerConfig = Option<(String, Vec<u8>)>;
|
2019-04-08 21:10:00 +00:00
|
|
|
|
2019-05-20 16:06:57 +00:00
|
|
|
/// Creates the JSON message send to compiler.ts's onmessage.
|
2019-06-08 18:42:28 +00:00
|
|
|
fn req(
|
|
|
|
root_names: Vec<String>,
|
|
|
|
compiler_config: CompilerConfig,
|
|
|
|
bundle: Option<String>,
|
|
|
|
) -> Buf {
|
2019-05-20 16:06:57 +00:00
|
|
|
let j = if let Some((config_path, config_data)) = compiler_config {
|
|
|
|
json!({
|
|
|
|
"rootNames": root_names,
|
2019-06-08 18:42:28 +00:00
|
|
|
"bundle": bundle,
|
2019-05-20 16:06:57 +00:00
|
|
|
"configPath": config_path,
|
|
|
|
"config": str::from_utf8(&config_data).unwrap(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
json!({
|
|
|
|
"rootNames": root_names,
|
2019-06-08 18:42:28 +00:00
|
|
|
"bundle": bundle,
|
2019-05-20 16:06:57 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
j.to_string().into_boxed_str().into_boxed_bytes()
|
2019-01-09 17:59:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:58:31 +00:00
|
|
|
/// Returns an optional tuple which represents the state of the compiler
|
|
|
|
/// configuration where the first is canonical name for the configuration file
|
|
|
|
/// and a vector of the bytes of the contents of the configuration file.
|
|
|
|
pub fn get_compiler_config(
|
|
|
|
parent_state: &ThreadSafeState,
|
|
|
|
_compiler_type: &str,
|
2019-05-20 16:06:57 +00:00
|
|
|
) -> CompilerConfig {
|
2019-04-29 14:58:31 +00:00
|
|
|
// The compiler type is being passed to make it easier to implement custom
|
|
|
|
// compilers in the future.
|
|
|
|
match (&parent_state.config_path, &parent_state.config) {
|
|
|
|
(Some(config_path), Some(config)) => {
|
|
|
|
Some((config_path.to_string(), config.to_vec()))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:42:28 +00:00
|
|
|
pub fn bundle_async(
|
|
|
|
state: ThreadSafeState,
|
|
|
|
module_name: String,
|
|
|
|
out_file: String,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> impl Future<Item = (), Error = DenoError> {
|
2019-06-08 18:42:28 +00:00
|
|
|
debug!(
|
|
|
|
"Invoking the compiler to bundle. module_name: {}",
|
|
|
|
module_name
|
|
|
|
);
|
|
|
|
|
|
|
|
let root_names = vec![module_name.clone()];
|
|
|
|
let compiler_config = get_compiler_config(&state, "typescript");
|
|
|
|
let req_msg = req(root_names, compiler_config, Some(out_file));
|
|
|
|
|
|
|
|
// Count how many times we start the compiler worker.
|
|
|
|
state.metrics.compiler_starts.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
|
|
|
let mut worker = Worker::new(
|
|
|
|
"TS".to_string(),
|
|
|
|
startup_data::compiler_isolate_init(),
|
|
|
|
// TODO(ry) Maybe we should use a separate state for the compiler.
|
|
|
|
// as was done previously.
|
|
|
|
state.clone(),
|
|
|
|
);
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(worker.execute("denoMain()"));
|
|
|
|
err_check(worker.execute("workerMain()"));
|
|
|
|
err_check(worker.execute("compilerMain()"));
|
2019-06-08 18:42:28 +00:00
|
|
|
|
|
|
|
let resource = worker.state.resource.clone();
|
|
|
|
let compiler_rid = resource.rid;
|
|
|
|
let first_msg_fut = resources::post_message_to_worker(compiler_rid, req_msg)
|
|
|
|
.then(move |_| worker)
|
|
|
|
.then(move |result| {
|
|
|
|
if let Err(err) = result {
|
|
|
|
// TODO(ry) Need to forward the error instead of exiting.
|
|
|
|
eprintln!("{}", err.to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
debug!("Sent message to worker");
|
|
|
|
let stream_future =
|
|
|
|
resources::get_message_stream_from_worker(compiler_rid).into_future();
|
|
|
|
stream_future.map(|(f, _rest)| f).map_err(|(f, _rest)| f)
|
|
|
|
});
|
|
|
|
|
|
|
|
first_msg_fut.map_err(|_| panic!("not handled")).and_then(
|
|
|
|
move |maybe_msg: Option<Buf>| {
|
|
|
|
debug!("Received message from worker");
|
|
|
|
|
|
|
|
if let Some(msg) = maybe_msg {
|
|
|
|
let json_str = std::str::from_utf8(&msg).unwrap();
|
|
|
|
debug!("Message: {}", json_str);
|
|
|
|
if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) {
|
2019-06-20 02:07:01 +00:00
|
|
|
return Err(DenoError::from(diagnostics));
|
2019-06-08 18:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-05 04:04:06 +00:00
|
|
|
pub fn compile_async(
|
2019-05-20 16:06:57 +00:00
|
|
|
state: ThreadSafeState,
|
2019-02-18 15:42:15 +00:00
|
|
|
module_meta_data: &ModuleMetaData,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> impl Future<Item = ModuleMetaData, Error = DenoError> {
|
2019-06-07 00:37:26 +00:00
|
|
|
let module_name = module_meta_data.module_name.clone();
|
|
|
|
|
2019-04-02 03:24:07 +00:00
|
|
|
debug!(
|
2019-06-07 00:37:26 +00:00
|
|
|
"Running rust part of compile_sync. module_name: {}",
|
|
|
|
&module_name
|
2019-04-02 03:24:07 +00:00
|
|
|
);
|
|
|
|
|
2019-06-07 00:37:26 +00:00
|
|
|
let root_names = vec![module_name.clone()];
|
2019-05-20 16:06:57 +00:00
|
|
|
let compiler_config = get_compiler_config(&state, "typescript");
|
2019-06-08 18:42:28 +00:00
|
|
|
let req_msg = req(root_names, compiler_config, None);
|
2019-05-20 16:06:57 +00:00
|
|
|
|
|
|
|
// Count how many times we start the compiler worker.
|
|
|
|
state.metrics.compiler_starts.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
|
|
|
let mut worker = Worker::new(
|
|
|
|
"TS".to_string(),
|
|
|
|
startup_data::compiler_isolate_init(),
|
|
|
|
// TODO(ry) Maybe we should use a separate state for the compiler.
|
|
|
|
// as was done previously.
|
|
|
|
state.clone(),
|
|
|
|
);
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(worker.execute("denoMain()"));
|
|
|
|
err_check(worker.execute("workerMain()"));
|
|
|
|
err_check(worker.execute("compilerMain()"));
|
2019-04-01 19:09:59 +00:00
|
|
|
|
2019-06-07 00:37:26 +00:00
|
|
|
let compiling_job = state.progress.add(format!("Compiling {}", module_name));
|
2019-05-11 14:23:19 +00:00
|
|
|
|
2019-05-20 16:06:57 +00:00
|
|
|
let resource = worker.state.resource.clone();
|
|
|
|
let compiler_rid = resource.rid;
|
|
|
|
let first_msg_fut = resources::post_message_to_worker(compiler_rid, req_msg)
|
|
|
|
.then(move |_| worker)
|
|
|
|
.then(move |result| {
|
|
|
|
if let Err(err) = result {
|
|
|
|
// TODO(ry) Need to forward the error instead of exiting.
|
|
|
|
eprintln!("{}", err.to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
debug!("Sent message to worker");
|
|
|
|
let stream_future =
|
|
|
|
resources::get_message_stream_from_worker(compiler_rid).into_future();
|
|
|
|
stream_future.map(|(f, _rest)| f).map_err(|(f, _rest)| f)
|
|
|
|
});
|
|
|
|
|
|
|
|
first_msg_fut
|
|
|
|
.map_err(|_| panic!("not handled"))
|
|
|
|
.and_then(move |maybe_msg: Option<Buf>| {
|
|
|
|
debug!("Received message from worker");
|
|
|
|
|
2019-06-04 13:03:56 +00:00
|
|
|
if let Some(msg) = maybe_msg {
|
|
|
|
let json_str = std::str::from_utf8(&msg).unwrap();
|
|
|
|
debug!("Message: {}", json_str);
|
|
|
|
if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) {
|
2019-06-20 02:07:01 +00:00
|
|
|
return Err(DenoError::from(diagnostics));
|
2019-06-04 13:03:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-20 16:06:57 +00:00
|
|
|
|
2019-06-08 11:10:12 +00:00
|
|
|
Ok(())
|
|
|
|
}).and_then(move |_| {
|
|
|
|
state.dir.fetch_module_meta_data_async(
|
|
|
|
&module_name,
|
|
|
|
".",
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
).map_err(|e| {
|
|
|
|
// TODO(95th) Instead of panicking, We could translate this error to Diagnostic.
|
|
|
|
panic!("{}", e)
|
|
|
|
})
|
|
|
|
}).and_then(move |module_meta_data_after_compile| {
|
2019-05-20 16:06:57 +00:00
|
|
|
// Explicit drop to keep reference alive until future completes.
|
|
|
|
drop(compiling_job);
|
2019-05-11 14:23:19 +00:00
|
|
|
|
2019-05-20 16:06:57 +00:00
|
|
|
Ok(module_meta_data_after_compile)
|
|
|
|
}).then(move |r| {
|
|
|
|
// TODO(ry) do this in worker's destructor.
|
|
|
|
// resource.close();
|
|
|
|
r
|
2019-04-05 04:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compile_sync(
|
2019-05-20 16:06:57 +00:00
|
|
|
state: ThreadSafeState,
|
2019-04-05 04:04:06 +00:00
|
|
|
module_meta_data: &ModuleMetaData,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> Result<ModuleMetaData, DenoError> {
|
2019-06-07 00:37:26 +00:00
|
|
|
tokio_util::block_on(compile_async(state, module_meta_data))
|
2019-01-09 17:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_compile_sync() {
|
2019-04-05 04:04:06 +00:00
|
|
|
tokio_util::init(|| {
|
|
|
|
let specifier = "./tests/002_hello.ts";
|
2019-06-12 23:55:59 +00:00
|
|
|
use deno::ModuleSpecifier;
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_name = ModuleSpecifier::resolve_root(specifier)
|
2019-05-20 16:06:57 +00:00
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
2019-04-05 04:04:06 +00:00
|
|
|
|
|
|
|
let mut out = ModuleMetaData {
|
2019-05-20 16:06:57 +00:00
|
|
|
module_name,
|
2019-04-05 04:04:06 +00:00
|
|
|
module_redirect_source_name: None,
|
|
|
|
filename: "/tests/002_hello.ts".to_owned(),
|
|
|
|
media_type: msg::MediaType::TypeScript,
|
|
|
|
source_code: include_bytes!("../tests/002_hello.ts").to_vec(),
|
|
|
|
maybe_output_code_filename: None,
|
|
|
|
maybe_output_code: None,
|
|
|
|
maybe_source_map_filename: None,
|
|
|
|
maybe_source_map: None,
|
|
|
|
};
|
|
|
|
|
2019-06-08 18:42:28 +00:00
|
|
|
out = compile_sync(
|
|
|
|
ThreadSafeState::mock(vec![
|
|
|
|
String::from("./deno"),
|
|
|
|
String::from("hello.js"),
|
|
|
|
]),
|
|
|
|
&out,
|
|
|
|
).unwrap();
|
2019-04-05 04:04:06 +00:00
|
|
|
assert!(
|
|
|
|
out
|
|
|
|
.maybe_output_code
|
|
|
|
.unwrap()
|
|
|
|
.starts_with("console.log(\"Hello World\");".as_bytes())
|
|
|
|
);
|
|
|
|
})
|
2019-04-04 09:33:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:58:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_get_compiler_config_no_flag() {
|
|
|
|
let compiler_type = "typescript";
|
2019-06-08 18:42:28 +00:00
|
|
|
let state = ThreadSafeState::mock(vec![
|
|
|
|
String::from("./deno"),
|
|
|
|
String::from("hello.js"),
|
|
|
|
]);
|
2019-04-29 14:58:31 +00:00
|
|
|
let out = get_compiler_config(&state, compiler_type);
|
|
|
|
assert_eq!(out, None);
|
|
|
|
}
|
2019-06-08 18:42:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bundle_async() {
|
|
|
|
let specifier = "./tests/002_hello.ts";
|
2019-06-12 23:55:59 +00:00
|
|
|
use deno::ModuleSpecifier;
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_name = ModuleSpecifier::resolve_root(specifier)
|
2019-06-08 18:42:28 +00:00
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let state = ThreadSafeState::mock(vec![
|
|
|
|
String::from("./deno"),
|
|
|
|
String::from("./tests/002_hello.ts"),
|
|
|
|
String::from("$deno$/bundle.js"),
|
|
|
|
]);
|
|
|
|
let out =
|
|
|
|
bundle_async(state, module_name, String::from("$deno$/bundle.js"));
|
2019-06-20 02:07:01 +00:00
|
|
|
assert!(tokio_util::block_on(out).is_ok());
|
2019-06-08 18:42:28 +00:00
|
|
|
}
|
2019-01-09 17:59:46 +00:00
|
|
|
}
|