Skip to content

Commit

Permalink
fix(c): Prefix import module name for resource-drop with [export] (#…
Browse files Browse the repository at this point in the history
…946)

* fix(c): Prefix import module name for resource-drop with `[export]`

Import module name for exported resource's drop should be prefixed with [export]

* test: disable resource drop check for c-sharp generator for now
  • Loading branch information
kateinoigakukun authored May 6, 2024
1 parent bcd136e commit 67e1774
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 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
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
6 changes: 6 additions & 0 deletions tests/runtime/resources/wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ 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)
{
// FIXME: c-sharp generator seems wrong here
// 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 67e1774

Please sign in to comment.