diff --git a/cli/ops/dispatch_json.rs b/cli/ops/dispatch_json.rs index 30a3a44333..c3a1c3b3fd 100644 --- a/cli/ops/dispatch_json.rs +++ b/cli/ops/dispatch_json.rs @@ -45,14 +45,36 @@ struct AsyncArgs { promise_id: Option, } -pub fn json_op(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; +} + +impl JsonOpDispatcher for F where - D: Fn( + F: Fn( &mut CoreIsolateState, Value, &mut [ZeroCopyBuf], ) -> Result, { + fn dispatch( + &self, + isolate_state: &mut CoreIsolateState, + json: Value, + zero_copy: &mut [ZeroCopyBuf], + ) -> Result { + self(isolate_state, json, zero_copy) + } +} + +pub fn json_op(d: impl JsonOpDispatcher) -> impl OpDispatcher { move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| { assert!(!zero_copy.is_empty(), "Expected JSON string at position 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]) .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 match result { diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index ef8c3bd0f1..331ed4aa19 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -4,6 +4,7 @@ mod dispatch_minimal; pub use dispatch_json::json_op; pub use dispatch_json::JsonOp; +pub use dispatch_json::JsonOpDispatcher; pub use dispatch_json::JsonResult; pub use dispatch_minimal::minimal_op; pub use dispatch_minimal::MinimalOp; diff --git a/cli/ops/plugin.rs b/cli/ops/plugin.rs index ba105cff8f..3edcaa962f 100644 --- a/cli/ops/plugin.rs +++ b/cli/ops/plugin.rs @@ -3,7 +3,6 @@ use crate::op_error::OpError; use crate::ops::dispatch_json::Deserialize; use crate::ops::dispatch_json::JsonOp; use crate::ops::dispatch_json::Value; -use crate::ops::json_op; use crate::state::State; use deno_core::plugin_api; use deno_core::CoreIsolate; @@ -21,10 +20,7 @@ use std::task::Context; use std::task::Poll; pub fn init(i: &mut CoreIsolate, s: &State) { - i.register_op( - "op_open_plugin", - s.core_op(json_op(s.stateful_op2(op_open_plugin))), - ); + i.register_op("op_open_plugin", s.stateful_json_op2(op_open_plugin)); } #[derive(Deserialize)] diff --git a/cli/ops/web_worker.rs b/cli/ops/web_worker.rs index 553278b074..4a661d2bed 100644 --- a/cli/ops/web_worker.rs +++ b/cli/ops/web_worker.rs @@ -2,6 +2,7 @@ use super::dispatch_json::{JsonOp, Value}; use crate::op_error::OpError; use crate::ops::json_op; +use crate::ops::JsonOpDispatcher; use crate::state::State; use crate::web_worker::WebWorkerHandle; use crate::worker::WorkerEvent; @@ -14,11 +15,7 @@ use std::convert::From; pub fn web_worker_op( sender: mpsc::Sender, dispatcher: D, -) -> impl Fn( - &mut CoreIsolateState, - Value, - &mut [ZeroCopyBuf], -) -> Result +) -> impl JsonOpDispatcher where D: Fn( &mpsc::Sender, @@ -36,11 +33,7 @@ pub fn web_worker_op2( handle: WebWorkerHandle, sender: mpsc::Sender, dispatcher: D, -) -> impl Fn( - &mut CoreIsolateState, - Value, - &mut [ZeroCopyBuf], -) -> Result +) -> impl JsonOpDispatcher where D: Fn( WebWorkerHandle, diff --git a/cli/state.rs b/cli/state.rs index 2d871d74d0..aa78f5c1ae 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -6,6 +6,7 @@ use crate::import_map::ImportMap; use crate::metrics::Metrics; use crate::op_error::OpError; use crate::ops::JsonOp; +use crate::ops::JsonOpDispatcher; use crate::ops::MinimalOp; use crate::permissions::Permissions; use crate::tsc::TargetLib; @@ -168,14 +169,7 @@ impl State { /// NOTE: This only works with JSON dispatcher. /// This is a band-aid for transition to `CoreIsolate.register_op` API as most of our /// ops require `state` argument. - pub fn stateful_op( - &self, - dispatcher: D, - ) -> impl Fn( - &mut deno_core::CoreIsolateState, - Value, - &mut [ZeroCopyBuf], - ) -> Result + pub fn stateful_op(&self, dispatcher: D) -> impl JsonOpDispatcher where D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result, { @@ -186,14 +180,7 @@ impl State { -> Result { dispatcher(&state, args, zero_copy) } } - pub fn stateful_op2( - &self, - dispatcher: D, - ) -> impl Fn( - &mut deno_core::CoreIsolateState, - Value, - &mut [ZeroCopyBuf], - ) -> Result + pub fn stateful_op2(&self, dispatcher: D) -> impl JsonOpDispatcher where D: Fn( &mut deno_core::CoreIsolateState, diff --git a/core/ops.rs b/core/ops.rs index 8aa13936b4..603526dbcb 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -20,11 +20,12 @@ pub enum Op { AsyncUnref(OpAsyncFuture), } +/// Main type describing Op pub trait OpDispatcher { fn dispatch( &self, - isolate: &mut CoreIsolateState, - buf: &mut [ZeroCopyBuf], + isolate_state: &mut CoreIsolateState, + zero_copy: &mut [ZeroCopyBuf], ) -> Op; } @@ -34,10 +35,10 @@ where { fn dispatch( &self, - isolate: &mut CoreIsolateState, - buf: &mut [ZeroCopyBuf], + isolate_state: &mut CoreIsolateState, + zero_copy: &mut [ZeroCopyBuf], ) -> Op { - self(isolate, buf) + self(isolate_state, zero_copy) } }