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-04-03 13:40:11 -04:00
|
|
|
use crate::inspector::DenoInspector;
|
2020-12-11 12:49:26 -05:00
|
|
|
use crate::inspector::InspectorServer;
|
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;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
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-12-11 12:49:26 -05:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_core::GetErrorClassFn;
|
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-12-11 12:49:26 -05:00
|
|
|
pub 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-12-11 12:49:26 -05:00
|
|
|
pub struct WorkerOptions {
|
|
|
|
pub apply_source_maps: bool,
|
2020-12-11 18:36:18 -05:00
|
|
|
/// Sets `Deno.args` in JS runtime.
|
2020-12-11 12:49:26 -05:00
|
|
|
pub args: Vec<String>,
|
|
|
|
pub debug_flag: bool,
|
|
|
|
pub unstable: bool,
|
2021-01-04 18:15:52 -05:00
|
|
|
pub ca_data: Option<Vec<u8>>,
|
2020-12-11 18:36:18 -05:00
|
|
|
pub user_agent: String,
|
2020-12-11 12:49:26 -05:00
|
|
|
pub seed: Option<u64>,
|
|
|
|
pub module_loader: Rc<dyn ModuleLoader>,
|
|
|
|
// Callback that will be invoked when creating new instance
|
|
|
|
// of WebWorker
|
|
|
|
pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>,
|
|
|
|
pub js_error_create_fn: Option<Rc<JsErrorCreateFn>>,
|
|
|
|
pub attach_inspector: bool,
|
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
|
|
|
pub should_break_on_first_statement: bool,
|
2020-12-11 18:36:18 -05:00
|
|
|
/// Sets `Deno.version.deno` in JS runtime.
|
|
|
|
pub runtime_version: String,
|
|
|
|
/// Sets `Deno.version.typescript` in JS runtime.
|
|
|
|
pub ts_version: String,
|
|
|
|
/// Sets `Deno.noColor` in JS runtime.
|
|
|
|
pub no_color: bool,
|
2020-12-13 13:45:53 -05:00
|
|
|
pub get_error_class_fn: Option<GetErrorClassFn>,
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
impl MainWorker {
|
2020-11-30 14:35:12 -05:00
|
|
|
pub fn from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
2020-12-11 12:49:26 -05:00
|
|
|
options: &WorkerOptions,
|
2020-11-30 14:35:12 -05:00
|
|
|
) -> Self {
|
2020-10-07 11:20:20 -04:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2020-12-11 12:49:26 -05:00
|
|
|
module_loader: Some(options.module_loader.clone()),
|
2020-11-26 09:17:45 -05:00
|
|
|
startup_snapshot: Some(js::deno_isolate_init()),
|
2020-12-11 12:49:26 -05:00
|
|
|
js_error_create_fn: options.js_error_create_fn.clone(),
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: options.get_error_class_fn,
|
2020-09-11 09:18:49 -04:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
let inspector = if options.attach_inspector {
|
|
|
|
Some(DenoInspector::new(
|
|
|
|
&mut js_runtime,
|
|
|
|
options.maybe_inspector_server.clone(),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-12-21 08:04:25 -05:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
let should_break_on_first_statement =
|
2020-12-11 12:49:26 -05:00
|
|
|
inspector.is_some() && options.should_break_on_first_statement;
|
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-11-02 17:37:55 -05:00
|
|
|
op_state.put::<Permissions>(permissions);
|
2020-12-11 12:49:26 -05:00
|
|
|
op_state.put::<ops::UnstableChecker>(ops::UnstableChecker {
|
|
|
|
unstable: options.unstable,
|
|
|
|
});
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
ops::runtime::init(js_runtime, main_module);
|
2020-12-13 13:45:53 -05:00
|
|
|
ops::fetch::init(
|
|
|
|
js_runtime,
|
|
|
|
options.user_agent.clone(),
|
2021-01-04 18:15:52 -05:00
|
|
|
options.ca_data.clone(),
|
2020-12-13 13:45:53 -05:00
|
|
|
);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::timers::init(js_runtime);
|
2020-12-11 12:49:26 -05:00
|
|
|
ops::worker_host::init(
|
|
|
|
js_runtime,
|
|
|
|
None,
|
|
|
|
options.create_web_worker_cb.clone(),
|
|
|
|
);
|
|
|
|
ops::crypto::init(js_runtime, options.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::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::signal::init(js_runtime);
|
|
|
|
ops::tls::init(js_runtime);
|
|
|
|
ops::tty::init(js_runtime);
|
2020-12-11 18:36:18 -05:00
|
|
|
ops::websocket::init(
|
|
|
|
js_runtime,
|
2021-01-04 18:15:52 -05:00
|
|
|
options.ca_data.clone(),
|
2020-12-11 18:36:18 -05:00
|
|
|
options.user_agent.clone(),
|
|
|
|
);
|
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 {
|
2020-12-16 11:14:12 -05:00
|
|
|
t.add(stream);
|
2020-07-09 15:06:51 -04:00
|
|
|
}
|
|
|
|
if let Some(stream) = stdout {
|
2020-12-16 11:14:12 -05:00
|
|
|
t.add(stream);
|
2020-07-09 15:06:51 -04:00
|
|
|
}
|
|
|
|
if let Some(stream) = stderr {
|
2020-12-16 11:14:12 -05:00
|
|
|
t.add(stream);
|
2020-07-09 15:06:51 -04:00
|
|
|
}
|
2020-06-01 22:44:17 -04:00
|
|
|
}
|
2020-12-11 12:49:26 -05:00
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
worker
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bootstrap(&mut self, options: &WorkerOptions) {
|
|
|
|
let runtime_options = json!({
|
|
|
|
"args": options.args,
|
|
|
|
"applySourceMaps": options.apply_source_maps,
|
|
|
|
"debugFlag": options.debug_flag,
|
2020-12-11 18:36:18 -05:00
|
|
|
"denoVersion": options.runtime_version,
|
|
|
|
"noColor": options.no_color,
|
2020-12-11 12:49:26 -05:00
|
|
|
"pid": std::process::id(),
|
|
|
|
"ppid": ops::runtime::ppid(),
|
|
|
|
"target": env!("TARGET"),
|
2020-12-11 18:36:18 -05:00
|
|
|
"tsVersion": options.ts_version,
|
2020-12-11 12:49:26 -05:00
|
|
|
"unstableFlag": options.unstable,
|
|
|
|
"v8Version": deno_core::v8_version(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let script = format!(
|
|
|
|
"bootstrap.mainRuntime({})",
|
|
|
|
serde_json::to_string_pretty(&runtime_options).unwrap()
|
|
|
|
);
|
|
|
|
self
|
|
|
|
.execute(&script)
|
2020-09-26 12:16:33 -04:00
|
|
|
.expect("Failed to execute bootstrap script");
|
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();
|
2020-12-20 09:14:19 -05:00
|
|
|
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::*;
|
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();
|
2020-12-11 12:49:26 -05:00
|
|
|
let permissions = Permissions::default();
|
|
|
|
|
|
|
|
let options = WorkerOptions {
|
|
|
|
apply_source_maps: false,
|
2020-12-11 18:36:18 -05:00
|
|
|
user_agent: "x".to_string(),
|
2020-12-11 12:49:26 -05:00
|
|
|
args: vec![],
|
|
|
|
debug_flag: false,
|
|
|
|
unstable: false,
|
2021-01-04 18:15:52 -05:00
|
|
|
ca_data: None,
|
2020-12-11 12:49:26 -05:00
|
|
|
seed: None,
|
|
|
|
js_error_create_fn: None,
|
|
|
|
create_web_worker_cb: Arc::new(|_| unreachable!()),
|
|
|
|
attach_inspector: false,
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
|
|
|
module_loader: Rc::new(deno_core::FsModuleLoader),
|
2020-12-11 18:36:18 -05:00
|
|
|
runtime_version: "x".to_string(),
|
|
|
|
ts_version: "x".to_string(),
|
|
|
|
no_color: true,
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: None,
|
2020-09-26 12:16:33 -04:00
|
|
|
};
|
2020-12-11 12:49:26 -05:00
|
|
|
|
|
|
|
MainWorker::from_options(main_module, permissions, &options)
|
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()
|
2020-12-11 12:49:26 -05:00
|
|
|
.join("tests/circular1.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();
|
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()
|
2020-12-11 12:49:26 -05:00
|
|
|
.join("cli/tests/001_hello.js");
|
2020-04-16 10:29:28 -04:00
|
|
|
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
|
|
|
}
|