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;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-21 03:49:47 -05:00
|
|
|
use deno_core::*;
|
|
|
|
use futures;
|
|
|
|
use futures::future::FutureExt;
|
|
|
|
use std;
|
|
|
|
use std::convert::From;
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn init(i: &mut Isolate, s: &State) {
|
2020-01-21 03:49:47 -05:00
|
|
|
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(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2020-01-21 03:49:47 -05:00
|
|
|
_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-02-08 14:34:31 -05:00
|
|
|
let fut = {
|
|
|
|
let state = state_.borrow();
|
|
|
|
state
|
|
|
|
.worker_channels_internal
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.get_message()
|
|
|
|
};
|
|
|
|
let maybe_buf = fut.await;
|
2020-01-21 03:49:47 -05:00
|
|
|
debug!("op_worker_get_message");
|
|
|
|
Ok(json!({ "data": maybe_buf }))
|
|
|
|
};
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
Ok(JsonOp::Async(op.boxed_local()))
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Post message to host as guest worker
|
|
|
|
fn op_worker_post_message(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2020-01-21 03:49:47 -05:00
|
|
|
_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-02-08 14:34:31 -05:00
|
|
|
let state = state.borrow();
|
|
|
|
let fut = state
|
|
|
|
.worker_channels_internal
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.post_message(d);
|
2020-02-05 02:40:38 -05:00
|
|
|
futures::executor::block_on(fut)
|
2020-01-21 03:49:47 -05:00
|
|
|
.map_err(|e| DenoError::new(ErrorKind::Other, e.to_string()))?;
|
|
|
|
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
}
|