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

Cannot wasm-pack build when using a js_sys array method (filter or map)

Open Nicolas-B-Braincube opened this issue 1 year ago • 2 comments

Describe the Bug

When I run wasm-pack build --target web --release build fails if I use an Array method like filter or map (I havn't tried other methods). The strange part is that wasm-pack build --target web --dev runs perfectly

Cargo.toml

[package]
name = "transforms-wasm"
version = "0.1.0"
authors = ["Nicolas Bruère <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.93"
js-sys = "0.3.70"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.43"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

lib.rs

#[wasm_bindgen]
pub fn get_histogram(input: &Array, binsize: f64, min: f64, max: f64, dimension: i32) -> Array {
    let output = Array::new();

    let mut i = min;
    while i <= max {
        let arr = Array::new();
        arr.push(&JsValue::from_f64(i));
        let count = input.filter(&mut |item, _, _| {
            let row = Array::from(&item);
            let o = row.at(dimension).as_f64().unwrap();
            if o >= i && o < (i + binsize) {
                return true;
            }
            false
        }).length();
        arr.push(&JsValue::from(count));
        output.push(&arr);
        i += binsize;
    }

    output
}

The error output:

[INFO]: ⬇️  Installing wasm-bindgen...
error: failed to generates bindings for adapter

Caused by:
    no function table found in module
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit status: 1
  full command: "/Users/nicolas.bruere/Library/Caches/.wasm-pack/wasm-bindgen-507ae24572712ef8/wasm-bindgen" "/Users/nicolas.bruere/workspace/reco-explorer/transforms-wasm/target/wasm32-unknown-unknown/release/transforms_wasm.wasm" "--out-dir" "/Users/nicolas.bruere/workspace/reco-explorer/transforms-wasm/pkg" "--typescript" "--target" "web"

Steps to Reproduce

  1. Run wasm-pack build --target web --release => you get an error
  2. Run wasm-pack build --target web --dev => you get 0 error

https://we.tl/t-V6pMfd5I4B

Expected Behavior

I'd like to have the same behavior whether I'm building on dev or release. I'd be ok if both return an error since it might be a mistake I made ;)

Actual Behavior

The build command outputs an error on release mode and no errors on dev mode

Nicolas-B-Braincube avatar Sep 18 '24 13:09 Nicolas-B-Braincube

For whatever reasons, all references to these functions are removed by some pre-processing done in wasm-bindgen, which then asks Walrus to do some cleanup in which this table is removed.

This would require some digging into to find out exactly what is being removed here and why.

daxpedda avatar Sep 19 '24 13:09 daxpedda

Thank you for your answer @daxpedda 👍 I'm sorry I'm not comfortable enough in Rust to do this digging

Nicolas-B-Braincube avatar Sep 23 '24 07:09 Nicolas-B-Braincube

I narrowed down the cause to this part: https://github.com/rustwasm/wasm-bindgen/blob/7302d644191bc36abe41bd0086732685061ce90e/crates/cli-support/src/wit/mod.rs#L170-L172

It assumes that the table should be preserved only if there are closure imports, but doesn't account for stack closures which use a different mechanism. That if should be simply removed so that table is preserved unconditionally.

RReverser avatar Jul 07 '25 11:07 RReverser