rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.83k stars 1.08k forks source link

Failed to import the same js functions with same rust fn name under different modules #4268

Closed EqualMa closed 1 week ago

EqualMa commented 1 week ago

Describe the Bug

If I use #[wasm_bindgen(js_name = parseFloat)] to import the same js function in two modules, with the same rust fn name, but with different signatures, things don't work well.

use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;

mod a {
    use wasm_bindgen::prelude::*;

    #[wasm_bindgen]
    extern "C" {
        #[wasm_bindgen(js_name = parseFloat)]
        pub fn parse_float(text: &JsValue) -> f64;
    }
}

mod b {
    use wasm_bindgen::prelude::*;

    #[wasm_bindgen]
    extern "C" {
        #[wasm_bindgen(js_name = parseFloat)]
        pub fn parse_float(text: &str) -> f64;
    }
}

#[wasm_bindgen_test]
pub fn pass_a() {
    let v = JsValue::from_str("1");
    assert_eq!(a::parse_float(&v), 1.);
}

#[wasm_bindgen_test]
pub fn pass_b() {
    assert_eq!(b::parse_float("2"), 2.);
}

The above test failed.

const wasmModule = new WebAssembly.Module(bytes);
                   ^

CompileError: WebAssembly.Module(): Compiling function #838:"f64::a::parse_float::hfd12f3d441bd8268" failed: not enough arguments on the stack for call (need 2, got 1) @+435657

Steps to Reproduce

  1. Test the above code or just clone https://github.com/EqualMa/example-wasm-bindgen-import-with-same-name
  2. Run wasm-pack test --node

Expected Behavior

Imports with the same js name and rust name should all work.

Actual Behavior

If the imports have the same js name and rust name, it seems that they all link to the same bindgen js function. So if they have different signatures, calling one of them failed at runtime.