Skip to content

Commit 1abbfe7

Browse files
fix(C): Resource name in import should be the original name (#806)
* fix(C): Resource name in import should be the original name * fix(tinygo): Resource C type name should be snake case * Add a runtime test for kebab-case resource names
1 parent a5e5ae8 commit 1abbfe7

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

crates/c/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ void {ns}_{snake}_destructor({ty_name} *rep);
10511051
__attribute__(( __import_module__("[export]{module}"), __import_name__("[resource-new]{name}")))
10521052
extern int32_t __wasm_import_{ns}_{snake}_new(int32_t);
10531053
1054-
__attribute__((__import_module__("[export]{module}"), __import_name__("[resource-rep]{snake}")))
1054+
__attribute__((__import_module__("[export]{module}"), __import_name__("[resource-rep]{name}")))
10551055
extern int32_t __wasm_import_{ns}_{snake}_rep(int32_t);
10561056
10571057
{own} {ns}_{snake}_new({ty_name} *rep) {{

crates/go/src/bindgen.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
342342
let snake = self.interface.resolve.types[resource]
343343
.name
344344
.as_ref()
345-
.unwrap();
345+
.unwrap()
346+
.to_snake_case();
346347
// If the resource is exported, then `type_resource` have created a
347348
// internal bookkeeping map for this resource. We will need to
348349
// use the map to get the resource interface. Otherwise, this resource
@@ -371,14 +372,14 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
371372
Own(_) => {
372373
let mut own = ns.clone();
373374
own.push_str("_own_");
374-
own.push_str(snake);
375+
own.push_str(&snake);
375376
own.push_str("_t");
376377
own
377378
}
378379
Borrow(_) => {
379380
let mut borrow = ns.clone();
380381
borrow.push_str("_borrow_");
381-
borrow.push_str(snake);
382+
borrow.push_str(&snake);
382383
borrow.push_str("_t");
383384
borrow
384385
}
@@ -639,7 +640,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
639640
let snake = self.interface.resolve.types[resource]
640641
.name
641642
.as_ref()
642-
.unwrap();
643+
.unwrap()
644+
.to_snake_case();
643645
if self.interface.gen.exported_resources.contains(&resource) {
644646
let resource_name: String =
645647
self.interface.get_ty(&Type::Id(resource)).to_snake_case();

crates/go/src/interface.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,10 +1060,14 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
10601060
// generate a typedef struct for export resource
10611061
let c_typedef_target = self.gen.c_type_names[&id].clone();
10621062
let ns = self.c_namespace_of_resource(id);
1063-
let snake = self.resolve.types[id].name.as_ref().unwrap();
1063+
let snake = self.resolve.types[id]
1064+
.name
1065+
.as_ref()
1066+
.unwrap()
1067+
.to_snake_case();
10641068
let mut own = ns.clone();
10651069
own.push_str("_own_");
1066-
own.push_str(snake);
1070+
own.push_str(&snake);
10671071
own.push_str("_t");
10681072

10691073
// generate a typedef struct for export resource

tests/runtime/resources/wasm.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ struct exports_z_t {
1111
int32_t a;
1212
};
1313

14+
struct exports_kebab_case_t {
15+
int32_t a;
16+
};
17+
1418
exports_own_x_t exports_constructor_x(int32_t a) {
1519
exports_x_t* x_instance = (exports_x_t*)malloc(sizeof(exports_x_t));
1620
x_instance->a = a;
@@ -56,6 +60,23 @@ void exports_z_destructor(exports_z_t* z) {
5660
free(z);
5761
}
5862

63+
exports_own_kebab_case_t exports_constructor_kebab_case(uint32_t a) {
64+
exports_kebab_case_t* kc_instance = (exports_kebab_case_t*)malloc(sizeof(exports_kebab_case_t));
65+
kc_instance->a = a;
66+
exports_own_kebab_case_t kc_own = exports_kebab_case_new(kc_instance);
67+
return kc_own;
68+
}
69+
70+
uint32_t exports_method_kebab_case_get_a(exports_borrow_kebab_case_t self) {
71+
return self->a;
72+
}
73+
74+
uint32_t exports_static_kebab_case_take_owned(exports_own_kebab_case_t k) {
75+
return exports_kebab_case_rep(k)->a;
76+
}
77+
78+
void exports_kebab_case_destructor(exports_kebab_case_t *rep) {}
79+
5980
bool exports_test_imports(resources_string_t *err) {
6081
imports_own_y_t y = imports_constructor_y(10);
6182
imports_borrow_y_t borrowed_y = imports_borrow_y(y);

tests/runtime/resources/wasm.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ type MyZ struct {
2020
a int32
2121
}
2222

23+
type MyKebabCase struct {
24+
a uint32
25+
}
26+
2327
func (e ExportsImpl) ConstructorX(a int32) ExportsX {
2428
return &MyX{a: a}
2529
}
@@ -28,6 +32,10 @@ func (e ExportsImpl) ConstructorZ(a int32) ExportsZ {
2832
return &MyZ{a: a}
2933
}
3034

35+
func (e ExportsImpl) ConstructorKebabCase(a uint32) ExportsKebabCase {
36+
return &MyKebabCase{a: a}
37+
}
38+
3139
func (x *MyX) MethodXGetA() int32 {
3240
return x.a
3341
}
@@ -48,6 +56,14 @@ func (e ExportsImpl) Add(z ExportsZ, b ExportsZ) ExportsZ {
4856
return &MyZ{a: z.MethodZGetA() + b.MethodZGetA()}
4957
}
5058

59+
func (k *MyKebabCase) MethodKebabCaseGetA() uint32 {
60+
return k.a
61+
}
62+
63+
func (e ExportsImpl) StaticKebabCaseTakeOwned(k ExportsKebabCase) uint32 {
64+
return k.MethodKebabCaseGetA()
65+
}
66+
5167
func (e ExportsImpl) TestImports() Result[struct{}, string] {
5268
y := NewY(1)
5369
if y.GetA() != 1 {

tests/runtime/resources/wasm.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ wit_bindgen::generate!({
77
"exports": Test,
88
"exports/x": ComponentX,
99
"exports/z": ComponentZ,
10+
"exports/kebab-case": ComponentKebabCase,
1011
}
1112
});
1213

1314
use exports::exports::OwnX;
15+
use exports::exports::OwnKebabCase;
1416

1517
pub struct Test {}
1618

@@ -22,6 +24,10 @@ pub struct ComponentZ {
2224
val: i32,
2325
}
2426

27+
pub struct ComponentKebabCase {
28+
val: u32,
29+
}
30+
2531
impl exports::exports::Guest for Test {
2632
fn add(a: &ComponentZ, b: &ComponentZ) -> wit_bindgen::Resource<ComponentZ> {
2733
wit_bindgen::Resource::new(ComponentZ { val: a.val + b.val })
@@ -78,3 +84,16 @@ impl exports::exports::GuestZ for ComponentZ {
7884
self.val
7985
}
8086
}
87+
88+
impl exports::exports::GuestKebabCase for ComponentKebabCase {
89+
fn new(a: u32) -> Self {
90+
Self { val: a }
91+
}
92+
fn get_a(&self) -> u32 {
93+
self.val
94+
}
95+
96+
fn take_owned(k: OwnKebabCase) -> u32 {
97+
k.get_a()
98+
}
99+
}

tests/runtime/resources/world.wit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ world resources {
2525

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

28+
resource kebab-case {
29+
constructor(a: u32);
30+
get-a: func() -> u32;
31+
take-owned: static func(k: own<kebab-case>) -> u32;
32+
}
33+
2834
test-imports: func() -> result<_, string>;
2935
}
3036
}

0 commit comments

Comments
 (0)