mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
minor clean ups in TS compiler (#3394)
This commit is contained in:
parent
8d977d0117
commit
363b968bfc
3 changed files with 92 additions and 141 deletions
|
@ -2,7 +2,7 @@
|
||||||
use crate::compilers::CompiledModule;
|
use crate::compilers::CompiledModule;
|
||||||
use crate::compilers::CompiledModuleFuture;
|
use crate::compilers::CompiledModuleFuture;
|
||||||
use crate::file_fetcher::SourceFile;
|
use crate::file_fetcher::SourceFile;
|
||||||
use crate::futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@ use deno::Buf;
|
||||||
use deno::ErrBox;
|
use deno::ErrBox;
|
||||||
use deno::ModuleSpecifier;
|
use deno::ModuleSpecifier;
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
use futures::future::TryFutureExt;
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
@ -272,33 +271,22 @@ impl TsCompiler {
|
||||||
|
|
||||||
let worker = TsCompiler::setup_worker(global_state.clone());
|
let worker = TsCompiler::setup_worker(global_state.clone());
|
||||||
let worker_ = worker.clone();
|
let worker_ = worker.clone();
|
||||||
let first_msg_fut = async move {
|
|
||||||
worker.post_message(req_msg).await.unwrap();
|
async move {
|
||||||
let result = worker.await;
|
worker.post_message(req_msg).await?;
|
||||||
if let Err(err) = result {
|
worker.await?;
|
||||||
// TODO(ry) Need to forward the error instead of exiting.
|
|
||||||
eprintln!("{}", err.to_string());
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
debug!("Sent message to worker");
|
debug!("Sent message to worker");
|
||||||
worker_.get_message().await
|
let maybe_msg = worker_.get_message().await?;
|
||||||
};
|
debug!("Received message from worker");
|
||||||
|
if let Some(msg) = maybe_msg {
|
||||||
first_msg_fut.map_err(|_| panic!("not handled")).and_then(
|
let json_str = std::str::from_utf8(&msg).unwrap();
|
||||||
move |maybe_msg: Option<Buf>| {
|
debug!("Message: {}", json_str);
|
||||||
debug!("Received message from worker");
|
if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) {
|
||||||
|
return Err(ErrBox::from(diagnostics));
|
||||||
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) {
|
|
||||||
return futures::future::err(ErrBox::from(diagnostics));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
futures::future::ok(())
|
Ok(())
|
||||||
},
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mark given module URL as compiled to avoid multiple compilations of same module
|
/// Mark given module URL as compiled to avoid multiple compilations of same module
|
||||||
|
@ -382,63 +370,27 @@ impl TsCompiler {
|
||||||
.add("Compile", &module_url.to_string());
|
.add("Compile", &module_url.to_string());
|
||||||
let global_state_ = global_state.clone();
|
let global_state_ = global_state.clone();
|
||||||
|
|
||||||
let first_msg_fut = async move {
|
async move {
|
||||||
worker.post_message(req_msg).await.unwrap();
|
worker.post_message(req_msg).await?;
|
||||||
let result = worker.await;
|
worker.await?;
|
||||||
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");
|
debug!("Sent message to worker");
|
||||||
worker_.get_message().await
|
let maybe_msg = worker_.get_message().await?;
|
||||||
};
|
if let Some(msg) = maybe_msg {
|
||||||
|
let json_str = std::str::from_utf8(&msg).unwrap();
|
||||||
let fut = first_msg_fut
|
debug!("Message: {}", json_str);
|
||||||
.map_err(|_| panic!("not handled"))
|
if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) {
|
||||||
.and_then(move |maybe_msg: Option<Buf>| {
|
return Err(ErrBox::from(diagnostics));
|
||||||
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) {
|
|
||||||
return futures::future::err(ErrBox::from(diagnostics));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
futures::future::ok(())
|
let compiled_module = global_state_
|
||||||
})
|
.ts_compiler
|
||||||
.and_then(move |_| {
|
.get_compiled_module(&source_file_.url)
|
||||||
// if we are this far it means compilation was successful and we can
|
.expect("Expected to find compiled file");
|
||||||
// load compiled filed from disk
|
drop(compiling_job);
|
||||||
futures::future::ready(
|
debug!(">>>>> compile_sync END");
|
||||||
global_state_
|
Ok(compiled_module)
|
||||||
.ts_compiler
|
}
|
||||||
.get_compiled_module(&source_file_.url)
|
.boxed()
|
||||||
.map_err(|e| {
|
|
||||||
// TODO: this situation shouldn't happen
|
|
||||||
panic!(
|
|
||||||
"Expected to find compiled file: {} {}",
|
|
||||||
e, source_file_.url
|
|
||||||
)
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.and_then(move |compiled_module| {
|
|
||||||
// Explicit drop to keep reference alive until future completes.
|
|
||||||
drop(compiling_job);
|
|
||||||
|
|
||||||
futures::future::ok(compiled_module)
|
|
||||||
})
|
|
||||||
.then(move |r| {
|
|
||||||
debug!(">>>>> compile_sync END");
|
|
||||||
// TODO(ry) do this in worker's destructor.
|
|
||||||
// resource.close();
|
|
||||||
futures::future::ready(r)
|
|
||||||
});
|
|
||||||
|
|
||||||
fut.boxed()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get associated `CompiledFileMetadata` for given module if it exists.
|
/// Get associated `CompiledFileMetadata` for given module if it exists.
|
||||||
|
@ -684,20 +636,22 @@ mod tests {
|
||||||
String::from("hello.js"),
|
String::from("hello.js"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
tokio_util::run(
|
let fut = async move {
|
||||||
mock_state
|
let result = mock_state
|
||||||
.ts_compiler
|
.ts_compiler
|
||||||
.compile_async(mock_state.clone(), &out)
|
.compile_async(mock_state.clone(), &out)
|
||||||
.then(|result| {
|
.await;
|
||||||
assert!(result.is_ok());
|
|
||||||
assert!(result
|
assert!(result.is_ok());
|
||||||
.unwrap()
|
assert!(result
|
||||||
.code
|
.unwrap()
|
||||||
.as_bytes()
|
.code
|
||||||
.starts_with("console.log(\"Hello World\");".as_bytes()));
|
.as_bytes()
|
||||||
futures::future::ok(())
|
.starts_with("console.log(\"Hello World\");".as_bytes()));
|
||||||
}),
|
Ok(())
|
||||||
)
|
};
|
||||||
|
|
||||||
|
tokio_util::run(fut.boxed())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -718,19 +672,20 @@ mod tests {
|
||||||
String::from("$deno$/bundle.js"),
|
String::from("$deno$/bundle.js"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
tokio_util::run(
|
let fut = async move {
|
||||||
state
|
let result = state
|
||||||
.ts_compiler
|
.ts_compiler
|
||||||
.bundle_async(
|
.bundle_async(
|
||||||
state.clone(),
|
state.clone(),
|
||||||
module_name,
|
module_name,
|
||||||
Some(String::from("$deno$/bundle.js")),
|
Some(String::from("$deno$/bundle.js")),
|
||||||
)
|
)
|
||||||
.then(|result| {
|
.await;
|
||||||
assert!(result.is_ok());
|
|
||||||
futures::future::ok(())
|
assert!(result.is_ok());
|
||||||
}),
|
Ok(())
|
||||||
)
|
};
|
||||||
|
tokio_util::run(fut.boxed())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -128,17 +128,19 @@ impl Worker {
|
||||||
maybe_code,
|
maybe_code,
|
||||||
loader,
|
loader,
|
||||||
modules,
|
modules,
|
||||||
)
|
);
|
||||||
.get_future(isolate);
|
|
||||||
recursive_load.and_then(move |id| {
|
async move {
|
||||||
|
let id = recursive_load.get_future(isolate).await?;
|
||||||
worker.state.global_state.progress.done();
|
worker.state.global_state.progress.done();
|
||||||
if is_prefetch {
|
|
||||||
futures::future::ok(())
|
if !is_prefetch {
|
||||||
} else {
|
|
||||||
let mut isolate = worker.isolate.lock().unwrap();
|
let mut isolate = worker.isolate.lock().unwrap();
|
||||||
futures::future::ready(isolate.mod_evaluate(id))
|
return isolate.mod_evaluate(id);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Post message to worker as a host.
|
/// Post message to worker as a host.
|
||||||
|
@ -235,15 +237,13 @@ mod tests {
|
||||||
tokio_util::run(async move {
|
tokio_util::run(async move {
|
||||||
let mut worker =
|
let mut worker =
|
||||||
Worker::new("TEST".to_string(), StartupData::None, state, ext);
|
Worker::new("TEST".to_string(), StartupData::None, state, ext);
|
||||||
worker
|
let result = worker
|
||||||
.execute_mod_async(&module_specifier, None, false)
|
.execute_mod_async(&module_specifier, None, false)
|
||||||
.then(|result| {
|
.await;
|
||||||
if let Err(err) = result {
|
if let Err(err) = result {
|
||||||
eprintln!("execute_mod err {:?}", err);
|
eprintln!("execute_mod err {:?}", err);
|
||||||
}
|
}
|
||||||
tokio_util::panic_on_error(worker)
|
tokio_util::panic_on_error(worker).await
|
||||||
})
|
|
||||||
.await
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let metrics = &state_.metrics;
|
let metrics = &state_.metrics;
|
||||||
|
@ -277,15 +277,13 @@ mod tests {
|
||||||
tokio_util::run(async move {
|
tokio_util::run(async move {
|
||||||
let mut worker =
|
let mut worker =
|
||||||
Worker::new("TEST".to_string(), StartupData::None, state, ext);
|
Worker::new("TEST".to_string(), StartupData::None, state, ext);
|
||||||
worker
|
let result = worker
|
||||||
.execute_mod_async(&module_specifier, None, false)
|
.execute_mod_async(&module_specifier, None, false)
|
||||||
.then(|result| {
|
.await;
|
||||||
if let Err(err) = result {
|
if let Err(err) = result {
|
||||||
eprintln!("execute_mod err {:?}", err);
|
eprintln!("execute_mod err {:?}", err);
|
||||||
}
|
}
|
||||||
tokio_util::panic_on_error(worker)
|
tokio_util::panic_on_error(worker).await
|
||||||
})
|
|
||||||
.await
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let metrics = &state_.metrics;
|
let metrics = &state_.metrics;
|
||||||
|
@ -328,15 +326,14 @@ mod tests {
|
||||||
ext,
|
ext,
|
||||||
);
|
);
|
||||||
worker.execute("denoMain()").unwrap();
|
worker.execute("denoMain()").unwrap();
|
||||||
worker
|
let result = worker
|
||||||
.execute_mod_async(&module_specifier, None, false)
|
.execute_mod_async(&module_specifier, None, false)
|
||||||
.then(|result| {
|
.await;
|
||||||
if let Err(err) = result {
|
|
||||||
eprintln!("execute_mod err {:?}", err);
|
if let Err(err) = result {
|
||||||
}
|
eprintln!("execute_mod err {:?}", err);
|
||||||
tokio_util::panic_on_error(worker)
|
}
|
||||||
})
|
tokio_util::panic_on_error(worker).await
|
||||||
.await
|
|
||||||
});
|
});
|
||||||
|
|
||||||
assert_eq!(state_.metrics.resolve_count.load(Ordering::SeqCst), 3);
|
assert_eq!(state_.metrics.resolve_count.load(Ordering::SeqCst), 3);
|
||||||
|
@ -386,14 +383,13 @@ mod tests {
|
||||||
|
|
||||||
let worker_ = worker.clone();
|
let worker_ = worker.clone();
|
||||||
|
|
||||||
tokio::spawn(
|
let fut = async move {
|
||||||
worker
|
let r = worker.await;
|
||||||
.then(move |r| {
|
r.unwrap();
|
||||||
r.unwrap();
|
Ok(())
|
||||||
futures::future::ok(())
|
};
|
||||||
})
|
|
||||||
.compat(),
|
tokio::spawn(fut.boxed().compat());
|
||||||
);
|
|
||||||
|
|
||||||
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue