mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-25 00:52:49 +03:00
02b7021053
This commit leverages two new attributes in the Rust compiler, `#[wasm_custom_section]` and `#[wasm_import_module]`. These two attributes allow removing a lot of hacks found in wasm-bindgen and also allows removing the requirement of `wasm-opt` to remove the unused data sections. This does require two new nightly features but we already required the `proc_macro` nightly feature and these will hopefully be stabilized before that feature!
86 lines
2.4 KiB
Rust
86 lines
2.4 KiB
Rust
extern crate test_support;
|
|
|
|
#[test]
|
|
fn c_style_enum() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro, wasm_custom_section)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen]
|
|
pub enum Color {
|
|
Green,
|
|
Yellow,
|
|
Red,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub 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();
|
|
}
|
|
|
|
#[test]
|
|
fn c_style_enum_with_custom_values() {
|
|
test_support::project()
|
|
.file("src/lib.rs", r#"
|
|
#![feature(proc_macro, wasm_custom_section)]
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen]
|
|
pub enum Color {
|
|
Green = 21,
|
|
Yellow = 34,
|
|
Red,
|
|
}
|
|
|
|
#[wasm_bindgen]
|
|
pub 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, 21);
|
|
assert.strictEqual(wasm.Color.Yellow, 34);
|
|
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();
|
|
}
|