2020-01-21 03:49:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use super::dispatch_json::{JsonOp, Value};
|
|
|
|
use crate::deno_error::DenoError;
|
|
|
|
use crate::deno_error::ErrorKind;
|
|
|
|
use crate::ops::json_op;
|
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno_core::*;
|
|
|
|
use futures;
|
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::sink::SinkExt;
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use std;
|
|
|
|
use std::convert::From;
|
|
|
|
|
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op(
|
|
|
|
"worker_post_message",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_worker_post_message))),
|
|
|
|
);
|
|
|
|
i.register_op(
|
|
|
|
"worker_get_message",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_worker_get_message))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get message from host as guest worker
|
|
|
|
fn op_worker_get_message(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
_args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_data: Option<ZeroCopyBuf>,
|
2020-01-21 03:49:47 -05:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2020-01-21 11:50:06 -05:00
|
|
|
let state_ = state.clone();
|
2020-01-21 03:49:47 -05:00
|
|
|
let op = async move {
|
2020-01-21 11:50:06 -05:00
|
|
|
let mut receiver = state_.worker_channels.receiver.lock().await;
|
|
|
|
let maybe_buf = receiver.next().await;
|
2020-01-21 03:49:47 -05:00
|
|
|
debug!("op_worker_get_message");
|
|
|
|
Ok(json!({ "data": maybe_buf }))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(JsonOp::Async(op.boxed()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Post message to host as guest worker
|
|
|
|
fn op_worker_post_message(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
_args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
data: Option<ZeroCopyBuf>,
|
2020-01-21 03:49:47 -05:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let d = Vec::from(data.unwrap().as_ref()).into_boxed_slice();
|
2020-01-21 11:50:06 -05:00
|
|
|
let mut sender = state.worker_channels.sender.clone();
|
2020-01-21 03:49:47 -05:00
|
|
|
futures::executor::block_on(sender.send(d))
|
|
|
|
.map_err(|e| DenoError::new(ErrorKind::Other, e.to_string()))?;
|
|
|
|
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
}
|