Skip to content

Commit

Permalink
Fix the issue that one could define two enums with the second one ove…
Browse files Browse the repository at this point in the history
…rwriting the first one.
  • Loading branch information
thomasetter committed Oct 26, 2023
1 parent 561d3c0 commit 0259a5c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
enhancing type safety and compatibility.
[#3647](https://github.com/rustwasm/wasm-bindgen/pull/3647)

* Stop tolerating name collisions on enums, this would previously just emit one of them.
[#3669](https://github.com/rustwasm/wasm-bindgen/pull/3669)

### Fixed

* Fixed `wasm_bindgen` macro to handle raw identifiers in field names.
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2455,7 +2455,7 @@ impl<'a> Context<'a> {
pairs.sort_by_key(|(k, _)| *k);
check_duplicated_getter_and_setter_names(&pairs)?;

for e in self.aux.enums.iter() {
for (_, e) in self.aux.enums.iter() {
self.generate_enum(e)?;
}

Expand Down
11 changes: 9 additions & 2 deletions crates/cli-support/src/wit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,15 @@ impl<'a> Context<'a> {
.collect(),
generate_typescript: enum_.generate_typescript,
};
self.aux.enums.push(aux);
Ok(())
let mut result = Ok(());
self.aux
.enums
.entry(aux.name.clone())
.and_modify(|existing| {
result = Err(anyhow!("duplicate enums:\n{:?}\n{:?}", existing, aux));
})
.or_insert(aux);
result
}

fn struct_(&mut self, struct_: decode::Struct<'_>) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/wit/nonstandard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct WasmBindgenAux {

/// Auxiliary information to go into JS/TypeScript bindings describing the
/// exported enums from Rust.
pub enums: Vec<AuxEnum>,
pub enums: HashMap<String, AuxEnum>,

/// Auxiliary information to go into JS/TypeScript bindings describing the
/// exported structs from Rust and their fields they've got exported.
Expand Down
16 changes: 8 additions & 8 deletions tests/wasm/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ exports.test_works = function() {
assert.strictEqual(r2.add(2), 13);
r2.free();

assert.strictEqual(wasm.Color.Green, 0);
assert.strictEqual(wasm.Color.Yellow, 1);
assert.strictEqual(wasm.Color.Red, 2);
assert.strictEqual(wasm.Color[0], 'Green');
assert.strictEqual(wasm.Color[1], 'Yellow');
assert.strictEqual(wasm.Color[2], 'Red');
assert.strictEqual(Object.keys(wasm.Color).length, 6);
assert.strictEqual(wasm.cycle(wasm.Color.Green), wasm.Color.Yellow);
assert.strictEqual(wasm.NodeColor.Green, 0);
assert.strictEqual(wasm.NodeColor.Yellow, 1);
assert.strictEqual(wasm.NodeColor.Red, 2);
assert.strictEqual(wasm.NodeColor[0], 'Green');
assert.strictEqual(wasm.NodeColor[1], 'Yellow');
assert.strictEqual(wasm.NodeColor[2], 'Red');
assert.strictEqual(Object.keys(wasm.NodeColor).length, 6);
assert.strictEqual(wasm.cycle(wasm.NodeColor.Green), wasm.NodeColor.Yellow);

wasm.node_math(1.0, 2.0);
};
11 changes: 6 additions & 5 deletions tests/wasm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ impl Foo {
}
}

// Use a different name to avoid a collision with the `Color` enum in enums.rs when --no-modules is used.
#[wasm_bindgen]
pub enum Color {
pub enum NodeColor {
Green,
Yellow,
Red,
}
#[wasm_bindgen]
pub fn cycle(color: Color) -> Color {
pub fn cycle(color: NodeColor) -> NodeColor {
match color {
Color::Green => Color::Yellow,
Color::Yellow => Color::Red,
Color::Red => Color::Green,
NodeColor::Green => NodeColor::Yellow,
NodeColor::Yellow => NodeColor::Red,
NodeColor::Red => NodeColor::Green,
}
}

Expand Down

0 comments on commit 0259a5c

Please sign in to comment.