Skip to content

TypedArray::from(slice) sporadically fails #4492

Open
@rkreis-v

Description

@rkreis-v

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:

impl<'a> From<&'a [$ty]> for $name {
#[inline]
fn from(slice: &'a [$ty]) -> $name {
// This is safe because the `new` function makes a copy if its argument is a TypedArray
unsafe { $name::new(&$name::view(slice)) }
}
}

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>)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions