1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00
denoland-deno/core/ops.rs

161 lines
4 KiB
Rust
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::CoreIsolate;
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;
use std::rc::Rc;
2019-09-30 14:59:44 -04: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),
/// 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
pub type OpDispatcher =
dyn Fn(&mut CoreIsolate, &[u8], Option<ZeroCopyBuf>) -> Op + 'static;
2019-09-30 14:59:44 -04:00
#[derive(Default)]
pub struct OpRegistry {
dispatchers: Vec<Rc<OpDispatcher>>,
name_to_id: HashMap<String, OpId>,
2019-09-30 14:59:44 -04:00
}
impl OpRegistry {
pub fn new() -> Self {
let mut registry = Self::default();
let op_id = registry.register("ops", |isolate, _, _| {
let buf = isolate.op_registry.json_map();
Op::Sync(buf)
2019-09-30 14:59:44 -04:00
});
assert_eq!(op_id, 0);
registry
}
pub fn register<F>(&mut self, name: &str, op: F) -> OpId
2019-09-30 14:59:44 -04:00
where
F: Fn(&mut CoreIsolate, &[u8], Option<ZeroCopyBuf>) -> Op + 'static,
2019-09-30 14:59:44 -04:00
{
let op_id = self.dispatchers.len() as u32;
2019-09-30 14:59:44 -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)
);
self.dispatchers.push(Rc::new(op));
2019-09-30 14:59:44 -04:00
op_id
}
fn json_map(&self) -> Buf {
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()
}
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
}
}
#[test]
fn test_op_registry() {
use std::sync::atomic;
use std::sync::Arc;
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();
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);
assert_eq!(op_registry.name_to_id, expected);
2019-09-30 14:59:44 -04:00
let mut isolate = CoreIsolate::new(crate::StartupData::None, false);
let dispatch = op_registry.get(test_id).unwrap();
let res = dispatch(&mut isolate, &[], None);
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);
assert!(op_registry.get(100).is_none());
2019-09-30 14:59:44 -04:00
}
#[test]
fn register_op_during_call() {
use std::sync::atomic;
use std::sync::Arc;
use std::sync::Mutex;
let op_registry = Arc::new(Mutex::new(OpRegistry::new()));
let c = Arc::new(atomic::AtomicUsize::new(0));
let c_ = c.clone();
let op_registry_ = op_registry.clone();
let test_id = {
let mut g = op_registry.lock().unwrap();
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([]))
});
2020-04-18 20:05:13 -04:00
Op::Sync(Box::new([]))
})
};
assert!(test_id != 0);
let mut isolate = CoreIsolate::new(crate::StartupData::None, false);
let dispatcher1 = {
let g = op_registry.lock().unwrap();
g.get(test_id).unwrap()
};
dispatcher1(&mut isolate, &[], None);
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);
{
let g = op_registry.lock().unwrap();
assert_eq!(g.name_to_id, expected);
}
let dispatcher2 = {
let g = op_registry.lock().unwrap();
g.get(2).unwrap()
};
let res = dispatcher2(&mut isolate, &[], None);
if let Op::Sync(buf) = res {
assert_eq!(buf.len(), 0);
} else {
unreachable!();
}
assert_eq!(c.load(atomic::Ordering::SeqCst), 1);
let g = op_registry.lock().unwrap();
assert!(g.get(100).is_none());
}