diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/core_isolate.rs | 13 | ||||
-rw-r--r-- | core/lib.rs | 1 | ||||
-rw-r--r-- | core/ops.rs | 89 |
3 files changed, 35 insertions, 68 deletions
diff --git a/core/core_isolate.rs b/core/core_isolate.rs index 17345e14c..199bbd832 100644 --- a/core/core_isolate.rs +++ b/core/core_isolate.rs @@ -419,11 +419,10 @@ impl CoreIsolate { /// corresponds to the second argument of Deno.core.dispatch(). /// /// Requires runtime to explicitly ask for op ids before using any of the ops. - pub fn register_op( - &mut self, - name: &str, - op: impl OpDispatcher + 'static, - ) -> OpId { + pub fn register_op<F>(&mut self, name: &str, op: F) -> OpId + where + F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static, + { let state_rc = Self::state(self); let mut state = state_rc.borrow_mut(); 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. pub fn register_op<F>(&mut self, name: &str, op: F) -> OpId where - F: OpDispatcher + 'static, + F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static, { self.op_registry.register(name, op) } @@ -612,7 +611,7 @@ impl CoreIsolateState { zero_copy_bufs: &mut [ZeroCopyBuf], ) -> Option<(OpId, Box<[u8]>)> { let op = if let Some(dispatcher) = self.op_registry.get(op_id) { - dispatcher.dispatch(self, zero_copy_bufs) + dispatcher(self, zero_copy_bufs) } else { let message = v8::String::new(scope, &format!("Unknown op id: {}", op_id)).unwrap(); diff --git a/core/lib.rs b/core/lib.rs index f348ed6cf..7358af1c4 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -46,7 +46,6 @@ pub use crate::modules::RecursiveModuleLoad; pub use crate::ops::Buf; pub use crate::ops::Op; pub use crate::ops::OpAsyncFuture; -pub use crate::ops::OpDispatcher; pub use crate::ops::OpId; pub use crate::resources::ResourceTable; pub use crate::zero_copy_buf::ZeroCopyBuf; diff --git a/core/ops.rs b/core/ops.rs index 603526dbc..65a0f325b 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -20,53 +20,31 @@ pub enum Op { AsyncUnref(OpAsyncFuture), } -/// Main type describing Op -pub trait OpDispatcher { - fn dispatch( - &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) - } -} +/// Main type describing op +pub type OpDispatcher = + dyn Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static; #[derive(Default)] pub struct OpRegistry { - dispatchers: Vec<Rc<dyn OpDispatcher>>, + dispatchers: Vec<Rc<OpDispatcher>>, name_to_id: HashMap<String, OpId>, } impl OpRegistry { pub fn new() -> Self { let mut registry = Self::default(); - let op_id = registry.register( - "ops", - |state: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| { - let buf = state.op_registry.json_map(); - Op::Sync(buf) - }, - ); + let op_id = registry.register("ops", |state, _| { + let buf = state.op_registry.json_map(); + Op::Sync(buf) + }); assert_eq!(op_id, 0); registry } - pub fn register( - &mut self, - name: &str, - op: impl OpDispatcher + 'static, - ) -> OpId { + pub fn register<F>(&mut self, name: &str, op: F) -> OpId + where + F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static, + { let op_id = self.dispatchers.len() as u32; 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() } - pub fn get(&self, op_id: OpId) -> Option<Rc<dyn OpDispatcher>> { - self.dispatchers.get(op_id as usize).cloned() + pub fn get(&self, op_id: OpId) -> Option<Rc<OpDispatcher>> { + self.dispatchers.get(op_id as usize).map(Rc::clone) } 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_ = c.clone(); - let test_id = op_registry.register( - "test", - move |_: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| { - c_.fetch_add(1, atomic::Ordering::SeqCst); - Op::Sync(Box::new([])) - }, - ); + let test_id = op_registry.register("test", move |_, _| { + c_.fetch_add(1, atomic::Ordering::SeqCst); + Op::Sync(Box::new([])) + }); assert!(test_id != 0); let mut expected = HashMap::new(); @@ -122,7 +97,7 @@ fn test_op_registry() { let dispatch = op_registry.get(test_id).unwrap(); let state_rc = CoreIsolate::state(&isolate); 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 { assert_eq!(buf.len(), 0); } else { @@ -152,21 +127,15 @@ fn register_op_during_call() { let test_id = { let mut g = op_registry.lock().unwrap(); - g.register( - "dynamic_register_op", - move |_: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| { - let c__ = c_.clone(); - let mut g = op_registry_.lock().unwrap(); - g.register( - "test", - move |_: &mut CoreIsolateState, _: &mut [ZeroCopyBuf]| { - c__.fetch_add(1, atomic::Ordering::SeqCst); - Op::Sync(Box::new([])) - }, - ); + g.register("dynamic_register_op", move |_, _| { + let c__ = c_.clone(); + let mut g = op_registry_.lock().unwrap(); + g.register("test", move |_, _| { + c__.fetch_add(1, atomic::Ordering::SeqCst); Op::Sync(Box::new([])) - }, - ) + }); + Op::Sync(Box::new([])) + }) }; assert!(test_id != 0); @@ -179,7 +148,7 @@ fn register_op_during_call() { { let state_rc = CoreIsolate::state(&isolate); let mut state = state_rc.borrow_mut(); - dispatcher1.dispatch(&mut state, &mut []); + dispatcher1(&mut state, &mut []); } let mut expected = HashMap::new(); @@ -197,7 +166,7 @@ fn register_op_during_call() { }; let state_rc = CoreIsolate::state(&isolate); 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 { assert_eq!(buf.len(), 0); } else { |