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

Passing i64 around always fails with the js feature #3773

Closed
kajacx opened this issue Apr 17, 2023 · 3 comments · Fixed by #3796
Closed

Passing i64 around always fails with the js feature #3773

kajacx opened this issue Apr 17, 2023 · 3 comments · Fixed by #3796
Labels
Milestone

Comments

@kajacx
Copy link
Contributor

kajacx commented Apr 17, 2023

Passing i64 to and from a module on the web always fails with the js feature

When compiling wasmer itself to WASM using the js feature so that it can run in the browser, there is an error when you then load another plugin from wasmer and try to pass i64 values around.

Steps to reproduce

  1. Clone this repo at the add-i64-result-error tag.
  2. Run the build.sh script (after reading the entire source code, of course).
  3. Open the webpage and see the result.

Expected behavior

i64 values should be added correctly inside the plugin and the values should be passed between the plugin and wasmer without a problem.

Actual behavior

Calling the add_i64 exported function always returns a runtime error, example:

RuntimeError { source: Js(JsValue(TypeError: can't convert 5 to BigInt
getImports/imports.wbg.__wbg_apply_aedce30790c00792/<@http://127.0.0.1:8094/hello_wasmer_u64.js:414:37
handleError@http://127.0.0.1:8094/hello_wasmer_u64.js:224:18
getImports/imports.wbg.__wbg_apply_aedce30790c00792@http://127.0.0.1:8094/hello_wasmer_u64.js:413:68
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[199]:0x21883
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[43]:0x2b1e
test_add_i64@http://127.0.0.1:8094/hello_wasmer_u64.js:212:14
@http://127.0.0.1:8094/:13:15
promise callback*@http://127.0.0.1:8094/:11:14
)) }

Additional context

This was discovered when I was doing testing for #3763. Although this is a different error than I was experiencing there. That might be because I was using wasmer from source in that PR, whereas here I am using the currently released version 3.1.1.

Also, I followed this example by Mozilla on how to run Rust on the browser using wasm-pack, if that's relevant.

@ptitSeb
Copy link
Contributor

ptitSeb commented Apr 18, 2023

Can you try again with current master, mainy things have changed on the Js part so it might just work now.

@ptitSeb ptitSeb added priority-medium Medium priority issue platform-browser labels Apr 18, 2023
@ptitSeb ptitSeb added this to the v3.3 milestone Apr 18, 2023
@kajacx
Copy link
Contributor Author

kajacx commented Apr 19, 2023

Can you try again with current master, many things have changed on the Js part so it might just work now.

Ok, I have tried just that, and it is giving me a different error. This time, calling the add_i64 panics inside the library code instead of returning an error value, which is even worse.

You can clone this repo again, this time at the add-i64-source-panic tag for a minimal(-ish) reproducible example.

This is the new error:

panicked at 'called `Result::unwrap()` on an `Err` value: --1', C:\Programs\Cargo\bin\git\checkouts\wasmer-f11f30e62739aa29\8a343d0\lib\api\src\js\as_js.rs:43:41

Stack:

getImports/imports.wbg.__wbg_new_abda76e883ba8a5f@http://127.0.0.1:8094/hello_wasmer_u64.js:356:21
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[344]:0x261a2
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[138]:0x20a04
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[229]:0x23a95
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[159]:0x21df2
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[67]:0x19b10
@http://127.0.0.1:8094/hello_wasmer_u64_bg.wasm:wasm-function[54]:0x31be
test_add_i64@http://127.0.0.1:8094/hello_wasmer_u64.js:221:14
@http://127.0.0.1:8094/:14:17
promise callback*@http://127.0.0.1:8094/:11:14

This is the line that throws the error:

#[inline]
pub fn param_from_js(ty: &Type, js_val: &JsValue) -> Value {
    match ty {
        Type::I32 => Value::I32(js_val.as_f64().unwrap() as _),
        Type::I64 => {
            let number = js_val.as_f64().map(|f| f as i64).unwrap_or_else(|| {
                if js_val.is_bigint() {
                    // To support BigInt
                    let big_num: u128 = js_sys::BigInt::from(js_val.clone()).try_into().unwrap(); // <-- HERE
                    big_num as i64
                } else {
                    (js_sys::Number::from(js_val.clone()).as_f64().unwrap()) as i64
                }
            });
            Value::I64(number)
        }
        Type::F32 => Value::F32(js_val.as_f64().unwrap() as _),
        Type::F64 => Value::F64(js_val.as_f64().unwrap()),
        Type::V128 => {
            let big_num: u128 = js_sys::BigInt::from(js_val.clone()).try_into().unwrap();
            Value::V128(big_num)
        }
        Type::ExternRef | Type::FuncRef => unimplemented!(
            "The type `{:?}` is not yet supported in the JS Function API",
            ty
        ),
    }
}

Clearly, something is wrong with the BigInt values. I will investigate further.

@kajacx
Copy link
Contributor Author

kajacx commented Apr 19, 2023

Ok, passing any i64 values into the wasm module is ok, but returning any negative i64 value causes the problem. Also, I have verified that it is indeed the communication between wasmer and the plugin module that is the problem, and not the communication between the web host and the wasmer wasm plugin.

You can check out this repo again at the return-negative-i64-is-the-problem tag.

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.

2 participants