-
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.
This means that `Vec`s can now be returned from `async` functions. Fixes #3155. I had to add a new `VectorIntoJsValue` trait, since similarly to `VectorIntoWasmAbi` the orphan rule won't let us directly implement `From<Vec<T>>` for `JsValue` outside of `wasm-bindgen`.
- Loading branch information
1 parent
ba244b9
commit 1e67de6
Showing
7 changed files
with
260 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
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
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,11 @@ | ||
const wasm = require('wasm-bindgen-test.js'); | ||
const assert = require('assert'); | ||
|
||
exports.js_works = async () => { | ||
assert.deepStrictEqual(await wasm.async_number_vec(), new Int32Array([1, -3, 7, 12])); | ||
assert.deepStrictEqual(await wasm.async_jsvalue_vec(), [1, "hi", new Float64Array(), null]); | ||
assert.deepStrictEqual(await wasm.async_import_vec(), [/hi|bye/, /hello w[a-z]rld/]); | ||
assert.deepStrictEqual(await wasm.async_string_vec(), ["a", "b", "c"]); | ||
assert.strictEqual((await wasm.async_struct_vec()).length, 2); | ||
assert.deepStrictEqual(await wasm.async_enum_vec(), [wasm.AnotherEnum.C, wasm.AnotherEnum.A, wasm.AnotherEnum.B]); | ||
}; |
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,66 @@ | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_test::*; | ||
|
||
#[wasm_bindgen(module = "tests/wasm/async_vecs.js")] | ||
extern "C" { | ||
async fn js_works(); | ||
} | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
pub type RegExp; | ||
#[wasm_bindgen(constructor)] | ||
fn new(re: &str) -> RegExp; | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub async fn async_number_vec() -> Vec<i32> { | ||
vec![1, -3, 7, 12] | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub async fn async_jsvalue_vec() -> Vec<JsValue> { | ||
vec![ | ||
1u32.into(), | ||
"hi".into(), | ||
Vec::<f64>::new().into(), | ||
JsValue::NULL, | ||
] | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub async fn async_import_vec() -> Vec<RegExp> { | ||
vec![RegExp::new("hi|bye"), RegExp::new("hello w[a-z]rld")] | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub async fn async_string_vec() -> Vec<String> { | ||
vec!["a".to_owned(), "b".to_owned(), "c".to_owned()] | ||
} | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone)] | ||
pub struct AnotherStruct; | ||
|
||
#[wasm_bindgen] | ||
pub async fn async_struct_vec() -> Vec<AnotherStruct> { | ||
vec![AnotherStruct; 2] | ||
} | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone)] | ||
pub enum AnotherEnum { | ||
A, | ||
B, | ||
C, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub async fn async_enum_vec() -> Vec<AnotherEnum> { | ||
vec![AnotherEnum::C, AnotherEnum::A, AnotherEnum::B] | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
async fn works() { | ||
js_works().await; | ||
} |
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