Skip to content

Commit 0231a18

Browse files
authored
Symbol keys in object wrap (#1017)
```rust #[fast] #[symbol("symbolMethod")] fn something(&self) {} ``` Equivalent to `Symbol.for("symbolMethod")`
1 parent 8092f36 commit 0231a18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+126
-4
lines changed

core/extensions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ pub struct OpDecl {
199199
pub name_fast: FastStaticString,
200200
pub is_async: bool,
201201
pub is_reentrant: bool,
202+
pub symbol_for: bool,
202203
pub accessor_type: AccessorType,
203204
pub arg_count: u8,
204205
pub no_side_effects: bool,
@@ -222,6 +223,7 @@ impl OpDecl {
222223
name: (&'static str, FastStaticString),
223224
is_async: bool,
224225
is_reentrant: bool,
226+
symbol_for: bool,
225227
arg_count: u8,
226228
no_side_effects: bool,
227229
slow_fn: OpFnRef,
@@ -237,6 +239,7 @@ impl OpDecl {
237239
name_fast: name.1,
238240
is_async,
239241
is_reentrant,
242+
symbol_for,
240243
arg_count,
241244
no_side_effects,
242245
slow_fn,

core/runtime/bindings.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use v8::MapFnTo;
1010
use super::jsruntime::BUILTIN_SOURCES;
1111
use super::jsruntime::CONTEXT_SETUP_SOURCES;
1212
use super::v8_static_strings::*;
13+
use crate::OpDecl;
1314
use crate::_ops::OpMethodDecl;
1415
use crate::cppgc::cppgc_template_constructor;
1516
use crate::cppgc::FunctionTemplateData;
@@ -328,6 +329,18 @@ pub(crate) fn initialize_primordials_and_infra(
328329
Ok(())
329330
}
330331

332+
fn name_key<'s>(
333+
scope: &mut v8::HandleScope<'s>,
334+
decl: &OpDecl,
335+
) -> v8::Local<'s, v8::Name> {
336+
let key_str = decl.name_fast.v8_string(scope).unwrap();
337+
if decl.symbol_for {
338+
v8::Symbol::for_key(scope, key_str).into()
339+
} else {
340+
key_str.into()
341+
}
342+
}
343+
331344
/// Set up JavaScript bindings for ops.
332345
pub(crate) fn initialize_deno_core_ops_bindings<'s>(
333346
scope: &mut v8::HandleScope<'s>,
@@ -389,8 +402,9 @@ pub(crate) fn initialize_deno_core_ops_bindings<'s>(
389402
let static_method_ctxs = &op_ctxs[index..index + decl.static_methods.len()];
390403
for method in static_method_ctxs.iter() {
391404
let op_fn = op_ctx_template(scope, method);
392-
let method_key = method.decl.name_fast;
393-
tmpl.set(method_key.v8_string(scope).unwrap().into(), op_fn.into());
405+
let method_key = name_key(scope, &method.decl);
406+
407+
tmpl.set(method_key, op_fn.into());
394408
}
395409

396410
index += decl.static_methods.len();
@@ -434,7 +448,7 @@ fn op_ctx_template_or_accessor<'s>(
434448
) {
435449
if !op_ctx.decl.is_accessor() {
436450
let op_fn = op_ctx_template(scope, op_ctx);
437-
let method_key = op_ctx.decl.name_fast.v8_string(scope).unwrap();
451+
let method_key = name_key(scope, &op_ctx.decl);
438452
if op_ctx.decl.is_async {
439453
let undefined = v8::undefined(scope);
440454
let op_fn = op_fn.get_function(scope).unwrap();
@@ -452,7 +466,7 @@ fn op_ctx_template_or_accessor<'s>(
452466
return;
453467
}
454468

455-
tmpl.set(method_key.into(), op_fn.into());
469+
tmpl.set(method_key, op_fn.into());
456470

457471
return;
458472
}

ops/op2/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub(crate) struct MacroConfig {
4343
pub required: u8,
4444
/// Rename the op to the given name.
4545
pub rename: Option<String>,
46+
/// Symbol.for("op_name") for the op.
47+
pub symbol: bool,
4648
}
4749

4850
impl MacroConfig {
@@ -144,6 +146,10 @@ impl MacroConfig {
144146
} else if flag.starts_with("rename(") {
145147
let tokens = syn::parse_str::<LitStr>(&flag[7..flag.len() - 1])?;
146148
config.rename = Some(tokens.value());
149+
} else if flag.starts_with("symbol(") {
150+
let tokens = syn::parse_str::<LitStr>(&flag[7..flag.len() - 1])?;
151+
config.rename = Some(tokens.value());
152+
config.symbol = true;
147153
} else {
148154
return Err(Op2Error::InvalidAttribute(flag));
149155
}

ops/op2/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ pub(crate) fn generate_op2(
310310
quote!(::deno_core::AccessorType::None)
311311
};
312312

313+
let symbol_for = config.symbol;
314+
313315
Ok(quote! {
314316
#[allow(non_camel_case_types)]
315317
#vis const fn #rust_name <#(#generic : #bound),*> () -> ::deno_core::_ops::OpDecl {
@@ -326,6 +328,7 @@ pub(crate) fn generate_op2(
326328
/*name*/ ::deno_core::__op_name_fast!(#name),
327329
/*is_async*/ #is_async,
328330
/*is_reentrant*/ #is_reentrant,
331+
/*symbol_for*/ #symbol_for,
329332
/*arg_count*/ #arg_count as u8,
330333
/*no_side_effect*/ #no_side_effect,
331334
/*slow_fn*/ Self::#slow_function as _,

ops/op2/test_cases/async/async_arg_return.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ops/op2/test_cases/async/async_arg_return_result.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ops/op2/test_cases/async/async_cppgc.out

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ops/op2/test_cases/async/async_deferred.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ops/op2/test_cases/async/async_jsbuffer.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ops/op2/test_cases/async/async_lazy.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)