Skip to content

Commit 1cec9c8

Browse files
committed
fix: emit bindings for imported resources without methods
Seed resource function groups from imported resource types so opaque handles receive the existing class and disposal bindings. Add an ownership, borrowing, and disposal regression.
1 parent bf50db6 commit 1cec9c8

5 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎crates/spidermonkey-embedding-splicer/src/bindgen.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,17 @@ impl JsBindgen<'_> {
587587
..
588588
} => {
589589
let iface = &self.resolve.interfaces[*i];
590+
let mut resource_function_groups = BTreeMap::<_, Vec<_>>::new();
590591
for id in iface.types.values() {
591592
if let TypeDefKind::Resource = &self.resolve.types[*id].kind {
592593
self.resource_directions
593594
.insert(*id, AbiVariant::GuestImport);
595+
resource_function_groups.entry(Some(*id)).or_default();
594596
}
595597
}
596598

597599
let by_resource = iface.functions.iter().fold(
598-
BTreeMap::<_, Vec<_>>::new(),
600+
resource_function_groups,
599601
|mut map, (name, func)| {
600602
map.entry(match &func.kind {
601603
FunctionKind::Freestanding | FunctionKind::AsyncFreestanding => {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export let drops = 0;
2+
3+
export class Item {
4+
constructor(value) {
5+
this.value = value;
6+
}
7+
8+
[Symbol.dispose]() {
9+
drops += 1;
10+
}
11+
}
12+
13+
export const make = value => new Item(value);
14+
export const value = item => item.value;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { make, value } from 'test:test/opaque-resource-import';
2+
3+
export function test() {
4+
const item = make(42);
5+
try {
6+
return value(item);
7+
} finally {
8+
item[Symbol.dispose]();
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { strictEqual } from 'node:assert';
2+
import { drops } from './opaque-resource-import.js';
3+
4+
export function test(instance) {
5+
strictEqual(instance.test(), 42);
6+
strictEqual(drops, 1);
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test:test;
2+
3+
interface opaque-resource-import {
4+
resource item;
5+
make: func(value: u32) -> item;
6+
value: func(item: borrow<item>) -> u32;
7+
}
8+
9+
world test {
10+
import opaque-resource-import;
11+
export test: func() -> u32;
12+
}

0 commit comments

Comments
 (0)