mirror of
https://github.com/denoland/deno.git
synced 2024-10-31 09:14:20 -04:00
2e74f164b6
This commit moves Deno JS runtime, ops, permissions and inspector implementation to new "deno_runtime" crate located in "runtime/" directory. Details in "runtime/README.md". Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use crate::web_worker::WebWorkerHandle;
|
|
use crate::web_worker::WorkerEvent;
|
|
use deno_core::futures::channel::mpsc;
|
|
use deno_core::serde_json::json;
|
|
|
|
pub fn init(
|
|
rt: &mut deno_core::JsRuntime,
|
|
sender: mpsc::Sender<WorkerEvent>,
|
|
handle: WebWorkerHandle,
|
|
) {
|
|
// Post message to host as guest worker.
|
|
let sender_ = sender.clone();
|
|
super::reg_json_sync(
|
|
rt,
|
|
"op_worker_post_message",
|
|
move |_state, _args, bufs| {
|
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
|
let msg_buf: Box<[u8]> = (*bufs[0]).into();
|
|
sender_
|
|
.clone()
|
|
.try_send(WorkerEvent::Message(msg_buf))
|
|
.expect("Failed to post message to host");
|
|
Ok(json!({}))
|
|
},
|
|
);
|
|
|
|
// Notify host that guest worker closes.
|
|
super::reg_json_sync(rt, "op_worker_close", move |_state, _args, _bufs| {
|
|
// Notify parent that we're finished
|
|
sender.clone().close_channel();
|
|
// Terminate execution of current worker
|
|
handle.terminate();
|
|
Ok(json!({}))
|
|
});
|
|
}
|