Skip to content

Commit

Permalink
fix: ensure resource imports and exports use kebab case (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Oct 29, 2024
1 parent 5197559 commit 97fe6aa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
23 changes: 13 additions & 10 deletions crates/spidermonkey-embedding-splicer/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,30 +252,32 @@ pub fn componentize_bindgen(
let iface_prefix = interface_name(resolve, *iface_id)
.map(|s| format!("{s}$"))
.unwrap_or_else(String::new);
let resource_name = ty.name.as_ref().unwrap().to_lower_camel_case();
let resource_name_camel = ty.name.as_ref().unwrap().to_lower_camel_case();
let resource_name_kebab = ty.name.as_ref().unwrap().to_kebab_case();
let module_name = format!("[export]{key_name}");
resource_bindings.push(format!("{iface_prefix}new${resource_name}"));
resource_bindings.push(format!("{iface_prefix}new${resource_name_camel}"));
resource_imports.push((
module_name.clone(),
format!("[resource-new]{resource_name}"),
format!("[resource-new]{resource_name_kebab}"),
1,
));
resource_bindings.push(format!("{iface_prefix}rep${resource_name}"));
resource_bindings.push(format!("{iface_prefix}rep${resource_name_camel}"));
resource_imports.push((
module_name.clone(),
format!("[resource-rep]{resource_name}"),
format!("[resource-rep]{resource_name_kebab}"),
1,
));
resource_bindings.push(format!("export${iface_prefix}drop${resource_name}"));
resource_bindings
.push(format!("export${iface_prefix}drop${resource_name_camel}"));
resource_imports.push((
module_name.clone(),
format!("[resource-drop]{resource_name}"),
format!("[resource-drop]{resource_name_kebab}"),
0,
));
finalization_registries.push(format!(
"const finalizationRegistry_export${iface_prefix}{resource_name} = \
"const finalizationRegistry_export${iface_prefix}{resource_name_camel} = \
new FinalizationRegistry((handle) => {{
$resource_export${iface_prefix}drop${resource_name}(handle);
$resource_export${iface_prefix}drop${resource_name_camel}(handle);
}});
"
));
Expand Down Expand Up @@ -312,6 +314,7 @@ pub fn componentize_bindgen(
let resource_name = ty.name.as_deref().unwrap();
let prefix = prefix.as_deref().unwrap_or("");
let resource_name_camel = resource_name.to_lower_camel_case();
let resource_name_kebab = resource_name.to_kebab_case();

finalization_registries.push(format!(
"const finalizationRegistry_import${prefix}{resource_name_camel} = \
Expand All @@ -323,7 +326,7 @@ pub fn componentize_bindgen(
resource_bindings.push(format!("import${prefix}drop${resource_name_camel}"));
resource_imports.push((
imported_resource_modules.get(&id).unwrap().clone(),
format!("[resource-drop]{resource_name}"),
format!("[resource-drop]{resource_name_kebab}"),
0,
));
}
Expand Down
4 changes: 2 additions & 2 deletions test/cases/resource-floats/resource-floats-imports.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Float {
export class MyFloat {
constructor(value) {
this.value = value + 2
}
Expand All @@ -8,6 +8,6 @@ export class Float {
}

static add(a, b) {
return new Float(a.value + b + 6)
return new MyFloat(a.value + b + 6)
}
}
2 changes: 1 addition & 1 deletion test/cases/resource-floats/resource-floats.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Float {
export class MyFloat {
constructor(value) {
this.value = value + 1
}
Expand Down
10 changes: 5 additions & 5 deletions test/cases/resource-floats/source.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Float as ImportFloat } from "resource-floats-imports";
import { Float as ImportFloat2 } from "test:test/resource-floats";
import { MyFloat as ImportFloat } from "resource-floats-imports";
import { MyFloat as ImportFloat2 } from "test:test/resource-floats";

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

class Float {
class MyFloat {
constructor(value) {
this.value = new ImportFloat(value + 1);
}
Expand All @@ -13,11 +13,11 @@ class Float {
}

static add(a, b) {
return new Float(ImportFloat.add(a.value, b).get() + 5);
return new MyFloat(ImportFloat.add(a.value, b).get() + 5);
}
}

export const resourceFloatsExports = { Float }
export const resourceFloatsExports = { MyFloat }

export function add(a, b) {
const out = new ImportFloat2(a.get() + b.get() + 5);
Expand Down
8 changes: 4 additions & 4 deletions test/cases/resource-floats/test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { strictEqual } from 'node:assert';
import { Float as HostFloat } from "./resource-floats.js";
import { MyFloat as HostFloat } from "./resource-floats.js";

export function test(instance) {
const { Float } = instance.resourceFloatsExports;
const { MyFloat } = instance.resourceFloatsExports;

let float1 = new HostFloat(42);
let float2 = new HostFloat(55);

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

let float3 = new Float(22);
let float3 = new MyFloat(22);

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

let result = Float.add(float3, 7);
let result = MyFloat.add(float3, 7);

strictEqual(result.get(), 22 + 1 + 2 + 7 + 6 + 2 + 4 + 5 + 1 + 2 + 4 + 3);
}
14 changes: 7 additions & 7 deletions test/cases/resource-floats/world.wit
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package test:test;

interface resource-floats {
resource float {
resource my-float {
constructor(v: f64);
get: func() -> f64;
}
}

world test {
use resource-floats.{float};
use resource-floats.{my-float};

export resource-floats-exports: interface {
resource float {
resource my-float {
constructor(v: f64);
get: func() -> f64;
add: static func(a: float, b: f64) -> float;
add: static func(a: my-float, b: f64) -> my-float;
}
}

import resource-floats-imports: interface {
resource float {
resource my-float {
constructor(v: f64);
get: func() -> f64;
add: static func(a: float, b: f64) -> float;
add: static func(a: my-float, b: f64) -> my-float;
}
}

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

0 comments on commit 97fe6aa

Please sign in to comment.