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:
parent
7be29fab8d
commit
f83d672ffa
6 changed files with 39 additions and 39 deletions
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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,
|
||||
|
|
19
cli/state.rs
19
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,
|
||||
|
|
11
core/ops.rs
11
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue