0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

cleanup(core): OpPair => OpDecl (#13952)

This commit is contained in:
Aaron O'Mullan 2022-03-15 23:43:17 +01:00 committed by GitHub
parent 5d60ee7f12
commit bb53135ed8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 31 deletions

View file

@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::error::is_instance_of_error; 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::get_module_type_from_assertions;
use crate::modules::parse_import_assertions; use crate::modules::parse_import_assertions;
use crate::modules::validate_import_assertions; use crate::modules::validate_import_assertions;
@ -142,7 +142,7 @@ pub fn module_origin<'a>(
pub fn initialize_context<'s>( pub fn initialize_context<'s>(
scope: &mut v8::HandleScope<'s, ()>, scope: &mut v8::HandleScope<'s, ()>,
ops: &[OpPair], ops: &[OpDecl],
snapshot_loaded: bool, snapshot_loaded: bool,
op_state: Rc<RefCell<OpState>>, op_state: Rc<RefCell<OpState>>,
) -> v8::Local<'s, v8::Context> { ) -> v8::Local<'s, v8::Context> {
@ -175,8 +175,8 @@ pub fn initialize_context<'s>(
.expect("`Deno.core.ops` not in global scope"); .expect("`Deno.core.ops` not in global scope");
let raw_op_state = Rc::as_ptr(&op_state) as *const c_void; let raw_op_state = Rc::as_ptr(&op_state) as *const c_void;
for (name, opfn) in ops { for op in ops {
set_func_raw(scope, ops_val, name, *opfn, raw_op_state); set_func_raw(scope, ops_val, op.name, op.v8_fn_ptr, raw_op_state);
} }
return scope.escape(context); return scope.escape(context);
} }
@ -246,8 +246,8 @@ pub fn initialize_context<'s>(
// Bind functions to Deno.core.ops.* // Bind functions to Deno.core.ops.*
let raw_op_state = Rc::as_ptr(&op_state) as *const c_void; let raw_op_state = Rc::as_ptr(&op_state) as *const c_void;
for (name, opfn) in ops { for op in ops {
set_func_raw(scope, ops_val, name, *opfn, raw_op_state); set_func_raw(scope, ops_val, op.name, op.v8_fn_ptr, raw_op_state);
} }
scope.escape(context) scope.escape(context)
} }

View file

