mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
feat(runtime/ops): strongly typed deserialization of JSON ops (#9532)
This commit is contained in:
parent
2225e83da2
commit
666c4b77b0
2 changed files with 28 additions and 17 deletions
|
@ -28,7 +28,8 @@ use crate::metrics::metrics_op;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::json_op_async;
|
use deno_core::json_op_async;
|
||||||
use deno_core::json_op_sync;
|
use deno_core::json_op_sync;
|
||||||
use deno_core::serde_json::Value;
|
use deno_core::serde::de::DeserializeOwned;
|
||||||
|
use deno_core::serde::Serialize;
|
||||||
use deno_core::BufVec;
|
use deno_core::BufVec;
|
||||||
use deno_core::JsRuntime;
|
use deno_core::JsRuntime;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
|
@ -37,18 +38,24 @@ use std::cell::RefCell;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub fn reg_json_async<F, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
|
pub fn reg_json_async<F, V, R, RV>(
|
||||||
where
|
rt: &mut JsRuntime,
|
||||||
F: Fn(Rc<RefCell<OpState>>, Value, BufVec) -> R + 'static,
|
name: &'static str,
|
||||||
R: Future<Output = Result<Value, AnyError>> + 'static,
|
op_fn: F,
|
||||||
|
) where
|
||||||
|
F: Fn(Rc<RefCell<OpState>>, V, BufVec) -> R + 'static,
|
||||||
|
V: DeserializeOwned,
|
||||||
|
R: Future<Output = Result<RV, AnyError>> + 'static,
|
||||||
|
RV: Serialize,
|
||||||
{
|
{
|
||||||
rt.register_op(name, metrics_op(json_op_async(op_fn)));
|
rt.register_op(name, metrics_op(json_op_async(op_fn)));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reg_json_sync<F>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
|
pub fn reg_json_sync<F, V, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
|
||||||
where
|
where
|
||||||
F: Fn(&mut OpState, Value, &mut [ZeroCopyBuf]) -> Result<Value, AnyError>
|
F: Fn(&mut OpState, V, &mut [ZeroCopyBuf]) -> Result<R, AnyError> + 'static,
|
||||||
+ 'static,
|
V: DeserializeOwned,
|
||||||
|
R: Serialize,
|
||||||
{
|
{
|
||||||
rt.register_op(name, metrics_op(json_op_sync(op_fn)));
|
rt.register_op(name, metrics_op(json_op_sync(op_fn)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use crate::web_worker::WebWorkerHandle;
|
use crate::web_worker::WebWorkerHandle;
|
||||||
use crate::web_worker::WorkerEvent;
|
use crate::web_worker::WorkerEvent;
|
||||||
use deno_core::futures::channel::mpsc;
|
use deno_core::futures::channel::mpsc;
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::{json, Value};
|
||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
rt: &mut deno_core::JsRuntime,
|
rt: &mut deno_core::JsRuntime,
|
||||||
|
@ -15,7 +15,7 @@ pub fn init(
|
||||||
super::reg_json_sync(
|
super::reg_json_sync(
|
||||||
rt,
|
rt,
|
||||||
"op_worker_post_message",
|
"op_worker_post_message",
|
||||||
move |_state, _args, bufs| {
|
move |_state, _args: Value, bufs| {
|
||||||
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
||||||
let msg_buf: Box<[u8]> = (*bufs[0]).into();
|
let msg_buf: Box<[u8]> = (*bufs[0]).into();
|
||||||
sender_
|
sender_
|
||||||
|
@ -27,11 +27,15 @@ pub fn init(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Notify host that guest worker closes.
|
// Notify host that guest worker closes.
|
||||||
super::reg_json_sync(rt, "op_worker_close", move |_state, _args, _bufs| {
|
super::reg_json_sync(
|
||||||
// Notify parent that we're finished
|
rt,
|
||||||
sender.clone().close_channel();
|
"op_worker_close",
|
||||||
// Terminate execution of current worker
|
move |_state, _args: Value, _bufs| {
|
||||||
handle.terminate();
|
// Notify parent that we're finished
|
||||||
Ok(json!({}))
|
sender.clone().close_channel();
|
||||||
});
|
// Terminate execution of current worker
|
||||||
|
handle.terminate();
|
||||||
|
Ok(json!({}))
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue