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

Allow object wrap without constructor #1029

Merged
merged 4 commits into from
Jan 8, 2025
Merged
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
9 changes: 6 additions & 3 deletions core/extension_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,13 @@ pub fn create_op_ctxs(
};

for (index, decl) in op_method_decls.iter_mut().enumerate() {
decl.constructor.name = decl.name.0;
decl.constructor.name_fast = decl.name.1;
if let Some(mut constructor) = decl.constructor {
constructor.name = decl.name.0;
constructor.name_fast = decl.name.1;

op_ctxs.push(create_ctx(index, constructor));
}

op_ctxs.push(create_ctx(index, decl.constructor));
for method in decl.methods {
op_ctxs.push(create_ctx(index, *method));
}
Expand Down
2 changes: 1 addition & 1 deletion core/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const NOOP_FN: CFunction = CFunction::new(
pub struct OpMethodDecl {
pub type_name: fn() -> &'static str,
pub name: (&'static str, FastStaticString),
pub constructor: OpDecl,
pub constructor: Option<OpDecl>,
pub methods: &'static [OpDecl],
pub static_methods: &'static [OpDecl],
}
Expand Down
30 changes: 21 additions & 9 deletions core/runtime/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,22 @@ pub(crate) fn initialize_deno_core_ops_bindings<'s>(
if index == methods_ctx_offset {
break;
}
let constructor_ctx = &op_ctxs[index];

let tmpl = op_ctx_template(scope, constructor_ctx);
let prototype = tmpl.prototype_template(scope);
let key = decl.constructor.name_fast.v8_string(scope).unwrap();
let tmpl = if decl.constructor.is_some() {
let constructor_ctx = &op_ctxs[index];

index += 1;
let tmpl = op_ctx_template(scope, constructor_ctx);

index += 1;

tmpl
} else {
crate::cppgc::make_cppgc_template(scope)
};

let key = decl.name.1.v8_string(scope).unwrap();

let prototype = tmpl.prototype_template(scope);
let method_ctxs = &op_ctxs[index..index + decl.methods.len()];

let accessor_store = create_accessor_store(method_ctxs);
Expand Down Expand Up @@ -1030,12 +1038,16 @@ pub fn create_exports_for_ops_virtual_module<'s>(
if index == methods_ctx_offset {
break;
}
let constructor_ctx = &op_ctxs[index];

let op_fn = get(scope, ops_obj, constructor_ctx.decl.name_fast, "op");
exports.push((constructor_ctx.decl.name_fast, op_fn));
if decl.constructor.is_some() {
index += 1;
}

let name = decl.name.1;
let op_fn = get(scope, ops_obj, name, "op");
exports.push((name, op_fn));

index += 1 + decl.methods.len() + decl.static_methods.len();
index += decl.methods.len() + decl.static_methods.len();
}

let op_ctxs = &op_ctxs[index..];
Expand Down
8 changes: 7 additions & 1 deletion ops/op2/object_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ pub(crate) fn generate_impl_ops(
}
}

let constructor = if let Some(constructor) = constructor {
quote! { Some(#self_ty::#constructor()) }
} else {
quote! { None }
};

let res = quote! {
impl #self_ty {
pub const DECL: deno_core::_ops::OpMethodDecl = deno_core::_ops::OpMethodDecl {
Expand All @@ -147,7 +153,7 @@ pub(crate) fn generate_impl_ops(
#self_ty::#static_methods(),
)*
],
constructor: #self_ty::#constructor(),
constructor: #constructor,
name: ::deno_core::__op_name_fast!(#self_ty),
type_name: || std::any::type_name::<#self_ty>(),
};
Expand Down
2 changes: 1 addition & 1 deletion ops/op2/test_cases/sync/object_wrap.out

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

3 changes: 2 additions & 1 deletion testing/checkin/runner/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ deno_core::extension!(
],
objects = [
ops::DOMPoint,
ops::TestObjectWrap
ops::TestObjectWrap,
ops::TestEnumWrap
],
esm_entry_point = "ext:checkin_runtime/__init.js",
esm = [
Expand Down
17 changes: 17 additions & 0 deletions testing/checkin/runner/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ impl DOMPoint {
fn with_symbol(&self) {}
}

#[repr(u8)]
#[derive(Clone, Copy)]
pub enum TestEnumWrap {
#[allow(dead_code)]
A,
}

impl GarbageCollected for TestEnumWrap {}

#[op2]
impl TestEnumWrap {
#[getter]
fn as_int(&self) -> u8 {
*self as u8
}
}

#[op2(fast)]
pub fn op_nop_generic<T: SomeType + 'static>(state: &mut OpState) {
state.take::<T>();
Expand Down
4 changes: 2 additions & 2 deletions testing/checkin/runtime/object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2025 the Deno authors. MIT license.

import { DOMPoint, TestObjectWrap } from "ext:core/ops";
import { DOMPoint, TestEnumWrap, TestObjectWrap } from "ext:core/ops";

export { DOMPoint, TestObjectWrap };
export { DOMPoint, TestEnumWrap, TestObjectWrap };
2 changes: 2 additions & 0 deletions testing/ops.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ export class TestObjectWrap {
with_RENAME(): void;
withAsyncFn(ms: number): Promise<void>;
}

export class TestEnumWrap {}
4 changes: 3 additions & 1 deletion testing/unit/resource_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2025 the Deno authors. MIT license.
import { assert, assertArrayEquals, assertEquals, test } from "checkin:testing";
import { DOMPoint, TestObjectWrap } from "checkin:object";
import { DOMPoint, TestEnumWrap, TestObjectWrap } from "checkin:object";

const {
op_pipe_create,
Expand Down Expand Up @@ -110,4 +110,6 @@ test(async function testDomPoint() {
assert(promise instanceof Promise);

await promise;

new TestEnumWrap();
});
Loading