2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2023-01-05 19:29:50 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::atomic::AtomicI32;
|
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
|
|
|
|
2021-05-22 16:08:24 +00:00
|
|
|
use deno_broadcast_channel::InMemoryBroadcastChannel;
|
2022-09-28 12:11:12 +00:00
|
|
|
use deno_cache::CreateCache;
|
|
|
|
use deno_cache::SqliteBackedCache;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2022-04-26 23:06:10 +00:00
|
|
|
use deno_core::error::JsError;
|
2021-05-26 15:47:33 +00:00
|
|
|
use deno_core::futures::Future;
|
2022-12-18 20:34:33 +00:00
|
|
|
use deno_core::v8;
|
2021-09-29 08:47:24 +00:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2021-04-28 16:41:50 +00:00
|
|
|
use deno_core::Extension;
|
2022-10-15 21:19:03 +00:00
|
|
|
use deno_core::FsModuleLoader;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_core::GetErrorClassFn;
|
2020-09-06 19:44:29 +00:00
|
|
|
use deno_core::JsRuntime;
|
2021-05-26 19:07:12 +00:00
|
|
|
use deno_core::LocalInspectorSession;
|
2020-02-18 15:08:18 +00:00
|
|
|
use deno_core::ModuleId;
|
2020-11-30 19:35:12 +00:00
|
|
|
use deno_core::ModuleLoader;
|
2020-01-05 16:56:18 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-11 13:18:49 +00:00
|
|
|
use deno_core::RuntimeOptions;
|
2021-07-06 17:42:52 +00:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2022-11-10 11:46:26 +00:00
|
|
|
use deno_core::Snapshot;
|
2022-04-15 14:08:09 +00:00
|
|
|
use deno_core::SourceMapGetter;
|
2023-03-05 00:39:48 +00:00
|
|
|
use deno_io::Stdio;
|
2022-09-13 15:59:01 +00:00
|
|
|
use deno_node::RequireNpmResolver;
|
2021-08-07 12:49:38 +00:00
|
|
|
use deno_tls::rustls::RootCertStore;
|
2021-07-05 13:34:37 +00:00
|
|
|
use deno_web::BlobStore;
|
2021-03-26 16:34:25 +00:00
|
|
|
use log::debug;
|
2023-01-05 19:29:50 +00:00
|
|
|
|
|
|
|
use crate::inspector_server::InspectorServer;
|
|
|
|
use crate::ops;
|
2023-01-07 16:25:34 +00:00
|
|
|
use crate::permissions::PermissionsContainer;
|
2023-01-05 19:29:50 +00:00
|
|
|
use crate::BootstrapOptions;
|
2018-10-05 17:21:15 +00:00
|
|
|
|
2022-04-26 23:06:10 +00:00
|
|
|
pub type FormatJsErrorFn = dyn Fn(&JsError) -> String + Sync + Send;
|
|
|
|
|
2022-06-30 00:26:58 +00:00
|
|
|
#[derive(Clone, Default)]
|
2022-06-08 15:45:38 +00: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 14:17:45 +00:00
|
|
|
/// This worker is created and used by almost all
|
|
|
|
/// subcommands in Deno executable.
|
2020-01-21 16:50:06 +00:00
|
|
|
///
|
2020-11-26 14:17:45 +00:00
|
|
|
/// It provides ops available in the `Deno` namespace.
|
2020-01-21 16:50:06 +00:00
|
|
|
///
|
2020-11-26 14:17:45 +00:00
|
|
|
/// All `WebWorker`s created during program execution
|
|
|
|
/// are descendants of this worker.
|
|
|
|
pub struct MainWorker {
|
2020-12-11 17:49:26 +00:00
|
|
|
pub js_runtime: JsRuntime,
|
2020-09-19 23:17:35 +00:00
|
|
|
should_break_on_first_statement: bool,
|
2022-12-12 14:33:30 +00:00
|
|
|
should_wait_for_inspector_session: bool,
|
2022-06-08 15:45:38 +00:00
|
|
|
exit_code: ExitCode,
|
2023-02-22 00:55:31 +00:00
|
|
|
bootstrap_fn_global: Option<v8::Global<v8::Function>>,
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 17:49:26 +00:00
|
|
|
pub struct WorkerOptions {
|
2021-10-05 20:41:14 +00:00
|
|
|
pub bootstrap: BootstrapOptions,
|
2022-12-19 02:55:50 +00:00
|
|
|
|
|
|
|
/// JsRuntime extensions, not to be confused with ES modules.
|
|
|
|
/// Only ops registered by extensions will be initialized. If you need
|
|
|
|
/// to execute JS code from extensions, use `extensions_with_js` options
|
|
|
|
/// instead.
|
2021-10-08 15:03:49 +00:00
|
|
|
pub extensions: Vec<Extension>,
|
2022-12-19 02:55:50 +00:00
|
|
|
|
|
|
|
/// JsRuntime extensions, not to be confused with ES modules.
|
|
|
|
/// Ops registered by extensions will be initialized and JS code will be
|
|
|
|
/// executed. If you don't need to execute JS code from extensions, use
|
|
|
|
/// `extensions` option instead.
|
|
|
|
///
|
|
|
|
/// This is useful when creating snapshots, in such case you would pass
|
|
|
|
/// extensions using `extensions_with_js`, later when creating a runtime
|
|
|
|
/// from the snapshot, you would pass these extensions using `extensions`
|
|
|
|
/// option.
|
|
|
|
pub extensions_with_js: Vec<Extension>,
|
|
|
|
|
|
|
|
/// V8 snapshot that should be loaded on startup.
|
2022-11-10 11:46:26 +00:00
|
|
|
pub startup_snapshot: Option<Snapshot>,
|
2021-08-10 11:19:45 +00:00
|
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
2021-08-07 12:49:38 +00:00
|
|
|
pub root_cert_store: Option<RootCertStore>,
|
2020-12-11 17:49:26 +00:00
|
|
|
pub seed: Option<u64>,
|
2022-12-19 02:55:50 +00:00
|
|
|
|
|
|
|
/// Implementation of `ModuleLoader` which will be
|
|
|
|
/// called when V8 requests to load ES modules.
|
|
|
|
///
|
|
|
|
/// If not provided runtime will error if code being
|
|
|
|
/// executed tries to load modules.
|
2020-12-11 17:49:26 +00:00
|
|
|
pub module_loader: Rc<dyn ModuleLoader>,
|
2022-09-13 15:59:01 +00:00
|
|
|
pub npm_resolver: Option<Rc<dyn RequireNpmResolver>>,
|
2022-02-11 12:41:56 +00:00
|
|
|
// Callbacks invoked when creating new instance of WebWorker
|
2020-12-11 17:49:26 +00:00
|
|
|
pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>,
|
2022-08-17 01:00:35 +00: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 23:06:10 +00:00
|
|
|
pub format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
|
2022-12-19 02:55:50 +00:00
|
|
|
|
|
|
|
/// Source map reference for errors.
|
2022-04-15 14:08:09 +00:00
|
|
|
pub source_map_getter: Option<Box<dyn SourceMapGetter>>,
|
2020-12-11 17:49:26 +00:00
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2022-12-12 14:33:30 +00:00
|
|
|
// If true, the worker will wait for inspector session and break on first
|
|
|
|
// statement of user code. Takes higher precedence than
|
|
|
|
// `should_wait_for_inspector_session`.
|
2020-12-11 17:49:26 +00:00
|
|
|
pub should_break_on_first_statement: bool,
|
2022-12-12 14:33:30 +00:00
|
|
|
// If true, the worker will wait for inspector session before executing
|
|
|
|
// user code.
|
|
|
|
pub should_wait_for_inspector_session: bool,
|
2022-12-19 02:55:50 +00:00
|
|
|
|
|
|
|
/// Allows to map error type to a string "class" used to represent
|
|
|
|
/// error in JavaScript.
|
2020-12-13 18:45:53 +00:00
|
|
|
pub get_error_class_fn: Option<GetErrorClassFn>,
|
2022-09-28 12:11:12 +00:00
|
|
|
pub cache_storage_dir: Option<std::path::PathBuf>,
|
2021-05-27 05:23:12 +00:00
|
|
|
pub origin_storage_dir: Option<std::path::PathBuf>,
|
2021-07-05 13:34:37 +00:00
|
|
|
pub blob_store: BlobStore,
|
2021-05-22 16:08:24 +00:00
|
|
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
2022-12-19 02:55:50 +00:00
|
|
|
|
|
|
|
/// The store to use for transferring SharedArrayBuffers between isolates.
|
|
|
|
/// If multiple isolates should have the possibility of sharing
|
|
|
|
/// SharedArrayBuffers, they should use the same [SharedArrayBufferStore]. If
|
|
|
|
/// no [SharedArrayBufferStore] is specified, SharedArrayBuffer can not be
|
|
|
|
/// serialized.
|
2021-07-06 17:42:52 +00:00
|
|
|
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
2022-12-19 02:55:50 +00:00
|
|
|
|
|
|
|
/// The store to use for transferring `WebAssembly.Module` objects between
|
|
|
|
/// isolates.
|
|
|
|
/// If multiple isolates should have the possibility of sharing
|
|
|
|
/// `WebAssembly.Module` objects, they should use the same
|
|
|
|
/// [CompiledWasmModuleStore]. If no [CompiledWasmModuleStore] is specified,
|
|
|
|
/// `WebAssembly.Module` objects cannot be serialized.
|
2021-09-29 08:47:24 +00:00
|
|
|
pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
|
2022-04-26 23:00:04 +00:00
|
|
|
pub stdio: Stdio,
|
2020-12-11 17:49:26 +00:00
|
|
|
}
|
2020-11-30 19:35:12 +00:00
|
|
|
|
2022-10-15 21:19:03 +00: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(),
|
2022-12-12 14:33:30 +00:00
|
|
|
should_wait_for_inspector_session: Default::default(),
|
2022-10-15 21:19:03 +00:00
|
|
|
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(),
|
2022-12-19 02:55:50 +00:00
|
|
|
extensions_with_js: Default::default(),
|
2022-11-10 11:46:26 +00:00
|
|
|
startup_snapshot: Default::default(),
|
2022-10-15 21:19:03 +00:00
|
|
|
bootstrap: Default::default(),
|
|
|
|
stdio: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 17:49:26 +00:00
|
|
|
impl MainWorker {
|
2021-10-05 20:41:14 +00:00
|
|
|
pub fn bootstrap_from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions: PermissionsContainer,
|
2021-10-05 20:41:14 +00:00
|
|
|
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 19:35:12 +00:00
|
|
|
pub fn from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions: PermissionsContainer,
|
2021-10-08 15:03:49 +00:00
|
|
|
mut options: WorkerOptions,
|
2020-11-30 19:35:12 +00:00
|
|
|
) -> Self {
|
2021-05-02 23:22:57 +00:00
|
|
|
// Permissions: many ops depend on this
|
2021-10-05 20:41:14 +00:00
|
|
|
let unstable = options.bootstrap.unstable;
|
|
|
|
let enable_testing_features = options.bootstrap.enable_testing_features;
|
2023-01-08 22:48:46 +00:00
|
|
|
let perm_ext = Extension::builder("deno_permissions_worker")
|
2021-05-02 23:22:57 +00:00
|
|
|
.state(move |state| {
|
2023-01-07 16:25:34 +00:00
|
|
|
state.put::<PermissionsContainer>(permissions.clone());
|
2021-05-02 23:22:57 +00:00
|
|
|
state.put(ops::UnstableChecker { unstable });
|
2021-08-16 12:29:54 +00:00
|
|
|
state.put(ops::TestingFeaturesEnabled(enable_testing_features));
|
2021-05-02 23:22:57 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build();
|
2022-06-08 15:45:38 +00:00
|
|
|
let exit_code = ExitCode(Arc::new(AtomicI32::new(0)));
|
2022-09-28 12:11:12 +00: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 23:22:57 +00:00
|
|
|
|
2021-04-28 16:41:50 +00:00
|
|
|
// Internal modules
|
2021-10-08 15:03:49 +00:00
|
|
|
let mut extensions: Vec<Extension> = vec![
|
2021-04-28 16:41:50 +00:00
|
|
|
// Web APIs
|
|
|
|
deno_webidl::init(),
|
|
|
|
deno_console::init(),
|
|
|
|
deno_url::init(),
|
2023-01-07 16:25:34 +00:00
|
|
|
deno_web::init::<PermissionsContainer>(
|
2021-10-05 20:41:14 +00:00
|
|
|
options.blob_store.clone(),
|
|
|
|
options.bootstrap.location.clone(),
|
|
|
|
),
|
2023-01-07 16:25:34 +00:00
|
|
|
deno_fetch::init::<PermissionsContainer>(deno_fetch::Options {
|
2022-05-14 10:00:02 +00:00
|
|
|
user_agent: options.bootstrap.user_agent.clone(),
|
2021-11-28 18:07:03 +00:00
|
|
|
root_cert_store: options.root_cert_store.clone(),
|
|
|
|
unsafely_ignore_certificate_errors: options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
2021-12-03 13:25:16 +00:00
|
|
|
file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler),
|
2021-11-28 18:07:03 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-09-28 12:11:12 +00:00
|
|
|
deno_cache::init::<SqliteBackedCache>(create_cache),
|
2023-01-07 16:25:34 +00:00
|
|
|
deno_websocket::init::<PermissionsContainer>(
|
2022-05-14 10:00:02 +00:00
|
|
|
options.bootstrap.user_agent.clone(),
|
2021-08-07 12:49:38 +00:00
|
|
|
options.root_cert_store.clone(),
|
2021-08-10 11:19:45 +00:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
2021-04-28 16:41:50 +00:00
|
|
|
),
|
2021-05-27 05:23:12 +00:00
|
|
|
deno_webstorage::init(options.origin_storage_dir.clone()),
|
2021-10-05 20:41:14 +00:00
|
|
|
deno_broadcast_channel::init(options.broadcast_channel.clone(), unstable),
|
2022-03-22 15:39:58 +00:00
|
|
|
deno_crypto::init(options.seed),
|
2021-10-05 20:41:14 +00:00
|
|
|
deno_webgpu::init(unstable),
|
2021-08-06 21:28:10 +00:00
|
|
|
// ffi
|
2023-01-07 16:25:34 +00:00
|
|
|
deno_ffi::init::<PermissionsContainer>(unstable),
|
2021-05-02 23:22:57 +00:00
|
|
|
// Runtime ops
|
2021-06-30 16:01:11 +00:00
|
|
|
ops::runtime::init(main_module.clone()),
|
2022-02-11 12:41:56 +00:00
|
|
|
ops::worker_host::init(
|
|
|
|
options.create_web_worker_cb.clone(),
|
|
|
|
options.web_worker_preload_module_cb.clone(),
|
2022-08-17 01:00:35 +00:00
|
|
|
options.web_worker_pre_execute_module_cb.clone(),
|
2022-04-26 23:06:10 +00:00
|
|
|
options.format_js_error_fn.clone(),
|
2022-02-11 12:41:56 +00:00
|
|
|
),
|
2021-05-02 23:22:57 +00:00
|
|
|
ops::fs_events::init(),
|
2023-03-04 23:44:59 +00:00
|
|
|
ops::fs::init::<PermissionsContainer>(),
|
2023-03-05 00:39:48 +00:00
|
|
|
deno_io::init(options.stdio),
|
2021-08-07 12:49:38 +00:00
|
|
|
deno_tls::init(),
|
2023-01-07 16:25:34 +00:00
|
|
|
deno_net::init::<PermissionsContainer>(
|
2021-08-07 12:49:38 +00:00
|
|
|
options.root_cert_store.clone(),
|
2021-10-05 20:41:14 +00:00
|
|
|
unstable,
|
2021-08-10 11:19:45 +00:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
2021-08-07 12:49:38 +00:00
|
|
|
),
|
2023-02-14 14:53:00 +00:00
|
|
|
deno_napi::init::<PermissionsContainer>(),
|
2023-02-14 16:38:45 +00:00
|
|
|
deno_node::init_polyfill(),
|
2023-01-07 16:25:34 +00:00
|
|
|
deno_node::init::<PermissionsContainer>(options.npm_resolver),
|
2022-06-08 15:45:38 +00:00
|
|
|
ops::os::init(exit_code.clone()),
|
2021-05-02 23:22:57 +00:00
|
|
|
ops::permissions::init(),
|
2023-03-05 12:19:34 +00:00
|
|
|
ops::process::init_ops(),
|
2021-05-02 23:22:57 +00:00
|
|
|
ops::signal::init(),
|
|
|
|
ops::tty::init(),
|
2021-07-12 10:44:49 +00:00
|
|
|
deno_http::init(),
|
2023-01-07 16:25:34 +00:00
|
|
|
deno_flash::init::<PermissionsContainer>(unstable),
|
2021-07-12 10:44:49 +00:00
|
|
|
ops::http::init(),
|
2021-05-02 23:22:57 +00:00
|
|
|
// Permissions ext (worker specific state)
|
|
|
|
perm_ext,
|
2021-04-28 16:41:50 +00:00
|
|
|
];
|
2021-10-08 15:03:49 +00:00
|
|
|
extensions.extend(std::mem::take(&mut options.extensions));
|
2021-04-28 16:41:50 +00:00
|
|
|
|
2023-02-20 20:45:34 +00:00
|
|
|
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
|
|
|
|
let startup_snapshot = options
|
|
|
|
.startup_snapshot
|
|
|
|
.unwrap_or_else(crate::js::deno_isolate_init);
|
|
|
|
#[cfg(feature = "dont_create_runtime_snapshot")]
|
|
|
|
let startup_snapshot = options.startup_snapshot
|
|
|
|
.expect("deno_runtime startup snapshot is not available with 'create_runtime_snapshot' Cargo feature.");
|
|
|
|
|
2020-10-07 15:20:20 +00:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2020-12-11 17:49:26 +00:00
|
|
|
module_loader: Some(options.module_loader.clone()),
|
2023-02-20 20:45:34 +00:00
|
|
|
startup_snapshot: Some(startup_snapshot),
|
2022-04-15 14:08:09 +00:00
|
|
|
source_map_getter: options.source_map_getter,
|
2020-12-13 18:45:53 +00:00
|
|
|
get_error_class_fn: options.get_error_class_fn,
|
2021-07-06 17:42:52 +00:00
|
|
|
shared_array_buffer_store: options.shared_array_buffer_store.clone(),
|
2021-09-29 08:47:24 +00:00
|
|
|
compiled_wasm_module_store: options.compiled_wasm_module_store.clone(),
|
2021-04-28 16:41:50 +00:00
|
|
|
extensions,
|
2022-12-19 02:55:50 +00:00
|
|
|
extensions_with_js: options.extensions_with_js,
|
2022-10-26 11:37:50 +00:00
|
|
|
inspector: options.maybe_inspector_server.is_some(),
|
2022-11-26 22:09:48 +00:00
|
|
|
is_main: true,
|
2020-09-11 13:18:49 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-25 08:24:51 +00:00
|
|
|
|
2021-06-21 17:37:51 +00:00
|
|
|
if let Some(server) = options.maybe_inspector_server.clone() {
|
2021-12-17 17:43:25 +00:00
|
|
|
server.register_inspector(
|
|
|
|
main_module.to_string(),
|
|
|
|
&mut js_runtime,
|
2022-12-12 14:33:30 +00:00
|
|
|
options.should_break_on_first_statement
|
|
|
|
|| options.should_wait_for_inspector_session,
|
2021-12-17 17:43:25 +00:00
|
|
|
);
|
2022-11-28 20:59:36 +00:00
|
|
|
|
|
|
|
// Put inspector handle into the op state so we can put a breakpoint when
|
|
|
|
// executing a CJS entrypoint.
|
|
|
|
let op_state = js_runtime.op_state();
|
|
|
|
let inspector = js_runtime.inspector();
|
|
|
|
op_state.borrow_mut().put(inspector);
|
2021-05-26 19:07:12 +00:00
|
|
|
}
|
2020-02-05 07:40:38 +00:00
|
|
|
|
2023-02-22 00:55:31 +00:00
|
|
|
let bootstrap_fn_global = {
|
|
|
|
let context = js_runtime.global_context();
|
|
|
|
let scope = &mut js_runtime.handle_scope();
|
|
|
|
let context_local = v8::Local::new(scope, context);
|
|
|
|
let global_obj = context_local.global(scope);
|
2023-03-06 16:37:46 +00:00
|
|
|
let bootstrap_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"bootstrap").unwrap();
|
2023-02-22 00:55:31 +00:00
|
|
|
let bootstrap_ns: v8::Local<v8::Object> = global_obj
|
|
|
|
.get(scope, bootstrap_str.into())
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2023-03-06 16:37:46 +00:00
|
|
|
let main_runtime_str =
|
|
|
|
v8::String::new_external_onebyte_static(scope, b"mainRuntime").unwrap();
|
2023-02-22 00:55:31 +00:00
|
|
|
let bootstrap_fn =
|
|
|
|
bootstrap_ns.get(scope, main_runtime_str.into()).unwrap();
|
|
|
|
let bootstrap_fn =
|
|
|
|
v8::Local::<v8::Function>::try_from(bootstrap_fn).unwrap();
|
|
|
|
v8::Global::new(scope, bootstrap_fn)
|
|
|
|
};
|
|
|
|
|
2021-05-02 23:22:57 +00:00
|
|
|
Self {
|
2020-10-11 11:20:40 +00:00
|
|
|
js_runtime,
|
2021-06-21 17:37:51 +00:00
|
|
|
should_break_on_first_statement: options.should_break_on_first_statement,
|
2022-12-12 14:33:30 +00:00
|
|
|
should_wait_for_inspector_session: options
|
|
|
|
.should_wait_for_inspector_session,
|
2022-06-08 15:45:38 +00:00
|
|
|
exit_code,
|
2023-02-22 00:55:31 +00:00
|
|
|
bootstrap_fn_global: Some(bootstrap_fn_global),
|
2020-06-02 02:44:17 +00:00
|
|
|
}
|
2020-12-11 17:49:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:41:14 +00:00
|
|
|
pub fn bootstrap(&mut self, options: &BootstrapOptions) {
|
2023-02-22 00:55:31 +00:00
|
|
|
let scope = &mut self.js_runtime.handle_scope();
|
|
|
|
let options_v8 =
|
|
|
|
deno_core::serde_v8::to_v8(scope, options.as_json()).unwrap();
|
|
|
|
let bootstrap_fn = self.bootstrap_fn_global.take().unwrap();
|
|
|
|
let bootstrap_fn = v8::Local::new(scope, bootstrap_fn);
|
|
|
|
let undefined = v8::undefined(scope);
|
|
|
|
bootstrap_fn
|
|
|
|
.call(scope, undefined.into(), &[options_v8])
|
|
|
|
.unwrap();
|
2020-01-21 16:50:06 +00:00
|
|
|
}
|
2020-09-28 10:14:11 +00:00
|
|
|
|
2021-06-21 23:45:41 +00:00
|
|
|
/// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script)
|
|
|
|
pub fn execute_script(
|
|
|
|
&mut self,
|
2021-12-21 14:49:27 +00:00
|
|
|
script_name: &str,
|
2021-06-21 23:45:41 +00:00
|
|
|
source_code: &str,
|
2022-12-18 20:34:33 +00:00
|
|
|
) -> Result<v8::Global<v8::Value>, AnyError> {
|
|
|
|
self.js_runtime.execute_script(script_name, source_code)
|
2020-09-28 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 22:10:51 +00:00
|
|
|
/// Loads and instantiates specified JavaScript module as "main" module.
|
|
|
|
pub async fn preload_main_module(
|
2020-11-26 14:17:45 +00:00
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleId, AnyError> {
|
2022-08-10 22:10:51 +00: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 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 21:29:48 +00:00
|
|
|
/// Executes specified JavaScript module.
|
|
|
|
pub async fn evaluate_module(
|
|
|
|
&mut self,
|
|
|
|
id: ModuleId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 22:10:51 +00:00
|
|
|
self.wait_for_inspector_session();
|
2021-03-04 12:19:47 +00:00
|
|
|
let mut receiver = self.js_runtime.mod_evaluate(id);
|
|
|
|
tokio::select! {
|
2022-04-13 09:50:57 +00:00
|
|
|
// Not using biased mode leads to non-determinism for relatively simple
|
|
|
|
// programs.
|
|
|
|
biased;
|
|
|
|
|
2021-07-30 11:36:43 +00:00
|
|
|
maybe_result = &mut receiver => {
|
2021-03-04 12:19:47 +00:00
|
|
|
debug!("received module evaluate {:#?}", maybe_result);
|
2021-06-17 19:56:30 +00:00
|
|
|
maybe_result.expect("Module evaluation result not provided.")
|
2021-03-04 12:19:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:07:12 +00:00
|
|
|
event_loop_result = self.run_event_loop(false) => {
|
2021-03-04 12:19:47 +00:00
|
|
|
event_loop_result?;
|
2021-07-30 11:36:43 +00:00
|
|
|
let maybe_result = receiver.await;
|
2021-06-17 19:56:30 +00:00
|
|
|
maybe_result.expect("Module evaluation result not provided.")
|
2021-03-04 12:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:44:53 +00:00
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
pub async fn execute_side_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-10 22:10:51 +00:00
|
|
|
let id = self.preload_side_module(module_specifier).await?;
|
2021-09-18 01:44:53 +00: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 22:10:51 +00:00
|
|
|
let id = self.preload_main_module(module_specifier).await?;
|
2021-09-18 01:44:53 +00:00
|
|
|
self.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
2020-11-26 14:17:45 +00:00
|
|
|
fn wait_for_inspector_session(&mut self) {
|
|
|
|
if self.should_break_on_first_statement {
|
|
|
|
self
|
2021-05-26 19:07:12 +00:00
|
|
|
.js_runtime
|
|
|
|
.inspector()
|
2022-09-02 10:43:39 +00:00
|
|
|
.borrow_mut()
|
2022-12-12 14:33:30 +00:00
|
|
|
.wait_for_session_and_break_on_next_statement();
|
|
|
|
} else if self.should_wait_for_inspector_session {
|
|
|
|
self.js_runtime.inspector().borrow_mut().wait_for_session();
|
2020-09-28 10:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 14:17:45 +00:00
|
|
|
/// Create new inspector session. This function panics if Worker
|
|
|
|
/// was not configured to create inspector.
|
2021-05-26 15:47:33 +00:00
|
|
|
pub async fn create_inspector_session(&mut self) -> LocalInspectorSession {
|
2022-10-26 11:37:50 +00:00
|
|
|
self.js_runtime.maybe_init_inspector();
|
2022-09-02 10:43:39 +00:00
|
|
|
self.js_runtime.inspector().borrow().create_local_session()
|
2020-09-28 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 11:20:40 +00:00
|
|
|
pub fn poll_event_loop(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context,
|
2021-05-26 19:07:12 +00:00
|
|
|
wait_for_inspector: bool,
|
2020-10-11 11:20:40 +00:00
|
|
|
) -> Poll<Result<(), AnyError>> {
|
2021-05-26 19:07:12 +00:00
|
|
|
self.js_runtime.poll_event_loop(cx, wait_for_inspector)
|
2020-09-28 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:07:12 +00:00
|
|
|
pub async fn run_event_loop(
|
|
|
|
&mut self,
|
|
|
|
wait_for_inspector: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-06-29 18:39:28 +00:00
|
|
|
self.js_runtime.run_event_loop(wait_for_inspector).await
|
2020-10-11 11:20:40 +00:00
|
|
|
}
|
2021-05-26 15:47:33 +00: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 09:26:57 +00:00
|
|
|
biased;
|
2021-05-26 15:47:33 +00:00
|
|
|
result = &mut fut => {
|
|
|
|
return result;
|
|
|
|
}
|
2021-05-26 19:07:12 +00:00
|
|
|
_ = self.run_event_loop(false) => {}
|
2021-05-26 15:47:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-12-11 14:56:45 +00:00
|
|
|
|
|
|
|
/// Return exit code set by the executed code (either in main worker
|
|
|
|
/// or one of child web workers).
|
2023-01-05 19:29:50 +00:00
|
|
|
pub fn exit_code(&self) -> i32 {
|
2022-06-08 15:45:38 +00:00
|
|
|
self.exit_code.get()
|
2021-12-11 14:56:45 +00:00
|
|
|
}
|
2021-12-21 14:49:27 +00: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 23:13:14 +00: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'))",
|
2022-12-18 20:34:33 +00:00
|
|
|
)?;
|
|
|
|
Ok(())
|
2021-12-21 14:49:27 +00: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 23:13:14 +00: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'))",
|
2022-12-18 20:34:33 +00:00
|
|
|
)?;
|
|
|
|
Ok(())
|
2021-12-21 14:49:27 +00:00
|
|
|
}
|
2022-06-28 14:49:30 +00: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 11:20:40 +00:00
|
|
|
}
|