Skip to content

Commit 97fe6aa

Browse files
author
Guy Bedford
authored
fix: ensure resource imports and exports use kebab case (#151)
1 parent 5197559 commit 97fe6aa

File tree

6 files changed

+32
-29
lines changed

6 files changed

+32
-29
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,30 +252,32 @@ pub fn componentize_bindgen(
252252
let iface_prefix = interface_name(resolve, *iface_id)
253253
.map(|s| format!("{s}$"))
254254
.unwrap_or_else(String::new);
255-
let resource_name = ty.name.as_ref().unwrap().to_lower_camel_case();
255+
let resource_name_camel = ty.name.as_ref().unwrap().to_lower_camel_case();
256+
let resource_name_kebab = ty.name.as_ref().unwrap().to_kebab_case();
256257
let module_name = format!("[export]{key_name}");
257-
resource_bindings.push(format!("{iface_prefix}new${resource_name}"));
258+
resource_bindings.push(format!("{iface_prefix}new${resource_name_camel}"));
258259
resource_imports.push((
259260
module_name.clone(),
260-
format!("[resource-new]{resource_name}"),
261+
format!("[resource-new]{resource_name_kebab}"),
261262
1,
262263
));
263-
resource_bindings.push(format!("{iface_prefix}rep${resource_name}"));
264+
resource_bindings.push(format!("{iface_prefix}rep${resource_name_camel}"));
264265
resource_imports.push((
265266
module_name.clone(),
266-
format!("[resource-rep]{resource_name}"),
267+
format!("[resource-rep]{resource_name_kebab}"),
267268
1,
268269
));
269-
resource_bindings.push(format!("export${iface_prefix}drop${resource_name}"));
270+
resource_bindings
271+
.push(format!("export${iface_prefix}drop${resource_name_camel}"));
270272
resource_imports.push((
271273
module_name.clone(),
272-
format!("[resource-drop]{resource_name}"),
274+
format!("[resource-drop]{resource_name_kebab}"),
273275
0,
274276
));
275277
finalization_registries.push(format!(
276-
"const finalizationRegistry_export${iface_prefix}{resource_name} = \
278+
"const finalizationRegistry_export${iface_prefix}{resource_name_camel} = \
277279
new FinalizationRegistry((handle) => {{
278-
$resource_export${iface_prefix}drop${resource_name}(handle);
280+
$resource_export${iface_prefix}drop${resource_name_camel}(handle);
279281
}});
280282
"
281283
));
@@ -312,6 +314,7 @@ pub fn componentize_bindgen(
312314
let resource_name = ty.name.as_deref().unwrap();
313315
let prefix = prefix.as_deref().unwrap_or("");
314316
let resource_name_camel = resource_name.to_lower_camel_case();
317+
let resource_name_kebab = resource_name.to_kebab_case();
315318

316319
finalization_registries.push(format!(
317320
"const finalizationRegistry_import${prefix}{resource_name_camel} = \
@@ -323,7 +326,7 @@ pub fn componentize_bindgen(
323326
resource_bindings.push(format!("import${prefix}drop${resource_name_camel}"));
324327
resource_imports.push((
325328
imported_resource_modules.get(&id).unwrap().clone(),
326-
format!("[resource-drop]{resource_name}"),
329+
format!("[resource-drop]{resource_name_kebab}"),
327330
0,
328331
));
329332
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class Float {
1+
export class MyFloat {
22
constructor(value) {
33
this.value = value + 2
44
}
@@ -8,6 +8,6 @@ export class Float {
88
}
99

1010
static add(a, b) {
11-
return new Float(a.value + b + 6)
11+
return new MyFloat(a.value + b + 6)
1212
}
1313
}

test/cases/resource-floats/resource-floats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class Float {
1+
export class MyFloat {
22
constructor(value) {
33
this.value = value + 1
44
}

test/cases/resource-floats/source.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Float as ImportFloat } from "resource-floats-imports";
2-
import { Float as ImportFloat2 } from "test:test/resource-floats";
1+
import { MyFloat as ImportFloat } from "resource-floats-imports";
2+
import { MyFloat as ImportFloat2 } from "test:test/resource-floats";
33

44
const symbolDispose = Symbol.for('dispose');
55

6-
class Float {
6+
class MyFloat {
77
constructor(value) {
88
this.value = new ImportFloat(value + 1);
99
}
@@ -13,11 +13,11 @@ class Float {
1313
}
1414

1515
static add(a, b) {
16-
return new Float(ImportFloat.add(a.value, b).get() + 5);
16+
return new MyFloat(ImportFloat.add(a.value, b).get() + 5);
1717
}
1818
}
1919

20-
export const resourceFloatsExports = { Float }
20+
export const resourceFloatsExports = { MyFloat }
2121

2222
export function add(a, b) {
2323
const out = new ImportFloat2(a.get() + b.get() + 5);

test/cases/resource-floats/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { strictEqual } from 'node:assert';
2-
import { Float as HostFloat } from "./resource-floats.js";
2+
import { MyFloat as HostFloat } from "./resource-floats.js";
33

44
export function test(instance) {
5-
const { Float } = instance.resourceFloatsExports;
5+
const { MyFloat } = instance.resourceFloatsExports;
66

77
let float1 = new HostFloat(42);
88
let float2 = new HostFloat(55);
99

1010
strictEqual(instance.add(float1, float2).value, 42 + 1 + 3 + 55 + 1 + 3 + 5 + 1);
1111

12-
let float3 = new Float(22);
12+
let float3 = new MyFloat(22);
1313

1414
strictEqual(float3.get(), 22 + 1 + 2 + 4 + 3);
1515

16-
let result = Float.add(float3, 7);
16+
let result = MyFloat.add(float3, 7);
1717

1818
strictEqual(result.get(), 22 + 1 + 2 + 7 + 6 + 2 + 4 + 5 + 1 + 2 + 4 + 3);
1919
}

test/cases/resource-floats/world.wit

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
package test:test;
22

33
interface resource-floats {
4-
resource float {
4+
resource my-float {
55
constructor(v: f64);
66
get: func() -> f64;
77
}
88
}
99

1010
world test {
11-
use resource-floats.{float};
11+
use resource-floats.{my-float};
1212

1313
export resource-floats-exports: interface {
14-
resource float {
14+
resource my-float {
1515
constructor(v: f64);
1616
get: func() -> f64;
17-
add: static func(a: float, b: f64) -> float;
17+
add: static func(a: my-float, b: f64) -> my-float;
1818
}
1919
}
2020

2121
import resource-floats-imports: interface {
22-
resource float {
22+
resource my-float {
2323
constructor(v: f64);
2424
get: func() -> f64;
25-
add: static func(a: float, b: f64) -> float;
25+
add: static func(a: my-float, b: f64) -> my-float;
2626
}
2727
}
2828

29-
export add: func(a: borrow<float>, b: borrow<float>) -> own<float>;
29+
export add: func(a: borrow<my-float>, b: borrow<my-float>) -> own<my-float>;
3030
}

0 commit comments

Comments
 (0)