1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

refactor: new trait JsonOpDispatcher (#6742)

This commit is contained in:
Gurwinder Singh 2020-07-14 23:52:02 +05:30 committed by GitHub
parent 7be29fab8d
commit f83d672ffa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 39 deletions

View file

@ -45,14 +45,36 @@ struct AsyncArgs {
promise_id: Option<u64>, promise_id: Option<u64>,
} }
pub fn json_op<D>(d: D) -> impl OpDispatcher /// Like OpDispatcher but with additional json `Value` parameter
/// and return a result of `JsonOp` instead of `Op`.
pub trait JsonOpDispatcher {
fn dispatch(
&self,
isolate_state: &mut CoreIsolateState,
json: Value,
zero_copy: &mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>;
}
impl<F> JsonOpDispatcher for F
where where
D: Fn( F: Fn(
&mut CoreIsolateState, &mut CoreIsolateState,
Value, Value,
&mut [ZeroCopyBuf], &mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>, ) -> Result<JsonOp, OpError>,
{ {
fn dispatch(
&self,
isolate_state: &mut CoreIsolateState,
json: Value,
zero_copy: &mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError> {
self(isolate_state, json, zero_copy)
}
}
pub fn json_op(d: impl JsonOpDispatcher) -> impl OpDispatcher {
move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| { move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| {
assert!(!zero_copy.is_empty(), "Expected JSON string at position 0"); assert!(!zero_copy.is_empty(), "Expected JSON string at position 0");
let async_args: AsyncArgs = match serde_json::from_slice(&zero_copy[0]) { let async_args: AsyncArgs = match serde_json::from_slice(&zero_copy[0]) {
@ -67,7 +89,7 @@ where
let result = serde_json::from_slice(&zero_copy[0]) let result = serde_json::from_slice(&zero_copy[0])
.map_err(OpError::from) .map_err(OpError::from)
.and_then(|args| d(isolate_state, args, &mut zero_copy[1..])); .and_then(|args| d.dispatch(isolate_state, args, &mut zero_copy[1..]));
// Convert to Op // Convert to Op
match result { match result {

View file

@ -4,6 +4,7 @@ mod dispatch_minimal;
pub use dispatch_json::json_op; pub use dispatch_json::json_op;
pub use dispatch_json::JsonOp; pub use dispatch_json::JsonOp;
pub use dispatch_json::JsonOpDispatcher;
pub use dispatch_json::JsonResult; pub use dispatch_json::JsonResult;
pub use dispatch_minimal::minimal_op; pub use dispatch_minimal::minimal_op;
pub use dispatch_minimal::MinimalOp; pub use dispatch_minimal::MinimalOp;

View file

@ -3,7 +3,6 @@ use crate::op_error::OpError;
use crate::ops::dispatch_json::Deserialize; use crate::ops::dispatch_json::Deserialize;
use crate::ops::dispatch_json::JsonOp; use crate::ops::dispatch_json::JsonOp;
use crate::ops::dispatch_json::Value; use crate::ops::dispatch_json::Value;
use crate::ops::json_op;
use crate::state::State; use crate::state::State;
use deno_core::plugin_api; use deno_core::plugin_api;
use deno_core::CoreIsolate; use deno_core::CoreIsolate;
@ -21,10 +20,7 @@ use std::task::Context;
use std::task::Poll; use std::task::Poll;
pub fn init(i: &mut CoreIsolate, s: &State) { pub fn init(i: &mut CoreIsolate, s: &State) {
i.register_op( i.register_op("op_open_plugin", s.stateful_json_op2(op_open_plugin));
"op_open_plugin",
s.core_op(json_op(s.stateful_op2(op_open_plugin))),
);
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -2,6 +2,7 @@
use super::dispatch_json::{JsonOp, Value}; use super::dispatch_json::{JsonOp, Value};
use crate::op_error::OpError; use crate::op_error::OpError;
use crate::ops::json_op; use crate::ops::json_op;
use crate::ops::JsonOpDispatcher;
use crate::state::State; use crate::state::State;
use crate::web_worker::WebWorkerHandle; use crate::web_worker::WebWorkerHandle;
use crate::worker::WorkerEvent; use crate::worker::WorkerEvent;
@ -14,11 +15,7 @@ use std::convert::From;
pub fn web_worker_op<D>( pub fn web_worker_op<D>(
sender: mpsc::Sender<WorkerEvent>, sender: mpsc::Sender<WorkerEvent>,
dispatcher: D, dispatcher: D,
) -> impl Fn( ) -> impl JsonOpDispatcher
&mut CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>
where where
D: Fn( D: Fn(
&mpsc::Sender<WorkerEvent>, &mpsc::Sender<WorkerEvent>,
@ -36,11 +33,7 @@ pub fn web_worker_op2<D>(
handle: WebWorkerHandle, handle: WebWorkerHandle,
sender: mpsc::Sender<WorkerEvent>, sender: mpsc::Sender<WorkerEvent>,
dispatcher: D, dispatcher: D,
) -> impl Fn( ) -> impl JsonOpDispatcher
&mut CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>
where where
D: Fn( D: Fn(
WebWorkerHandle, WebWorkerHandle,

View file

@ -6,6 +6,7 @@ use crate::import_map::ImportMap;
use crate::metrics::Metrics; use crate::metrics::Metrics;
use crate::op_error::OpError; use crate::op_error::OpError;
use crate::ops::JsonOp; use crate::ops::JsonOp;
use crate::ops::JsonOpDispatcher;
use crate::ops::MinimalOp; use crate::ops::MinimalOp;
use crate::permissions::Permissions; use crate::permissions::Permissions;
use crate::tsc::TargetLib; use crate::tsc::TargetLib;
@ -168,14 +169,7 @@ impl State {
/// NOTE: This only works with JSON dispatcher. /// NOTE: This only works with JSON dispatcher.
/// This is a band-aid for transition to `CoreIsolate.register_op` API as most of our /// This is a band-aid for transition to `CoreIsolate.register_op` API as most of our
/// ops require `state` argument. /// ops require `state` argument.
pub fn stateful_op<D>( pub fn stateful_op<D>(&self, dispatcher: D) -> impl JsonOpDispatcher
&self,
dispatcher: D,
) -> impl Fn(
&mut deno_core::CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>
where where
D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, OpError>, D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, OpError>,
{ {
@ -186,14 +180,7 @@ impl State {
-> Result<JsonOp, OpError> { dispatcher(&state, args, zero_copy) } -> Result<JsonOp, OpError> { dispatcher(&state, args, zero_copy) }
} }
pub fn stateful_op2<D>( pub fn stateful_op2<D>(&self, dispatcher: D) -> impl JsonOpDispatcher
&self,
dispatcher: D,
) -> impl Fn(
&mut deno_core::CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>
where where
D: Fn( D: Fn(
&mut deno_core::CoreIsolateState, &mut deno_core::CoreIsolateState,

View file

@ -20,11 +20,12 @@ pub enum Op {
AsyncUnref(OpAsyncFuture), AsyncUnref(OpAsyncFuture),
} }
/// Main type describing Op
pub trait OpDispatcher { pub trait OpDispatcher {
fn dispatch( fn dispatch(
&self, &self,
isolate: &mut CoreIsolateState, isolate_state: &mut CoreIsolateState,
buf: &mut [ZeroCopyBuf], zero_copy: &mut [ZeroCopyBuf],
) -> Op; ) -> Op;
} }
@ -34,10 +35,10 @@ where
{ {
fn dispatch( fn dispatch(
&self, &self,
isolate: &mut CoreIsolateState, isolate_state: &mut CoreIsolateState,
buf: &mut [ZeroCopyBuf], zero_copy: &mut [ZeroCopyBuf],
) -> Op { ) -> Op {
self(isolate, buf) self(isolate_state, zero_copy)
} }
} }