Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename op2 to op #808

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ runs:
# Only regression tests here for now.
# Regression test for https://github.com/denoland/deno/pull/19615.
run: |-
cargo run -p deno_core --example op2
cargo run -p deno_core --example op2 --features include_js_files_for_snapshotting
cargo run -p deno_core --example op
cargo run -p deno_core --example op --features include_js_files_for_snapshotting
30 changes: 15 additions & 15 deletions core/benches/ops/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ deno_core::extension!(
],
);

#[op2]
#[op]
pub fn op_call_promise_resolver(scope: &mut v8::HandleScope, f: &v8::Function) {
let recv = v8::undefined(scope).into();
f.call(scope, recv, &[]);
}

#[op2]
#[op]
pub fn op_resolve_promise<'s>(
scope: &'s mut v8::HandleScope,
) -> v8::Local<'s, v8::Promise> {
Expand All @@ -42,57 +42,57 @@ pub fn op_resolve_promise<'s>(
resolver.get_promise(scope)
}

#[op2(fast)]
#[op(fast)]
pub fn op_void() {}

#[op2(fast)]
#[op(fast)]
pub fn op_make_external() -> *const c_void {
std::ptr::null()
}

#[op2(async)]
#[op(async)]
pub async fn op_async_void() {}

#[op2(async)]
#[op(async)]
pub async fn op_async_yield() {
tokio::task::yield_now().await
}

#[op2(async(lazy), fast)]
#[op(async(lazy), fast)]
pub async fn op_async_yield_lazy() {
tokio::task::yield_now().await
}

#[op2(async(lazy), nofast)]
#[op(async(lazy), nofast)]
pub async fn op_async_yield_lazy_nofast() {
tokio::task::yield_now().await
}

#[op2(async(deferred), fast)]
#[op(async(deferred), fast)]
pub async fn op_async_yield_deferred() {
tokio::task::yield_now().await
}

#[op2(async(deferred), nofast)]
#[op(async(deferred), nofast)]
pub async fn op_async_yield_deferred_nofast() {
tokio::task::yield_now().await
}

#[op2(async(lazy), fast)]
#[op(async(lazy), fast)]
pub async fn op_async_void_lazy() {}

#[op2(async(lazy), nofast)]
#[op(async(lazy), nofast)]
pub async fn op_async_void_lazy_nofast() {}

#[op2(async(deferred), fast)]
#[op(async(deferred), fast)]
pub async fn op_async_void_deferred_return() -> u32 {
1
}

#[op2(async(deferred), fast)]
#[op(async(deferred), fast)]
pub async fn op_async_void_deferred() {}

#[op2(async(deferred), nofast)]
#[op(async(deferred), nofast)]
pub async fn op_async_void_deferred_nofast() {}

fn bench_op(
Expand Down
52 changes: 26 additions & 26 deletions core/benches/ops/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,104 +42,104 @@ deno_core::extension!(
}
);

#[op2(fast)]
#[op(fast)]
pub fn op_void() {}

#[op2(nofast)]
#[op(nofast)]
pub fn op_void_nofast() {}

#[op2(fast)]
#[op(fast)]
pub fn op_void_metrics() {}

#[op2(nofast)]
#[op(nofast)]
pub fn op_void_nofast_metrics() {}

#[op2(fast)]
#[op(fast)]
pub fn op_u32() -> u32 {
1
}

#[op2]
#[op]
pub fn op_option_u32() -> Option<u32> {
Some(1)
}

#[op2(fast)]
#[op(fast)]
pub fn op_string(#[string] s: &str) -> u32 {
s.len() as _
}

#[op2(fast)]
#[op(fast)]
pub fn op_string_onebyte(#[string(onebyte)] s: Cow<[u8]>) -> u32 {
s.len() as _
}

#[op2]
#[op]
pub fn op_string_bytestring(#[serde] s: ByteString) -> u32 {
s.len() as _
}

#[op2]
#[op]
pub fn op_string_option_u32(#[string] s: &str) -> Option<u32> {
Some(s.len() as _)
}

