From 65d9bfb53361bfce6dc594c6a9df92c017dea6cb Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Sat, 24 Jun 2023 13:54:10 +0200 Subject: refactor(ops): Adding op2 macro and implementing in a couple of places (#19534) This is a new op system that will eventually replace `#[op]`. Features - More maintainable, generally less-coupled code - More modern Rust proc-macro libraries - Enforces correct `fast` labelling for fast ops, allowing for visual scanning of fast ops - Explicit marking of `#[string]`, `#[serde]` and `#[smi]` parameters. This first version of op2 supports integer and Option parameters only, and allows us to start working on converting ops and adding features. --- ops/op2/test_cases/sync/add.out | 54 +++++++++++++++++++++++++++++++++ ops/op2/test_cases/sync/add.rs | 6 ++++ ops/op2/test_cases/sync/add_options.out | 44 +++++++++++++++++++++++++++ ops/op2/test_cases/sync/add_options.rs | 6 ++++ ops/op2/test_cases/sync/doc_comment.out | 35 +++++++++++++++++++++ ops/op2/test_cases/sync/doc_comment.rs | 5 +++ ops/op2/test_cases/sync/smi.out | 52 +++++++++++++++++++++++++++++++ ops/op2/test_cases/sync/smi.rs | 4 +++ 8 files changed, 206 insertions(+) create mode 100644 ops/op2/test_cases/sync/add.out create mode 100644 ops/op2/test_cases/sync/add.rs create mode 100644 ops/op2/test_cases/sync/add_options.out create mode 100644 ops/op2/test_cases/sync/add_options.rs create mode 100644 ops/op2/test_cases/sync/doc_comment.out create mode 100644 ops/op2/test_cases/sync/doc_comment.rs create mode 100644 ops/op2/test_cases/sync/smi.out create mode 100644 ops/op2/test_cases/sync/smi.rs (limited to 'ops/op2/test_cases') diff --git a/ops/op2/test_cases/sync/add.out b/ops/op2/test_cases/sync/add.out new file mode 100644 index 000000000..a7269c5cf --- /dev/null +++ b/ops/op2/test_cases/sync/add.out @@ -0,0 +1,54 @@ +#[allow(non_camel_case_types)] +struct op_add {} +impl op_add { + pub const fn name() -> &'static str { + stringify!(op_add) + } + pub const fn decl() -> deno_core::_ops::OpDecl { + deno_core::_ops::OpDecl { + name: stringify!(op_add), + v8_fn_ptr: Self::slow_function as _, + enabled: true, + fast_fn: Some({ + use deno_core::v8::fast_api::Type; + use deno_core::v8::fast_api::CType; + deno_core::v8::fast_api::FastFunction::new( + &[Type::Uint32, Type::Uint32], + CType::Uint32, + Self::fast_function as *const ::std::ffi::c_void, + ) + }), + is_async: false, + is_unstable: false, + is_v8: false, + arg_count: 2usize as u8, + } + } + pub extern "C" fn slow_function(info: *const deno_core::v8::FunctionCallbackInfo) { + let mut rv = deno_core::v8::ReturnValue::from_function_callback_info(unsafe { + &*info + }); + let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { + &*info + }); + let arg0 = args.get(0usize as i32); + let arg0 = deno_core::_ops::to_u32(&arg0) as _; + let arg1 = args.get(1usize as i32); + let arg1 = deno_core::_ops::to_u32(&arg1) as _; + let result = Self::call(arg0, arg1); + rv.set_uint32(result as u32); + } + fn fast_function( + _: deno_core::v8::Local, + arg0: u32, + arg1: u32, + ) -> u32 { + let arg0 = arg0 as _; + let arg1 = arg1 as _; + Self::call(arg0, arg1) + } + #[inline(always)] + fn call(a: u32, b: u32) -> u32 { + a + b + } +} diff --git a/ops/op2/test_cases/sync/add.rs b/ops/op2/test_cases/sync/add.rs new file mode 100644 index 000000000..74dbb1893 --- /dev/null +++ b/ops/op2/test_cases/sync/add.rs @@ -0,0 +1,6 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +#[op2(fast)] +fn op_add(a: u32, b: u32) -> u32 { + a + b +} diff --git a/ops/op2/test_cases/sync/add_options.out b/ops/op2/test_cases/sync/add_options.out new file mode 100644 index 000000000..682a77309 --- /dev/null +++ b/ops/op2/test_cases/sync/add_options.out @@ -0,0 +1,44 @@ +#[allow(non_camel_case_types)] +pub struct op_test_add_option {} +impl op_test_add_option { + pub const fn name() -> &'static str { + stringify!(op_test_add_option) + } + pub const fn decl() -> crate::deno_core::_ops::OpDecl { + crate::deno_core::_ops::OpDecl { + name: stringify!(op_test_add_option), + v8_fn_ptr: Self::slow_function as _, + enabled: true, + fast_fn: None, + is_async: false, + is_unstable: false, + is_v8: false, + arg_count: 2usize as u8, + } + } + pub extern "C" fn slow_function( + info: *const crate::deno_core::v8::FunctionCallbackInfo, + ) { + let mut rv = crate::deno_core::v8::ReturnValue::from_function_callback_info(unsafe { + &*info + }); + let args = crate::deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { + &*info + }); + let arg0 = args.get(0usize as i32); + let arg0 = crate::deno_core::_ops::to_u32(&arg0) as _; + let arg1 = args.get(1usize as i32); + let arg1 = if arg1.is_null_or_undefined() { + None + } else { + let arg1 = crate::deno_core::_ops::to_u32(&arg1) as _; + Some(arg1) + }; + let result = Self::call(arg0, arg1); + rv.set_uint32(result as u32); + } + #[inline(always)] + pub fn call(a: u32, b: Option) -> u32 { + a + b.unwrap_or(100) + } +} diff --git a/ops/op2/test_cases/sync/add_options.rs b/ops/op2/test_cases/sync/add_options.rs new file mode 100644 index 000000000..a5f2c8f4a --- /dev/null +++ b/ops/op2/test_cases/sync/add_options.rs @@ -0,0 +1,6 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +#[op2(core)] +pub fn op_test_add_option(a: u32, b: Option) -> u32 { + a + b.unwrap_or(100) +} diff --git a/ops/op2/test_cases/sync/doc_comment.out b/ops/op2/test_cases/sync/doc_comment.out new file mode 100644 index 000000000..bd0d0b21f --- /dev/null +++ b/ops/op2/test_cases/sync/doc_comment.out @@ -0,0 +1,35 @@ +#[allow(non_camel_case_types)] +pub struct op_has_doc_comment {} +impl op_has_doc_comment { + pub const fn name() -> &'static str { + stringify!(op_has_doc_comment) + } + pub const fn decl() -> deno_core::_ops::OpDecl { + deno_core::_ops::OpDecl { + name: stringify!(op_has_doc_comment), + v8_fn_ptr: Self::slow_function as _, + enabled: true, + fast_fn: Some({ + use deno_core::v8::fast_api::Type; + use deno_core::v8::fast_api::CType; + deno_core::v8::fast_api::FastFunction::new( + &[], + CType::Void, + Self::fast_function as *const ::std::ffi::c_void, + ) + }), + is_async: false, + is_unstable: false, + is_v8: false, + arg_count: 0usize as u8, + } + } + pub extern "C" fn slow_function(info: *const deno_core::v8::FunctionCallbackInfo) { + let result = Self::call(); + } + fn fast_function(_: deno_core::v8::Local) -> () { + Self::call() + } + #[inline(always)] + pub fn call() -> () {} +} diff --git a/ops/op2/test_cases/sync/doc_comment.rs b/ops/op2/test_cases/sync/doc_comment.rs new file mode 100644 index 000000000..b729a64bd --- /dev/null +++ b/ops/op2/test_cases/sync/doc_comment.rs @@ -0,0 +1,5 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +/// This is a doc comment. +#[op2(fast)] +pub fn op_has_doc_comment() -> () {} diff --git a/ops/op2/test_cases/sync/smi.out b/ops/op2/test_cases/sync/smi.out new file mode 100644 index 000000000..e6c1bc1e3 --- /dev/null +++ b/ops/op2/test_cases/sync/smi.out @@ -0,0 +1,52 @@ +#[allow(non_camel_case_types)] +struct op_add {} +impl op_add { + pub const fn name() -> &'static str { + stringify!(op_add) + } + pub const fn decl() -> deno_core::_ops::OpDecl { + deno_core::_ops::OpDecl { + name: stringify!(op_add), + v8_fn_ptr: Self::slow_function as _, + enabled: true, + fast_fn: Some({ + use deno_core::v8::fast_api::Type; + use deno_core::v8::fast_api::CType; + deno_core::v8::fast_api::FastFunction::new( + &[Type::Int32, Type::Uint32], + CType::Uint32, + Self::fast_function as *const ::std::ffi::c_void, + ) + }), + is_async: false, + is_unstable: false, + is_v8: false, + arg_count: 2usize as u8, + } + } + pub extern "C" fn slow_function(info: *const deno_core::v8::FunctionCallbackInfo) { + let mut rv = deno_core::v8::ReturnValue::from_function_callback_info(unsafe { + &*info + }); + let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { + &*info + }); + let arg0 = args.get(0usize as i32); + let arg0 = deno_core::_ops::to_i32(&arg0) as _; + let arg1 = args.get(1usize as i32); + let arg1 = deno_core::_ops::to_u32(&arg1) as _; + let result = Self::call(arg0, arg1); + rv.set_uint32(result as u32); + } + fn fast_function( + _: deno_core::v8::Local, + arg0: i32, + arg1: u32, + ) -> u32 { + let arg0 = arg0 as _; + let arg1 = arg1 as _; + Self::call(arg0, arg1) + } + #[inline(always)] + fn call(id: ResourceId, extra: u16) -> u32 {} +} diff --git a/ops/op2/test_cases/sync/smi.rs b/ops/op2/test_cases/sync/smi.rs new file mode 100644 index 000000000..a5a441845 --- /dev/null +++ b/ops/op2/test_cases/sync/smi.rs @@ -0,0 +1,4 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +#[op2(fast)] +fn op_add(#[smi] id: ResourceId, extra: u16) -> u32 {} -- cgit v1.2.3