@ -8,9 +8,9 @@ use deno_core::RuntimeOptions;
fn main() { fn main() {
let my_ext = Extension::builder() let my_ext = Extension::builder()
.middleware(|name, opfn| match name { .middleware(|op| match op.name {
"op_print" => deno_core::op_void_sync::v8_cb(), "op_print" => deno_core::op_void_sync::decl(),
_ => opfn, _ => op,
}) })
.build(); .build();

View file

@ -6,15 +6,20 @@ use std::task::Context;
pub type SourcePair = (&'static str, Box<SourceLoadFn>); pub type SourcePair = (&'static str, Box<SourceLoadFn>);
pub type SourceLoadFn = dyn Fn() -> Result<String, Error>; pub type SourceLoadFn = dyn Fn() -> Result<String, Error>;
pub type OpFnRef = v8::FunctionCallback; pub type OpFnRef = v8::FunctionCallback;
pub type OpPair = (&'static str, OpFnRef); pub type OpMiddlewareFn = dyn Fn(OpDecl) -> OpDecl;
pub type OpMiddlewareFn = dyn Fn(&'static str, OpFnRef) -> OpFnRef;
pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), Error>; pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), Error>;
pub type OpEventLoopFn = dyn Fn(&mut OpState, &mut Context) -> bool; 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)] #[derive(Default)]
pub struct Extension { pub struct Extension {
js_files: Option<Vec<SourcePair>>, js_files: Option<Vec<SourcePair>>,
ops: Option<Vec<OpPair>>, ops: Option<Vec<OpDecl>>,
opstate_fn: Option<Box<OpStateFn>>, opstate_fn: Option<Box<OpStateFn>>,
middleware_fn: Option<Box<OpMiddlewareFn>>, middleware_fn: Option<Box<OpMiddlewareFn>>,
event_loop_middleware: Option<Box<OpEventLoopFn>>, event_loop_middleware: Option<Box<OpEventLoopFn>>,
@ -38,7 +43,7 @@ impl Extension {
} }
/// Called at JsRuntime startup to initialize ops in the isolate. /// 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 // TODO(@AaronO): maybe make op registration idempotent
if self.initialized { if self.initialized {
panic!("init_ops called twice: not idempotent or correct"); panic!("init_ops called twice: not idempotent or correct");
@ -82,7 +87,7 @@ impl Extension {
#[derive(Default)] #[derive(Default)]
pub struct ExtensionBuilder { pub struct ExtensionBuilder {
js: Vec<SourcePair>, js: Vec<SourcePair>,
ops: Vec<OpPair>, ops: Vec<OpDecl>,
state: Option<Box<OpStateFn>>, state: Option<Box<OpStateFn>>,
middleware: Option<Box<OpMiddlewareFn>>, middleware: Option<Box<OpMiddlewareFn>>,
event_loop_middleware: Option<Box<OpEventLoopFn>>, event_loop_middleware: Option<Box<OpEventLoopFn>>,
@ -94,7 +99,7 @@ impl ExtensionBuilder {
self 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.ops.extend(ops);
self self
} }
@ -109,7 +114,7 @@ impl ExtensionBuilder {
pub fn middleware<F>(&mut self, middleware_fn: F) -> &mut Self pub fn middleware<F>(&mut self, middleware_fn: F) -> &mut Self
where where
F: Fn(&'static str, OpFnRef) -> OpFnRef + 'static, F: Fn(OpDecl) -> OpDecl + 'static,
{ {
self.middleware = Some(Box::new(middleware_fn)); self.middleware = Some(Box::new(middleware_fn));
self self

View file

@ -45,8 +45,8 @@ pub use crate::async_cell::RcLike;
pub use crate::async_cell::RcRef; pub use crate::async_cell::RcRef;
pub use crate::extensions::Extension; pub use crate::extensions::Extension;
pub use crate::extensions::ExtensionBuilder; pub use crate::extensions::ExtensionBuilder;
pub use crate::extensions::OpDecl;
pub use crate::extensions::OpMiddlewareFn; pub use crate::extensions::OpMiddlewareFn;
pub use crate::extensions::OpPair;
pub use crate::flags::v8_set_flags; pub use crate::flags::v8_set_flags;
pub use crate::inspector::InspectorMsg; pub use crate::inspector::InspectorMsg;
pub use crate::inspector::InspectorMsgKind; pub use crate::inspector::InspectorMsgKind;

View file

@ -5,8 +5,8 @@ use crate::error::attach_handle_to_error;
use crate::error::generic_error; use crate::error::generic_error;
use crate::error::ErrWithV8Handle; use crate::error::ErrWithV8Handle;
use crate::error::JsError; use crate::error::JsError;
use crate::extensions::OpDecl;
use crate::extensions::OpEventLoopFn; use crate::extensions::OpEventLoopFn;
use crate::extensions::OpPair;
use crate::inspector::JsRuntimeInspector; use crate::inspector::JsRuntimeInspector;
use crate::module_specifier::ModuleSpecifier; use crate::module_specifier::ModuleSpecifier;
use crate::modules::ModuleId; use crate::modules::ModuleId;
@ -462,7 +462,7 @@ impl JsRuntime {
} }
/// Collects ops from extensions & applies middleware /// Collects ops from extensions & applies middleware
fn collect_ops(extensions: &mut [Extension]) -> Vec<OpPair> { fn collect_ops(extensions: &mut [Extension]) -> Vec<OpDecl> {
// Middleware // Middleware
let middleware: Vec<Box<OpMiddlewareFn>> = extensions let middleware: Vec<Box<OpMiddlewareFn>> = extensions
.iter_mut() .iter_mut()
@ -470,15 +470,17 @@ impl JsRuntime {
.collect(); .collect();
// macroware wraps an opfn in all the middleware // macroware wraps an opfn in all the middleware
let macroware = let macroware = move |d| middleware.iter().fold(d, |d, m| m(d));
move |name, opfn| middleware.iter().fold(opfn, |opfn, m| m(name, opfn));
// Flatten ops & apply middlware // Flatten ops & apply middlware
extensions extensions
.iter_mut() .iter_mut()
.filter_map(|e| e.init_ops()) .filter_map(|e| e.init_ops())
.flatten() .flatten()
.map(|(name, opfn)| (name, macroware(name, opfn))) .map(|d| OpDecl {
name: d.name,
..macroware(d)
})
.collect() .collect()
} }

View file

@ -15,7 +15,7 @@ use deno_core::AsyncRefCell;
use deno_core::ByteString; use deno_core::ByteString;
use deno_core::CancelHandle; use deno_core::CancelHandle;
use deno_core::CancelTryFuture; use deno_core::CancelTryFuture;
use deno_core::OpPair; use deno_core::OpDecl;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
@ -50,7 +50,7 @@ use crate::io::UnixStreamResource;
#[cfg(unix)] #[cfg(unix)]
use std::path::Path; use std::path::Path;
pub fn init<P: NetPermissions + 'static>() -> Vec<OpPair> { pub fn init<P: NetPermissions + 'static>() -> Vec<OpDecl> {
vec![ vec![
op_net_accept::decl(), op_net_accept::decl(),
op_net_connect::decl::<P>(), op_net_connect::decl::<P>(),

View file

@ -33,7 +33,7 @@ use deno_core::AsyncResult;
use deno_core::ByteString; use deno_core::ByteString;
use deno_core::CancelHandle; use deno_core::CancelHandle;
use deno_core::CancelTryFuture; use deno_core::CancelTryFuture;
use deno_core::OpPair; use deno_core::OpDecl;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; 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![ vec![
op_tls_start::decl::<P>(), op_tls_start::decl::<P>(),
op_tls_connect::decl::<P>(), op_tls_connect::decl::<P>(),

View file

@ -5,7 +5,7 @@ use deno_core::include_js_files;
use deno_core::op; use deno_core::op;
use deno_core::Extension; use deno_core::Extension;
use deno_core::OpPair; use deno_core::OpDecl;
use deno_core::OpState; use deno_core::OpState;
use deno_core::Resource; use deno_core::Resource;
use deno_core::ResourceId; use deno_core::ResourceId;
@ -562,7 +562,7 @@ pub fn op_webgpu_create_query_set(
) => state, WebGpuQuerySet) ) => state, WebGpuQuerySet)
} }
fn declare_webgpu_ops() -> Vec<OpPair> { fn declare_webgpu_ops() -> Vec<OpDecl> {
vec![ vec![
// Request device/adapter // Request device/adapter
op_webgpu_request_adapter::decl(), op_webgpu_request_adapter::decl(),

View file

@ -66,13 +66,16 @@ pub fn op(_attr: TokenStream, item: TokenStream) -> TokenStream {
stringify!(#name) 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; use #core::v8::MapFnTo;
Self::v8_func::<#type_params>.map_fn_to() Self::v8_func::<#type_params>.map_fn_to()
} }
pub fn decl #generics () -> (&'static str, #core::v8::FunctionCallback) #where_clause { pub fn decl #generics () -> #core::OpDecl #where_clause {
(Self::name(), Self::v8_cb::<#type_params>()) #core::OpDecl {
name: Self::name(),
v8_fn_ptr: Self::v8_fn_ptr::<#type_params>(),
}
} }
#[inline] #[inline]