Skip to content

Commit

Permalink
Symbol keys in object wrap (#1017)
Browse files Browse the repository at this point in the history
```rust
#[fast]
#[symbol("symbolMethod")]
fn something(&self) {}
```

Equivalent to `Symbol.for("symbolMethod")`
  • Loading branch information
littledivy authored Jan 2, 2025
1 parent 8092f36 commit 0231a18
Show file tree
Hide file tree
Showing 60 changed files with 126 additions and 4 deletions.
3 changes: 3 additions & 0 deletions core/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pub struct OpDecl {
pub name_fast: FastStaticString,
pub is_async: bool,
pub is_reentrant: bool,
pub symbol_for: bool,
pub accessor_type: AccessorType,
pub arg_count: u8,
pub no_side_effects: bool,
Expand All @@ -222,6 +223,7 @@ impl OpDecl {
name: (&'static str, FastStaticString),
is_async: bool,
is_reentrant: bool,
symbol_for: bool,
arg_count: u8,
no_side_effects: bool,
slow_fn: OpFnRef,
Expand All @@ -237,6 +239,7 @@ impl OpDecl {
name_fast: name.1,
is_async,
is_reentrant,
symbol_for,
arg_count,
no_side_effects,
slow_fn,
Expand Down
22 changes: 18 additions & 4 deletions core/runtime/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use v8::MapFnTo;
use super::jsruntime::BUILTIN_SOURCES;
use super::jsruntime::CONTEXT_SETUP_SOURCES;
use super::v8_static_strings::*;
use crate::OpDecl;
use crate::_ops::OpMethodDecl;
use crate::cppgc::cppgc_template_constructor;
use crate::cppgc::FunctionTemplateData;
Expand Down Expand Up @@ -328,6 +329,18 @@ pub(crate) fn initialize_primordials_and_infra(
Ok(())
}

fn name_key<'s>(
scope: &mut v8::HandleScope<'s>,
decl: &OpDecl,
) -> v8::Local<'s, v8::Name> {
let key_str = decl.name_fast.v8_string(scope).unwrap();
if decl.symbol_for {
v8::Symbol::for_key(scope, key_str).into()
} else {
key_str.into()
}
}

/// Set up JavaScript bindings for ops.
pub(crate) fn initialize_deno_core_ops_bindings<'s>(
scope: &mut v8::HandleScope<'s>,
Expand Down Expand Up @@ -389,8 +402,9 @@ pub(crate) fn initialize_deno_core_ops_bindings<'s>(
let static_method_ctxs = &op_ctxs[index..index + decl.static_methods.len()];
for method in static_method_ctxs.iter() {
let op_fn = op_ctx_template(scope, method);
let method_key = method.decl.name_fast;
tmpl.set(method_key.v8_string(scope).unwrap().into(), op_fn.into());
let method_key = name_key(scope, &method.decl);

tmpl.set(method_key, op_fn.into());
}

index += decl.static_methods.len();
Expand Down Expand Up @@ -434,7 +448,7 @@ fn op_ctx_template_or_accessor<'s>(
) {
if !op_ctx.decl.is_accessor() {
let op_fn = op_ctx_template(scope, op_ctx);
let method_key = op_ctx.decl.name_fast.v8_string(scope).unwrap();
let method_key = name_key(scope, &op_ctx.decl);
if op_ctx.decl.is_async {
let undefined = v8::undefined(scope);
let op_fn = op_fn.get_function(scope).unwrap();
Expand All @@ -452,7 +466,7 @@ fn op_ctx_template_or_accessor<'s>(
return;
}

tmpl.set(method_key.into(), op_fn.into());
tmpl.set(method_key, op_fn.into());

return;
}
Expand Down
6 changes: 6 additions & 0 deletions ops/op2/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub(crate) struct MacroConfig {
pub required: u8,
/// Rename the op to the given name.
pub rename: Option<String>,
/// Symbol.for("op_name") for the op.
pub symbol: bool,
}

impl MacroConfig {
Expand Down Expand Up @@ -144,6 +146,10 @@ impl MacroConfig {
} else if flag.starts_with("rename(") {
let tokens = syn::parse_str::<LitStr>(&flag[7..flag.len() - 1])?;
config.rename = Some(tokens.value());
} else if flag.starts_with("symbol(") {
let tokens = syn::parse_str::<LitStr>(&flag[7..flag.len() - 1])?;
config.rename = Some(tokens.value());
config.symbol = true;
} else {
return Err(Op2Error::InvalidAttribute(flag));
}
Expand Down
3 changes: 3 additions & 0 deletions ops/op2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ pub(crate) fn generate_op2(
quote!(::deno_core::AccessorType::None)
};

let symbol_for = config.symbol;

Ok(quote! {
#[allow(non_camel_case_types)]
#vis const fn #rust_name <#(#generic : #bound),*> () -> ::deno_core::_ops::OpDecl {
Expand All @@ -326,6 +328,7 @@ pub(crate) fn generate_op2(
/*name*/ ::deno_core::__op_name_fast!(#name),
/*is_async*/ #is_async,
/*is_reentrant*/ #is_reentrant,
/*symbol_for*/ #symbol_for,
/*arg_count*/ #arg_count as u8,
/*no_side_effect*/ #no_side_effect,
/*slow_fn*/ Self::#slow_function as _,
Expand Down
1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_arg_return.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_arg_return_result.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ops/op2/test_cases/async/async_cppgc.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_deferred.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_jsbuffer.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_lazy.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ops/op2/test_cases/async/async_op_metadata.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_opstate.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_result.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_result_impl.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_result_smi.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_stack_trace.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_v8_global.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/async/async_void.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/sync/add.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/sync/add_options.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/sync/bigint.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/sync/bool.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ops/op2/test_cases/sync/bool_result.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ops/op2/test_cases/sync/buffers.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ops/op2/test_cases/sync/buffers_copy.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ops/op2/test_cases/sync/buffers_out.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ops/op2/test_cases/sync/cfg.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ops/op2/test_cases/sync/clippy_allow.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0231a18

Please sign in to comment.