Skip to content

Commit e3643cd

Browse files
committed
Allow creating Vecs of exported enums
Also implements `TryFrom<JsValue>` and `Into<JsValue>` for them. Enums were left behind in #3554. This adds the missing bits.
1 parent 2b7ab44 commit e3643cd

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

crates/backend/src/codegen.rs

+59
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,65 @@ impl ToTokens for ast::Enum {
14271427
inform(#hole);
14281428
}
14291429
}
1430+
1431+
#[automatically_derived]
1432+
impl #wasm_bindgen::__rt::core::convert::From<#enum_name> for
1433+
#wasm_bindgen::JsValue
1434+
{
1435+
fn from(value: #enum_name) -> Self {
1436+
#wasm_bindgen::JsValue::from_f64((value as u32).into())
1437+
}
1438+
}
1439+
1440+
#[allow(clippy::all)]
1441+
impl #wasm_bindgen::__rt::core::convert::TryFrom<#wasm_bindgen::JsValue> for #enum_name {
1442+
type Error = #wasm_bindgen::JsValue;
1443+
1444+
fn try_from(value: #wasm_bindgen::JsValue)
1445+
-> #wasm_bindgen::__rt::std::result::Result<Self, Self::Error> {
1446+
let val = f64::try_from(value)? as u32;
1447+
1448+
unsafe {
1449+
#wasm_bindgen::__rt::std::result::Result::Ok(
1450+
<Self as #wasm_bindgen::convert::FromWasmAbi>::from_abi(val)
1451+
)
1452+
}
1453+
}
1454+
}
1455+
1456+
impl #wasm_bindgen::describe::WasmDescribeVector for #enum_name {
1457+
fn describe_vector() {
1458+
use #wasm_bindgen::describe::*;
1459+
inform(VECTOR);
1460+
<#wasm_bindgen::JsValue as #wasm_bindgen::describe::WasmDescribe>::describe();
1461+
}
1462+
}
1463+
1464+
impl #wasm_bindgen::convert::VectorIntoWasmAbi for #enum_name {
1465+
type Abi = <
1466+
#wasm_bindgen::__rt::std::boxed::Box<[#wasm_bindgen::JsValue]>
1467+
as #wasm_bindgen::convert::IntoWasmAbi
1468+
>::Abi;
1469+
1470+
fn vector_into_abi(
1471+
vector: #wasm_bindgen::__rt::std::boxed::Box<[#enum_name]>
1472+
) -> Self::Abi {
1473+
#wasm_bindgen::convert::js_value_vector_into_abi(vector)
1474+
}
1475+
}
1476+
1477+
impl #wasm_bindgen::convert::VectorFromWasmAbi for #enum_name {
1478+
type Abi = <
1479+
#wasm_bindgen::__rt::std::boxed::Box<[#wasm_bindgen::JsValue]>
1480+
as #wasm_bindgen::convert::FromWasmAbi
1481+
>::Abi;
1482+
1483+
unsafe fn vector_from_abi(
1484+
js: Self::Abi
1485+
) -> #wasm_bindgen::__rt::std::boxed::Box<[#enum_name]> {
1486+
#wasm_bindgen::convert::js_value_vector_from_abi(js)
1487+
}
1488+
}
14301489
})
14311490
.to_tokens(into);
14321491
}

tests/wasm/enum_vecs.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const wasm = require('wasm-bindgen-test.js');
2+
const assert = require('assert');
3+
4+
exports.pass_enum_vec = () => {
5+
const el1 = wasm.EnumArrayElement.Unit;
6+
const el2 = wasm.EnumArrayElement.Unit;
7+
const ret = wasm.consume_enum_vec([el1, el2]);
8+
assert.strictEqual(ret.length, 3);
9+
10+
const ret2 = wasm.consume_optional_enum_vec(ret);
11+
assert.strictEqual(ret2.length, 4);
12+
13+
assert.strictEqual(wasm.consume_optional_enum_vec(undefined), undefined);
14+
};
15+
16+
exports.pass_invalid_enum_vec = () => {
17+
try {
18+
wasm.consume_enum_vec(['not an enum value']);
19+
} catch (e) {
20+
assert.match(e.message, /invalid enum value passed/)
21+
assert.match(e.stack, /consume_enum_vec/)
22+
}
23+
};

tests/wasm/enum_vecs.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use wasm_bindgen::prelude::*;
2+
use wasm_bindgen_test::*;
3+
4+
#[wasm_bindgen(module = "tests/wasm/enum_vecs.js")]
5+
extern "C" {
6+
fn pass_enum_vec();
7+
fn pass_invalid_enum_vec();
8+
}
9+
10+
#[wasm_bindgen]
11+
pub enum EnumArrayElement {
12+
Unit,
13+
}
14+
15+
#[wasm_bindgen]
16+
pub fn consume_enum_vec(mut vec: Vec<EnumArrayElement>) -> Vec<EnumArrayElement> {
17+
vec.push(EnumArrayElement::Unit);
18+
vec
19+
}
20+
21+
#[wasm_bindgen]
22+
pub fn consume_optional_enum_vec(vec: Option<Vec<EnumArrayElement>>) -> Option<Vec<EnumArrayElement>> {
23+
vec.map(consume_enum_vec)
24+
}
25+
26+
#[wasm_bindgen_test]
27+
fn test_valid() {
28+
pass_enum_vec();
29+
}
30+
31+
#[wasm_bindgen_test]
32+
fn test_invalid() {
33+
pass_invalid_enum_vec();
34+
}

tests/wasm/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod comments;
2424
pub mod duplicate_deps;
2525
pub mod duplicates;
2626
pub mod enums;
27+
pub mod enum_vecs;
2728
#[path = "final.rs"]
2829
pub mod final_;
2930
pub mod futures;

0 commit comments

Comments
 (0)