2021-01-11 02:59:07 +00:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2021-05-26 19:07:12 +00:00
|
|
|
use crate::inspector_server::InspectorServer;
|
2020-09-11 13:18:49 +00:00
|
|
|
use crate::js;
|
2019-10-11 18:41:54 +00:00
|
|
|
use crate::ops;
|
2020-09-19 23:17:35 +00:00
|
|
|
use crate::permissions::Permissions;
|
2021-10-05 20:41:14 +00:00
|
|
|
use crate::BootstrapOptions;
|
2021-05-22 16:08:24 +00:00
|
|
|
use deno_broadcast_channel::InMemoryBroadcastChannel;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2021-05-26 15:47:33 +00:00
|
|
|
use deno_core::futures::Future;
|
2021-06-21 23:45:41 +00:00
|
|
|
use deno_core::located_script_name;
|
2021-09-29 08:47:24 +00:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2021-04-28 16:41:50 +00:00
|
|
|
use deno_core::Extension;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_core::GetErrorClassFn;
|
2020-12-01 22:33:44 +00:00
|
|
|
use deno_core::JsErrorCreateFn;
|
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;
|
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;
|
2021-05-26 15:47:33 +00:00
|
|
|
use std::pin::Pin;
|
2020-11-30 19:35:12 +00:00
|
|
|
use std::rc::Rc;
|
2021-12-11 14:56:45 +00:00
|
|
|
use std::sync::atomic::AtomicI32;
|
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
2019-06-05 20:35:38 +00:00
|
|
|
use std::sync::Arc;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2018-10-05 17:21:15 +00:00
|
|
|
|
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,
|
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,
|
2021-10-08 15:03:49 +00:00
|
|
|
pub extensions: Vec<Extension>,
|
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 23:36:18 +00:00
|
|
|
pub user_agent: String,
|
2020-12-11 17:49:26 +00:00
|
|
|
pub seed: Option<u64>,
|
|
|
|
pub module_loader: Rc<dyn ModuleLoader>,
|
2021-09-24 20:25:45 +00:00
|
|
|
// Callback 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>,
|
|
|
|
pub js_error_create_fn: Option<Rc<JsErrorCreateFn>>,
|
|
|
|
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
|
|
|
|
pub should_break_on_first_statement: bool,
|
2020-12-13 18:45:53 +00:00
|
|
|
pub get_error_class_fn: Option<GetErrorClassFn>,
|
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,
|
2021-07-06 17:42:52 +00:00
|
|
|
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
2021-09-29 08:47:24 +00:00
|
|
|
pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
|
2020-12-11 17:49:26 +00:00
|
|
|
}
|
2020-11-30 19:35:12 +00:00
|
|
|
|
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,
|
|
|
|
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 19:35:12 +00:00
|
|
|
pub fn from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
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;
|
2021-05-02 23:22:57 +00:00
|
|
|
let perm_ext = Extension::builder()
|
|
|
|
.state(move |state| {
|
|
|
|
state.put::<Permissions>(permissions.clone());
|
|
|
|
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();
|
|
|
|
|
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(),
|
2021-10-05 20:41:14 +00:00
|
|
|
deno_web::init(
|
|
|
|
options.blob_store.clone(),
|
|
|
|
options.bootstrap.location.clone(),
|
|
|
|
),
|
2021-11-28 18:07:03 +00:00
|
|
|
deno_fetch::init::<Permissions>(deno_fetch::Options {
|
|
|
|
user_agent: options.user_agent.clone(),
|
|
|
|
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()
|
|
|
|
}),
|
2021-04-28 16:41:50 +00:00
|
|
|
deno_websocket::init::<Permissions>(
|
|
|
|
options.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-04-28 16:41:50 +00:00
|
|
|
deno_crypto::init(options.seed),
|
2021-10-05 20:41:14 +00:00
|
|
|
deno_broadcast_channel::init(options.broadcast_channel.clone(), unstable),
|
|
|
|
deno_webgpu::init(unstable),
|
2021-04-28 16:41:50 +00:00
|
|
|
deno_timers::init::<Permissions>(),
|
2021-08-06 21:28:10 +00:00
|
|
|
// ffi
|
2021-10-05 20:41:14 +00:00
|
|
|
deno_ffi::init::<Permissions>(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()),
|
2021-05-11 19:09:09 +00:00
|
|
|
ops::worker_host::init(options.create_web_worker_cb.clone()),
|
2021-05-02 23:22:57 +00:00
|
|
|
ops::fs_events::init(),
|
|
|
|
ops::fs::init(),
|
|
|
|
ops::io::init(),
|
|
|
|
ops::io::init_stdio(),
|
2021-08-07 12:49:38 +00:00
|
|
|
deno_tls::init(),
|
|
|
|
deno_net::init::<Permissions>(
|
|
|
|
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
|
|
|
),
|
2021-12-11 14:56:45 +00:00
|
|
|
ops::os::init(None),
|
2021-05-02 23:22:57 +00:00
|
|
|
ops::permissions::init(),
|
|
|
|
ops::process::init(),
|
|
|
|
ops::signal::init(),
|
|
|
|
ops::tty::init(),
|
2021-07-12 10:44:49 +00:00
|
|
|
deno_http::init(),
|
|
|
|
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
|
|
|
|
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()),
|
2020-11-26 14:17:45 +00:00
|
|
|
startup_snapshot: Some(js::deno_isolate_init()),
|
2020-12-11 17:49:26 +00:00
|
|
|
js_error_create_fn: options.js_error_create_fn.clone(),
|
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,
|
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-08-25 11:39:23 +00:00
|
|
|
server.register_inspector(main_module.to_string(), &mut js_runtime);
|
2021-05-26 19:07:12 +00:00
|
|
|
}
|
2020-02-05 07:40:38 +00:00
|
|
|
|
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,
|
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) {
|
|
|
|
let script = format!("bootstrap.mainRuntime({})", options.as_json());
|
2020-12-11 17:49:26 +00:00
|
|
|
self
|
2021-06-21 23:45:41 +00:00
|
|
|
.execute_script(&located_script_name!(), &script)
|
2020-09-26 16:16:33 +00:00
|
|
|
.expect("Failed to execute bootstrap script");
|
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,
|
|
|
|
name: &str,
|
|
|
|
source_code: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-07-08 16:56:53 +00:00
|
|
|
self.js_runtime.execute_script(name, source_code)?;
|
|
|
|
Ok(())
|
2020-09-28 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:44:53 +00:00
|
|
|
/// Loads and instantiates specified JavaScript module
|
|
|
|
/// as "main" or "side" module.
|
2020-11-26 14:17:45 +00:00
|
|
|
pub async fn preload_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2021-09-18 01:44:53 +00:00
|
|
|
main: bool,
|
2020-11-26 14:17:45 +00:00
|
|
|
) -> Result<ModuleId, AnyError> {
|
2021-09-18 01:44:53 +00:00
|
|
|
if main {
|
|
|
|
self
|
|
|
|
.js_runtime
|
|
|
|
.load_main_module(module_specifier, None)
|
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
.js_runtime
|
|
|
|
.load_side_module(module_specifier, None)
|
|
|
|
.await
|
|
|
|
}
|
2020-09-28 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:44:53 +00:00
|
|
|
async fn evaluate_module(&mut self, id: ModuleId) -> Result<(), AnyError> {
|
2021-03-04 12:19:47 +00:00
|
|
|
let mut receiver = self.js_runtime.mod_evaluate(id);
|
|
|
|
tokio::select! {
|
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> {
|
|
|
|
let id = self.preload_module(module_specifier, false).await?;
|
2021-12-15 23:38:27 +00:00
|
|
|
self.wait_for_inspector_session();
|
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> {
|
|
|
|
let id = self.preload_module(module_specifier, true).await?;
|
|
|
|
self.wait_for_inspector_session();
|
|
|
|
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()
|
2020-11-26 14:17:45 +00:00
|
|
|
.wait_for_session_and_break_on_next_statement()
|
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 {
|
2021-06-21 17:37:51 +00:00
|
|
|
let inspector = self.js_runtime.inspector();
|
2021-05-26 15:47:33 +00:00
|
|
|
inspector.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! {
|
|
|
|
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).
|
|
|
|
pub fn get_exit_code(&mut self) -> i32 {
|
|
|
|
let op_state_rc = self.js_runtime.op_state();
|
|
|
|
let op_state = op_state_rc.borrow();
|
|
|
|
let exit_code = op_state.borrow::<Arc<AtomicI32>>().load(Relaxed);
|
|
|
|
exit_code
|
|
|
|
}
|
2020-10-11 11:20:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 18:53:16 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-02-17 18:47:18 +00:00
|
|
|
use deno_core::resolve_url_or_path;
|
2019-01-01 11:24:05 +00:00
|
|
|
|
2020-09-26 16:16:33 +00:00
|
|
|
fn create_test_worker() -> MainWorker {
|
2021-02-17 18:47:18 +00:00
|
|
|
let main_module = resolve_url_or_path("./hello.js").unwrap();
|
2020-12-11 17:49:26 +00:00
|
|
|
let permissions = Permissions::default();
|
|
|
|
|
|
|
|
let options = WorkerOptions {
|
2021-10-05 20:41:14 +00:00
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
apply_source_maps: false,
|
|
|
|
args: vec![],
|
|
|
|
cpu_count: 1,
|
|
|
|
debug_flag: false,
|
|
|
|
enable_testing_features: false,
|
|
|
|
location: None,
|
|
|
|
no_color: true,
|
|
|
|
runtime_version: "x".to_string(),
|
|
|
|
ts_version: "x".to_string(),
|
|
|
|
unstable: false,
|
|
|
|
},
|
2021-10-08 15:03:49 +00:00
|
|
|
extensions: vec![],
|
2020-12-11 23:36:18 +00:00
|
|
|
user_agent: "x".to_string(),
|
2021-08-10 11:19:45 +00:00
|
|
|
unsafely_ignore_certificate_errors: None,
|
2021-08-07 12:49:38 +00:00
|
|
|
root_cert_store: None,
|
2020-12-11 17:49:26 +00:00
|
|
|
seed: None,
|
|
|
|
js_error_create_fn: None,
|
|
|
|
create_web_worker_cb: Arc::new(|_| unreachable!()),
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
|
|
|
module_loader: Rc::new(deno_core::FsModuleLoader),
|
2020-12-13 18:45:53 +00:00
|
|
|
get_error_class_fn: None,
|
2021-05-27 05:23:12 +00:00
|
|
|
origin_storage_dir: None,
|
2021-07-05 13:34:37 +00:00
|
|
|
blob_store: BlobStore::default(),
|
2021-05-22 16:08:24 +00:00
|
|
|
broadcast_channel: InMemoryBroadcastChannel::default(),
|
2021-07-06 17:42:52 +00:00
|
|
|
shared_array_buffer_store: None,
|
2021-09-29 08:47:24 +00:00
|
|
|
compiled_wasm_module_store: None,
|
2020-09-26 16:16:33 +00:00
|
|
|
};
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2021-10-05 20:41:14 +00:00
|
|
|
MainWorker::bootstrap_from_options(main_module, permissions, options)
|
2020-09-26 16:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_esm_imports_a() {
|
2021-08-11 14:20:47 +00:00
|
|
|
let p = test_util::testdata_path().join("esm_imports_a.js");
|
2021-02-17 18:47:18 +00:00
|
|
|
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 16:16:33 +00:00
|
|
|
let mut worker = create_test_worker();
|
2021-09-18 01:44:53 +00:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-09-26 16:16:33 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2021-05-26 19:07:12 +00:00
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2020-09-26 16:16:33 +00:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-01-30 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 16:16:33 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_circular() {
|
2019-09-04 21:16:46 +00:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2020-12-11 17:49:26 +00:00
|
|
|
.join("tests/circular1.js");
|
2021-02-17 18:47:18 +00:00
|
|
|
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 16:16:33 +00:00
|
|
|
let mut worker = create_test_worker();
|
2021-09-18 01:44:53 +00:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-02-03 23:08:44 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2021-05-26 19:07:12 +00:00
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2020-02-03 23:08:44 +00:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-04-08 21:10:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 14:29:28 +00: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 18:47:18 +00:00
|
|
|
let module_specifier = resolve_url_or_path("does-not-exist").unwrap();
|
2021-09-18 01:44:53 +00:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-04-16 14:29:28 +00:00
|
|
|
assert!(result.is_err());
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 14:29:28 +00: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();
|
2021-08-11 14:20:47 +00:00
|
|
|
let p = test_util::testdata_path().join("001_hello.js");
|
2021-02-17 18:47:18 +00:00
|
|
|
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2021-09-18 01:44:53 +00:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
2020-04-16 14:29:28 +00:00
|
|
|
assert!(result.is_ok());
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|