summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
-rw-r--r--ext/net/ops.rs4
-rw-r--r--ext/net/ops_tls.rs4
-rw-r--r--ext/webgpu/src/lib.rs4
-rw-r--r--ops/lib.rs9
9 files changed, 41 insertions, 31 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()
}
diff --git a/ext/net/ops.rs b/ext/net/ops.rs
index 1cd3ad8e6..0167448dd 100644
--- a/ext/net/ops.rs
+++ b/ext/net/ops.rs
@@ -15,7 +15,7 @@ use deno_core::AsyncRefCell;
use deno_core::ByteString;
use deno_core::CancelHandle;
use deno_core::CancelTryFuture;
-use deno_core::OpPair;
+use deno_core::OpDecl;
use deno_core::OpState;
use deno_core::RcRef;
use deno_core::Resource;
@@ -50,7 +50,7 @@ use crate::io::UnixStreamResource;
#[cfg(unix)]
use std::path::Path;
-pub fn init<P: NetPermissions + 'static>() -> Vec<OpPair> {
+pub fn init<P: NetPermissions + 'static>() -> Vec<OpDecl> {
vec![
op_net_accept::decl(),
op_net_connect::decl::<P>(),
diff --git a/ext/net/ops_tls.rs b/ext/net/ops_tls.rs
index fea59cc22..05e007176 100644
--- a/ext/net/ops_tls.rs
+++ b/ext/net/ops_tls.rs
@@ -33,7 +33,7 @@ use deno_core::AsyncResult;
use deno_core::ByteString;
use deno_core::CancelHandle;
use deno_core::CancelTryFuture;
-use deno_core::OpPair;
+use deno_core::OpDecl;
use deno_core::OpState;
use deno_core::RcRef;
use deno_core::Resource;
@@ -642,7 +642,7 @@ impl Write for ImplementWriteTrait<'_, TcpStream> {
}
}
-pub fn init<P: NetPermissions + 'static>() -> Vec<OpPair> {
+pub fn init<P: NetPermissions + 'static>() -> Vec<OpDecl> {
vec![
op_tls_start::decl::<P>(),
op_tls_connect::decl::<P>(),
diff --git a/ext/webgpu/src/lib.rs b/ext/webgpu/src/lib.rs
index 329bec755..6904b68e4 100644
--- a/ext/webgpu/src/lib.rs
+++ b/ext/webgpu/src/lib.rs
@@ -5,7 +5,7 @@ use deno_core::include_js_files;
use deno_core::op;
use deno_core::Extension;
-use deno_core::OpPair;
+use deno_core::OpDecl;
use deno_core::OpState;
use deno_core::Resource;
use deno_core::ResourceId;
@@ -562,7 +562,7 @@ pub fn op_webgpu_create_query_set(
) => state, WebGpuQuerySet)
}
-fn declare_webgpu_ops() -> Vec<OpPair> {
+fn declare_webgpu_ops() -> Vec<OpDecl> {
vec![
// Request device/adapter
op_webgpu_request_adapter::decl(),
diff --git a/ops/lib.rs b/ops/lib.rs
index 126da368a..718922c59 100644
--- a/ops/lib.rs
+++ b/ops/lib.rs
@@ -66,13 +66,16 @@ pub fn op(_attr: TokenStream, item: TokenStream) -> TokenStream {
stringify!(#name)
}
- pub fn v8_cb #generics () -> #core::v8::FunctionCallback #where_clause {
+ pub fn v8_fn_ptr #generics () -> #core::v8::FunctionCallback #where_clause {
use #core::v8::MapFnTo;
Self::v8_func::<#type_params>.map_fn_to()
}
- pub fn decl #generics () -> (&'static str, #core::v8::FunctionCallback) #where_clause {
- (Self::name(), Self::v8_cb::<#type_params>())
+ pub fn decl #generics () -> #core::OpDecl #where_clause {
+ #core::OpDecl {
+ name: Self::name(),
+ v8_fn_ptr: Self::v8_fn_ptr::<#type_params>(),
+ }
}
#[inline]