summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/bindings.rs12
-rw-r--r--core/examples/disable_ops.rs6
-rw-r--r--core/extensions.rs19
-rw-r--r--core/lib.rs2
-rw-r--r--core/runtime.rs12
5 files changed, 29 insertions, 22 deletions
diff --git a/core/bindings.rs b/core/bindings.rs
index 0a87c0ffa..2c879c21a 100644
--- a/core/bindings.rs
+++ b/core/bindings.rs
@@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::error::is_instance_of_error;
-use crate::extensions::OpPair;
+use crate::extensions::OpDecl;
use crate::modules::get_module_type_from_assertions;
use crate::modules::parse_import_assertions;
use crate::modules::validate_import_assertions;
@@ -142,7 +142,7 @@ pub fn module_origin<'a>(
pub fn initialize_context<'s>(
scope: &mut v8::HandleScope<'s, ()>,
- ops: &[OpPair],
+ ops: &[OpDecl],
snapshot_loaded: bool,
op_state: Rc<RefCell<OpState>>,
) -> v8::Local<'s, v8::Context> {
@@ -175,8 +175,8 @@ pub fn initialize_context<'s>(
.expect("`Deno.core.ops` not in global scope");
let raw_op_state = Rc::as_ptr(&op_state) as *const c_void;
- for (name, opfn) in ops {
- set_func_raw(scope, ops_val, name, *opfn, raw_op_state);
+ for op in ops {
+ set_func_raw(scope, ops_val, op.name, op.v8_fn_ptr, raw_op_state);
}
return scope.escape(context);
}
@@ -246,8 +246,8 @@ pub fn initialize_context<'s>(
// Bind functions to Deno.core.ops.*
let raw_op_state = Rc::as_ptr(&op_state) as *const c_void;
- for (name, opfn) in ops {
- set_func_raw(scope, ops_val, name, *opfn, raw_op_state);
+ for op in ops {
+ set_func_raw(scope, ops_val, op.name, op.v8_fn_ptr, raw_op_state);
}
scope.escape(context)
}
diff --git a/core/examples/disable_ops.rs b/core/examples/disable_ops.rs
index 9850e58e2..7421961d4 100644
--- a/core/examples/disable_ops.rs
+++ b/core/examples/disable_ops.rs
@@ -8,9 +8,9 @@ use deno_core::RuntimeOptions;
fn main() {
let my_ext = Extension::builder()
- .middleware(|name, opfn| match name {
- "op_print" => deno_core::op_void_sync::v8_cb(),
- _ => opfn,
+ .middleware(|op| match op.name {
+ "op_print" => deno_core::op_void_sync::decl(),
+ _ => op,
})
.build();
diff --git a/core/extensions.rs b/core/extensions.rs
index 7361165f0..ae4537af4 100644
--- a/core/extensions.rs
+++ b/core/extensions.rs
@@ -6,15 +6,20 @@ use std::task::Context;
pub type SourcePair = (&'static str, Box<SourceLoadFn>);
pub type SourceLoadFn = dyn Fn() -> Result<String, Error>;
pub type OpFnRef = v8::FunctionCallback;
-pub type OpPair = (&'static str, OpFnRef);
-pub type OpMiddlewareFn = dyn Fn(&'static str, OpFnRef) -> OpFnRef;
+pub type OpMiddlewareFn = dyn Fn(OpDecl) -> OpDecl;
pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), Error>;
pub type OpEventLoopFn = dyn Fn(&mut OpState, &mut Context) -> bool;
+#[derive(Clone, Copy)]
+pub struct OpDecl {
+ pub name: &'static str,
+ pub v8_fn_ptr: OpFnRef,
+}
+
#[derive(Default)]
pub struct Extension {
js_files: Option<Vec<SourcePair>>,
- ops: Option<Vec<OpPair>>,
+ ops: Option<Vec<OpDecl>>,
opstate_fn: Option<Box<OpStateFn>>,
middleware_fn: Option<Box<OpMiddlewareFn>>,
event_loop_middleware: Option<Box<OpEventLoopFn>>,
@@ -38,7 +43,7 @@ impl Extension {
}
/// Called at JsRuntime startup to initialize ops in the isolate.
- pub fn init_ops(&mut self) -> Option<Vec<OpPair>> {
+ pub fn init_ops(&mut self) -> Option<Vec<OpDecl>> {
// TODO(@AaronO): maybe make op registration idempotent
if self.initialized {
panic!("init_ops called twice: not idempotent or correct");
@@ -82,7 +87,7 @@ impl Extension {
#[derive(Default)]
pub struct ExtensionBuilder {
js: Vec<SourcePair>,
- ops: Vec<OpPair>,
+ ops: Vec<OpDecl>,
state: Option<Box<OpStateFn>>,
middleware: Option<Box<OpMiddlewareFn>>,
event_loop_middleware: Option<Box<OpEventLoopFn>>,
@@ -94,7 +99,7 @@ impl ExtensionBuilder {
self
}
- pub fn ops(&mut self, ops: Vec<OpPair>) -> &mut Self {
+ pub fn ops(&mut self, ops: Vec<OpDecl>) -> &mut Self {
self.ops.extend(ops);
self
}
@@ -109,7 +114,7 @@ impl ExtensionBuilder {
pub fn middleware<F>(&mut self, middleware_fn: F) -> &mut Self
where
- F: Fn(&'static str, OpFnRef) -> OpFnRef + 'static,
+ F: Fn(OpDecl) -> OpDecl + 'static,
{
self.middleware = Some(Box::new(middleware_fn));
self
diff --git a/core/lib.rs b/core/lib.rs
index 929a68cbb..de794b30f 100644
--- a/core/lib.rs
+++ b/core/lib.rs
@@ -45,8 +45,8 @@ pub use crate::async_cell::RcLike;
pub use crate::async_cell::RcRef;
pub use crate::extensions::Extension;
pub use crate::extensions::ExtensionBuilder;
+pub use crate::extensions::OpDecl;
pub use crate::extensions::OpMiddlewareFn;
-pub use crate::extensions::OpPair;
pub use crate::flags::v8_set_flags;
pub use crate::inspector::InspectorMsg;
pub use crate::inspector::InspectorMsgKind;
diff --git a/core/runtime.rs b/core/runtime.rs
index c95ef6a20..17840a2d0 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -5,8 +5,8 @@ use crate::error::attach_handle_to_error;
use crate::error::generic_error;
use crate::error::ErrWithV8Handle;
use crate::error::JsError;
+use crate::extensions::OpDecl;
use crate::extensions::OpEventLoopFn;
-use crate::extensions::OpPair;
use crate::inspector::JsRuntimeInspector;
use crate::module_specifier::ModuleSpecifier;
use crate::modules::ModuleId;
@@ -462,7 +462,7 @@ impl JsRuntime {
}
/// Collects ops from extensions & applies middleware
- fn collect_ops(extensions: &mut [Extension]) -> Vec<OpPair> {
+ fn collect_ops(extensions: &mut [Extension]) -> Vec<OpDecl> {
// Middleware
let middleware: Vec<Box<OpMiddlewareFn>> = extensions
.iter_mut()
@@ -470,15 +470,17 @@ impl JsRuntime {
.collect();
// macroware wraps an opfn in all the middleware
- let macroware =
- move |name, opfn| middleware.iter().fold(opfn, |opfn, m| m(name, opfn));
+ let macroware = move |d| middleware.iter().fold(d, |d, m| m(d));
// Flatten ops & apply middlware
extensions
.iter_mut()
.filter_map(|e| e.init_ops())
.flatten()
- .map(|(name, opfn)| (name, macroware(name, opfn)))
+ .map(|d| OpDecl {
+ name: d.name,
+ ..macroware(d)
+ })
.collect()
}