-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
C-Style Enums #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
C-Style Enums #36
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
01c31cb
Create Rust wrapping code for enums
rylev 45543c5
Pass numbers in js as enums to Rust successfully
rylev f11121b
Generate enum js code
rylev 7f8316f
Clean up warnings
rylev b78343a
Fix enum formatting issues
rylev 3a270b6
Add test for enums
rylev 71880b8
Enums are numbers
rylev f783876
Support C-Style enums with custom int values
rylev 3ae6614
Add test for custom values in enums
rylev f1b300c
get rid of unnecessary mutable var
rylev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
extern crate test_support; | ||
|
||
#[test] | ||
fn c_style_enum() { | ||
test_support::project() | ||
.file("src/lib.rs", r#" | ||
#![feature(proc_macro)] | ||
|
||
extern crate wasm_bindgen; | ||
|
||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
pub enum Color { | ||
Green, | ||
Yellow, | ||
Red, | ||
} | ||
|
||
#[no_mangle] | ||
#[wasm_bindgen] | ||
pub extern fn cycle(color: Color) -> Color { | ||
match color { | ||
Color::Green => Color::Yellow, | ||
Color::Yellow => Color::Red, | ||
Color::Red => Color::Green, | ||
} | ||
} | ||
"#) | ||
.file("test.ts", r#" | ||
import * as assert from "assert"; | ||
import * as wasm from "./out"; | ||
|
||
export function test() { | ||
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.cycle(wasm.Color.Green), wasm.Color.Yellow); | ||
} | ||
"#) | ||
.test(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it not possible to expose actual enum name instead of generic
number
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is. It requires a bit of a refactoring of how things are handled though. Specifically we lose all information about enums inside of functions right now, and we'll have to refactor how we handle "custom types" to make it work. I would prefer to save it for the next PR where I'll handle non-C-style enums. The changes necessary for that will sure this up.