Skip to content

Commit

Permalink
add reverse mappings from value to name on enums exported from rust/w…
Browse files Browse the repository at this point in the history
…asm (#2240)
  • Loading branch information
Treeki authored Jul 17, 2020
1 parent 6742d96 commit d70ae96
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3068,6 +3068,7 @@ impl<'a> Context<'a> {
variants.push_str(&variant_docs);
}
variants.push_str(&format!("{}:{},", name, value));
variants.push_str(&format!("\"{}\":\"{}\",", value, name));
if enum_.generate_typescript {
self.typescript.push_str("\n");
if !variant_docs.is_empty() {
Expand Down
10 changes: 8 additions & 2 deletions tests/wasm/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ exports.js_c_style_enum = () => {
assert.strictEqual(wasm.Color.Green, 0);
assert.strictEqual(wasm.Color.Yellow, 1);
assert.strictEqual(wasm.Color.Red, 2);
assert.strictEqual(Object.keys(wasm.Color).length, 3);
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.enum_cycle(wasm.Color.Green), wasm.Color.Yellow);
};
Expand All @@ -14,7 +17,10 @@ exports.js_c_style_enum_with_custom_values = () => {
assert.strictEqual(wasm.ColorWithCustomValues.Green, 21);
assert.strictEqual(wasm.ColorWithCustomValues.Yellow, 34);
assert.strictEqual(wasm.ColorWithCustomValues.Red, 2);
assert.strictEqual(Object.keys(wasm.ColorWithCustomValues).length, 3);
assert.strictEqual(wasm.ColorWithCustomValues[21], 'Green');
assert.strictEqual(wasm.ColorWithCustomValues[34], 'Yellow');
assert.strictEqual(wasm.ColorWithCustomValues[2], 'Red');
assert.strictEqual(Object.keys(wasm.ColorWithCustomValues).length, 6);

assert.strictEqual(wasm.enum_with_custom_values_cycle(wasm.ColorWithCustomValues.Green), wasm.ColorWithCustomValues.Yellow);
};
Expand Down
5 changes: 4 additions & 1 deletion tests/wasm/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ exports.test_works = function() {
assert.strictEqual(wasm.Color.Green, 0);
assert.strictEqual(wasm.Color.Yellow, 1);
assert.strictEqual(wasm.Color.Red, 2);
assert.strictEqual(Object.keys(wasm.Color).length, 3);
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);

wasm.node_math(1.0, 2.0);
Expand Down

0 comments on commit d70ae96

Please sign in to comment.