2020-01-21 03:49:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use crate::ops;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-21 11:50:06 -05:00
|
|
|
use crate::worker::Worker;
|
2020-02-18 14:47:11 -05:00
|
|
|
use crate::worker::WorkerEvent;
|
2020-01-21 03:49:47 -05:00
|
|
|
use deno_core;
|
|
|
|
use deno_core::ErrBox;
|
|
|
|
use deno_core::StartupData;
|
|
|
|
use futures::future::FutureExt;
|
2020-02-18 14:47:11 -05:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use futures::SinkExt;
|
2020-01-21 03:49:47 -05:00
|
|
|
use std::future::Future;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2020-01-21 03:49:47 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2020-01-21 11:50:06 -05:00
|
|
|
/// 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`.
|
2020-02-18 14:47:11 -05:00
|
|
|
pub struct WebWorker {
|
|
|
|
worker: Worker,
|
|
|
|
is_ready: bool,
|
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
|
|
|
|
impl WebWorker {
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
2020-01-21 11:50:06 -05:00
|
|
|
let state_ = state.clone();
|
2020-02-05 02:40:38 -05:00
|
|
|
let mut worker = Worker::new(name, startup_data, state_);
|
2020-01-21 11:50:06 -05:00
|
|
|
{
|
2020-02-03 18:08:44 -05:00
|
|
|
let isolate = &mut worker.isolate;
|
|
|
|
ops::runtime::init(isolate, &state);
|
2020-02-11 04:04:59 -05:00
|
|
|
ops::web_worker::init(isolate, &state, &worker.internal_channels.sender);
|
2020-02-03 18:08:44 -05:00
|
|
|
ops::worker_host::init(isolate, &state);
|
2020-02-21 10:35:41 -05:00
|
|
|
ops::io::init(isolate, &state);
|
2020-03-07 07:20:27 -05:00
|
|
|
ops::resources::init(isolate, &state);
|
2020-02-03 18:08:44 -05:00
|
|
|
ops::errors::init(isolate, &state);
|
|
|
|
ops::timers::init(isolate, &state);
|
|
|
|
ops::fetch::init(isolate, &state);
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 14:47:11 -05:00
|
|
|
Self {
|
|
|
|
worker,
|
|
|
|
is_ready: false,
|
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
impl Deref for WebWorker {
|
|
|
|
type Target = Worker;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2020-02-18 14:47:11 -05:00
|
|
|
&self.worker
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
impl DerefMut for WebWorker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
2020-02-18 14:47:11 -05:00
|
|
|
&mut self.worker
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for WebWorker {
|
|
|
|
type Output = Result<(), ErrBox>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
2020-02-18 14:47:11 -05:00
|
|
|
let worker = &mut inner.worker;
|
|
|
|
|
|
|
|
if !inner.is_ready {
|
|
|
|
match worker.poll_unpin(cx) {
|
|
|
|
Poll::Ready(r) => {
|
|
|
|
if let Err(e) = r {
|
|
|
|
let mut sender = worker.internal_channels.sender.clone();
|
|
|
|
futures::executor::block_on(sender.send(WorkerEvent::Error(e)))
|
|
|
|
.expect("Failed to post message to host");
|
|
|
|
}
|
|
|
|
inner.is_ready = true;
|
|
|
|
}
|
|
|
|
Poll::Pending => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let maybe_msg = {
|
|
|
|
match worker.internal_channels.receiver.poll_next_unpin(cx) {
|
|
|
|
Poll::Ready(r) => match r {
|
|
|
|
Some(msg) => {
|
|
|
|
let msg_str = String::from_utf8(msg.to_vec()).unwrap();
|
|
|
|
debug!("received message from host: {}", msg_str);
|
|
|
|
Some(msg_str)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
debug!("channel closed by host, worker event loop shuts down");
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Poll::Pending => None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(msg) = maybe_msg {
|
|
|
|
// TODO: just add second value and then bind using rusty_v8
|
|
|
|
// to get structured clone/transfer working
|
|
|
|
let script = format!("workerMessageRecvCallback({})", msg);
|
|
|
|
worker
|
|
|
|
.execute(&script)
|
|
|
|
.expect("Failed to execute message cb");
|
|
|
|
// Let worker be polled again
|
|
|
|
inner.is_ready = false;
|
|
|
|
worker.waker.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-27 21:12:25 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::startup_data;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-27 21:12:25 -05:00
|
|
|
use crate::tokio_util;
|
2020-02-11 04:04:59 -05:00
|
|
|
use crate::worker::WorkerEvent;
|
|
|
|
use crate::worker::WorkerHandle;
|
2020-01-27 21:12:25 -05:00
|
|
|
|
|
|
|
fn create_test_worker() -> WebWorker {
|
2020-02-08 14:34:31 -05:00
|
|
|
let state = State::mock("./hello.js");
|
2020-01-27 21:12:25 -05:00
|
|
|
let mut worker = WebWorker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state,
|
|
|
|
);
|
|
|
|
worker.execute("bootstrapWorkerRuntime(\"TEST\")").unwrap();
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_worker_messages() {
|
2020-02-11 04:04:59 -05:00
|
|
|
let (handle_sender, handle_receiver) =
|
|
|
|
std::sync::mpsc::sync_channel::<WorkerHandle>(1);
|
|
|
|
|
|
|
|
let join_handle = std::thread::spawn(move || {
|
|
|
|
let mut worker = create_test_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");
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
2020-02-11 04:04:59 -05:00
|
|
|
"#;
|
|
|
|
worker.execute(source).unwrap();
|
|
|
|
let handle = worker.thread_safe_handle();
|
|
|
|
handle_sender.send(handle).unwrap();
|
2020-02-18 14:47:11 -05:00
|
|
|
let r = tokio_util::run_basic(worker);
|
2020-02-11 04:04:59 -05:00
|
|
|
assert!(r.is_ok())
|
2020-02-03 18:08:44 -05:00
|
|
|
});
|
2020-02-08 14:34:31 -05:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
let mut handle = handle_receiver.recv().unwrap();
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
tokio_util::run_basic(async move {
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
let r = handle.post_message(msg.clone()).await;
|
|
|
|
assert!(r.is_ok());
|
2020-02-08 14:34:31 -05:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
let maybe_msg = handle.get_event().await;
|
|
|
|
assert!(maybe_msg.is_some());
|
2020-02-08 14:34:31 -05:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
let r = handle.post_message(msg.clone()).await;
|
2020-02-08 14:34:31 -05:00
|
|
|
assert!(r.is_ok());
|
2020-02-11 04:04:59 -05:00
|
|
|
|
|
|
|
let maybe_msg = handle.get_event().await;
|
|
|
|
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).await;
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let event = handle.get_event().await;
|
|
|
|
assert!(event.is_none());
|
|
|
|
handle.sender.close_channel();
|
|
|
|
});
|
|
|
|
join_handle.join().expect("Failed to join worker thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn removed_from_resource_table_on_close() {
|
|
|
|
let (handle_sender, handle_receiver) =
|
|
|
|
std::sync::mpsc::sync_channel::<WorkerHandle>(1);
|
|
|
|
|
|
|
|
let join_handle = std::thread::spawn(move || {
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
worker.execute("onmessage = () => { close(); }").unwrap();
|
|
|
|
let handle = worker.thread_safe_handle();
|
|
|
|
handle_sender.send(handle).unwrap();
|
2020-02-18 14:47:11 -05:00
|
|
|
let r = tokio_util::run_basic(worker);
|
2020-02-11 04:04:59 -05:00
|
|
|
assert!(r.is_ok())
|
2020-02-03 18:08:44 -05:00
|
|
|
});
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
let mut handle = handle_receiver.recv().unwrap();
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
tokio_util::run_basic(async move {
|
2020-02-11 04:04:59 -05:00
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
let r = handle.post_message(msg.clone()).await;
|
2020-02-03 18:08:44 -05:00
|
|
|
assert!(r.is_ok());
|
2020-02-11 04:04:59 -05:00
|
|
|
let event = handle.get_event().await;
|
|
|
|
assert!(event.is_none());
|
|
|
|
handle.sender.close_channel();
|
2020-02-03 18:08:44 -05:00
|
|
|
});
|
2020-02-11 04:04:59 -05:00
|
|
|
join_handle.join().expect("Failed to join worker thread");
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
}
|