#[op2(fast)]
#[op(fast)]
pub fn op_local(_s: v8::Local<v8::String>) {}

#[op2]
#[op]
pub fn op_local_scope(_scope: &mut v8::HandleScope, _s: v8::Local<v8::String>) {
}

#[op2(nofast)]
#[op(nofast)]
pub fn op_local_nofast(_s: v8::Local<v8::String>) {}

#[op2]
#[op]
pub fn op_global(#[global] _s: v8::Global<v8::String>) {}

#[op2]
#[op]
pub fn op_global_scope(
_scope: &mut v8::HandleScope,
#[global] _s: v8::Global<v8::String>,
) {
}

#[op2]
#[op]
pub fn op_scope(_scope: &mut v8::HandleScope) {}

#[op2(nofast)]
#[op(nofast)]
pub fn op_isolate_nofast(_isolate: *mut v8::Isolate) {}

#[op2(fast)]
#[op(fast)]
pub fn op_make_external() -> *const c_void {
std::ptr::null()
}

#[op2(fast)]
#[op(fast)]
pub fn op_bigint(#[bigint] _input: u64) {}

#[op2(fast)]
#[op(fast)]
#[bigint]
pub fn op_bigint_return() -> u64 {
0
}

#[op2(fast)]
#[op(fast)]
pub fn op_external(_input: *const c_void) {}

#[op2(nofast)]
#[op(nofast)]
pub fn op_external_nofast(_input: *const c_void) {}

#[op2(fast)]
#[op(fast)]
pub fn op_buffer(#[buffer] _buffer: &[u8]) {}

#[op2]
#[op]
pub fn op_buffer_jsbuffer(#[buffer] _buffer: JsBuffer) {}

#[op2(nofast)]
#[op(nofast)]
pub fn op_buffer_nofast(#[buffer] _buffer: &[u8]) {}

#[op2(fast)]
#[op(fast)]
pub fn op_arraybuffer(#[arraybuffer] _buffer: &[u8]) {}

fn bench_op(
Expand Down
2 changes: 1 addition & 1 deletion core/benches/snapshot/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro_rules! fake_extensions {
);

mod ops {
#[deno_core::op2(fast)]
#[deno_core::op(fast)]
pub fn $name() {
}
}
Expand Down
20 changes: 10 additions & 10 deletions core/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ use std::convert::Infallible;
/// When passing data from Rust into JS, either
/// via an op or by calling a JS function directly,
/// you need to serialize the data into a native
/// V8 value. When using the [`op2`][deno_core::op2] macro, the return
/// V8 value. When using the [`op`][deno_core::op] macro, the return
/// value is converted to a `v8::Local<Value>` automatically,
/// and the strategy for conversion is controlled by attributes
/// like `#[smi]`, `#[number]`, `#[string]`. For types with support
/// built-in to the op2 macro, like primitives, strings, and buffers,
/// built-in to the op macro, like primitives, strings, and buffers,
/// these attributes are sufficient and you don't need to worry about this trait.
///
/// If, however, you want to return a custom type from an op, or
/// simply want more control over the conversion process,
/// you can implement the `ToV8` trait. This allows you the
/// choose the best serialization strategy for your specific use case.
/// You can then use the `#[to_v8]` attribute to indicate
/// that the `#[op2]` macro should call your implementation for the conversion.
/// that the `#[op]` macro should call your implementation for the conversion.
///
/// # Example
///
/// ```ignore
/// use deno_core::ToV8;
/// use deno_core::convert::Smi;
/// use deno_core::op2;
/// use deno_core::op;
///
/// struct Foo(i32);
///
Expand All @@ -44,8 +44,8 @@ use std::convert::Infallible;
/// }
/// }
///
/// // using the `#[to_v8]` attribute tells the `op2` macro to call this implementation.
/// #[op2]
/// // using the `#[to_v8]` attribute tells the `op` macro to call this implementation.
/// #[op]
/// #[to_v8]
/// fn op_foo() -> Foo {
/// Foo(42)
Expand Down Expand Up @@ -80,15 +80,15 @@ pub trait ToV8<'a> {
/// To convert these values into custom Rust types, you can implement the [`FromV8`] trait.
///
/// Once you've implemented this trait, you can use the `#[from_v8]` attribute
/// to tell the [`op2`][deno_core::op2] macro to use your implementation to convert the argument
/// to tell the [`op`][deno_core::op] macro to use your implementation to convert the argument
/// to the desired type.
///
/// # Example
///
/// ```ignore
/// use deno_core::FromV8;
/// use deno_core::convert::Smi;
/// use deno_core::op2;
/// use deno_core::op;
///
/// struct Foo(i32);
///
Expand All @@ -103,8 +103,8 @@ pub trait ToV8<'a> {
/// }
/// }
///
/// // using the `#[from_v8]` attribute tells the `op2` macro to call this implementation.
/// #[op2]
/// // using the `#[from_v8]` attribute tells the `op` macro to call this implementation.
/// #[op]
/// fn op_foo(#[from_v8] foo: Foo) {
/// let Foo(_) = foo;
/// }
Expand Down
2 changes: 1 addition & 1 deletion core/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use deno_core::*;

/// An op for summing an array of numbers. The op-layer automatically
/// deserializes inputs and serializes the returned Result & value.
#[op2]
#[op]
fn op_sum(#[serde] nums: Vec<f64>) -> Result<f64, deno_core::error::AnyError> {
// Sum inputs
let sum = nums.iter().fold(0.0, |a, v| a + v);
Expand Down
2 changes: 1 addition & 1 deletion core/examples/op2.js → core/examples/op.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
globalThis.op2_sample = {
globalThis.op_sample = {
"use_state": (f) => Deno.core.ops.op_use_state(f),
};
14 changes: 7 additions & 7 deletions core/examples/op2.rs → core/examples/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use deno_core::anyhow::Error;
use deno_core::*;
use std::rc::Rc;

#[op2]
#[op]
fn op_use_state(
state: &mut OpState,
#[global] callback: v8::Global<v8::Function>,
Expand All @@ -14,25 +14,25 @@ fn op_use_state(
}

extension!(
op2_sample,
op_sample,
ops = [op_use_state],
esm_entry_point = "ext:op2_sample/op2.js",
esm = [ dir "examples", "op2.js" ],
docs = "A small example demonstrating op2 usage.", "Contains one op."
esm_entry_point = "ext:op_sample/op.js",
esm = [ dir "examples", "op.js" ],
docs = "A small example demonstrating op usage.", "Contains one op."
);

fn main() -> Result<(), Error> {
let module_name = "test.js";
let module_code = "
op2_sample.use_state(() => {
op_sample.use_state(() => {
console.log('Hello World');
});
"
.to_string();

let mut js_runtime = JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(FsModuleLoader)),
extensions: vec![op2_sample::init_ops_and_esm()],
extensions: vec![op_sample::init_ops_and_esm()],
..Default::default()
});

Expand Down
8 changes: 4 additions & 4 deletions core/examples/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ fn wasm_memory_unchecked(state: &mut OpState) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(ptr, len) }
}

#[op2]
#[op]
#[buffer]
fn op_get_wasm_module() -> Vec<u8> {
include_bytes!("wasm.wasm").as_slice().to_vec()
}

#[op2(fast)]
#[op(fast)]
fn op_wasm(state: &mut OpState, #[memory(caller)] memory: Option<&mut [u8]>) {
let memory = memory.unwrap_or_else(|| wasm_memory_unchecked(state));
memory[0] = 69;
}

#[op2(fast)]
#[op(fast)]
fn op_wasm_mem(memory: &v8::WasmMemoryObject) {
// let memory = memory.unwrap_or_else(|| wasm_memory_unchecked(state));
// memory[0] = 69;
Expand All @@ -50,7 +50,7 @@ fn op_wasm_mem(memory: &v8::WasmMemoryObject) {
slice[0] = 68;
}

#[op2]
#[op]
fn op_set_wasm_mem(
state: &mut OpState,
#[global] memory: v8::Global<v8::WasmMemoryObject>,
Expand Down
Loading
Loading