2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-10-20 00:05:42 -04:00
|
|
|
use crate::colors;
|
2020-11-19 14:37:22 -05:00
|
|
|
use crate::fmt_errors::PrettyJsError;
|
2020-04-03 13:40:11 -04:00
|
|
|
use crate::inspector::DenoInspector;
|
2020-10-11 07:20:40 -04:00
|
|
|
use crate::inspector::InspectorSession;
|
2020-09-11 09:18:49 -04:00
|
|
|
use crate::js;
|
2020-09-18 14:39:47 -04:00
|
|
|
use crate::metrics::Metrics;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::module_loader::CliModuleLoader;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops;
|
2020-06-01 22:44:17 -04:00
|
|
|
use crate::ops::io::get_stdio;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2020-11-19 14:37:22 -05:00
|
|
|
use crate::source_maps::apply_source_map;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::channel::mpsc;
|
2020-10-11 07:20:40 -04:00
|
|
|
use deno_core::futures::future::poll_fn;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::futures::stream::StreamExt;
|
|
|
|
use deno_core::futures::task::AtomicWaker;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2020-09-28 06:14:11 -04:00
|
|
|
use deno_core::v8;
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-02-18 10:08:18 -05:00
|
|
|
use deno_core::ModuleId;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-11 09:18:49 -04:00
|
|
|
use deno_core::RuntimeOptions;
|
|
|
|
use deno_core::Snapshot;
|
2019-08-09 19:33:59 -04:00
|
|
|
use std::env;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2020-02-14 19:18:36 -05:00
|
|
|
use std::rc::Rc;
|
2020-09-28 06:14:11 -04:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use std::sync::atomic::Ordering;
|
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;
|
2020-01-08 09:06:04 -05:00
|
|
|
use tokio::sync::Mutex as AsyncMutex;
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
/// Events that are sent to host from child
|
|
|
|
/// worker.
|
|
|
|
pub enum WorkerEvent {
|
2020-09-05 20:34:02 -04:00
|
|
|
Message(Box<[u8]>),
|
2020-09-14 12:48:57 -04:00
|
|
|
Error(AnyError),
|
|
|
|
TerminalError(AnyError),
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
pub struct WorkerChannelsInternal {
|
|
|
|
pub sender: mpsc::Sender<WorkerEvent>,
|
2020-09-05 20:34:02 -04:00
|
|
|
pub receiver: mpsc::Receiver<Box<[u8]>>,
|
2020-02-03 18:08:44 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WorkerHandle {
|
2020-09-05 20:34:02 -04:00
|
|
|
pub sender: mpsc::Sender<Box<[u8]>>,
|
2020-02-11 04:04:59 -05:00
|
|
|
pub receiver: Arc<AsyncMutex<mpsc::Receiver<WorkerEvent>>>,
|
2020-02-05 02:40:38 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
impl WorkerHandle {
|
|
|
|
/// Post message to worker as a host.
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn post_message(&self, buf: Box<[u8]>) -> Result<(), AnyError> {
|
2020-02-11 04:04:59 -05:00
|
|
|
let mut sender = self.sender.clone();
|
2020-08-25 18:22:15 -04:00
|
|
|
sender.try_send(buf)?;
|
|
|
|
Ok(())
|
2020-02-05 02:40:38 -05:00
|
|
|
}
|
|
|
|
|
2020-05-17 00:50:38 -04:00
|
|
|
/// Get the event with lock.
|
|
|
|
/// Return error if more than one listener tries to get event
|
2020-09-14 12:48:57 -04:00
|
|
|
pub async fn get_event(&self) -> Result<Option<WorkerEvent>, AnyError> {
|
2020-05-17 00:50:38 -04:00
|
|
|
let mut receiver = self.receiver.try_lock()?;
|
|
|
|
Ok(receiver.next().await)
|
2020-02-05 02:40:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
fn create_channels() -> (WorkerChannelsInternal, WorkerHandle) {
|
2020-09-05 20:34:02 -04:00
|
|
|
let (in_tx, in_rx) = mpsc::channel::<Box<[u8]>>(1);
|
2020-02-11 04:04:59 -05:00
|
|
|
let (out_tx, out_rx) = mpsc::channel::<WorkerEvent>(1);
|
|
|
|
let internal_channels = WorkerChannelsInternal {
|
2020-02-05 02:40:38 -05:00
|
|
|
sender: out_tx,
|
2020-02-11 04:04:59 -05:00
|
|
|
receiver: in_rx,
|
|
|
|
};
|
|
|
|
let external_channels = WorkerHandle {
|
2020-02-05 02:40:38 -05:00
|
|
|
sender: in_tx,
|
|
|
|
receiver: Arc::new(AsyncMutex::new(out_rx)),
|
2020-02-11 04:04:59 -05:00
|
|
|
};
|
2020-02-05 02:40:38 -05:00
|
|
|
(internal_channels, external_channels)
|
|
|
|
}
|
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
/// Worker is a CLI wrapper for `deno_core::Isolate`.
|
|
|
|
///
|
|
|
|
/// It provides infrastructure to communicate with a worker and
|
|
|
|
/// consequently between workers.
|
|
|
|
///
|
|
|
|
/// This struct is meant to be used as a base struct for concrete
|
|
|
|
/// type of worker that registers set of ops.
|
|
|
|
///
|
2020-09-26 12:16:33 -04:00
|
|
|
/// Currently there are two types of workers:
|
2020-01-21 11:50:06 -05:00
|
|
|
/// - `MainWorker`
|
|
|
|
/// - `WebWorker`
|
2019-04-08 17:10:00 -04:00
|
|
|
pub struct Worker {
|
2020-10-10 05:41:11 -04:00
|
|
|
external_channels: WorkerHandle,
|
2020-10-11 07:20:40 -04:00
|
|
|
inspector: Option<Box<DenoInspector>>,
|
|
|
|
// Following fields are pub because they are accessed
|
|
|
|
// when creating a new WebWorker instance.
|
|
|
|
pub(crate) internal_channels: WorkerChannelsInternal,
|
|
|
|
pub(crate) js_runtime: JsRuntime,
|
|
|
|
pub(crate) name: String,
|
2020-09-19 19:17:35 -04:00
|
|
|
should_break_on_first_statement: bool,
|
2020-10-11 07:20:40 -04:00
|
|
|
waker: AtomicWaker,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
impl Worker {
|
2020-08-18 12:30:13 -04:00
|
|
|
pub fn new(
|
|
|
|
name: String,
|
2020-09-28 06:14:11 -04:00
|
|
|
startup_snapshot: Snapshot,
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state: Arc<ProgramState>,
|
2020-09-28 06:14:11 -04:00
|
|
|
module_loader: Rc<CliModuleLoader>,
|
2020-09-19 19:17:35 -04:00
|
|
|
is_main: bool,
|
2020-08-18 12:30:13 -04:00
|
|
|
) -> Self {
|
2020-10-13 07:35:35 -04:00
|
|
|
let global_state_ = program_state.clone();
|
2020-09-14 21:23:48 -04:00
|
|
|
|
2020-11-21 09:56:14 -05:00
|
|
|
let js_error_create_fn = Box::new(move |core_js_error| {
|
|
|
|
let source_mapped_error =
|
|
|
|
apply_source_map(&core_js_error, global_state_.clone());
|
|
|
|
PrettyJsError::create(source_mapped_error)
|
|
|
|
});
|
|
|
|
|
2020-10-07 11:20:20 -04:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2020-09-28 06:14:11 -04:00
|
|
|
module_loader: Some(module_loader),
|
|
|
|
startup_snapshot: Some(startup_snapshot),
|
2020-11-21 09:56:14 -05:00
|
|
|
js_error_create_fn: Some(js_error_create_fn),
|
|
|
|
get_error_class_fn: Some(&crate::errors::get_error_class_name),
|
2020-09-11 09:18:49 -04:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-25 04:24:51 -04:00
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
let inspector =
|
2020-10-13 07:35:35 -04:00
|
|
|
if let Some(inspector_server) = &program_state.maybe_inspector_server {
|
2020-09-26 12:16:33 -04:00
|
|
|
Some(DenoInspector::new(
|
2020-10-07 11:20:20 -04:00
|
|
|
&mut js_runtime,
|
2020-09-26 12:16:33 -04:00
|
|
|
Some(inspector_server.clone()),
|
|
|
|
))
|
2020-10-13 07:35:35 -04:00
|
|
|
} else if program_state.flags.coverage || program_state.flags.repl {
|
2020-10-07 11:20:20 -04:00
|
|
|
Some(DenoInspector::new(&mut js_runtime, None))
|
2020-09-26 12:16:33 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-11-09 15:07:14 -05:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
let should_break_on_first_statement = inspector.is_some()
|
|
|
|
&& is_main
|
2020-10-13 07:35:35 -04:00
|
|
|
&& program_state.flags.inspect_brk.is_some();
|
2020-09-19 19:17:35 -04:00
|
|
|
|
2020-02-05 02:40:38 -05:00
|
|
|
let (internal_channels, external_channels) = create_channels();
|
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
Self {
|
2020-10-11 07:20:40 -04:00
|
|
|
external_channels,
|
2020-05-24 16:28:19 -04:00
|
|
|
inspector,
|
2020-02-11 04:04:59 -05:00
|
|
|
internal_channels,
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime,
|
|
|
|
name,
|
2020-09-19 19:17:35 -04:00
|
|
|
should_break_on_first_statement,
|
2020-10-11 07:20:40 -04:00
|
|
|
waker: AtomicWaker::new(),
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
|
2019-08-09 19:33:59 -04:00
|
|
|
/// Same as execute2() but the filename defaults to "$CWD/__anonymous__".
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn execute(&mut self, js_source: &str) -> Result<(), AnyError> {
|
2019-08-09 19:33:59 -04:00
|
|
|
let path = env::current_dir().unwrap().join("__anonymous__");
|
|
|
|
let url = Url::from_file_path(path).unwrap();
|
|
|
|
self.execute2(url.as_str(), js_source)
|
2018-12-11 14:03:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes the provided JavaScript source code. The js_filename argument is
|
|
|
|
/// provided only for debugging purposes.
|
|
|
|
pub fn execute2(
|
2019-03-14 19:17:52 -04:00
|
|
|
&mut self,
|
2018-09-17 20:41:13 -04:00
|
|
|
js_filename: &str,
|
|
|
|
js_source: &str,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-10-07 11:20:20 -04:00
|
|
|
self.js_runtime.execute(js_filename, js_source)
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
/// Loads and instantiates specified JavaScript module.
|
|
|
|
pub async fn preload_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<ModuleId, AnyError> {
|
2020-10-07 11:20:20 -04:00
|
|
|
self.js_runtime.load_module(module_specifier, None).await
|
2020-02-18 10:08:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
pub async fn execute_module(
|
2019-06-05 16:35:38 -04:00
|
|
|
&mut self,
|
2019-06-12 15:00:08 -04:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-02-18 10:08:18 -05:00
|
|
|
let id = self.preload_module(module_specifier).await?;
|
2020-05-12 15:08:22 -04:00
|
|
|
self.wait_for_inspector_session();
|
2020-10-14 08:04:09 -04:00
|
|
|
self.js_runtime.mod_evaluate(id).await
|
2020-02-18 10:08:18 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
/// Returns a way to communicate with the Worker from other threads.
|
2020-02-11 04:04:59 -05:00
|
|
|
pub fn thread_safe_handle(&self) -> WorkerHandle {
|
2020-02-03 18:08:44 -05:00
|
|
|
self.external_channels.clone()
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2020-05-12 15:08:22 -04:00
|
|
|
|
2020-05-24 16:28:19 -04:00
|
|
|
fn wait_for_inspector_session(&mut self) {
|
2020-09-19 19:17:35 -04:00
|
|
|
if self.should_break_on_first_statement {
|
2020-05-12 15:08:22 -04:00
|
|
|
self
|
2020-05-24 16:28:19 -04:00
|
|
|
.inspector
|
2020-05-12 15:08:22 -04:00
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.wait_for_session_and_break_on_next_statement()
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 05:41:11 -04:00
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
/// Create new inspector session. This function panics if Worker
|
|
|
|
/// was not configured to create inspector.
|
|
|
|
pub fn create_inspector_session(&mut self) -> Box<InspectorSession> {
|
|
|
|
let inspector = self.inspector.as_mut().unwrap();
|
2020-10-10 05:41:11 -04:00
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
InspectorSession::new(&mut **inspector)
|
|
|
|
}
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
pub fn poll_event_loop(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Result<(), AnyError>> {
|
2020-05-12 15:08:22 -04:00
|
|
|
// We always poll the inspector if it exists.
|
2020-10-11 07:20:40 -04:00
|
|
|
let _ = self.inspector.as_mut().map(|i| i.poll_unpin(cx));
|
|
|
|
self.waker.register(cx.waker());
|
|
|
|
self.js_runtime.poll_event_loop(cx)
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
2019-03-28 16:05:41 -04:00
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
pub async fn run_event_loop(&mut self) -> Result<(), AnyError> {
|
|
|
|
poll_fn(|cx| self.poll_event_loop(cx)).await
|
2020-04-21 09:48:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
impl Drop for Worker {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// The Isolate object must outlive the Inspector object, but this is
|
|
|
|
// currently not enforced by the type system.
|
|
|
|
self.inspector.take();
|
2020-04-21 09:48:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
/// This worker is created and used by Deno executable.
|
|
|
|
///
|
|
|
|
/// It provides ops available in the `Deno` namespace.
|
|
|
|
///
|
2020-05-14 00:38:42 -04:00
|
|
|
/// All WebWorkers created during program execution are descendants of
|
2020-01-21 11:50:06 -05:00
|
|
|
/// this worker.
|
|
|
|
pub struct MainWorker(Worker);
|
|
|
|
|
|
|
|
impl MainWorker {
|
2020-09-26 12:16:33 -04:00
|
|
|
pub fn new(
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state: &Arc<ProgramState>,
|
2020-09-19 19:17:35 -04:00
|
|
|
main_module: ModuleSpecifier,
|
2020-11-02 17:37:55 -05:00
|
|
|
permissions: Permissions,
|
2020-09-11 09:18:49 -04:00
|
|
|
) -> Self {
|
2020-10-13 07:35:35 -04:00
|
|
|
let loader = CliModuleLoader::new(program_state.maybe_import_map.clone());
|
2020-09-19 19:17:35 -04:00
|
|
|
let mut worker = Worker::new(
|
2020-09-26 12:16:33 -04:00
|
|
|
"main".to_string(),
|
2020-09-28 06:14:11 -04:00
|
|
|
js::deno_isolate_init(),
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state.clone(),
|
2020-09-19 19:17:35 -04:00
|
|
|
loader,
|
|
|
|
true,
|
|
|
|
);
|
2020-10-11 07:20:40 -04:00
|
|
|
let js_runtime = &mut worker.js_runtime;
|
2020-01-21 11:50:06 -05:00
|
|
|
{
|
2020-09-28 06:14:11 -04:00
|
|
|
// All ops registered in this function depend on these
|
|
|
|
{
|
2020-10-11 07:20:40 -04:00
|
|
|
let op_state = js_runtime.op_state();
|
2020-09-28 06:14:11 -04:00
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
op_state.put::<Metrics>(Default::default());
|
2020-10-13 07:35:35 -04:00
|
|
|
op_state.put::<Arc<ProgramState>>(program_state.clone());
|
2020-11-02 17:37:55 -05:00
|
|
|
op_state.put::<Permissions>(permissions);
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::runtime::init(js_runtime, main_module);
|
2020-10-13 07:35:35 -04:00
|
|
|
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::timers::init(js_runtime);
|
2020-10-20 00:05:42 -04:00
|
|
|
ops::worker_host::init(js_runtime, None);
|
2020-11-13 16:01:57 -05:00
|
|
|
ops::crypto::init(js_runtime, program_state.flags.seed);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
|
|
|
|
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
|
2020-09-17 13:13:20 -04:00
|
|
|
ops::reg_json_sync(
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime,
|
2020-09-17 13:13:20 -04:00
|
|
|
"op_domain_to_ascii",
|
|
|
|
deno_web::op_domain_to_ascii,
|
|
|
|
);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::errors::init(js_runtime);
|
|
|
|
ops::fs_events::init(js_runtime);
|
|
|
|
ops::fs::init(js_runtime);
|
|
|
|
ops::io::init(js_runtime);
|
|
|
|
ops::net::init(js_runtime);
|
|
|
|
ops::os::init(js_runtime);
|
|
|
|
ops::permissions::init(js_runtime);
|
|
|
|
ops::plugin::init(js_runtime);
|
|
|
|
ops::process::init(js_runtime);
|
|
|
|
ops::runtime_compiler::init(js_runtime);
|
|
|
|
ops::signal::init(js_runtime);
|
|
|
|
ops::tls::init(js_runtime);
|
|
|
|
ops::tty::init(js_runtime);
|
|
|
|
ops::websocket::init(js_runtime);
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-06-01 22:44:17 -04:00
|
|
|
{
|
2020-10-11 07:20:40 -04:00
|
|
|
let op_state = js_runtime.op_state();
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
let t = &mut op_state.resource_table;
|
2020-09-05 20:34:02 -04:00
|
|
|
let (stdin, stdout, stderr) = get_stdio();
|
2020-07-09 15:06:51 -04:00
|
|
|
if let Some(stream) = stdin {
|
|
|
|
t.add("stdin", Box::new(stream));
|
|
|
|
}
|
|
|
|
if let Some(stream) = stdout {
|
|
|
|
t.add("stdout", Box::new(stream));
|
|
|
|
}
|
|
|
|
if let Some(stream) = stderr {
|
|
|
|
t.add("stderr", Box::new(stream));
|
|
|
|
}
|
2020-06-01 22:44:17 -04:00
|
|
|
}
|
2020-09-26 12:16:33 -04:00
|
|
|
worker
|
|
|
|
.execute("bootstrap.mainRuntime()")
|
|
|
|
.expect("Failed to execute bootstrap script");
|
|
|
|
Self(worker)
|
2020-06-01 22:44:17 -04:00
|
|
|
}
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for MainWorker {
|
|
|
|
type Target = Worker;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
impl DerefMut for MainWorker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:14:11 -04:00
|
|
|
/// Wrapper for `WorkerHandle` that adds functionality
|
|
|
|
/// for terminating workers.
|
|
|
|
///
|
|
|
|
/// This struct is used by host as well as worker itself.
|
|
|
|
///
|
|
|
|
/// Host uses it to communicate with worker and terminate it,
|
|
|
|
/// while worker uses it only to finish execution on `self.close()`.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WebWorkerHandle {
|
|
|
|
worker_handle: WorkerHandle,
|
|
|
|
terminate_tx: mpsc::Sender<()>,
|
|
|
|
terminated: Arc<AtomicBool>,
|
|
|
|
isolate_handle: v8::IsolateHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for WebWorkerHandle {
|
|
|
|
type Target = WorkerHandle;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.worker_handle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for WebWorkerHandle {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.worker_handle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebWorkerHandle {
|
|
|
|
pub fn terminate(&self) {
|
|
|
|
// This function can be called multiple times by whomever holds
|
|
|
|
// the handle. However only a single "termination" should occur so
|
|
|
|
// we need a guard here.
|
|
|
|
let already_terminated = self.terminated.swap(true, Ordering::Relaxed);
|
|
|
|
|
|
|
|
if !already_terminated {
|
|
|
|
self.isolate_handle.terminate_execution();
|
|
|
|
let mut sender = self.terminate_tx.clone();
|
|
|
|
// This call should be infallible hence the `expect`.
|
|
|
|
// This might change in the future.
|
|
|
|
sender.try_send(()).expect("Failed to terminate");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This worker is implementation of `Worker` Web API
|
|
|
|
///
|
|
|
|
/// At the moment this type of worker supports only
|
|
|
|
/// communication with parent and creating new workers.
|
|
|
|
///
|
|
|
|
/// Each `WebWorker` is either a child of `MainWorker` or other
|
|
|
|
/// `WebWorker`.
|
|
|
|
pub struct WebWorker {
|
|
|
|
worker: Worker,
|
|
|
|
event_loop_idle: bool,
|
|
|
|
terminate_rx: mpsc::Receiver<()>,
|
|
|
|
handle: WebWorkerHandle,
|
|
|
|
pub has_deno_namespace: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebWorker {
|
|
|
|
pub fn new(
|
|
|
|
name: String,
|
|
|
|
permissions: Permissions,
|
|
|
|
main_module: ModuleSpecifier,
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state: Arc<ProgramState>,
|
2020-09-28 06:14:11 -04:00
|
|
|
has_deno_namespace: bool,
|
|
|
|
) -> Self {
|
|
|
|
let loader = CliModuleLoader::new_for_worker();
|
|
|
|
let mut worker = Worker::new(
|
|
|
|
name,
|
|
|
|
js::deno_isolate_init(),
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state.clone(),
|
2020-09-28 06:14:11 -04:00
|
|
|
loader,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
let terminated = Arc::new(AtomicBool::new(false));
|
2020-10-07 11:20:20 -04:00
|
|
|
let isolate_handle = worker.js_runtime.v8_isolate().thread_safe_handle();
|
2020-09-28 06:14:11 -04:00
|
|
|
let (terminate_tx, terminate_rx) = mpsc::channel::<()>(1);
|
|
|
|
|
|
|
|
let handle = WebWorkerHandle {
|
|
|
|
worker_handle: worker.thread_safe_handle(),
|
|
|
|
terminated,
|
|
|
|
isolate_handle,
|
|
|
|
terminate_tx,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut web_worker = Self {
|
|
|
|
worker,
|
|
|
|
event_loop_idle: false,
|
|
|
|
terminate_rx,
|
|
|
|
handle,
|
|
|
|
has_deno_namespace,
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
let handle = web_worker.thread_safe_handle();
|
|
|
|
let sender = web_worker.worker.internal_channels.sender.clone();
|
2020-10-11 07:20:40 -04:00
|
|
|
let js_runtime = &mut web_worker.js_runtime;
|
2020-09-28 06:14:11 -04:00
|
|
|
// All ops registered in this function depend on these
|
|
|
|
{
|
2020-10-11 07:20:40 -04:00
|
|
|
let op_state = js_runtime.op_state();
|
2020-09-28 06:14:11 -04:00
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
op_state.put::<Metrics>(Default::default());
|
2020-10-13 07:35:35 -04:00
|
|
|
op_state.put::<Arc<ProgramState>>(program_state.clone());
|
2020-09-28 06:14:11 -04:00
|
|
|
op_state.put::<Permissions>(permissions);
|
|
|
|
}
|
|
|
|
|
2020-10-20 00:05:42 -04:00
|
|
|
ops::web_worker::init(js_runtime, sender.clone(), handle);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::runtime::init(js_runtime, main_module);
|
2020-10-13 07:35:35 -04:00
|
|
|
ops::fetch::init(js_runtime, program_state.flags.ca_file.as_deref());
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::timers::init(js_runtime);
|
2020-10-20 00:05:42 -04:00
|
|
|
ops::worker_host::init(js_runtime, Some(sender));
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::reg_json_sync(js_runtime, "op_close", deno_core::op_close);
|
|
|
|
ops::reg_json_sync(js_runtime, "op_resources", deno_core::op_resources);
|
2020-09-28 06:14:11 -04:00
|
|
|
ops::reg_json_sync(
|
2020-10-11 07:20:40 -04:00
|
|
|
js_runtime,
|
2020-09-28 06:14:11 -04:00
|
|
|
"op_domain_to_ascii",
|
|
|
|
deno_web::op_domain_to_ascii,
|
|
|
|
);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::errors::init(js_runtime);
|
|
|
|
ops::io::init(js_runtime);
|
|
|
|
ops::websocket::init(js_runtime);
|
2020-09-28 06:14:11 -04:00
|
|
|
|
|
|
|
if has_deno_namespace {
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::fs_events::init(js_runtime);
|
|
|
|
ops::fs::init(js_runtime);
|
|
|
|
ops::net::init(js_runtime);
|
|
|
|
ops::os::init(js_runtime);
|
|
|
|
ops::permissions::init(js_runtime);
|
|
|
|
ops::plugin::init(js_runtime);
|
|
|
|
ops::process::init(js_runtime);
|
2020-11-13 16:01:57 -05:00
|
|
|
ops::crypto::init(js_runtime, program_state.flags.seed);
|
2020-10-11 07:20:40 -04:00
|
|
|
ops::runtime_compiler::init(js_runtime);
|
|
|
|
ops::signal::init(js_runtime);
|
|
|
|
ops::tls::init(js_runtime);
|
|
|
|
ops::tty::init(js_runtime);
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
web_worker
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebWorker {
|
|
|
|
/// Returns a way to communicate with the Worker from other threads.
|
|
|
|
pub fn thread_safe_handle(&self) -> WebWorkerHandle {
|
|
|
|
self.handle.clone()
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
pub async fn run_event_loop(&mut self) -> Result<(), AnyError> {
|
|
|
|
poll_fn(|cx| self.poll_event_loop(cx)).await
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
pub fn poll_event_loop(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Result<(), AnyError>> {
|
|
|
|
let worker = &mut self.worker;
|
2020-10-10 05:41:11 -04:00
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
let terminated = self.handle.terminated.load(Ordering::Relaxed);
|
2020-09-28 06:14:11 -04:00
|
|
|
|
|
|
|
if terminated {
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
if !self.event_loop_idle {
|
|
|
|
match worker.poll_event_loop(cx) {
|
2020-09-28 06:14:11 -04:00
|
|
|
Poll::Ready(r) => {
|
2020-10-11 07:20:40 -04:00
|
|
|
let terminated = self.handle.terminated.load(Ordering::Relaxed);
|
2020-09-28 06:14:11 -04:00
|
|
|
if terminated {
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = r {
|
2020-10-20 00:05:42 -04:00
|
|
|
eprintln!(
|
|
|
|
"{}: Uncaught (in worker \"{}\") {}",
|
|
|
|
colors::red_bold("error"),
|
|
|
|
worker.name.to_string(),
|
|
|
|
e.to_string().trim_start_matches("Uncaught "),
|
|
|
|
);
|
2020-09-28 06:14:11 -04:00
|
|
|
let mut sender = worker.internal_channels.sender.clone();
|
|
|
|
sender
|
|
|
|
.try_send(WorkerEvent::Error(e))
|
|
|
|
.expect("Failed to post message to host");
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
self.event_loop_idle = true;
|
2020-09-28 06:14:11 -04:00
|
|
|
}
|
|
|
|
Poll::Pending => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Poll::Ready(r) = self.terminate_rx.poll_next_unpin(cx) {
|
2020-09-28 06:14:11 -04:00
|
|
|
// terminate_rx should never be closed
|
|
|
|
assert!(r.is_some());
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Poll::Ready(r) =
|
|
|
|
worker.internal_channels.receiver.poll_next_unpin(cx)
|
|
|
|
{
|
|
|
|
match r {
|
|
|
|
Some(msg) => {
|
|
|
|
let msg = String::from_utf8(msg.to_vec()).unwrap();
|
|
|
|
let script = format!("workerMessageRecvCallback({})", msg);
|
|
|
|
|
|
|
|
if let Err(e) = worker.execute(&script) {
|
|
|
|
// If execution was terminated during message callback then
|
|
|
|
// just ignore it
|
2020-10-11 07:20:40 -04:00
|
|
|
if self.handle.terminated.load(Ordering::Relaxed) {
|
2020-09-28 06:14:11 -04:00
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise forward error to host
|
|
|
|
let mut sender = worker.internal_channels.sender.clone();
|
|
|
|
sender
|
|
|
|
.try_send(WorkerEvent::Error(e))
|
|
|
|
.expect("Failed to post message to host");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let event loop be polled again
|
2020-10-11 07:20:40 -04:00
|
|
|
self.event_loop_idle = false;
|
2020-09-28 06:14:11 -04:00
|
|
|
worker.waker.wake();
|
|
|
|
}
|
|
|
|
None => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:20:40 -04:00
|
|
|
impl Deref for WebWorker {
|
|
|
|
type Target = Worker;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.worker
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for WebWorker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.worker
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-09-26 12:16:33 -04:00
|
|
|
use crate::flags::DenoSubcommand;
|
|
|
|
use crate::flags::Flags;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2020-09-28 06:14:11 -04:00
|
|
|
use crate::tokio_util;
|
|
|
|
use crate::worker::WorkerEvent;
|
|
|
|
use deno_core::serde_json::json;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
fn create_test_worker() -> MainWorker {
|
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
|
|
|
|
let flags = Flags {
|
|
|
|
subcommand: DenoSubcommand::Run {
|
|
|
|
script: main_module.to_string(),
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state =
|
|
|
|
ProgramState::mock(vec!["deno".to_string()], Some(flags));
|
2020-11-02 17:37:55 -05:00
|
|
|
MainWorker::new(&program_state, main_module, permissions)
|
2020-09-26 12:16:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_esm_imports_a() {
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2020-02-02 16:55:22 -05:00
|
|
|
.join("cli/tests/esm_imports_a.js");
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 12:16:33 -04:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Err(e) = worker.run_event_loop().await {
|
2020-09-26 12:16:33 -04:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2020-09-26 12:16:33 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_circular() {
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-12-23 09:59:44 -05:00
|
|
|
.join("tests/circular1.ts");
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 12:16:33 -04:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Err(e) = worker.run_event_loop().await {
|
2020-09-26 12:16:33 -04:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-01-01 06:24:05 -05:00
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_006_url_imports() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-12-23 09:59:44 -05:00
|
|
|
.join("cli/tests/006_url_imports.ts");
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-09-26 12:16:33 -04:00
|
|
|
let mut worker = create_test_worker();
|
2020-02-18 10:08:18 -05:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Err(e) = worker.run_event_loop().await {
|
2020-02-03 18:08:44 -05:00
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:29:28 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_resolve_error() {
|
|
|
|
// "foo" is not a valid module specifier so this should return an error.
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
assert!(result.is_err());
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:29:28 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_002_hello() {
|
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("cli/tests/002_hello.ts");
|
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2020-09-28 06:14:11 -04:00
|
|
|
|
|
|
|
fn create_test_web_worker() -> WebWorker {
|
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::mock(vec!["deno".to_string()], None);
|
2020-09-28 06:14:11 -04:00
|
|
|
let mut worker = WebWorker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
Permissions::allow_all(),
|
|
|
|
main_module,
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state,
|
2020-09-28 06:14:11 -04:00
|
|
|
false,
|
|
|
|
);
|
|
|
|
worker
|
|
|
|
.execute("bootstrap.workerRuntime(\"TEST\", false)")
|
|
|
|
.unwrap();
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_worker_messages() {
|
|
|
|
let (handle_sender, handle_receiver) =
|
|
|
|
std::sync::mpsc::sync_channel::<WebWorkerHandle>(1);
|
|
|
|
|
|
|
|
let join_handle = std::thread::spawn(move || {
|
|
|
|
let mut worker = create_test_web_worker();
|
|
|
|
let source = r#"
|
|
|
|
onmessage = function(e) {
|
|
|
|
console.log("msg from main script", e.data);
|
|
|
|
if (e.data == "exit") {
|
|
|
|
return close();
|
|
|
|
} else {
|
|
|
|
console.assert(e.data === "hi");
|
|
|
|
}
|
|
|
|
postMessage([1, 2, 3]);
|
|
|
|
console.log("after postMessage");
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
worker.execute(source).unwrap();
|
|
|
|
let handle = worker.thread_safe_handle();
|
|
|
|
handle_sender.send(handle).unwrap();
|
2020-10-11 07:20:40 -04:00
|
|
|
let r = tokio_util::run_basic(worker.run_event_loop());
|
2020-09-28 06:14:11 -04:00
|
|
|
assert!(r.is_ok())
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut handle = handle_receiver.recv().unwrap();
|
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
let r = handle.post_message(msg.clone());
|
|
|
|
assert!(r.is_ok());
|
|
|
|
|
|
|
|
let maybe_msg = handle.get_event().await.unwrap();
|
|
|
|
assert!(maybe_msg.is_some());
|
|
|
|
|
|
|
|
let r = handle.post_message(msg.clone());
|
|
|
|
assert!(r.is_ok());
|
|
|
|
|
|
|
|
let maybe_msg = handle.get_event().await.unwrap();
|
|
|
|
assert!(maybe_msg.is_some());
|
|
|
|
match maybe_msg {
|
|
|
|
Some(WorkerEvent::Message(buf)) => {
|
|
|
|
assert_eq!(*buf, *b"[1,2,3]");
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = json!("exit")
|
|
|
|
.to_string()
|
|
|
|
.into_boxed_str()
|
|
|
|
.into_boxed_bytes();
|
|
|
|
let r = handle.post_message(msg);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let event = handle.get_event().await.unwrap();
|
|
|
|
assert!(event.is_none());
|
|
|
|
handle.sender.close_channel();
|
|
|
|
join_handle.join().expect("Failed to join worker thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn removed_from_resource_table_on_close() {
|
|
|
|
let (handle_sender, handle_receiver) =
|
|
|
|
std::sync::mpsc::sync_channel::<WebWorkerHandle>(1);
|
|
|
|
|
|
|
|
let join_handle = std::thread::spawn(move || {
|
|
|
|
let mut worker = create_test_web_worker();
|
|
|
|
worker.execute("onmessage = () => { close(); }").unwrap();
|
|
|
|
let handle = worker.thread_safe_handle();
|
|
|
|
handle_sender.send(handle).unwrap();
|
2020-10-11 07:20:40 -04:00
|
|
|
let r = tokio_util::run_basic(worker.run_event_loop());
|
2020-09-28 06:14:11 -04:00
|
|
|
assert!(r.is_ok())
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut handle = handle_receiver.recv().unwrap();
|
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
let r = handle.post_message(msg.clone());
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let event = handle.get_event().await.unwrap();
|
|
|
|
assert!(event.is_none());
|
|
|
|
handle.sender.close_channel();
|
|
|
|
|
|
|
|
join_handle.join().expect("Failed to join worker thread");
|
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|