2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
use crate::fmt_errors::PrettyJsError;
|
2020-04-03 13:40:11 -04:00
|
|
|
use crate::inspector::DenoInspector;
|
2020-10-11 07:20:40 -04:00
|
|
|
use crate::inspector::InspectorSession;
|
2020-09-11 09:18:49 -04:00
|
|
|
use crate::js;
|
2020-09-18 14:39:47 -04:00
|
|
|
use crate::metrics::Metrics;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::module_loader::CliModuleLoader;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2020-11-19 14:37:22 -05:00
|
|
|
use crate::source_maps::apply_source_map;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-10-11 07:20:40 -04:00
|
|
|
use deno_core::futures::future::poll_fn;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::FutureExt;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2020-12-01 17:33:44 -05:00
|
|
|
use deno_core::JsErrorCreateFn;
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-02-18 10:08:18 -05:00
|
|
|
use deno_core::ModuleId;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-11 09:18:49 -04:00
|
|
|
use deno_core::RuntimeOptions;
|
2019-08-09 19:33:59 -04:00
|
|
|
use std::env;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::rc::Rc;
|
2019-06-05 16:35:38 -04:00
|
|
|
use std::sync::Arc;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
/// This worker is created and used by almost all
|
|
|
|
/// subcommands in Deno executable.
|
2020-01-21 11:50:06 -05:00
|
|
|
///
|
2020-11-26 09:17:45 -05:00
|
|
|
/// It provides ops available in the `Deno` namespace.
|
2020-01-21 11:50:06 -05:00
|
|
|
///
|
2020-11-26 09:17:45 -05:00
|
|
|
/// All `WebWorker`s created during program execution
|
|
|
|
/// are descendants of this worker.
|
|
|
|
pub struct MainWorker {
|
2020-10-11 07:20:40 -04:00
|
|
|
inspector: Option<Box<DenoInspector>>,
|
2020-11-26 09:17:45 -05:00
|
|
|
js_runtime: JsRuntime,
|
2020-09-19 19:17:35 -04:00
|
|
|
should_break_on_first_statement: bool,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
impl MainWorker {
|
2020-08-18 12:30:13 -04:00
|
|
|
pub fn new(
|
2020-11-26 09:17:45 -05:00
|
|
|
program_state: &Arc<ProgramState>,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
2020-08-18 12:30:13 -04:00
|
|
|
) -> Self {
|
2020-12-07 05:03:03 -05:00
|
|
|
let module_loader = CliModuleLoader::new(program_state.clone());
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2020-12-01 17:33:44 -05:00
|
|
|
let global_state_ = program_state.clone();
|
|
|
|
|
|
|
|
let js_error_create_fn = Box::new(move |core_js_error| {
|
|
|
|
let source_mapped_error =
|
|
|
|
apply_source_map(&core_js_error, global_state_.clone());
|
|
|
|
PrettyJsError::create(source_mapped_error)
|
|
|
|
});
|
|
|
|
|
|
|
|
Self::from_options(
|
|
|
|
program_state,
|
|
|
|
main_module,
|
|
|
|
permissions,
|
|
|
|
module_loader,
|
|
|
|
Some(js_error_create_fn),
|
|
|
|
)
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_options(
|
|
|
|
program_state: &Arc<ProgramState>,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
|
|
|
module_loader: Rc<dyn ModuleLoader>,
|
2020-12-01 17:33:44 -05:00
|
|
|
js_error_create_fn: Option<Box<JsErrorCreateFn>>,
|
2020-11-30 14:35:12 -05:00
|
|
|
) -> Self {
|
2020-12-01 17:33:44 -05:00
|
|
|
// TODO(bartlomieju): this is hacky way to not apply source
|
|
|
|
// maps in JS
|
|
|
|
let apply_source_maps = js_error_create_fn.is_some();
|
2020-11-21 09:56:14 -05:00
|
|
|
|
2020-10-07 11:20:20 -04:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2020-09-28 06:14:11 -04:00
|
|
|
module_loader: Some(module_loader),
|
2020-11-26 09:17:45 -05:00
|
|
|
startup_snapshot: Some(js::deno_isolate_init()),
|
2020-12-01 17:33:44 -05:00
|
|
|
js_error_create_fn,
|
2020-11-21 09:56:14 -05:00
|
|
|
get_error_class_fn: Some(&crate::errors::get_error_class_name),
|
2020-09-11 09:18:49 -04:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
let inspector =
|
2020-10-13 07:35:35 -04:00
|
|
|
if let Some(inspector_server) = &program_state.maybe_inspector_server {
|
2020-09-26 12:16:33 -04:00
|
|
|
Some(DenoInspector::new(
|
2020-10-07 11:20:20 -04:00
|
|
|
&mut js_runtime,
|
2020-09-26 12:16:33 -04:00
|
|
|
Some(inspector_server.clone()),
|
|
|
|
))
|
2020-10-13 07:35:35 -04:00
|
|
|
} else if program_state.flags.coverage || program_state.flags.repl {
|
2020-10-07 11:20:20 -04:00
|
|
|
Some(DenoInspector::new(&mut js_runtime, None))
|
2020-09-26 12:16:33 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-11-09 15:07:14 -05:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
let should_break_on_first_statement =
|
|
|
|
inspector.is_some() && program_state.flags.inspect_brk.is_some();
|
2020-02-05 02:40:38 -05:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
let mut worker = Self {
|
2020-05-24 16:28:19 -04:00
|
|
|
inspector,
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime,
|
2020-09-19 19:17:35 -04:00
|
|
|
should_break_on_first_statement,
|
2020-11-26 09:17:45 -05:00
|
|
|
};
|
2020-01-21 11:50:06 -05:00
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
let js_runtime = &mut worker.js_runtime;
|
2020-01-21 11:50:06 -05:00
|
|
|
{
|
2020-09-28 06:14:11 -04:00
|
|
|
// All ops registered in this function depend on these
|
|
|
|
{
|
2020-10-11 07:20:40 -04:00
|
|
|
let op_state = js_runtime.op_state();
|
2020-09-28 06:14:11 -04:00
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
op_state.put::<Metrics>(Default::default());
|
2020-10-13 07:35:35 -04:00
|
|
|
op_state.put::<Arc<ProgramState>>(program_state.clone());
|
2020-11-02 17:37:55 -05:00
|
|
|
op_state.put::<Permissions>(permissions);
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 17:33:44 -05:00
|
|
|
ops::runtime::init(js_runtime, main_module, apply_source_maps);
|
2020-10-13 07:35:35 -04:00
|
|
|
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::timers::init(js_runtime);
|
2020-10-20 00:05:42 -04:00
|
|
|
ops::worker_host::init(js_runtime, None);
|
2020-11-13 16:01:57 -05:00
|
|
|
ops::crypto::init(js_runtime, program_state.flags.seed);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
|
|
|
|
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
|
2020-09-17 13:13:20 -04:00
|
|
|
ops::reg_json_sync(
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime,
|
2020-09-17 13:13:20 -04:00
|
|
|
"op_domain_to_ascii",
|
|
|
|
deno_web::op_domain_to_ascii,
|
|
|
|
);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::errors::init(js_runtime);
|
|
|
|
ops::fs_events::init(js_runtime);
|
|
|
|
ops::fs::init(js_runtime);
|
|
|
|
ops::io::init(js_runtime);
|
|
|
|
ops::net::init(js_runtime);
|
|
|
|
ops::os::init(js_runtime);
|
|
|
|
ops::permissions::init(js_runtime);
|
|
|
|
ops::plugin::init(js_runtime);
|
|
|
|
ops::process::init(js_runtime);
|
|
|
|
ops::runtime_compiler::init(js_runtime);
|
|
|
|
ops::signal::init(js_runtime);
|
|
|
|
ops::tls::init(js_runtime);
|
|
|
|
ops::tty::init(js_runtime);
|
|
|
|
ops::websocket::init(js_runtime);
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-06-01 22:44:17 -04:00
|
|
|
{
|
2020-10-11 07:20:40 -04:00
|
|
|
let op_state = js_runtime.op_state();
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
let t = &mut op_state.resource_table;
|
2020-12-06 22:30:40 -05:00
|
|
|
let (stdin, stdout, stderr) = ops::io::get_stdio();
|
2020-07-09 15:06:51 -04:00
|
|
|
if let Some(stream) = stdin {
|
|
|
|
t.add("stdin", Box::new(stream));
|
|
|
|
}
|
|
|
|
if let Some(stream) = stdout {
|
|
|
|
t.add("stdout", Box::new(stream));
|
|
|
|
}
|
|
|
|
if let Some(stream) = stderr {
|
|
|
|
t.add("stderr", Box::new(stream));
|
|
|
|
}
|
2020-06-01 22:44:17 -04:00
|
|
|
}
|
2020-09-26 12:16:33 -04:00
|
|
|
worker
|
|
|
|
.execute("bootstrap.mainRuntime()")
|
|
|
|
.expect("Failed to execute bootstrap script");
|
2020-11-26 09:17:45 -05:00
|
|
|
worker
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-09-28 06:14:11 -04:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
/// Same as execute2() but the filename defaults to "$CWD/__anonymous__".
|
|
|
|
pub fn execute(&mut self, js_source: &str) -> Result<(), AnyError> {
|
|
|
|
let path = env::current_dir().unwrap().join("__anonymous__");
|
|
|
|
let url = Url::from_file_path(path).unwrap();
|
|
|
|
self.js_runtime.execute(url.as_str(), js_source)
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
/// Loads and instantiates specified JavaScript module.
|
|
|
|
pub async fn preload_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleId, AnyError> {
|
|
|
|
self.js_runtime.load_module(module_specifier, None).await
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
pub async fn execute_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let id = self.preload_module(module_specifier).await?;
|
|
|
|
self.wait_for_inspector_session();
|
|
|
|
self.js_runtime.mod_evaluate(id).await
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
fn wait_for_inspector_session(&mut self) {
|
|
|
|
if self.should_break_on_first_statement {
|
|
|
|
self
|
|
|
|
.inspector
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.wait_for_session_and_break_on_next_statement()
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
/// Create new inspector session. This function panics if Worker
|
|
|
|
/// was not configured to create inspector.
|
|
|
|
pub fn create_inspector_session(&mut self) -> Box<InspectorSession> {
|
|
|
|
let inspector = self.inspector.as_mut().unwrap();
|
2020-09-28 06:14:11 -04:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
InspectorSession::new(&mut **inspector)
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
pub fn poll_event_loop(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Result<(), AnyError>> {
|
2020-11-26 09:17:45 -05:00
|
|
|
// We always poll the inspector if it exists.
|
|
|
|
let _ = self.inspector.as_mut().map(|i| i.poll_unpin(cx));
|
|
|
|
self.js_runtime.poll_event_loop(cx)
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
pub async fn run_event_loop(&mut self) -> Result<(), AnyError> {
|
|
|
|
poll_fn(|cx| self.poll_event_loop(cx)).await
|
2020-10-11 07:20:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
impl Drop for MainWorker {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// The Isolate object must outlive the Inspector object, but this is
|
|
|
|
// currently not enforced by the type system.
|
|
|
|
self.inspector.take();
|
2020-10-11 07:20:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-09-26 12:16:33 -04:00
|
|
|
use crate::flags::DenoSubcommand;
|
|
|
|
use crate::flags::Flags;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
fn create_test_worker() -> MainWorker {
|
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
|
|
|
|
let flags = Flags {
|
|
|
|
subcommand: DenoSubcommand::Run {
|
|
|
|
script: main_module.to_string(),
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state =
|
|
|
|
ProgramState::mock(vec!["deno".to_string()], Some(flags));
|
2020-11-02 17:37:55 -05:00
|
|
|
MainWorker::new(&program_state, main_module, permissions)
|
2020-09-26 12:16:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_esm_imports_a() {
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2020-02-02 16:55:22 -05:00
|
|
|
.join("cli/tests/esm_imports_a.js");
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 12:16:33 -04:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Err(e) = worker.run_event_loop().await {
|
2020-09-26 12:16:33 -04:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_circular() {
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-12-23 09:59:44 -05:00
|
|
|
.join("tests/circular1.ts");
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 12:16:33 -04:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Err(e) = worker.run_event_loop().await {
|
2020-09-26 12:16:33 -04:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-01-01 06:24:05 -05:00
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_006_url_imports() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-12-23 09:59:44 -05:00
|
|
|
.join("cli/tests/006_url_imports.ts");
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 12:16:33 -04:00
|
|
|
let mut worker = create_test_worker();
|
2020-02-18 10:08:18 -05:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Err(e) = worker.run_event_loop().await {
|
2020-02-03 18:08:44 -05:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:29:28 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_resolve_error() {
|
|
|
|
// "foo" is not a valid module specifier so this should return an error.
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
assert!(result.is_err());
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:29:28 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_002_hello() {
|
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("cli/tests/002_hello.ts");
|
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|