-
-
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.
* Implement `Into<JsValue>` for `Vec` 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`. * Implement Into<JsValue> for boxed slices of numbers as well * Add changelog entry * Fix memory leak * Add missing if_std! * Move the changelog entry to the right spot
- Loading branch information
1 parent
78463a2
commit 88efe46
Showing
8 changed files
with
284 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
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,17 @@ | ||
const wasm = require('wasm-bindgen-test.js'); | ||
const assert = require('assert'); | ||
|
||
exports.js_works = async () => { | ||
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]); | ||
|
||
const numberVec = await wasm.async_number_vec(); | ||
assert.deepStrictEqual(numberVec, new Int32Array([1, -3, 7, 12])); | ||
// Make sure `numberVec` is a fresh `Int32Array`, not a view into Wasm memory, | ||
// so that it can be GC'd without the whole Wasm module having to be GC'd as | ||
// well. | ||
assert.strictEqual(numberVec.byteLength, numberVec.buffer.byteLength); | ||
}; |
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