summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/ops/dispatch_json.rs28
-rw-r--r--cli/ops/mod.rs1
-rw-r--r--cli/ops/plugin.rs6
-rw-r--r--cli/ops/web_worker.rs13
-rw-r--r--cli/state.rs19
-rw-r--r--core/ops.rs11
6 files changed, 39 insertions, 39 deletions
diff --git a/cli/ops/dispatch_json.rs b/cli/ops/dispatch_json.rs
index 30a3a4433..c3a1c3b3f 100644
--- a/cli/ops/dispatch_json.rs
+++ b/cli/ops/dispatch_json.rs
@@ -45,14 +45,36 @@ struct AsyncArgs {
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
- D: Fn(
+ F: Fn(
&mut CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> 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]| {
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 ef8c3bd0f..331ed4aa1 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 ba105cff8..3edcaa962 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 553278b07..4a661d2be 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<D>(
sender: mpsc::Sender<WorkerEvent>,
dispatcher: D,
-) -> impl Fn(
- &mut CoreIsolateState,
- Value,
- &mut [ZeroCopyBuf],
-) -> Result<JsonOp, OpError>
+) -> impl JsonOpDispatcher
where
D: Fn(
&mpsc::Sender<WorkerEvent>,
@@ -36,11 +33,7 @@ pub fn web_worker_op2<D>(
handle: WebWorkerHandle,
sender: mpsc::Sender<WorkerEvent>,
dispatcher: D,
-) -> impl Fn(
- &mut CoreIsolateState,
- Value,
- &mut [ZeroCopyBuf],
-) -> Result<JsonOp, OpError>
+) -> impl JsonOpDispatcher
where
D: Fn(
WebWorkerHandle,
diff --git a/cli/state.rs b/cli/state.rs
index 2d871d74d..aa78f5c1a 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<D>(
- &self,
- dispatcher: D,
- ) -> impl Fn(
- &mut deno_core::CoreIsolateState,
- Value,
- &mut [ZeroCopyBuf],
- ) -> Result<JsonOp, OpError>
+ pub fn stateful_op<D>(&self, dispatcher: D) -> impl JsonOpDispatcher
where
D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, OpError>,
{
@@ -186,14 +180,7 @@ impl State {
-> Result<JsonOp, OpError> { dispatcher(&state, args, zero_copy) }
}
- pub fn stateful_op2<D>(
- &self,
- dispatcher: D,
- ) -> impl Fn(
- &mut deno_core::CoreIsolateState,
- Value,
- &mut [ZeroCopyBuf],
- ) -> Result<JsonOp, OpError>
+ pub fn stateful_op2<D>(&self, dispatcher: D) -> impl JsonOpDispatcher
where
D: Fn(
&mut deno_core::CoreIsolateState,
diff --git a/core/ops.rs b/core/ops.rs
index 8aa13936b..603526dbc 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)
}
}