-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow creating
Vec
s of exported enums
Also implements `TryFrom<JsValue>` and `Into<JsValue>` for them. Enums were left behind in #3554. This adds the missing bits.
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,23 @@ | ||
const wasm = require('wasm-bindgen-test.js'); | ||
const assert = require('assert'); | ||
|
||
exports.pass_enum_vec = () => { | ||
const el1 = wasm.EnumArrayElement.Unit; | ||
const el2 = wasm.EnumArrayElement.Unit; | ||
const ret = wasm.consume_enum_vec([el1, el2]); | ||
assert.strictEqual(ret.length, 3); | ||
|
||
const ret2 = wasm.consume_optional_enum_vec(ret); | ||
assert.strictEqual(ret2.length, 4); | ||
|
||
assert.strictEqual(wasm.consume_optional_enum_vec(undefined), undefined); | ||
}; | ||
|
||
exports.pass_invalid_enum_vec = () => { | ||
try { | ||
wasm.consume_enum_vec(['not an enum value']); | ||
} catch (e) { | ||
assert.match(e.message, /invalid enum value passed/) | ||
assert.match(e.stack, /consume_enum_vec/) | ||
} | ||
}; |
This file contains 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,36 @@ | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_test::*; | ||
|
||
#[wasm_bindgen(module = "tests/wasm/enum_vecs.js")] | ||
extern "C" { | ||
fn pass_enum_vec(); | ||
fn pass_invalid_enum_vec(); | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub enum EnumArrayElement { | ||
Unit, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn consume_enum_vec(mut vec: Vec<EnumArrayElement>) -> Vec<EnumArrayElement> { | ||
vec.push(EnumArrayElement::Unit); | ||
vec | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn consume_optional_enum_vec( | ||
vec: Option<Vec<EnumArrayElement>>, | ||
) -> Option<Vec<EnumArrayElement>> { | ||
vec.map(consume_enum_vec) | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn test_valid() { | ||
pass_enum_vec(); | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn test_invalid() { | ||
pass_invalid_enum_vec(); | ||
} |
This file contains 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