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

Can't return Number values as u64 #4113

Open
shanemadden opened this issue Sep 12, 2024 · 1 comment
Open

Can't return Number values as u64 #4113

shanemadden opened this issue Sep 12, 2024 · 1 comment

Comments

@shanemadden
Copy link

shanemadden commented Sep 12, 2024

Describe the Bug

When creating bindings to a function with a u64 return value, the returned value is assumed to be of type BigInt in the generated bindings. It seems like it would be possible to get Number values containing integers between 2^32 and 2^53-1 as an integer type directly in the bindings, but it doesn't appear to be possible currently.

I couldn't find any documentation explicitly saying "use f64 if you want integers over u32 and convert on the Rust side if you need, it's the same size anyway" (I may have just missed it), but that seems to be the approach that's been taken eg for Date.now() in js_sys.

Steps to Reproduce

  1. Create a simple alternative binding to Date.now() expecting a u64 and a test that calls it:
#[wasm_bindgen]
extern "C" {
    pub type Date;

    #[wasm_bindgen(static_method_of = Date)]
    pub fn now() -> u64;
}

#[cfg(test)]
pub mod tests {
    use super::Date;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    pub fn u64_now() {
        Date::now();
    }
}
  1. Run wasm-pack test --node

Expected Behavior

A binding is produced that can handle a Number or BigInt return value

Actual Behavior

Test fails with

wasm-bindgen: imported JS function that was not marked as `catch` threw an error: expected a bigint argument, found number
@daxpedda
Copy link
Collaborator

That seems sensible to me, I'm happy to review a PR.

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

No branches or pull requests

2 participants