Skip to content

Commit 3eee57f

Browse files
refactor(bindgen): clearer autovivicaion of individual handle table
1 parent 3536b3e commit 3eee57f

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

crates/js-component-bindgen/src/intrinsics/resource.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,16 @@ impl ResourceIntrinsic {
216216
function {resource_transfer_borrow_fn}(handle, fromTid, toTid) {{
217217
const fromTable = {handle_tables}[fromTid];
218218
const fromHandle = fromTable[(handle << 1) + 1];
219-
const toTable = {handle_tables}[toTid] || ({handle_tables}[toTid] = [T_FLAG, 0]);
220219
const isOwn = (fromHandle & T_FLAG) !== 0;
221220
const rep = isOwn ? fromHandle & ~T_FLAG : {rsc_table_remove}(fromTable, fromHandle).rep;
222221
222+
let toTable = {handle_tables}[toTid];
223+
if (!toTable) {{
224+
{handle_tables}[toTid] = [T_FLAG, 0];
225+
toTable = {handle_tables}[toTid];
226+
toTable._createdReps = new Set();
227+
}}
228+
223229
if (toTable._createdReps.has(rep)) {{
224230
return rep;
225231
}}
@@ -240,15 +246,20 @@ impl ResourceIntrinsic {
240246
output.push_str(&format!(r#"
241247
function resourceTransferBorrowValidLifting(handle, fromTid, toTid) {{
242248
const fromTable = {handle_tables}[fromTid];
243-
const toTable = {handle_tables}[toTid];
244249
const isOwn = (fromTable[(handle << 1) + 1] & T_FLAG) !== 0;
245250
const rep = isOwn ? fromTable[(handle << 1) + 1] & ~T_FLAG : {rsc_table_remove}(fromTable, handle).rep;
246251
252+
let toTable = {handle_tables}[toTid];
253+
if (!toTable) {{
254+
{handle_tables}[toTid] = [T_FLAG, 0];
255+
toTable = {handle_tables}[toTid];
256+
toTable._createdReps = new Set();
257+
}}
258+
247259
if (toTable._createdReps.has(rep)) {{
248260
return rep;
249261
}}
250262
251-
const toTable = {handle_tables}[toTid] || ({handle_tables}[toTid] = [T_FLAG, 0]);
252263
return {rsc_table_create_borrow}(toTable, rep, {scope_id});
253264
}}
254265
"#));
@@ -258,14 +269,23 @@ impl ResourceIntrinsic {
258269
let handle_tables = Intrinsic::HandleTables.name();
259270
let rsc_table_remove = Self::ResourceTableRemove.name();
260271
let rsc_table_create_own = Self::ResourceTableCreateOwn.name();
261-
output.push_str(&format!(r#"
272+
output.push_str(&format!(
273+
r#"
262274
function resourceTransferOwn(handle, fromTid, toTid) {{
263275
const {{ rep }} = {rsc_table_remove}({handle_tables}[fromTid], handle);
264-
const toTable = {handle_tables}[toTid] || ({handle_tables}[toTid] = [T_FLAG, 0]);
276+
277+
let toTable = {handle_tables}[toTid];
278+
if (!toTable) {{
279+
{handle_tables}[toTid] = [T_FLAG, 0];
280+
toTable = {handle_tables}[toTid];
281+
toTable._createdReps = new Set();
282+
}}
283+
265284
const newHandle = {rsc_table_create_own}(toTable, rep);
266285
return newHandle;
267286
}}
268-
"#));
287+
"#
288+
));
269289
}
270290

271291
Self::ResourceCallBorrows => {

crates/js-component-bindgen/src/transpile_bindgen.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,6 @@ impl<'a> Instantiator<'a, '_> {
10751075
self.src.js,
10761076
r#"
10771077
const handleTable{rtid} = [{rsc_table_flag}, 0];
1078-
handleTable{rtid}._tableID = {rtid};
10791078
handleTable{rtid}._createdReps = new Set();
10801079
"#,
10811080
);
@@ -1099,7 +1098,6 @@ impl<'a> Instantiator<'a, '_> {
10991098
self.src.js,
11001099
r#"
11011100
const handleTable{rtid} = [{rsc_table_flag}, 0];
1102-
handleTable{rtid}._tableID = {rtid};
11031101
handleTable{rtid}._createdReps = new Set();
11041102
const finalizationRegistry{rtid} = {finalization_registry_create}((handle) => {{
11051103
const {{ rep }} = {rsc_table_remove}(handleTable{rtid}, handle);{maybe_dtor}

0 commit comments

Comments
 (0)