2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-29 17:41:39 -04:00
|
|
|
use crate::core_isolate::CoreIsolateState;
|
2020-01-24 15:10:49 -05:00
|
|
|
use crate::ZeroCopyBuf;
|
2019-09-30 14:59:44 -04:00
|
|
|
use futures::Future;
|
|
|
|
use std::collections::HashMap;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-02-14 19:18:36 -05:00
|
|
|
use std::rc::Rc;
|
2019-09-30 14:59:44 -04:00
|
|
|
|
2020-01-06 14:07:35 -05:00
|
|
|
pub type OpId = u32;
|
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
pub type Buf = Box<[u8]>;
|
|
|
|
|
2020-04-18 20:05:13 -04:00
|
|
|
pub type OpAsyncFuture = Pin<Box<dyn Future<Output = Buf>>>;
|
2019-09-30 14:59:44 -04:00
|
|
|
|
2020-04-18 20:05:13 -04:00
|
|
|
pub enum Op {
|
2019-09-30 14:59:44 -04:00
|
|
|
Sync(Buf),
|
2020-04-18 20:05:13 -04:00
|
|
|
Async(OpAsyncFuture),
|
2020-01-21 12:01:10 -05:00
|
|
|
/// AsyncUnref is the variation of Async, which doesn't block the program
|
|
|
|
/// exiting.
|
2020-04-18 20:05:13 -04:00
|
|
|
AsyncUnref(OpAsyncFuture),
|
2019-09-30 14:59:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Main type describing op
|
2020-04-19 23:54:46 -04:00
|
|
|
pub type OpDispatcher =
|
2020-07-08 11:23:50 -04:00
|
|
|
dyn Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static;
|
2019-09-30 14:59:44 -04:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct OpRegistry {
|
2020-04-19 23:54:46 -04:00
|
|
|
dispatchers: Vec<Rc<OpDispatcher>>,
|
|
|
|
name_to_id: HashMap<String, OpId>,
|
2019-09-30 14:59:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OpRegistry {
|
|
|
|
pub fn new() -> Self {
|
2020-04-19 23:54:46 -04:00
|
|
|
let mut registry = Self::default();
|
2020-07-08 11:23:50 -04:00
|
|
|
let op_id = registry.register("ops", |state, _| {
|
2020-05-29 17:41:39 -04:00
|
|
|
let buf = state.op_registry.json_map();
|
2020-04-19 23:54:46 -04:00
|
|
|
Op::Sync(buf)
|
2019-09-30 14:59:44 -04:00
|
|
|
});
|
|
|
|
assert_eq!(op_id, 0);
|
|
|
|
registry
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:54:46 -04:00
|
|
|
pub fn register<F>(&mut self, name: &str, op: F) -> OpId
|
2019-09-30 14:59:44 -04:00
|
|
|
where
|
2020-07-08 11:23:50 -04:00
|
|
|
F: Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op + 'static,
|
2019-09-30 14:59:44 -04:00
|
|
|
{
|
2020-04-19 23:54:46 -04:00
|
|
|
let op_id = self.dispatchers.len() as u32;
|
2019-09-30 14:59:44 -04:00
|
|
|
|
2020-04-19 23:54:46 -04:00
|
|
|
let existing = self.name_to_id.insert(name.to_string(), op_id);
|
2019-09-30 14:59:44 -04:00
|
|
|
assert!(
|
|
|
|
existing.is_none(),
|
|
|
|
format!("Op already registered: {}", name)
|
|
|
|
);
|
2020-04-19 23:54:46 -04:00
|
|
|
self.dispatchers.push(Rc::new(op));
|
2019-09-30 14:59:44 -04:00
|
|
|
op_id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn json_map(&self) -> Buf {
|
2020-04-19 23:54:46 -04:00
|
|
|
let op_map_json = serde_json::to_string(&self.name_to_id).unwrap();
|
2019-09-30 14:59:44 -04:00
|
|
|
op_map_json.as_bytes().to_owned().into_boxed_slice()
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:54:46 -04:00
|
|
|
pub fn get(&self, op_id: OpId) -> Option<Rc<OpDispatcher>> {
|
|
|
|
self.dispatchers.get(op_id as usize).map(Rc::clone)
|
2019-09-30 14:59:44 -04:00
|
|
|
}
|
2020-06-09 18:14:13 -04:00
|
|
|
|
|
|
|
pub fn unregister_op(&mut self, name: &str) {
|
|
|
|
let id = self.name_to_id.remove(name).unwrap();
|
|
|
|
drop(self.dispatchers.remove(id as usize));
|
|
|
|
}
|
2019-09-30 14:59:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_op_registry() {
|
2020-05-29 17:41:39 -04:00
|
|
|
use crate::CoreIsolate;
|
2019-09-30 14:59:44 -04:00
|
|
|
use std::sync::atomic;
|
|
|
|
use std::sync::Arc;
|
2020-04-19 23:54:46 -04:00
|
|
|
let mut op_registry = OpRegistry::new();
|
2019-09-30 14:59:44 -04:00
|
|
|
|
|
|
|
let c = Arc::new(atomic::AtomicUsize::new(0));
|
|
|
|
let c_ = c.clone();
|
|
|
|
|
2020-07-08 11:23:50 -04:00
|
|
|
let test_id = op_registry.register("test", move |_, _| {
|
2019-09-30 14:59:44 -04:00
|
|
|
c_.fetch_add(1, atomic::Ordering::SeqCst);
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::Sync(Box::new([]))
|
2019-09-30 14:59:44 -04:00
|
|
|
});
|
|
|
|
assert!(test_id != 0);
|
|
|
|
|
|
|
|
let mut expected = HashMap::new();
|
|
|
|
expected.insert("ops".to_string(), 0);
|
|
|
|
expected.insert("test".to_string(), 1);
|
2020-04-19 23:54:46 -04:00
|
|
|
assert_eq!(op_registry.name_to_id, expected);
|
2019-09-30 14:59:44 -04:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let isolate = CoreIsolate::new(crate::StartupData::None, false);
|
2020-04-19 23:54:46 -04:00
|
|
|
|
|
|
|
let dispatch = op_registry.get(test_id).unwrap();
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = CoreIsolate::state(&isolate);
|
|
|
|
let mut state = state_rc.borrow_mut();
|
2020-07-08 11:23:50 -04:00
|
|
|
let res = dispatch(&mut state, &mut []);
|
2019-09-30 14:59:44 -04:00
|
|
|
if let Op::Sync(buf) = res {
|
|
|
|
assert_eq!(buf.len(), 0);
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
assert_eq!(c.load(atomic::Ordering::SeqCst), 1);
|
2019-10-22 10:49:58 -04:00
|
|
|
|
2020-04-19 23:54:46 -04:00
|
|
|
assert!(op_registry.get(100).is_none());
|
2020-06-09 18:14:13 -04:00
|
|
|
op_registry.unregister_op("test");
|
|
|
|
expected.remove("test");
|
|
|
|
assert_eq!(op_registry.name_to_id, expected);
|
|
|
|
assert!(op_registry.get(1).is_none());
|
2019-09-30 14:59:44 -04:00
|
|
|
}
|
2019-11-18 21:13:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn register_op_during_call() {
|
2020-05-29 17:41:39 -04:00
|
|
|
use crate::CoreIsolate;
|
2019-11-18 21:13:04 -05:00
|
|
|
use std::sync::atomic;
|
|
|
|
use std::sync::Arc;
|
2020-04-19 23:54:46 -04:00
|
|
|
use std::sync::Mutex;
|
|
|
|
let op_registry = Arc::new(Mutex::new(OpRegistry::new()));
|
2019-11-18 21:13:04 -05:00
|
|
|
|
|
|
|
let c = Arc::new(atomic::AtomicUsize::new(0));
|
|
|
|
let c_ = c.clone();
|
|
|
|
|
|
|
|
let op_registry_ = op_registry.clone();
|
2020-04-19 23:54:46 -04:00
|
|
|
|
|
|
|
let test_id = {
|
|
|
|
let mut g = op_registry.lock().unwrap();
|
2020-07-08 11:23:50 -04:00
|
|
|
g.register("dynamic_register_op", move |_, _| {
|
2020-04-19 23:54:46 -04:00
|
|
|
let c__ = c_.clone();
|
|
|
|
let mut g = op_registry_.lock().unwrap();
|
2020-07-08 11:23:50 -04:00
|
|
|
g.register("test", move |_, _| {
|
2020-04-19 23:54:46 -04:00
|
|
|
c__.fetch_add(1, atomic::Ordering::SeqCst);
|
|
|
|
Op::Sync(Box::new([]))
|
|
|
|
});
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::Sync(Box::new([]))
|
2020-04-19 23:54:46 -04:00
|
|
|
})
|
|
|
|
};
|
2019-11-18 21:13:04 -05:00
|
|
|
assert!(test_id != 0);
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let isolate = CoreIsolate::new(crate::StartupData::None, false);
|
2020-04-19 23:54:46 -04:00
|
|
|
|
|
|
|
let dispatcher1 = {
|
|
|
|
let g = op_registry.lock().unwrap();
|
|
|
|
g.get(test_id).unwrap()
|
|
|
|
};
|
2020-05-29 17:41:39 -04:00
|
|
|
{
|
|
|
|
let state_rc = CoreIsolate::state(&isolate);
|
|
|
|
let mut state = state_rc.borrow_mut();
|
2020-07-08 11:23:50 -04:00
|
|
|
dispatcher1(&mut state, &mut []);
|
2020-05-29 17:41:39 -04:00
|
|
|
}
|
2019-11-18 21:13:04 -05:00
|
|
|
|
|
|
|
let mut expected = HashMap::new();
|
|
|
|
expected.insert("ops".to_string(), 0);
|
|
|
|
expected.insert("dynamic_register_op".to_string(), 1);
|
|
|
|
expected.insert("test".to_string(), 2);
|
2020-04-19 23:54:46 -04:00
|
|
|
{
|
|
|
|
let g = op_registry.lock().unwrap();
|
|
|
|
assert_eq!(g.name_to_id, expected);
|
|
|
|
}
|
2019-11-18 21:13:04 -05:00
|
|
|
|
2020-04-19 23:54:46 -04:00
|
|
|
let dispatcher2 = {
|
|
|
|
let g = op_registry.lock().unwrap();
|
|
|
|
g.get(2).unwrap()
|
|
|
|
};
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = CoreIsolate::state(&isolate);
|
|
|
|
let mut state = state_rc.borrow_mut();
|
2020-07-08 11:23:50 -04:00
|
|
|
let res = dispatcher2(&mut state, &mut []);
|
2019-11-18 21:13:04 -05:00
|
|
|
if let Op::Sync(buf) = res {
|
|
|
|
assert_eq!(buf.len(), 0);
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
assert_eq!(c.load(atomic::Ordering::SeqCst), 1);
|
|
|
|
|
2020-04-19 23:54:46 -04:00
|
|
|
let g = op_registry.lock().unwrap();
|
|
|
|
assert!(g.get(100).is_none());
|
2019-11-18 21:13:04 -05:00
|
|
|
}
|