2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 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;
|
2021-04-28 12:41:50 -04:00
|
|
|
use crate::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;
|
2021-04-21 11:52:00 -04:00
|
|
|
use deno_core::error::Context as ErrorContext;
|
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;
|
2021-03-04 07:19:47 -05:00
|
|
|
use deno_core::futures::stream::StreamExt;
|
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;
|
2021-04-28 12:41:50 -04:00
|
|
|
use deno_core::Extension;
|
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;
|
2021-04-07 09:22:14 -04:00
|
|
|
use deno_file::BlobUrlStore;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
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>,
|
2021-01-07 13:06:08 -05:00
|
|
|
pub location: Option<Url>,
|
2021-04-07 09:22:14 -04:00
|
|
|
pub blob_url_store: BlobUrlStore,
|
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 {
|
2021-05-02 19:22:57 -04:00
|
|
|
// Permissions: many ops depend on this
|
|
|
|
let unstable = options.unstable;
|
|
|
|
let perm_ext = Extension::builder()
|
|
|
|
.state(move |state| {
|
|
|
|
state.put::<Permissions>(permissions.clone());
|
|
|
|
state.put(ops::UnstableChecker { unstable });
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
|
2021-04-28 12:41:50 -04:00
|
|
|
// Internal modules
|
|
|
|
let extensions: Vec<Extension> = vec![
|
|
|
|
// Web APIs
|
|
|
|
deno_webidl::init(),
|
|
|
|
deno_console::init(),
|
|
|
|
deno_url::init(),
|
|
|
|
deno_web::init(),
|
|
|
|
deno_file::init(options.blob_url_store.clone(), options.location.clone()),
|
|
|
|
deno_fetch::init::<Permissions>(
|
|
|
|
options.user_agent.clone(),
|
|
|
|
options.ca_data.clone(),
|
|
|
|
),
|
|
|
|
deno_websocket::init::<Permissions>(
|
|
|
|
options.user_agent.clone(),
|
|
|
|
options.ca_data.clone(),
|
|
|
|
),
|
|
|
|
deno_crypto::init(options.seed),
|
|
|
|
deno_webgpu::init(options.unstable),
|
|
|
|
deno_timers::init::<Permissions>(),
|
|
|
|
// Metrics
|
|
|
|
metrics::init(),
|
2021-05-02 19:22:57 -04:00
|
|
|
// Runtime ops
|
|
|
|
ops::runtime::init(main_module),
|
|
|
|
ops::worker_host::init(true, options.create_web_worker_cb.clone()),
|
|
|
|
ops::fs_events::init(),
|
|
|
|
ops::fs::init(),
|
|
|
|
ops::http::init(),
|
|
|
|
ops::io::init(),
|
|
|
|
ops::io::init_stdio(),
|
|
|
|
ops::net::init(),
|
|
|
|
ops::os::init(),
|
|
|
|
ops::permissions::init(),
|
|
|
|
ops::plugin::init(),
|
|
|
|
ops::process::init(),
|
|
|
|
ops::signal::init(),
|
|
|
|
ops::tls::init(),
|
|
|
|
ops::tty::init(),
|
|
|
|
// Permissions ext (worker specific state)
|
|
|
|
perm_ext,
|
2021-04-28 12:41:50 -04:00
|
|
|
];
|
|
|
|
|
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,
|
2021-04-28 12:41:50 -04:00
|
|
|
extensions,
|
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
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
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-06-01 22:44:17 -04:00
|
|
|
}
|
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(),
|
2021-01-07 13:06:08 -05:00
|
|
|
"location": options.location,
|
2020-12-11 12:49:26 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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> {
|
2021-04-21 11:52:00 -04:00
|
|
|
let path = env::current_dir()
|
|
|
|
.context("Failed to get current working directory")?
|
|
|
|
.join("__anonymous__");
|
2020-11-26 09:17:45 -05:00
|
|
|
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();
|
2021-03-04 07:19:47 -05:00
|
|
|
let mut receiver = self.js_runtime.mod_evaluate(id);
|
|
|
|
tokio::select! {
|
|
|
|
maybe_result = receiver.next() => {
|
|
|
|
debug!("received module evaluate {:#?}", maybe_result);
|
|
|
|
let result = maybe_result.expect("Module evaluation result not provided.");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_loop_result = self.run_event_loop() => {
|
|
|
|
event_loop_result?;
|
|
|
|
let maybe_result = receiver.next().await;
|
|
|
|
let result = maybe_result.expect("Module evaluation result not provided.");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
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::*;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url_or_path;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
fn create_test_worker() -> MainWorker {
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = 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,
|
2021-01-07 13:06:08 -05:00
|
|
|
location: None,
|
2021-04-07 09:22:14 -04:00
|
|
|
blob_url_store: BlobUrlStore::default(),
|
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");
|
2021-02-17 13:47:18 -05:00
|
|
|
let module_specifier = 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");
|
2021-02-17 13:47:18 -05:00
|
|
|
let module_specifier = 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();
|
2021-02-17 13:47:18 -05:00
|
|
|
let module_specifier = resolve_url_or_path("does-not-exist").unwrap();
|
2020-04-16 10:29:28 -04:00
|
|
|
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");
|
2021-02-17 13:47:18 -05:00
|
|
|
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-04-16 10:29:28 -04:00
|
|
|
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
|
|
|
}
|