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;
|
2021-04-28 16:41:50 +00:00
|
|
|
use crate::metrics;
|
2019-10-11 18:41:54 +00:00
|
|
|
use crate::ops;
|
2020-09-19 23:17:35 +00:00
|
|
|
use crate::permissions::Permissions;
|
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;
|
2020-12-11 17:49:26 +00:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
2020-09-16 18:28:07 +00:00
|
|
|
use deno_core::url::Url;
|
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;
|
2019-08-09 23:33:59 +00:00
|
|
|
use std::env;
|
2021-05-26 15:47:33 +00:00
|
|
|
use std::pin::Pin;
|
2020-11-30 19:35:12 +00:00
|
|
|
use std::rc::Rc;
|
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 {
|
|
|
|
pub apply_source_maps: bool,
|
2020-12-11 23:36:18 +00:00
|
|
|
/// Sets `Deno.args` in JS runtime.
|
2020-12-11 17:49:26 +00:00
|
|
|
pub args: Vec<String>,
|
|
|
|
pub debug_flag: bool,
|
|
|
|
pub unstable: bool,
|
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>,
|
|
|
|
// 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 maybe_inspector_server: Option<Arc<InspectorServer>>,
|
|
|
|
pub should_break_on_first_statement: bool,
|
2020-12-11 23:36:18 +00: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 18:45:53 +00:00
|
|
|
pub get_error_class_fn: Option<GetErrorClassFn>,
|
2021-01-07 18:06:08 +00:00
|
|
|
pub location: Option<Url>,
|
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-07-29 19:45:11 +00:00
|
|
|
pub cpu_count: usize,
|
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 {
|
2020-11-30 19:35:12 +00:00
|
|
|
pub fn from_options(
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
2020-12-11 17:49:26 +00:00
|
|
|
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
|
|
|
|
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 16:41:50 +00:00
|
|
|
// Internal modules
|
|
|
|
let extensions: Vec<Extension> = vec![
|
|
|
|
// Web APIs
|
|
|
|
deno_webidl::init(),
|
|
|
|
deno_console::init(),
|
|
|
|
deno_url::init(),
|
2021-07-05 13:34:37 +00:00
|
|
|
deno_web::init(options.blob_store.clone(), options.location.clone()),
|
2021-04-28 16:41:50 +00:00
|
|
|
deno_fetch::init::<Permissions>(
|
|
|
|
options.user_agent.clone(),
|
2021-08-07 12:49:38 +00:00
|
|
|
options.root_cert_store.clone(),
|
2021-06-22 03:21:57 +00:00
|
|
|
None,
|
2021-07-27 23:04:08 +00:00
|
|
|
None,
|
2021-08-10 11:19:45 +00:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
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-05-22 16:08:24 +00:00
|
|
|
deno_broadcast_channel::init(
|
|
|
|
options.broadcast_channel.clone(),
|
|
|
|
options.unstable,
|
|
|
|
),
|
2021-04-28 16:41:50 +00:00
|
|
|
deno_webgpu::init(options.unstable),
|
|
|
|
deno_timers::init::<Permissions>(),
|
2021-08-06 21:28:10 +00:00
|
|
|
// ffi
|
|
|
|
deno_ffi::init::<Permissions>(options.unstable),
|
2021-04-28 16:41:50 +00:00
|
|
|
// Metrics
|
|
|
|
metrics::init(),
|
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(),
|
|
|
|
options.unstable,
|
2021-08-10 11:19:45 +00:00
|
|
|
options.unsafely_ignore_certificate_errors.clone(),
|
2021-08-07 12:49:38 +00:00
|
|
|
),
|
2021-05-02 23:22:57 +00:00
|
|
|
ops::os::init(),
|
|
|
|
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
|
|
|
];
|
|
|
|
|
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-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() {
|
|
|
|
let inspector = js_runtime.inspector();
|
|
|
|
let session_sender = inspector.get_session_sender();
|
|
|
|
let deregister_rx = inspector.add_deregister_handler();
|
2021-06-30 16:01:11 +00:00
|
|
|
server.register_inspector(
|
|
|
|
session_sender,
|
|
|
|
deregister_rx,
|
|
|
|
main_module.to_string(),
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 23:36:18 +00:00
|
|
|
"denoVersion": options.runtime_version,
|
|
|
|
"noColor": options.no_color,
|
2020-12-11 17:49:26 +00:00
|
|
|
"pid": std::process::id(),
|
|
|
|
"ppid": ops::runtime::ppid(),
|
|
|
|
"target": env!("TARGET"),
|
2020-12-11 23:36:18 +00:00
|
|
|
"tsVersion": options.ts_version,
|
2020-12-11 17:49:26 +00:00
|
|
|
"unstableFlag": options.unstable,
|
|
|
|
"v8Version": deno_core::v8_version(),
|
2021-01-07 18:06:08 +00:00
|
|
|
"location": options.location,
|
2021-07-29 19:45:11 +00:00
|
|
|
"cpuCount": options.cpu_count,
|
2020-12-11 17:49:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let script = format!(
|
|
|
|
"bootstrap.mainRuntime({})",
|
|
|
|
serde_json::to_string_pretty(&runtime_options).unwrap()
|
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-26 14:17:45 +00: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 10:14:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 14:17:45 +00: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 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
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
apply_source_maps: false,
|
2020-12-11 23:36:18 +00:00
|
|
|
user_agent: "x".to_string(),
|
2020-12-11 17:49:26 +00:00
|
|
|
args: vec![],
|
|
|
|
debug_flag: false,
|
|
|
|
unstable: false,
|
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-11 23:36:18 +00:00
|
|
|
runtime_version: "x".to_string(),
|
|
|
|
ts_version: "x".to_string(),
|
|
|
|
no_color: true,
|
2020-12-13 18:45:53 +00:00
|
|
|
get_error_class_fn: None,
|
2021-01-07 18:06:08 +00:00
|
|
|
location: 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-07-29 19:45:11 +00:00
|
|
|
cpu_count: 1,
|
2020-09-26 16:16:33 +00:00
|
|
|
};
|
2020-12-11 17:49:26 +00:00
|
|
|
|
|
|
|
MainWorker::from_options(main_module, permissions, &options)
|
2020-09-26 16:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_esm_imports_a() {
|
2019-09-04 21:16:46 +00:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2020-02-02 21:55:22 +00:00
|
|
|
.join("cli/tests/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();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
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();
|
2020-02-18 15:08:18 +00:00
|
|
|
let result = worker.execute_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();
|
2020-04-16 14:29:28 +00:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
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();
|
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2020-12-11 17:49:26 +00:00
|
|
|
.join("cli/tests/001_hello.js");
|
2021-02-17 18:47:18 +00:00
|
|
|
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-04-16 14:29:28 +00:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|