Skip to content

Commit d1694ab

Browse files
authored
feat(op2): support cppgc in async ops (denoland#673)
`#[cppgc]` return values were added in denoland/deno_core@a736d86 which made creation of cppgc backed objects possible without direct access to V8 APIs. This patch builds on top of that and adds `#[cppgc]` support for async ops.
1 parent 28d0561 commit d1694ab

File tree

6 files changed

+145
-2
lines changed

6 files changed

+145
-2
lines changed

ops/op2/dispatch_slow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ pub fn return_value_v8_value(
782782
quote!(deno_core::_ops::RustToV8Marker::<deno_core::_ops::NumberMarker, _>::from(#result))
783783
}
784784
ArgMarker::Cppgc => {
785-
quote!(deno_core::cppgc::make_cppgc_object(&mut #scope, #result))
785+
quote!(deno_core::cppgc::make_cppgc_object(#scope, #result))
786786
}
787787
ArgMarker::None => quote!(#result),
788788
};

ops/op2/test_cases/sync/cppgc_resource.out

Lines changed: 107 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/sync/cppgc_resource.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ struct Wrap;
1212
fn op_make_cppgc_object() -> Wrap {
1313
Wrap
1414
}
15+
16+
#[op2(async)]
17+
#[cppgc]
18+
async fn op_make_cppgc_object_async() -> Wrap {
19+
Wrap
20+
}

testing/checkin/runner/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ deno_core::extension!(
5252
ops_async::op_async_barrier_create,
5353
ops_async::op_async_barrier_await,
5454
ops_async::op_async_spin_on_state,
55+
ops_async::op_async_make_cppgc_resource,
56+
ops_async::op_async_get_cppgc_resource,
5557
ops_error::op_async_throw_error_eager,
5658
ops_error::op_async_throw_error_lazy,
5759
ops_error::op_async_throw_error_deferred,

testing/checkin/runner/ops_async.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ pub async fn op_async_spin_on_state(state: Rc<RefCell<OpState>>) {
4646
})
4747
.await
4848
}
49+
50+
pub struct TestResource {
51+
value: u32,
52+
}
53+
54+
#[op2(async)]
55+
#[cppgc]
56+
pub async fn op_async_make_cppgc_resource() -> TestResource {
57+
TestResource { value: 42 }
58+
}
59+
60+
#[op2(async)]
61+
#[smi]
62+
pub async fn op_async_get_cppgc_resource(
63+
#[cppgc] resource: &TestResource,
64+
) -> u32 {
65+
resource.value
66+
}

testing/unit/resource_test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22
import { assert, assertArrayEquals, assertEquals, test } from "checkin:testing";
33

4-
const { op_pipe_create, op_file_open } = Deno.core.ops;
4+
const {
5+
op_pipe_create,
6+
op_file_open,
7+
op_async_make_cppgc_resource,
8+
op_async_get_cppgc_resource,
9+
} = Deno.core.ops;
510

611
test(async function testPipe() {
712
const [p1, p2] = op_pipe_create();
@@ -52,3 +57,8 @@ test(async function testFileIsNotTerminal() {
5257
const file = await op_file_open("./README.md");
5358
assert(!Deno.core.isTerminal(file));
5459
});
60+
61+
test(async function testCppgcAsync() {
62+
const resource = await op_async_make_cppgc_resource();
63+
assertEquals(await op_async_get_cppgc_resource(resource), 42);
64+
});

0 commit comments

Comments
 (0)