From bb53135ed87ec063c9238e1b7de8cf3b44535685 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Tue, 15 Mar 2022 23:43:17 +0100 Subject: [PATCH] cleanup(core): OpPair => OpDecl (#13952) --- core/bindings.rs | 12 ++++++------ core/examples/disable_ops.rs | 6 +++--- core/extensions.rs | 19 ++++++++++++------- core/lib.rs | 2 +- core/runtime.rs | 12 +++++++----- ext/net/ops.rs | 4 ++-- ext/net/ops_tls.rs | 4 ++-- ext/webgpu/src/lib.rs | 4 ++-- ops/lib.rs | 9 ++++++--- 9 files changed, 41 insertions(+), 31 deletions(-) diff --git a/core/bindings.rs b/core/bindings.rs index 0a87c0ffa6..2c879c21aa 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>, ) -> 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 9850e58e2f..7421961d4f 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 7361165f02..ae4537af4f 100644 --- a/core/extensions.rs +++ b/core/extensions.rs @@ -6,15 +6,20 @@ use std::task::Context; pub type SourcePair = (&'static str, Box); pub type SourceLoadFn = dyn Fn() -> Result; 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>, - ops: Option>, + ops: Option>, opstate_fn: Option>, middleware_fn: Option>, event_loop_middleware: Option>, @@ -38,7 +43,7 @@ impl Extension { } /// Called at JsRuntime startup to initialize ops in the isolate. - pub fn init_ops(&mut self) -> Option> { + pub fn init_ops(&mut self) -> Option> { // 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, - ops: Vec, + ops: Vec, state: Option>, middleware: Option>, event_loop_middleware: Option>, @@ -94,7 +99,7 @@ impl ExtensionBuilder { self } - pub fn ops(&mut self, ops: Vec) -> &mut Self { + pub fn ops(&mut self, ops: Vec) -> &mut Self { self.ops.extend(ops); self } @@ -109,7 +114,7 @@ impl ExtensionBuilder { pub fn middleware(&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 929a68cbb7..de794b30f3 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 c95ef6a20d..17840a2d05 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 { + fn collect_ops(extensions: &mut [Extension]) -> Vec { // Middleware let middleware: Vec> = 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 1cd3ad8e6b..0167448ddd 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() -> Vec { +pub fn init() -> Vec { vec![ op_net_accept::decl(), op_net_connect::decl::

(), diff --git a/ext/net/ops_tls.rs b/ext/net/ops_tls.rs index fea59cc22f..05e0071768 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() -> Vec { +pub fn init() -> Vec { vec![ op_tls_start::decl::

(), op_tls_connect::decl::

(), diff --git a/ext/webgpu/src/lib.rs b/ext/webgpu/src/lib.rs index 329bec7551..6904b68e43 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 { +fn declare_webgpu_ops() -> Vec { vec![ // Request device/adapter op_webgpu_request_adapter::decl(), diff --git a/ops/lib.rs b/ops/lib.rs index 126da368a3..718922c59b 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]