mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
Undo JsonOpDispatcher and OpDispatcher traits (#7023)
This reverts commitf83d672ffa
. This reverts commitd51972377c
.
This commit is contained in:
parent
6706eb5515
commit
f5a4f1fdc0
11 changed files with 101 additions and 116 deletions
|
@ -4,7 +4,6 @@
|
||||||
pub use deno_core::v8_set_flags;
|
pub use deno_core::v8_set_flags;
|
||||||
use deno_core::CoreIsolateState;
|
use deno_core::CoreIsolateState;
|
||||||
use deno_core::Op;
|
use deno_core::Op;
|
||||||
use deno_core::OpDispatcher;
|
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
@ -88,7 +87,7 @@ fn get_asset(name: &str) -> Option<&'static str> {
|
||||||
/// CoreIsolate.
|
/// CoreIsolate.
|
||||||
pub fn op_fetch_asset<S: ::std::hash::BuildHasher>(
|
pub fn op_fetch_asset<S: ::std::hash::BuildHasher>(
|
||||||
custom_assets: HashMap<String, PathBuf, S>,
|
custom_assets: HashMap<String, PathBuf, S>,
|
||||||
) -> impl OpDispatcher {
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op {
|
||||||
for (_, path) in custom_assets.iter() {
|
for (_, path) in custom_assets.iter() {
|
||||||
println!("cargo:rerun-if-changed={}", path.display());
|
println!("cargo:rerun-if-changed={}", path.display());
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
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 deno_core::CoreIsolate;
|
use deno_core::CoreIsolate;
|
||||||
use deno_core::CoreIsolateState;
|
use deno_core::CoreIsolateState;
|
||||||
|
@ -32,7 +31,11 @@ pub fn init(
|
||||||
pub fn compiler_op<D>(
|
pub fn compiler_op<D>(
|
||||||
response: Arc<Mutex<Option<String>>>,
|
response: Arc<Mutex<Option<String>>>,
|
||||||
dispatcher: D,
|
dispatcher: D,
|
||||||
) -> impl JsonOpDispatcher
|
) -> impl Fn(
|
||||||
|
&mut deno_core::CoreIsolateState,
|
||||||
|
Value,
|
||||||
|
&mut [ZeroCopyBuf],
|
||||||
|
) -> Result<JsonOp, OpError>
|
||||||
where
|
where
|
||||||
D: Fn(
|
D: Fn(
|
||||||
Arc<Mutex<Option<String>>>,
|
Arc<Mutex<Option<String>>>,
|
||||||
|
|
|
@ -3,7 +3,6 @@ use crate::op_error::OpError;
|
||||||
use deno_core::Buf;
|
use deno_core::Buf;
|
||||||
use deno_core::CoreIsolateState;
|
use deno_core::CoreIsolateState;
|
||||||
use deno_core::Op;
|
use deno_core::Op;
|
||||||
use deno_core::OpDispatcher;
|
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
pub use serde_derive::Deserialize;
|
pub use serde_derive::Deserialize;
|
||||||
|
@ -45,36 +44,16 @@ struct AsyncArgs {
|
||||||
promise_id: Option<u64>,
|
promise_id: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like OpDispatcher but with additional json `Value` parameter
|
pub fn json_op<D>(
|
||||||
/// and return a result of `JsonOp` instead of `Op`.
|
d: D,
|
||||||
pub trait JsonOpDispatcher {
|
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
||||||
fn dispatch(
|
|
||||||
&self,
|
|
||||||
isolate_state: &mut CoreIsolateState,
|
|
||||||
json: Value,
|
|
||||||
zero_copy: &mut [ZeroCopyBuf],
|
|
||||||
) -> Result<JsonOp, OpError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> JsonOpDispatcher for F
|
|
||||||
where
|
where
|
||||||
F: Fn(
|
D: 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]) {
|
||||||
|
@ -89,7 +68,7 @@ pub fn json_op(d: impl JsonOpDispatcher) -> impl OpDispatcher {
|
||||||
|
|
||||||
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.dispatch(isolate_state, args, &mut zero_copy[1..]));
|
.and_then(|args| d(isolate_state, args, &mut zero_copy[1..]));
|
||||||
|
|
||||||
// Convert to Op
|
// Convert to Op
|
||||||
match result {
|
match result {
|
||||||
|
|
|
@ -9,7 +9,6 @@ use byteorder::{LittleEndian, WriteBytesExt};
|
||||||
use deno_core::Buf;
|
use deno_core::Buf;
|
||||||
use deno_core::CoreIsolateState;
|
use deno_core::CoreIsolateState;
|
||||||
use deno_core::Op;
|
use deno_core::Op;
|
||||||
use deno_core::OpDispatcher;
|
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
@ -119,7 +118,9 @@ fn test_parse_min_record() {
|
||||||
assert_eq!(parse_min_record(&buf), None);
|
assert_eq!(parse_min_record(&buf), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn minimal_op<D>(d: D) -> impl OpDispatcher
|
pub fn minimal_op<D>(
|
||||||
|
d: D,
|
||||||
|
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
||||||
where
|
where
|
||||||
D: Fn(&mut CoreIsolateState, bool, i32, &mut [ZeroCopyBuf]) -> MinimalOp,
|
D: Fn(&mut CoreIsolateState, bool, i32, &mut [ZeroCopyBuf]) -> MinimalOp,
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,7 +4,6 @@ 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;
|
||||||
|
|
|
@ -3,6 +3,7 @@ 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;
|
||||||
|
@ -20,7 +21,10 @@ 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("op_open_plugin", s.stateful_json_op2(op_open_plugin));
|
i.register_op(
|
||||||
|
"op_open_plugin",
|
||||||
|
s.core_op(json_op(s.stateful_op2(op_open_plugin))),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -106,8 +110,7 @@ impl<'a> plugin_api::Interface for PluginInterface<'a> {
|
||||||
let plugin_lib = self.plugin_lib.clone();
|
let plugin_lib = self.plugin_lib.clone();
|
||||||
self.isolate_state.op_registry.register(
|
self.isolate_state.op_registry.register(
|
||||||
name,
|
name,
|
||||||
move |isolate_state: &mut CoreIsolateState,
|
move |isolate_state, zero_copy| {
|
||||||
zero_copy: &mut [ZeroCopyBuf]| {
|
|
||||||
let mut interface = PluginInterface::new(isolate_state, &plugin_lib);
|
let mut interface = PluginInterface::new(isolate_state, &plugin_lib);
|
||||||
let op = dispatch_op_fn(&mut interface, zero_copy);
|
let op = dispatch_op_fn(&mut interface, zero_copy);
|
||||||
match op {
|
match op {
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
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;
|
||||||
|
@ -15,7 +14,11 @@ 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 JsonOpDispatcher
|
) -> impl Fn(
|
||||||
|
&mut CoreIsolateState,
|
||||||
|
Value,
|
||||||
|
&mut [ZeroCopyBuf],
|
||||||
|
) -> Result<JsonOp, OpError>
|
||||||
where
|
where
|
||||||
D: Fn(
|
D: Fn(
|
||||||
&mpsc::Sender<WorkerEvent>,
|
&mpsc::Sender<WorkerEvent>,
|
||||||
|
@ -33,7 +36,11 @@ pub fn web_worker_op2<D>(
|
||||||
handle: WebWorkerHandle,
|
handle: WebWorkerHandle,
|
||||||
sender: mpsc::Sender<WorkerEvent>,
|
sender: mpsc::Sender<WorkerEvent>,
|
||||||
dispatcher: D,
|
dispatcher: D,
|
||||||
) -> impl JsonOpDispatcher
|
) -> impl Fn(
|
||||||
|
&mut CoreIsolateState,
|
||||||
|
Value,
|
||||||
|
&mut [ZeroCopyBuf],
|
||||||
|
) -> Result<JsonOp, OpError>
|
||||||
where
|
where
|
||||||
D: Fn(
|
D: Fn(
|
||||||
WebWorkerHandle,
|
WebWorkerHandle,
|
||||||
|
|
45
cli/state.rs
45
cli/state.rs
|
@ -7,7 +7,6 @@ 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;
|
||||||
|
@ -18,7 +17,6 @@ use deno_core::ModuleLoadId;
|
||||||
use deno_core::ModuleLoader;
|
use deno_core::ModuleLoader;
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
use deno_core::Op;
|
use deno_core::Op;
|
||||||
use deno_core::OpDispatcher;
|
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
@ -66,7 +64,10 @@ pub struct StateInner {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
pub fn stateful_json_op<D>(&self, dispatcher: D) -> impl OpDispatcher
|
pub fn stateful_json_op<D>(
|
||||||
|
&self,
|
||||||
|
dispatcher: D,
|
||||||
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
||||||
where
|
where
|
||||||
D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, OpError>,
|
D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, OpError>,
|
||||||
{
|
{
|
||||||
|
@ -74,7 +75,10 @@ impl State {
|
||||||
self.core_op(json_op(self.stateful_op(dispatcher)))
|
self.core_op(json_op(self.stateful_op(dispatcher)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stateful_json_op2<D>(&self, dispatcher: D) -> impl OpDispatcher
|
pub fn stateful_json_op2<D>(
|
||||||
|
&self,
|
||||||
|
dispatcher: D,
|
||||||
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
||||||
where
|
where
|
||||||
D: Fn(
|
D: Fn(
|
||||||
&mut deno_core::CoreIsolateState,
|
&mut deno_core::CoreIsolateState,
|
||||||
|
@ -90,7 +94,13 @@ impl State {
|
||||||
/// Wrap core `OpDispatcher` to collect metrics.
|
/// Wrap core `OpDispatcher` to collect metrics.
|
||||||
// TODO(ry) this should be private. Is called by stateful_json_op or
|
// TODO(ry) this should be private. Is called by stateful_json_op or
|
||||||
// stateful_minimal_op
|
// stateful_minimal_op
|
||||||
pub fn core_op(&self, dispatcher: impl OpDispatcher) -> impl OpDispatcher {
|
pub fn core_op<D>(
|
||||||
|
&self,
|
||||||
|
dispatcher: D,
|
||||||
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
||||||
|
where
|
||||||
|
D: Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op,
|
||||||
|
{
|
||||||
let state = self.clone();
|
let state = self.clone();
|
||||||
|
|
||||||
move |isolate_state: &mut deno_core::CoreIsolateState,
|
move |isolate_state: &mut deno_core::CoreIsolateState,
|
||||||
|
@ -101,7 +111,7 @@ impl State {
|
||||||
let bytes_sent_zero_copy =
|
let bytes_sent_zero_copy =
|
||||||
zero_copy[1..].iter().map(|b| b.len()).sum::<usize>() as u64;
|
zero_copy[1..].iter().map(|b| b.len()).sum::<usize>() as u64;
|
||||||
|
|
||||||
let op = dispatcher.dispatch(isolate_state, zero_copy);
|
let op = dispatcher(isolate_state, zero_copy);
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Op::Sync(buf) => {
|
Op::Sync(buf) => {
|
||||||
|
@ -144,7 +154,10 @@ impl State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stateful_minimal_op2<D>(&self, dispatcher: D) -> impl OpDispatcher
|
pub fn stateful_minimal_op2<D>(
|
||||||
|
&self,
|
||||||
|
dispatcher: D,
|
||||||
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
||||||
where
|
where
|
||||||
D: Fn(
|
D: Fn(
|
||||||
&mut deno_core::CoreIsolateState,
|
&mut deno_core::CoreIsolateState,
|
||||||
|
@ -171,7 +184,14 @@ 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>(&self, dispatcher: D) -> impl JsonOpDispatcher
|
pub fn stateful_op<D>(
|
||||||
|
&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>,
|
||||||
{
|
{
|
||||||
|
@ -182,7 +202,14 @@ impl State {
|
||||||
-> Result<JsonOp, OpError> { dispatcher(&state, args, zero_copy) }
|
-> Result<JsonOp, OpError> { dispatcher(&state, args, zero_copy) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stateful_op2<D>(&self, dispatcher: D) -> impl JsonOpDispatcher
|
pub fn stateful_op2<D>(
|
||||||
|
&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,
|
||||||
|
|
|
@ -419,11 +419,10 @@ impl CoreIsolate {
|
||||||
/// corresponds to the second argument of Deno.core.dispatch().
|
/// corresponds to the second argument of Deno.core.dispatch().
|
||||||
///
|
///
|
||||||
/// Requires runtime to explicitly ask for op ids before using any of the ops.
|
/// Requires runtime to explicitly ask for op ids before using any of the ops.
|
||||||
pub fn register_op(
|
pub fn register_op<F>(&mut self, name: &str, op: F) -> OpId
|
||||||
&mut self,
|
where
|
||||||
name: &str,
|
F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static,
|
||||||
op: impl OpDispatcher + 'static,
|
{
|
||||||
) -> OpId {
|
|
||||||
let state_rc = Self::state(self);
|
let state_rc = Self::state(self);
|
||||||
let mut state = state_rc.borrow_mut();
|
let mut state = state_rc.borrow_mut();
|
||||||
state.op_registry.register(name, op)
|
state.op_registry.register(name, op)
|
||||||
|
@ -590,7 +589,7 @@ impl CoreIsolateState {
|
||||||
/// Requires runtime to explicitly ask for op ids before using any of the ops.
|
/// Requires runtime to explicitly ask for op ids before using any of the ops.
|
||||||
pub fn register_op<F>(&mut self, name: &str, op: F) -> OpId
|
pub fn register_op<F>(&mut self, name: &str, op: F) -> OpId
|
||||||
where
|
where
|
||||||
F: OpDispatcher + 'static,
|
F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static,
|
||||||
{
|
{
|
||||||
self.op_registry.register(name, op)
|
self.op_registry.register(name, op)
|
||||||
}
|
}
|
||||||
|
@ -612,7 +611,7 @@ impl CoreIsolateState {
|
||||||
zero_copy_bufs: &mut [ZeroCopyBuf],
|
zero_copy_bufs: &mut [ZeroCopyBuf],
|
||||||
) -> Option<(OpId, Box<[u8]>)> {
|
) -> Option<(OpId, Box<[u8]>)> {
|
||||||
let op = if let Some(dispatcher) = self.op_registry.get(op_id) {
|
let op = if let Some(dispatcher) = self.op_registry.get(op_id) {
|
||||||
dispatcher.dispatch(self, zero_copy_bufs)
|
dispatcher(self, zero_copy_bufs)
|
||||||
} else {
|
} else {
|
||||||
let message =
|
let message =
|
||||||
v8::String::new(scope, &format!("Unknown op id: {}", op_id)).unwrap();
|
v8::String::new(scope, &format!("Unknown op id: {}", op_id)).unwrap();
|
||||||
|
|
|
@ -46,7 +46,6 @@ pub use crate::modules::RecursiveModuleLoad;
|
||||||
pub use crate::ops::Buf;
|
pub use crate::ops::Buf;
|
||||||
pub use crate::ops::Op;
|
pub use crate::ops::Op;
|
||||||
pub use crate::ops::OpAsyncFuture;
|
pub use crate::ops::OpAsyncFuture;
|
||||||
pub use crate::ops::OpDispatcher;
|
|
||||||
pub use crate::ops::OpId;
|
pub use crate::ops::OpId;
|
||||||
pub use crate::resources::ResourceTable;
|
pub use crate::resources::ResourceTable;
|
||||||
pub use crate::zero_copy_buf::ZeroCopyBuf;
|
pub use crate::zero_copy_buf::ZeroCopyBuf;
|
||||||
|
|
73
core/ops.rs
73
core/ops.rs
|
@ -20,53 +20,31 @@ pub enum Op {
|
||||||
AsyncUnref(OpAsyncFuture),
|
AsyncUnref(OpAsyncFuture),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Main type describing Op
|
/// Main type describing op
|
||||||
pub trait OpDispatcher {
|
pub type OpDispatcher =
|
||||||
fn dispatch(
|
dyn Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static;
|
||||||
&self,
|
|
||||||
isolate_state: &mut CoreIsolateState,
|
|
||||||
zero_copy: &mut [ZeroCopyBuf],
|
|
||||||
) -> Op;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F> OpDispatcher for F
|
|
||||||
where
|
|
||||||
F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op,
|
|
||||||
{
|
|
||||||
fn dispatch(
|
|
||||||
&self,
|
|
||||||
isolate_state: &mut CoreIsolateState,
|
|
||||||
zero_copy: &mut [ZeroCopyBuf],
|
|
||||||
) -> Op {
|
|
||||||
self(isolate_state, zero_copy)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct OpRegistry {
|
pub struct OpRegistry {
|
||||||
dispatchers: Vec<Rc<dyn OpDispatcher>>,
|
dispatchers: Vec<Rc<OpDispatcher>>,
|
||||||
name_to_id: HashMap<String, OpId>,
|
name_to_id: HashMap<String, OpId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OpRegistry {
|
impl OpRegistry {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut registry = Self::default();
|
let mut registry = Self::default();
|
||||||
let op_id = registry.register(
|
let op_id = registry.register("ops", |state, _| {
|
||||||
"ops",
|
|
||||||
|state: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| {
|
|
||||||
let buf = state.op_registry.json_map();
|
let buf = state.op_registry.json_map();
|
||||||
Op::Sync(buf)
|
Op::Sync(buf)
|
||||||
},
|
});
|
||||||
);
|
|
||||||
assert_eq!(op_id, 0);
|
assert_eq!(op_id, 0);
|
||||||
registry
|
registry
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register(
|
pub fn register<F>(&mut self, name: &str, op: F) -> OpId
|
||||||
&mut self,
|
where
|
||||||
name: &str,
|
F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static,
|
||||||
op: impl OpDispatcher + 'static,
|
{
|
||||||
) -> OpId {
|
|
||||||
let op_id = self.dispatchers.len() as u32;
|
let op_id = self.dispatchers.len() as u32;
|
||||||
|
|
||||||
let existing = self.name_to_id.insert(name.to_string(), op_id);
|
let existing = self.name_to_id.insert(name.to_string(), op_id);
|
||||||
|
@ -83,8 +61,8 @@ impl OpRegistry {
|
||||||
op_map_json.as_bytes().to_owned().into_boxed_slice()
|
op_map_json.as_bytes().to_owned().into_boxed_slice()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, op_id: OpId) -> Option<Rc<dyn OpDispatcher>> {
|
pub fn get(&self, op_id: OpId) -> Option<Rc<OpDispatcher>> {
|
||||||
self.dispatchers.get(op_id as usize).cloned()
|
self.dispatchers.get(op_id as usize).map(Rc::clone)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unregister_op(&mut self, name: &str) {
|
pub fn unregister_op(&mut self, name: &str) {
|
||||||
|
@ -103,13 +81,10 @@ fn test_op_registry() {
|
||||||
let c = Arc::new(atomic::AtomicUsize::new(0));
|
let c = Arc::new(atomic::AtomicUsize::new(0));
|
||||||
let c_ = c.clone();
|
let c_ = c.clone();
|
||||||
|
|
||||||
let test_id = op_registry.register(
|
let test_id = op_registry.register("test", move |_, _| {
|
||||||
"test",
|
|
||||||
move |_: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| {
|
|
||||||
c_.fetch_add(1, atomic::Ordering::SeqCst);
|
c_.fetch_add(1, atomic::Ordering::SeqCst);
|
||||||
Op::Sync(Box::new([]))
|
Op::Sync(Box::new([]))
|
||||||
},
|
});
|
||||||
);
|
|
||||||
assert!(test_id != 0);
|
assert!(test_id != 0);
|
||||||
|
|
||||||
let mut expected = HashMap::new();
|
let mut expected = HashMap::new();
|
||||||
|
@ -122,7 +97,7 @@ fn test_op_registry() {
|
||||||
let dispatch = op_registry.get(test_id).unwrap();
|
let dispatch = op_registry.get(test_id).unwrap();
|
||||||
let state_rc = CoreIsolate::state(&isolate);
|
let state_rc = CoreIsolate::state(&isolate);
|
||||||
let mut state = state_rc.borrow_mut();
|
let mut state = state_rc.borrow_mut();
|
||||||
let res = dispatch.dispatch(&mut state, &mut []);
|
let res = dispatch(&mut state, &mut []);
|
||||||
if let Op::Sync(buf) = res {
|
if let Op::Sync(buf) = res {
|
||||||
assert_eq!(buf.len(), 0);
|
assert_eq!(buf.len(), 0);
|
||||||
} else {
|
} else {
|
||||||
|
@ -152,21 +127,15 @@ fn register_op_during_call() {
|
||||||
|
|
||||||
let test_id = {
|
let test_id = {
|
||||||
let mut g = op_registry.lock().unwrap();
|
let mut g = op_registry.lock().unwrap();
|
||||||
g.register(
|
g.register("dynamic_register_op", move |_, _| {
|
||||||
"dynamic_register_op",
|
|
||||||
move |_: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| {
|
|
||||||
let c__ = c_.clone();
|
let c__ = c_.clone();
|
||||||
let mut g = op_registry_.lock().unwrap();
|
let mut g = op_registry_.lock().unwrap();
|
||||||
g.register(
|
g.register("test", move |_, _| {
|
||||||
"test",
|
|
||||||
move |_: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| {
|
|
||||||
c__.fetch_add(1, atomic::Ordering::SeqCst);
|
c__.fetch_add(1, atomic::Ordering::SeqCst);
|
||||||
Op::Sync(Box::new([]))
|
Op::Sync(Box::new([]))
|
||||||
},
|
});
|
||||||
);
|
|
||||||
Op::Sync(Box::new([]))
|
Op::Sync(Box::new([]))
|
||||||
},
|
})
|
||||||
)
|
|
||||||
};
|
};
|
||||||
assert!(test_id != 0);
|
assert!(test_id != 0);
|
||||||
|
|
||||||
|
@ -179,7 +148,7 @@ fn register_op_during_call() {
|
||||||
{
|
{
|
||||||
let state_rc = CoreIsolate::state(&isolate);
|
let state_rc = CoreIsolate::state(&isolate);
|
||||||
let mut state = state_rc.borrow_mut();
|
let mut state = state_rc.borrow_mut();
|
||||||
dispatcher1.dispatch(&mut state, &mut []);
|
dispatcher1(&mut state, &mut []);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut expected = HashMap::new();
|
let mut expected = HashMap::new();
|
||||||
|
@ -197,7 +166,7 @@ fn register_op_during_call() {
|
||||||
};
|
};
|
||||||
let state_rc = CoreIsolate::state(&isolate);
|
let state_rc = CoreIsolate::state(&isolate);
|
||||||
let mut state = state_rc.borrow_mut();
|
let mut state = state_rc.borrow_mut();
|
||||||
let res = dispatcher2.dispatch(&mut state, &mut []);
|
let res = dispatcher2(&mut state, &mut []);
|
||||||
if let Op::Sync(buf) = res {
|
if let Op::Sync(buf) = res {
|
||||||
assert_eq!(buf.len(), 0);
|
assert_eq!(buf.len(), 0);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue