wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

TypedArray::from(slice) sporadically fails

Open rkreis-v opened this issue 7 months ago • 1 comments

Describe the Bug

Closely related to #4395 - TypedArray::from with slice input sporadically fails: "Cannot perform Construct on a detached ArrayBuffer". It uses TypedArray::view: https://github.com/rustwasm/wasm-bindgen/blob/c35cc9369d5e0dc418986f7811a0dd702fb33ef9/crates/js-sys/src/lib.rs#L6379-L6385

Note that the function is not marked unsafe itself.

Steps to Reproduce

Add test case to crates/js-sys/tests/wasm/TypedArray.rs:

#[wasm_bindgen_test]
fn from_succeeds_when_heap_grows() {
    let _ = (0..10_000).map(|i| {
        Int32Array::from([i].as_slice())
    }).collect::<Vec<_>>();
}

Run test:

# cargo test --target wasm32-unknown-unknown from_succeeds_when_heap_grows
[...]
running 1 test
test TypedArray::from_succeeds_when_heap_grows ... FAIL

failures:

---- TypedArray::from_succeeds_when_heap_grows output ----
    error output:
        wasm-bindgen: imported JS function that was not marked as `catch` threw an error: Cannot perform Construct on a detached ArrayBuffer

        Stack:
        TypeError: Cannot perform Construct on a detached ArrayBuffer
            at new Int32Array (<anonymous>)

rkreis-v avatar Apr 28 '25 11:04 rkreis-v