Skip to content

Commit d70ae96

Browse files
authored
add reverse mappings from value to name on enums exported from rust/wasm (#2240)
1 parent 6742d96 commit d70ae96

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,6 +3068,7 @@ impl<'a> Context<'a> {
30683068
variants.push_str(&variant_docs);
30693069
}
30703070
variants.push_str(&format!("{}:{},", name, value));
3071+
variants.push_str(&format!("\"{}\":\"{}\",", value, name));
30713072
if enum_.generate_typescript {
30723073
self.typescript.push_str("\n");
30733074
if !variant_docs.is_empty() {

tests/wasm/enums.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ exports.js_c_style_enum = () => {
55
assert.strictEqual(wasm.Color.Green, 0);
66
assert.strictEqual(wasm.Color.Yellow, 1);
77
assert.strictEqual(wasm.Color.Red, 2);
8-
assert.strictEqual(Object.keys(wasm.Color).length, 3);
8+
assert.strictEqual(wasm.Color[0], 'Green');
9+
assert.strictEqual(wasm.Color[1], 'Yellow');
10+
assert.strictEqual(wasm.Color[2], 'Red');
11+
assert.strictEqual(Object.keys(wasm.Color).length, 6);
912

1013
assert.strictEqual(wasm.enum_cycle(wasm.Color.Green), wasm.Color.Yellow);
1114
};
@@ -14,7 +17,10 @@ exports.js_c_style_enum_with_custom_values = () => {
1417
assert.strictEqual(wasm.ColorWithCustomValues.Green, 21);
1518
assert.strictEqual(wasm.ColorWithCustomValues.Yellow, 34);
1619
assert.strictEqual(wasm.ColorWithCustomValues.Red, 2);
17-
assert.strictEqual(Object.keys(wasm.ColorWithCustomValues).length, 3);
20+
assert.strictEqual(wasm.ColorWithCustomValues[21], 'Green');
21+
assert.strictEqual(wasm.ColorWithCustomValues[34], 'Yellow');
22+
assert.strictEqual(wasm.ColorWithCustomValues[2], 'Red');
23+
assert.strictEqual(Object.keys(wasm.ColorWithCustomValues).length, 6);
1824

1925
assert.strictEqual(wasm.enum_with_custom_values_cycle(wasm.ColorWithCustomValues.Green), wasm.ColorWithCustomValues.Yellow);
2026
};

tests/wasm/node.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ exports.test_works = function() {
2727
assert.strictEqual(wasm.Color.Green, 0);
2828
assert.strictEqual(wasm.Color.Yellow, 1);
2929
assert.strictEqual(wasm.Color.Red, 2);
30-
assert.strictEqual(Object.keys(wasm.Color).length, 3);
30+
assert.strictEqual(wasm.Color[0], 'Green');
31+
assert.strictEqual(wasm.Color[1], 'Yellow');
32+
assert.strictEqual(wasm.Color[2], 'Red');
33+
assert.strictEqual(Object.keys(wasm.Color).length, 6);
3134
assert.strictEqual(wasm.cycle(wasm.Color.Green), wasm.Color.Yellow);
3235

3336
wasm.node_math(1.0, 2.0);

0 commit comments

Comments
 (0)