2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-05-26 15:07:12 -04:00
|
|
|
use crate::inspector_server::InspectorServer;
|
2020-09-11 09:18:49 -04:00
|
|
|
use crate::js;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops;
|
2022-04-26 19:00:04 -04:00
|
|
|
use crate::ops::io::Stdio;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2021-10-05 16:41:14 -04:00
|
|
|
use crate::BootstrapOptions;
|
2021-05-22 12:08:24 -04:00
|
|
|
use deno_broadcast_channel::InMemoryBroadcastChannel;
|
2022-09-28 08:11:12 -04:00
|
|
|
use deno_cache::CreateCache;
|
|
|
|
use deno_cache::SqliteBackedCache;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-04-26 19:06:10 -04:00
|
|
|
use deno_core::error::JsError;
|
2021-05-26 11:47:33 -04:00
|
|
|
use deno_core::futures::Future;
|
2021-06-21 19:45:41 -04:00
|
|
|
use deno_core::located_script_name;
|
2022-09-02 13:44:45 -04:00
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_v8;
|
|
|
|
use deno_core::v8;
|
2021-09-29 04:47:24 -04:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2021-04-28 12:41:50 -04:00
|
|
|
use deno_core::Extension;
|
2022-10-15 17:19:03 -04:00
|
|
|
use deno_core::FsModuleLoader;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_core::GetErrorClassFn;
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2021-05-26 15:07:12 -04:00
|
|
|
use deno_core::LocalInspectorSession;
|
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-07-06 13:42:52 -04:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2022-04-15 10:08:09 -04:00
|
|
|
use deno_core::SourceMapGetter;
|
2022-09-13 11:59:01 -04:00
|
|
|
use deno_node::RequireNpmResolver;
|
2021-08-07 08:49:38 -04:00
|
|
|
use deno_tls::rustls::RootCertStore;
|
2021-07-05 09:34:37 -04:00
|
|
|
use deno_web::BlobStore;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2021-05-26 11:47:33 -04:00
|
|
|
use std::pin::Pin;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::rc::Rc;
|
2021-12-11 09:56:45 -05:00
|
|
|
use std::sync::atomic::AtomicI32;
|
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
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
|
|
|
|
2022-04-26 19:06:10 -04:00
|
|
|
pub type FormatJsErrorFn = dyn Fn(&JsError) -> String + Sync + Send;
|
|
|
|
|
2022-06-29 20:26:58 -04:00
|
|
|
#[derive(Clone, Default)]
|
2022-06-08 11:45:38 -04:00
|
|
|
pub struct ExitCode(Arc<AtomicI32>);
|
|
|
|
|
|
|
|
impl ExitCode {
|
|
|
|
pub fn get(&self) -> i32 {
|
|
|
|
self.0.load(Relaxed)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, code: i32) {
|
|
|
|
self.0.store(code, Relaxed);
|
|
|
|
}
|
|
|
|
}
|
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-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,
|
2022-06-08 11:45:38 -04:00
|
|
|
exit_code: ExitCode,
|
2022-09-02 13:44:45 -04:00
|
|
|
js_run_tests_callback: v8::Global<v8::Function>,
|
|
|
|
js_run_benchmarks_callback: v8::Global<v8::Function>,
|
|
|
|
js_enable_test_callback: v8::Global<v8::Function>,
|
|
|
|
js_enable_bench_callback: v8::Global<v8::Function>,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
pub struct WorkerOptions {
|
2021-10-05 16:41:14 -04:00
|
|
|
pub bootstrap: BootstrapOptions,
|
2021-10-08 11:03:49 -04:00
|
|
|
pub extensions: Vec<Extension>,
|
2021-08-10 07:19:45 -04:00
|
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
2021-08-07 08:49:38 -04:00
|
|
|
pub root_cert_store: Option<RootCertStore>,
|
2020-12-11 12:49:26 -05:00
|
|
|
pub seed: Option<u64>,
|
|
|
|
pub module_loader: Rc<dyn ModuleLoader>,
|
2022-09-13 11:59:01 -04:00
|
|
|
pub npm_resolver: Option<Rc<dyn RequireNpmResolver>>,
|
2022-02-11 07:41:56 -05:00
|
|
|
// Callbacks invoked when creating new instance of WebWorker
|
2020-12-11 12:49:26 -05:00
|
|
|
pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>,
|
2022-08-16 21:00:35 -04:00
|
|
|
pub web_worker_preload_module_cb: Arc<ops::worker_host::WorkerEventCb>,
|
|
|
|
pub web_worker_pre_execute_module_cb: Arc<ops::worker_host::WorkerEventCb>,
|
2022-04-26 19:06:10 -04:00
|
|
|
pub format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
|
2022-04-15 10:08:09 -04:00
|
|
|
pub source_map_getter: Option<Box<dyn SourceMapGetter>>,
|
2020-12-11 12:49:26 -05:00
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
|
|
|
pub should_break_on_first_statement: bool,
|
2020-12-13 13:45:53 -05:00
|
|
|
pub get_error_class_fn: Option<GetErrorClassFn>,
|
2022-09-28 08:11:12 -04:00
|
|
|
pub cache_storage_dir: Option<std::path::PathBuf>,
|
2021-05-27 01:23:12 -04:00
|
|
|
pub origin_storage_dir: Option<std::path::PathBuf>,
|
2021-07-05 09:34:37 -04:00
|
|
|
pub blob_store: BlobStore,
|
2021-05-22 12:08:24 -04:00
|
|
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
2021-07-06 13:42:52 -04:00
|
|
|
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
2021-09-29 04:47:24 -04:00
|
|
|
pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
|
2022-04-26 19:00:04 -04:00
|
|
|
pub stdio: Stdio,
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2022-09-02 13:44:45 -04:00
|
|
|
fn grab_cb(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
path: &str,
|
|
|
|
) -> v8::Global<v8::Function> {
|
|
|
|
let cb = JsRuntime::grab_global::<v8::Function>(scope, path)
|
|
|
|
.unwrap_or_else(|| panic!("{} must be defined", path));
|
|
|
|
v8::Global::new(scope, cb)
|
|
|
|
}
|
|
|
|
|
2022-10-15 17:19:03 -04:00
|
|
|
impl Default for WorkerOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
web_worker_preload_module_cb: Arc::new(|_| {
|
|
|
|
unimplemented!("web workers are not supported")
|
|
|
|
}),
|
|
|
|
web_worker_pre_execute_module_cb: Arc::new(|_| {
|
|
|
|
unimplemented!("web workers are not supported")
|
|
|
|
}),
|
|
|
|
create_web_worker_cb: Arc::new(|_| {
|
|
|
|
unimplemented!("web workers are not supported")
|
|
|
|
}),
|
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
|
|
|
seed: None,
|
|
|
|
unsafely_ignore_certificate_errors: Default::default(),
|
|
|
|
should_break_on_first_statement: Default::default(),
|
|
|
|
compiled_wasm_module_store: Default::default(),
|
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
maybe_inspector_server: Default::default(),
|
|
|
|
format_js_error_fn: Default::default(),
|
|
|
|
get_error_class_fn: Default::default(),
|
|
|
|
origin_storage_dir: Default::default(),
|
|
|
|
cache_storage_dir: Default::default(),
|
|
|
|
broadcast_channel: Default::default(),
|
|
|
|
source_map_getter: Default::default(),
|
|
|
|
root_cert_store: Default::default(),
|
|
|
|
npm_resolver: Default::default(),
|
|
|
|
blob_store: Default::default(),
|
|
|
|
extensions: Default::default(),
|
|
|
|
bootstrap: Default::default(),
|
|
|
|
stdio: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
impl MainWorker {
|
2021-10-05 16:41:14 -04:00
|
|
|
pub fn bootstrap_from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
|
|
|
options: WorkerOptions,
|
|
|
|
) -> Self {
|
|
|
|
let bootstrap_options = options.bootstrap.clone();
|
|
|
|
let mut worker = Self::from_options(main_module, permissions, options);
|
|
|
|
worker.bootstrap(&bootstrap_options);
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
pub fn from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
2021-10-08 11:03:49 -04:00
|
|
|
mut 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
|
2021-10-05 16:41:14 -04:00
|
|
|
let unstable = options.bootstrap.unstable;
|
|
|
|
let enable_testing_features = options.bootstrap.enable_testing_features;
|
2021-05-02 19:22:57 -04:00
|
|
|
let perm_ext = Extension::builder()
|
|
|
|
.state(move |state| {
|
|
|
|
state.put::<Permissions>(permissions.clone());
|
|
|
|
state.put(ops::UnstableChecker { unstable });
|
2021-08-16 08:29:54 -04:00
|
|
|
state.put(ops::TestingFeaturesEnabled(enable_testing_features));
|
2021-05-02 19:22:57 -04:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build();
|
2022-06-08 11:45:38 -04:00
|
|
|
let exit_code = ExitCode(Arc::new(AtomicI32::new(0)));
|
2022-09-28 08:11:12 -04:00
|
|
|
let create_cache = options.cache_storage_dir.map(|storage_dir| {
|
|
|
|
let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone());
|
|
|
|
CreateCache(Arc::new(create_cache_fn))
|
|
|
|
});
|
2021-05-02 19:22:57 -04:00
|
|
|
|
2021-04-28 12:41:50 -04:00
|
|
|
// Internal modules
|
2021-10-08 11:03:49 -04:00
|
|
|
let mut extensions: Vec<Extension> = vec![
|
2021-04-28 12:41:50 -04:00
|
|
|
// Web APIs
|
|
|
|
deno_webidl::init(),
|
|
|
|
deno_console::init(),
|
|
|
|
deno_url::init(),
|
2022-02-15 06:17:30 -05:00
|
|
|
deno_web::init::<Permissions>(
|
2021-10-05 16:41:14 -04:00
|
|
|
options.blob_store.clone(),
|
|
|
|
options.bootstrap.location.clone(),
|
|
|
|
),
|
2021-11-28 13:07:03 -05:00
|
|
|
deno_fetch::init::<Permissions>(deno_fetch::Options {
|
2022-05-14 06:00:02 -04:00
|
|
|
user_agent: options.bootstrap.user_agent.clone(),
|
2021-11-28 13:07:03 -05:00
|
|
|
root_cert_store: options.root_cert_store.clone(),
|
|
|
|
unsafely_ignore_certificate_errors: options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
2021-12-03 08:25:16 -05:00
|
|
|
file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler),
|
2021-11-28 13:07:03 -05:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-09-28 08:11:12 -04:00
|
|
|
deno_cache::init::<SqliteBackedCache>(create_cache),
|
2021-04-28 12:41:50 -04:00
|
|
|
deno_websocket::init::<Permissions>(
|
2022-05-14 06:00:02 -04:00
|
|
|
options.bootstrap.user_agent.clone(),
|
2021-08-07 08:49:38 -04:00
|
|
|
options.root_cert_store.clone(),
|
2021-08-10 07:19:45 -04:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
2021-04-28 12:41:50 -04:00
|
|
|
),
|
2021-05-27 01:23:12 -04:00
|
|
|
deno_webstorage::init(options.origin_storage_dir.clone()),
|
2021-10-05 16:41:14 -04:00
|
|
|
deno_broadcast_channel::init(options.broadcast_channel.clone(), unstable),
|
2022-03-22 11:39:58 -04:00
|
|
|
deno_crypto::init(options.seed),
|
2021-10-05 16:41:14 -04:00
|
|
|
deno_webgpu::init(unstable),
|
2021-08-06 17:28:10 -04:00
|
|
|
// ffi
|
2021-10-05 16:41:14 -04:00
|
|
|
deno_ffi::init::<Permissions>(unstable),
|
2021-05-02 19:22:57 -04:00
|
|
|
// Runtime ops
|
2021-06-30 12:01:11 -04:00
|
|
|
ops::runtime::init(main_module.clone()),
|
2022-02-11 07:41:56 -05:00
|
|
|
ops::worker_host::init(
|
|
|
|
options.create_web_worker_cb.clone(),
|
|
|
|
options.web_worker_preload_module_cb.clone(),
|
2022-08-16 21:00:35 -04:00
|
|
|
options.web_worker_pre_execute_module_cb.clone(),
|
2022-04-26 19:06:10 -04:00
|
|
|
options.format_js_error_fn.clone(),
|
2022-02-11 07:41:56 -05:00
|
|
|
),
|
2022-04-20 18:20:33 -04:00
|
|
|
ops::spawn::init(),
|
2021-05-02 19:22:57 -04:00
|
|
|
ops::fs_events::init(),
|
|
|
|
ops::fs::init(),
|
|
|
|
ops::io::init(),
|
2022-04-26 19:00:04 -04:00
|
|
|
ops::io::init_stdio(options.stdio),
|
2021-08-07 08:49:38 -04:00
|
|
|
deno_tls::init(),
|
|
|
|
deno_net::init::<Permissions>(
|
|
|
|
options.root_cert_store.clone(),
|
2021-10-05 16:41:14 -04:00
|
|
|
unstable,
|
2021-08-10 07:19:45 -04:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
2021-08-07 08:49:38 -04:00
|
|
|
),
|
2022-10-05 10:06:44 -04:00
|
|
|
deno_napi::init::<Permissions>(unstable),
|
2022-08-24 12:07:49 -04:00
|
|
|
deno_node::init::<Permissions>(unstable, options.npm_resolver),
|
2022-06-08 11:45:38 -04:00
|
|
|
ops::os::init(exit_code.clone()),
|
2021-05-02 19:22:57 -04:00
|
|
|
ops::permissions::init(),
|
|
|
|
ops::process::init(),
|
|
|
|
ops::signal::init(),
|
|
|
|
ops::tty::init(),
|
2021-07-12 06:44:49 -04:00
|
|
|
deno_http::init(),
|
2022-08-18 08:05:02 -04:00
|
|
|
deno_flash::init::<Permissions>(unstable),
|
2021-07-12 06:44:49 -04:00
|
|
|
ops::http::init(),
|
2021-05-02 19:22:57 -04:00
|
|
|
// Permissions ext (worker specific state)
|
|
|
|
perm_ext,
|
2021-04-28 12:41:50 -04:00
|
|
|
];
|
2021-10-08 11:03:49 -04:00
|
|
|
extensions.extend(std::mem::take(&mut options.extensions));
|
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()),
|
2022-07-04 18:12:41 -04:00
|
|
|
startup_snapshot: Some(js::deno_isolate_init()),
|
2022-04-15 10:08:09 -04:00
|
|
|
source_map_getter: options.source_map_getter,
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: options.get_error_class_fn,
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store: options.shared_array_buffer_store.clone(),
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store: options.compiled_wasm_module_store.clone(),
|
2021-04-28 12:41:50 -04:00
|
|
|
extensions,
|
2022-10-26 07:37:50 -04:00
|
|
|
inspector: options.maybe_inspector_server.is_some(),
|
2020-09-11 09:18:49 -04:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2021-06-21 13:37:51 -04:00
|
|
|
if let Some(server) = options.maybe_inspector_server.clone() {
|
2021-12-17 12:43:25 -05:00
|
|
|
server.register_inspector(
|
|
|
|
main_module.to_string(),
|
|
|
|
&mut js_runtime,
|
|
|
|
options.should_break_on_first_statement,
|
|
|
|
);
|
2021-05-26 15:07:12 -04:00
|
|
|
}
|
2020-02-05 02:40:38 -05:00
|
|
|
|
2022-09-02 13:44:45 -04:00
|
|
|
let (
|
|
|
|
js_run_tests_callback,
|
|
|
|
js_run_benchmarks_callback,
|
|
|
|
js_enable_test_callback,
|
|
|
|
js_enable_bench_callback,
|
|
|
|
) = {
|
|
|
|
let scope = &mut js_runtime.handle_scope();
|
|
|
|
(
|
|
|
|
grab_cb(scope, "__bootstrap.testing.runTests"),
|
|
|
|
grab_cb(scope, "__bootstrap.testing.runBenchmarks"),
|
|
|
|
grab_cb(scope, "__bootstrap.testing.enableTest"),
|
|
|
|
grab_cb(scope, "__bootstrap.testing.enableBench"),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
Self {
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime,
|
2021-06-21 13:37:51 -04:00
|
|
|
should_break_on_first_statement: options.should_break_on_first_statement,
|
2022-06-08 11:45:38 -04:00
|
|
|
exit_code,
|
2022-09-02 13:44:45 -04:00
|
|
|
js_run_tests_callback,
|
|
|
|
js_run_benchmarks_callback,
|
|
|
|
js_enable_test_callback,
|
|
|
|
js_enable_bench_callback,
|
2020-06-01 22:44:17 -04:00
|
|
|
}
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
|
|
|
|
2021-10-05 16:41:14 -04:00
|
|
|
pub fn bootstrap(&mut self, options: &BootstrapOptions) {
|
|
|
|
let script = format!("bootstrap.mainRuntime({})", options.as_json());
|
2020-12-11 12:49:26 -05:00
|
|
|
self
|
2021-06-21 19:45:41 -04:00
|
|
|
.execute_script(&located_script_name!(), &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
|
|
|
|
2021-06-21 19:45:41 -04:00
|
|
|
/// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script)
|
|
|
|
pub fn execute_script(
|
|
|
|
&mut self,
|
2021-12-21 09:49:27 -05:00
|
|
|
script_name: &str,
|
2021-06-21 19:45:41 -04:00
|
|
|
source_code: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-12-21 09:49:27 -05:00
|
|
|
self.js_runtime.execute_script(script_name, source_code)?;
|
2021-07-08 12:56:53 -04:00
|
|
|
Ok(())
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2022-08-10 18:10:51 -04:00
|
|
|
/// Loads and instantiates specified JavaScript module as "main" module.
|
|
|
|
pub async fn preload_main_module(
|
2020-11-26 09:17:45 -05:00
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleId, AnyError> {
|
2022-08-10 18:10:51 -04:00
|
|
|
self
|
|
|
|
.js_runtime
|
|
|
|
.load_main_module(module_specifier, None)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads and instantiates specified JavaScript module as "side" module.
|
|
|
|
pub async fn preload_side_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleId, AnyError> {
|
|
|
|
self
|
|
|
|
.js_runtime
|
|
|
|
.load_side_module(module_specifier, None)
|
|
|
|
.await
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2022-06-19 17:29:48 -04:00
|
|
|
/// Executes specified JavaScript module.
|
|
|
|
pub async fn evaluate_module(
|
|
|
|
&mut self,
|
|
|
|
id: ModuleId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 18:10:51 -04:00
|
|
|
self.wait_for_inspector_session();
|
2021-03-04 07:19:47 -05:00
|
|
|
let mut receiver = self.js_runtime.mod_evaluate(id);
|
|
|
|
tokio::select! {
|
2022-04-13 05:50:57 -04:00
|
|
|
// Not using biased mode leads to non-determinism for relatively simple
|
|
|
|
// programs.
|
|
|
|
biased;
|
|
|
|
|
2021-07-30 07:36:43 -04:00
|
|
|
maybe_result = &mut receiver => {
|
2021-03-04 07:19:47 -05:00
|
|
|
debug!("received module evaluate {:#?}", maybe_result);
|
2021-06-17 15:56:30 -04:00
|
|
|
maybe_result.expect("Module evaluation result not provided.")
|
2021-03-04 07:19:47 -05:00
|
|
|
}
|
|
|
|
|
2021-05-26 15:07:12 -04:00
|
|
|
event_loop_result = self.run_event_loop(false) => {
|
2021-03-04 07:19:47 -05:00
|
|
|
event_loop_result?;
|
2021-07-30 07:36:43 -04:00
|
|
|
let maybe_result = receiver.await;
|
2021-06-17 15:56:30 -04:00
|
|
|
maybe_result.expect("Module evaluation result not provided.")
|
2021-03-04 07:19:47 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2021-09-17 21:44:53 -04:00
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
pub async fn execute_side_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 18:10:51 -04:00
|
|
|
let id = self.preload_side_module(module_specifier).await?;
|
2021-09-17 21:44:53 -04:00
|
|
|
self.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
///
|
|
|
|
/// This module will have "import.meta.main" equal to true.
|
|
|
|
pub async fn execute_main_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 18:10:51 -04:00
|
|
|
let id = self.preload_main_module(module_specifier).await?;
|
2021-09-17 21:44:53 -04:00
|
|
|
self.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:44:45 -04:00
|
|
|
/// Run tests declared with `Deno.test()`. Test events will be dispatched
|
|
|
|
/// by calling ops which are currently only implemented in the CLI crate.
|
|
|
|
// TODO(nayeemrmn): Move testing ops to deno_runtime and redesign/unhide.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub async fn run_tests(
|
|
|
|
&mut self,
|
|
|
|
shuffle: &Option<u64>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let promise = {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let cb = self.js_run_tests_callback.open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
let options =
|
|
|
|
serde_v8::to_v8(scope, json!({ "shuffle": shuffle })).unwrap();
|
|
|
|
let promise = cb.call(scope, this, &[options]).unwrap();
|
|
|
|
v8::Global::new(scope, promise)
|
|
|
|
};
|
|
|
|
self.js_runtime.resolve_value(promise).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run benches declared with `Deno.bench()`. Bench events will be dispatched
|
|
|
|
/// by calling ops which are currently only implemented in the CLI crate.
|
|
|
|
// TODO(nayeemrmn): Move benchmark ops to deno_runtime and redesign/unhide.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub async fn run_benchmarks(&mut self) -> Result<(), AnyError> {
|
|
|
|
let promise = {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let cb = self.js_run_benchmarks_callback.open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
let promise = cb.call(scope, this, &[]).unwrap();
|
|
|
|
v8::Global::new(scope, promise)
|
|
|
|
};
|
|
|
|
self.js_runtime.resolve_value(promise).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable `Deno.test()`. If this isn't called before executing user code,
|
|
|
|
/// `Deno.test()` calls will noop.
|
|
|
|
// TODO(nayeemrmn): Move testing ops to deno_runtime and redesign/unhide.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn enable_test(&mut self) {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let cb = self.js_enable_test_callback.open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
cb.call(scope, this, &[]).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable `Deno.bench()`. If this isn't called before executing user code,
|
|
|
|
/// `Deno.bench()` calls will noop.
|
|
|
|
// TODO(nayeemrmn): Move benchmark ops to deno_runtime and redesign/unhide.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn enable_bench(&mut self) {
|
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let cb = self.js_enable_bench_callback.open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
cb.call(scope, this, &[]).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
fn wait_for_inspector_session(&mut self) {
|
|
|
|
if self.should_break_on_first_statement {
|
|
|
|
self
|
2021-05-26 15:07:12 -04:00
|
|
|
.js_runtime
|
|
|
|
.inspector()
|
2022-09-02 06:43:39 -04:00
|
|
|
.borrow_mut()
|
2020-11-26 09:17:45 -05:00
|
|
|
.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.
|
2021-05-26 11:47:33 -04:00
|
|
|
pub async fn create_inspector_session(&mut self) -> LocalInspectorSession {
|
2022-10-26 07:37:50 -04:00
|
|
|
self.js_runtime.maybe_init_inspector();
|
2022-09-02 06:43:39 -04:00
|
|
|
self.js_runtime.inspector().borrow().create_local_session()
|
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,
|
2021-05-26 15:07:12 -04:00
|
|
|
wait_for_inspector: bool,
|
2020-10-11 07:20:40 -04:00
|
|
|
) -> Poll<Result<(), AnyError>> {
|
2021-05-26 15:07:12 -04:00
|
|
|
self.js_runtime.poll_event_loop(cx, wait_for_inspector)
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2021-05-26 15:07:12 -04:00
|
|
|
pub async fn run_event_loop(
|
|
|
|
&mut self,
|
|
|
|
wait_for_inspector: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-06-29 14:39:28 -04:00
|
|
|
self.js_runtime.run_event_loop(wait_for_inspector).await
|
2020-10-11 07:20:40 -04:00
|
|
|
}
|
2021-05-26 11:47:33 -04:00
|
|
|
|
|
|
|
/// A utility function that runs provided future concurrently with the event loop.
|
|
|
|
///
|
|
|
|
/// Useful when using a local inspector session.
|
|
|
|
pub async fn with_event_loop<'a, T>(
|
|
|
|
&mut self,
|
|
|
|
mut fut: Pin<Box<dyn Future<Output = T> + 'a>>,
|
|
|
|
) -> T {
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
2022-05-10 05:26:57 -04:00
|
|
|
biased;
|
2021-05-26 11:47:33 -04:00
|
|
|
result = &mut fut => {
|
|
|
|
return result;
|
|
|
|
}
|
2021-05-26 15:07:12 -04:00
|
|
|
_ = self.run_event_loop(false) => {}
|
2021-05-26 11:47:33 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-12-11 09:56:45 -05:00
|
|
|
|
|
|
|
/// Return exit code set by the executed code (either in main worker
|
|
|
|
/// or one of child web workers).
|
2022-06-08 11:45:38 -04:00
|
|
|
pub fn get_exit_code(&self) -> i32 {
|
|
|
|
self.exit_code.get()
|
2021-12-11 09:56:45 -05:00
|
|
|
}
|
2021-12-21 09:49:27 -05:00
|
|
|
|
|
|
|
/// Dispatches "load" event to the JavaScript runtime.
|
|
|
|
///
|
|
|
|
/// Does not poll event loop, and thus not await any of the "load" event handlers.
|
|
|
|
pub fn dispatch_load_event(
|
|
|
|
&mut self,
|
|
|
|
script_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-01-17 18:13:14 -05:00
|
|
|
self.execute_script(
|
|
|
|
script_name,
|
|
|
|
// NOTE(@bartlomieju): not using `globalThis` here, because user might delete
|
|
|
|
// it. Instead we're using global `dispatchEvent` function which will
|
|
|
|
// used a saved reference to global scope.
|
|
|
|
"dispatchEvent(new Event('load'))",
|
|
|
|
)
|
2021-12-21 09:49:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dispatches "unload" event to the JavaScript runtime.
|
|
|
|
///
|
|
|
|
/// Does not poll event loop, and thus not await any of the "unload" event handlers.
|
|
|
|
pub fn dispatch_unload_event(
|
|
|
|
&mut self,
|
|
|
|
script_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-01-17 18:13:14 -05:00
|
|
|
self.execute_script(
|
|
|
|
script_name,
|
|
|
|
// NOTE(@bartlomieju): not using `globalThis` here, because user might delete
|
|
|
|
// it. Instead we're using global `dispatchEvent` function which will
|
|
|
|
// used a saved reference to global scope.
|
|
|
|
"dispatchEvent(new Event('unload'))",
|
|
|
|
)
|
2021-12-21 09:49:27 -05:00
|
|
|
}
|
2022-06-28 10:49:30 -04:00
|
|
|
|
|
|
|
/// Dispatches "beforeunload" event to the JavaScript runtime. Returns a boolean
|
|
|
|
/// indicating if the event was prevented and thus event loop should continue
|
|
|
|
/// running.
|
|
|
|
pub fn dispatch_beforeunload_event(
|
|
|
|
&mut self,
|
|
|
|
script_name: &str,
|
|
|
|
) -> Result<bool, AnyError> {
|
|
|
|
let value = self.js_runtime.execute_script(
|
|
|
|
script_name,
|
|
|
|
// NOTE(@bartlomieju): not using `globalThis` here, because user might delete
|
|
|
|
// it. Instead we're using global `dispatchEvent` function which will
|
|
|
|
// used a saved reference to global scope.
|
|
|
|
"dispatchEvent(new Event('beforeunload', { cancelable: true }));",
|
|
|
|
)?;
|
|
|
|
let local_value = value.open(&mut self.js_runtime.handle_scope());
|
|
|
|
Ok(local_value.is_false())
|
|
|
|
}
|
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 {
|
2021-10-05 16:41:14 -04:00
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: vec![],
|
|
|
|
cpu_count: 1,
|
|
|
|
debug_flag: false,
|
|
|
|
enable_testing_features: false,
|
2022-10-18 09:33:35 -04:00
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
2021-10-05 16:41:14 -04:00
|
|
|
location: None,
|
|
|
|
no_color: true,
|
2022-02-28 22:37:50 -05:00
|
|
|
is_tty: false,
|
2021-10-05 16:41:14 -04:00
|
|
|
runtime_version: "x".to_string(),
|
|
|
|
ts_version: "x".to_string(),
|
|
|
|
unstable: false,
|
2022-05-14 06:00:02 -04:00
|
|
|
user_agent: "x".to_string(),
|
2022-09-17 06:04:43 -04:00
|
|
|
inspect: false,
|
2021-10-05 16:41:14 -04:00
|
|
|
},
|
2021-10-08 11:03:49 -04:00
|
|
|
extensions: vec![],
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors: None,
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store: None,
|
2020-12-11 12:49:26 -05:00
|
|
|
seed: None,
|
2022-04-26 19:06:10 -04:00
|
|
|
format_js_error_fn: None,
|
2022-04-15 10:08:09 -04:00
|
|
|
source_map_getter: None,
|
2022-02-11 07:41:56 -05:00
|
|
|
web_worker_preload_module_cb: Arc::new(|_| unreachable!()),
|
2022-08-16 21:00:35 -04:00
|
|
|
web_worker_pre_execute_module_cb: Arc::new(|_| unreachable!()),
|
2020-12-11 12:49:26 -05:00
|
|
|
create_web_worker_cb: Arc::new(|_| unreachable!()),
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
2022-10-15 17:19:03 -04:00
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
2022-08-20 11:31:33 -04:00
|
|
|
npm_resolver: None,
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: None,
|
2022-09-28 08:11:12 -04:00
|
|
|
cache_storage_dir: None,
|
2021-05-27 01:23:12 -04:00
|
|
|
origin_storage_dir: None,
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store: BlobStore::default(),
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel: InMemoryBroadcastChannel::default(),
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store: None,
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store: None,
|
2022-04-26 19:00:04 -04:00
|
|
|
stdio: Default::default(),
|
2020-09-26 12:16:33 -04:00
|
|
|
};
|
2020-12-11 12:49:26 -05:00
|
|
|
|
2021-10-05 16:41:14 -04:00
|
|
|
MainWorker::bootstrap_from_options(main_module, permissions, options)
|
2020-09-26 12:16:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_esm_imports_a() {
|
2022-09-19 10:32:21 -04:00
|
|
|
let p = test_util::testdata_path().join("runtime/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();
|
2021-09-17 21:44:53 -04:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-09-26 12:16:33 -04:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2021-05-26 15:07:12 -04:00
|
|
|
if let Err(e) = worker.run_event_loop(false).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();
|
2021-09-17 21:44:53 -04:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2021-05-26 15:07:12 -04:00
|
|
|
if let Err(e) = worker.run_event_loop(false).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();
|
2021-09-17 21:44:53 -04:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-04-16 10:29:28 -04:00
|
|
|
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();
|
2022-09-19 10:32:21 -04:00
|
|
|
let p = test_util::testdata_path().join("run/001_hello.js");
|
2021-02-17 13:47:18 -05:00
|
|
|
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2021-09-17 21:44:53 -04:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-04-16 10:29:28 -04:00
|
|
|
assert!(result.is_ok());
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|