Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed May 7, 2024
2 parents 1d2c675 + 2622cf2 commit c22bc06
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 3 deletions.
5 changes: 3 additions & 2 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,11 @@ extern void {ns}_{snake}_drop_own({own} handle);
let import_module = if self.in_import {
self.wasm_import_module.unwrap().to_string()
} else {
match self.interface {
let module = match self.interface {
Some((_, key)) => self.resolve.name_world_key(key),
None => unimplemented!("resource exports from worlds"),
}
};
format!("[export]{module}")
};

let drop_fn = format!("__wasm_import_{ns}_{snake}_drop");
Expand Down
4 changes: 3 additions & 1 deletion crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,9 @@ impl InterfaceGenerator<'_> {
[UnmanagedCallersOnly(EntryPoint = "{prefix}[dtor]{name}")]
public static unsafe void wasmExportResourceDtor{upper_camel}(int rep) {{
var val = ({qualified}) {qualified}.repTable.Remove(rep);
val.Handle = 0;
// Note we call `Dispose` here even though the handle has already been disposed in case
// the implementation has overridden `Dispose(bool)`.
val.Dispose();
}}
"#
Expand Down Expand Up @@ -1441,7 +1444,6 @@ impl InterfaceGenerator<'_> {
if (Handle != 0) {{
var handle = Handle;
Handle = 0;
repTable.Remove(WasmInterop.wasmImportResourceRep(handle));
WasmInterop.wasmImportResourceDrop(handle);
}}
}}
Expand Down
2 changes: 2 additions & 0 deletions tests/runtime/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ fn run_test(exports: Guest, store: &mut Store<crate::Wasi<MyImports>>) -> Result
ResourceAny::resource_drop(z_instance_1, &mut *store)?;
ResourceAny::resource_drop(z_instance_2, &mut *store)?;

exports.call_consume(&mut *store, x_add)?;

let dropped_zs_end = z.call_num_dropped(&mut *store)?;
if dropped_zs_start != 0 {
assert_eq!(dropped_zs_end, dropped_zs_start + 2);
Expand Down
4 changes: 4 additions & 0 deletions tests/runtime/resources/wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ exports_own_z_t exports_add(exports_z_t* a, exports_z_t* b) {
return exports_constructor_z(c);
}

void exports_consume(exports_own_x_t x) {
exports_x_drop_own(x);
}

void exports_x_destructor(exports_x_t* x) {
free(x);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/runtime/resources/wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public static IExports.Z Add(IExports.Z a, IExports.Z b)
{
return new Z(((Z) a).val + ((Z) b).val);
}

public static void Consume(IExports.X x)
{
x.Dispose();
}

public static Result<None, string> TestImports()
{
Expand Down
4 changes: 4 additions & 0 deletions tests/runtime/resources/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (e ExportsImpl) Add(z ExportsZ, b ExportsZ) ExportsZ {
return &MyZ{a: z.MethodZGetA() + b.MethodZGetA()}
}

func (e ExportsImpl) Consume(x ExportsX) {
DropExportsX(x)
}

func (k *MyKebabCase) MethodKebabCaseGetA() uint32 {
return k.a
}
Expand Down
5 changes: 5 additions & 0 deletions tests/runtime/resources/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ impl exports::exports::Guest for Test {
let b = b.get::<ComponentZ>();
Z::new(ComponentZ { val: a.val + b.val })
}

fn consume(x: exports::exports::X) {
drop(x);
}

fn test_imports() -> Result<(), String> {
use imports::*;
let y = Y::new(10);
Expand Down
2 changes: 2 additions & 0 deletions tests/runtime/resources/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ world resources {

add: func(a: borrow<z>, b: borrow<z>) -> own<z>;

consume: func(x: x);

resource kebab-case {
constructor(a: u32);
get-a: func() -> u32;
Expand Down

0 comments on commit c22bc06

Please sign in to comment.