File tree Expand file tree Collapse file tree 7 files changed +45
-11
lines changed Expand file tree Collapse file tree 7 files changed +45
-11
lines changed Original file line number Diff line number Diff line change 37
37
3 . The name of an imported function if the function is not a method and does not have a ` js_namespace ` or ` module ` attribute.
38
38
- Using JS keywords on imports in places other than the above will no longer cause the keywords to be escaped as ` _{keyword} ` .
39
39
40
+ * Fixed passing large arrays into Rust failing because of internal memory allocations invalidating the memory buffer.
41
+ [ #4353 ] ( https://github.com/rustwasm/wasm-bindgen/pull/4353 )
42
+
40
43
--------------------------------------------------------------------------------
41
44
42
45
## [ 0.2.99] ( https://github.com/rustwasm/wasm-bindgen/compare/0.2.98...0.2.99 )
Original file line number Diff line number Diff line change @@ -1641,34 +1641,32 @@ __wbg_set_wasm(wasm);"
1641
1641
let add = self . expose_add_to_externref_table ( table, alloc) ?;
1642
1642
self . global ( & format ! (
1643
1643
"
1644
- function {}(array, malloc) {{
1644
+ function {ret }(array, malloc) {{
1645
1645
const ptr = malloc(array.length * 4, 4) >>> 0;
1646
- const mem = {}();
1647
1646
for (let i = 0; i < array.length; i++) {{
1648
- mem.setUint32(ptr + 4 * i, {}(array[i]), true);
1647
+ const add = {add}(array[i]);
1648
+ {mem}().setUint32(ptr + 4 * i, add, true);
1649
1649
}}
1650
1650
WASM_VECTOR_LEN = array.length;
1651
1651
return ptr;
1652
1652
}}
1653
1653
" ,
1654
- ret, mem, add,
1655
1654
) ) ;
1656
1655
}
1657
1656
_ => {
1658
1657
self . expose_add_heap_object ( ) ;
1659
1658
self . global ( & format ! (
1660
1659
"
1661
- function {}(array, malloc) {{
1660
+ function {ret }(array, malloc) {{
1662
1661
const ptr = malloc(array.length * 4, 4) >>> 0;
1663
- const mem = {}();
1662
+ const mem = {mem }();
1664
1663
for (let i = 0; i < array.length; i++) {{
1665
1664
mem.setUint32(ptr + 4 * i, addHeapObject(array[i]), true);
1666
1665
}}
1667
1666
WASM_VECTOR_LEN = array.length;
1668
1667
return ptr;
1669
1668
}}
1670
1669
" ,
1671
- ret, mem,
1672
1670
) ) ;
1673
1671
}
1674
1672
}
Original file line number Diff line number Diff line change @@ -666,9 +666,9 @@ function addToExternrefTable0(obj) {
666
666
667
667
function passArrayJsValueToWasm0 ( array , malloc ) {
668
668
const ptr = malloc ( array . length * 4 , 4 ) >>> 0 ;
669
- const mem = getDataViewMemory0 ( ) ;
670
669
for ( let i = 0 ; i < array . length ; i ++ ) {
671
- mem . setUint32 ( ptr + 4 * i , addToExternrefTable0 ( array [ i ] ) , true ) ;
670
+ const add = addToExternrefTable0 ( array [ i ] ) ;
671
+ getDataViewMemory0 ( ) . setUint32 ( ptr + 4 * i , add , true ) ;
672
672
}
673
673
WASM_VECTOR_LEN = array . length ;
674
674
return ptr ;
Original file line number Diff line number Diff line change @@ -49,9 +49,9 @@ function addToExternrefTable0(obj) {
49
49
50
50
function passArrayJsValueToWasm0 ( array , malloc ) {
51
51
const ptr = malloc ( array . length * 4 , 4 ) >>> 0 ;
52
- const mem = getDataViewMemory0 ( ) ;
53
52
for ( let i = 0 ; i < array . length ; i ++ ) {
54
- mem . setUint32 ( ptr + 4 * i , addToExternrefTable0 ( array [ i ] ) , true ) ;
53
+ const add = addToExternrefTable0 ( array [ i ] ) ;
54
+ getDataViewMemory0 ( ) . setUint32 ( ptr + 4 * i , add , true ) ;
55
55
}
56
56
WASM_VECTOR_LEN = array . length ;
57
57
return ptr ;
Original file line number Diff line number Diff line change
1
+ const assert = require ( 'assert' ) ;
2
+ const wasm = require ( 'wasm-bindgen-test' ) ;
3
+
4
+ // Test if passing large arrays which cause allocation in Wasm are properly handled.
5
+ exports . pass_array_with_allocation = ( ) => {
6
+ const values = new Array ( 10_000 ) . fill ( 1 )
7
+ assert . strictEqual ( wasm . test_sum ( values ) , 10_000 ) ;
8
+ } ;
Original file line number Diff line number Diff line change
1
+ use js_sys:: Number ;
2
+ use wasm_bindgen:: prelude:: wasm_bindgen;
3
+ use wasm_bindgen_test:: wasm_bindgen_test;
4
+
5
+ #[ wasm_bindgen( module = "tests/wasm/js_vec.js" ) ]
6
+ extern "C" {
7
+ fn pass_array_with_allocation ( ) ;
8
+ }
9
+
10
+ #[ wasm_bindgen]
11
+ pub fn test_sum ( param : Vec < Number > ) -> f64 {
12
+ let mut sum = 0. ;
13
+
14
+ for data in param {
15
+ sum += data. value_of ( ) ;
16
+ }
17
+
18
+ sum
19
+ }
20
+
21
+ #[ wasm_bindgen_test]
22
+ fn test ( ) {
23
+ pass_array_with_allocation ( ) ;
24
+ }
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ pub mod inner_self;
40
40
pub mod intrinsics;
41
41
pub mod js_keywords;
42
42
pub mod js_objects;
43
+ pub mod js_vec;
43
44
pub mod jscast;
44
45
pub mod link_to;
45
46
pub mod macro_rules;
You can’t perform that action at this time.
0 commit comments