diff --git a/crates/wasm-bindgen-cli-support/src/js.rs b/crates/wasm-bindgen-cli-support/src/js.rs index 56a82442..d191d0c7 100644 --- a/crates/wasm-bindgen-cli-support/src/js.rs +++ b/crates/wasm-bindgen-cli-support/src/js.rs @@ -1054,8 +1054,15 @@ impl<'a, 'b> SubContext<'a, 'b> { passed_args.push_str(arg); }; match *arg { - shared::TYPE_ENUM | shared::TYPE_NUMBER => { - // TODO: TS for Enum + shared::TYPE_ENUM => { + dst_ts.push_str(&format!(": {}", "any")); + if self.cx.config.debug { + self.cx.expose_assert_num(); + arg_conversions.push_str(&format!("_assertNum({});\n", name)); + } + pass(&name) + } + shared::TYPE_NUMBER => { dst_ts.push_str(": number"); if self.cx.config.debug { self.cx.expose_assert_num(); @@ -1160,6 +1167,10 @@ impl<'a, 'b> SubContext<'a, 'b> { dst_ts.push_str(": void"); format!("return ret;") } + Some(shared::TYPE_ENUM) => { + dst_ts.push_str(": any"); + format!("return ret;") + } Some(shared::TYPE_NUMBER) => { dst_ts.push_str(": number"); format!("return ret;") @@ -1439,6 +1450,14 @@ impl<'a, 'b> SubContext<'a, 'b> { self.cx.globals.push_str(&format!("export const {} = {{", enum_.name)); self.cx.globals.push_str(&variants); self.cx.globals.push_str("}\n"); + self.cx.typescript.push_str(&format!("export enum {} {{", enum_.name)); + + variants.clear(); + for variant in enum_.variants.iter() { + variants.push_str(&format!("{},", variant.name)); + } + self.cx.typescript.push_str(&variants); + self.cx.typescript.push_str("}\n"); } } diff --git a/tests/enums.rs b/tests/enums.rs new file mode 100644 index 00000000..a1d02188 --- /dev/null +++ b/tests/enums.rs @@ -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(); +}