Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using exported Rust structs causes detached Buffer issue #4352

Closed
Anoromi opened this issue Dec 11, 2024 · 2 comments · Fixed by #4353
Closed

Using exported Rust structs causes detached Buffer issue #4352

Anoromi opened this issue Dec 11, 2024 · 2 comments · Fixed by #4353
Labels

Comments

@Anoromi
Copy link

Anoromi commented Dec 11, 2024

Describe the Bug

When creating a function that accepts a vector of exported rust structs in rust > 1.82 on big enough sizes function returns an error.

Steps to Reproduce

  1. Use hello_world example and set wasm_bindgen to version 2.99.
  2. Create a function and a struct in rust:
#[wasm_bindgen]
#[derive(Clone, Copy, Debug)]
pub struct PartialTestData {
    pub a: u32,
    pub b: f64,
    pub c: f64,
}

#[wasm_bindgen]
impl PartialTestData {
    #[wasm_bindgen(constructor)]
    pub fn new(a: u32, b: f64, c: f64) -> Self {
        return Self { a, b, c };
    }
}

#[wasm_bindgen]
pub fn test_sum(param: Vec<PartialTestData>) -> u64 {
    let mut k = 0u64;
    for v in param {
        k += u64::from(v.a)
    }
    return k;
}
  1. Replace index.js file with call to a function:
import { PartialTestData, test_sum } from "./pkg";

const size = 1_000_000;
const values = new Array(size)
  .fill(0)
  .map((_, i) => new PartialTestData(i, 0, 0));
alert(test_sum(values));
  1. Run npm install, npm run serve
  2. Open the browser
  3. See error

Expected Behavior

Alert with a result of a computation.

Actual Behavior

An exception in console TypeError: attempting to access detached ArrayBuffer in Firefox or TypeError: Cannot perform DataView.prototype.setUint32 on a detached ArrayBuffer in Chrome

image

Additional Context

The issue doesn't happen in versions of rust below 1.82. I also didn't test options other than bundler.

@Anoromi Anoromi added the bug label Dec 11, 2024
@mProjectsCode
Copy link

mProjectsCode commented Dec 14, 2024

I am facing the same issue. A rollback to wasm-bindgen = "=0.2.92" fixed the problem for me.

@daxpedda
Copy link
Collaborator

Note: the bug is specific to the reference type transformations. So it doesn't show up in 0.2.92 because reference type transformations were not enabled by default back then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants