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

Can't return `Number` values as `u64`

Open shanemadden opened this issue 1 year ago • 1 comments

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

shanemadden avatar Sep 12 '24 17:09 shanemadden

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

daxpedda avatar Sep 16 '24 11:09 daxpedda