summaryrefslogtreecommitdiff
path: root/cli/lib.rs
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2019-10-19 14:19:19 -0700
committerRy Dahl <ry@tinyclouds.org>2019-10-19 17:19:19 -0400
commiteff2a27bd02f8987e904907ae6ebb6cb9c07944b (patch)
tree7f20c97207fe4ec4fca4be5871a40645e073fb1d /cli/lib.rs
parent4ae1838a6ebadbe156af101e298ffb8b198cc238 (diff)
feat: Allow "deno eval" to run code as module (#3148)
Diffstat (limited to 'cli/lib.rs')
-rw-r--r--cli/lib.rs51
1 files changed, 28 insertions, 23 deletions
diff --git a/cli/lib.rs b/cli/lib.rs
index 3c093cda4..5e416c6ac 100644
--- a/cli/lib.rs
+++ b/cli/lib.rs
@@ -241,7 +241,7 @@ fn info_command(flags: DenoFlags, argv: Vec<String>) {
debug!("main_module {}", main_module);
worker
- .execute_mod_async(&main_module, true)
+ .execute_mod_async(&main_module, None, true)
.map_err(print_err_and_exit)
.and_then(move |()| print_file_info(worker, &main_module))
.and_then(|worker| {
@@ -263,36 +263,41 @@ fn fetch_command(flags: DenoFlags, argv: Vec<String>) {
js_check(worker.execute("denoMain()"));
debug!("main_module {}", main_module);
- worker.execute_mod_async(&main_module, true).then(|result| {
- js_check(result);
- Ok(())
- })
+ worker
+ .execute_mod_async(&main_module, None, true)
+ .then(|result| {
+ js_check(result);
+ Ok(())
+ })
});
tokio_util::run(main_future);
}
fn eval_command(flags: DenoFlags, argv: Vec<String>) {
let (mut worker, state) = create_worker_and_state(flags, argv);
- // Wrap provided script in async function so asynchronous methods
- // work. This is required until top-level await is not supported.
- let js_source = format!(
- "async function _topLevelWrapper(){{
- {}
- }}
- _topLevelWrapper();
- ",
- &state.argv[1]
- );
+ let ts_source = state.argv[1].clone();
+ // Force TypeScript compile.
+ let main_module =
+ ModuleSpecifier::resolve_url_or_path("./__$deno$eval.ts").unwrap();
let main_future = lazy(move || {
js_check(worker.execute("denoMain()"));
- // ATM imports in `deno eval` are not allowed
- // TODO Support ES modules once Worker supports evaluating anonymous modules.
- js_check(worker.execute(&js_source));
- worker.then(|result| {
- js_check(result);
- Ok(())
- })
+ debug!("main_module {}", &main_module);
+
+ let mut worker_ = worker.clone();
+ worker
+ .execute_mod_async(&main_module, Some(ts_source), false)
+ .and_then(move |()| {
+ js_check(worker.execute("window.dispatchEvent(new Event('load'))"));
+ worker.then(move |result| {
+ js_check(result);
+ js_check(
+ worker_.execute("window.dispatchEvent(new Event('unload'))"),
+ );
+ Ok(())
+ })
+ })
+ .map_err(print_err_and_exit)
});
tokio_util::run(main_future);
}
@@ -356,7 +361,7 @@ fn run_script(flags: DenoFlags, argv: Vec<String>) {
let mut worker_ = worker.clone();
worker
- .execute_mod_async(&main_module, false)
+ .execute_mod_async(&main_module, None, false)
.and_then(move |()| {
js_check(worker.execute("window.dispatchEvent(new Event('load'))"));
worker.then(move |result| {