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

Better support for u64 through JSValue::as_u64() and u64::into() -> JsValue #2350

Closed
lucacasonato opened this issue Nov 8, 2020 · 4 comments · Fixed by #3058
Closed

Better support for u64 through JSValue::as_u64() and u64::into() -> JsValue #2350

lucacasonato opened this issue Nov 8, 2020 · 4 comments · Fixed by #3058

Comments

@lucacasonato
Copy link

Motivation

BigInt is now supported in stable releases of all modern browsers. Currently interfacing with BigInt generically in wasm-bindgen with JSValue is quite painful. JSValue is not aware of BigInt (and thus u64) at all.

Proposed Solution

  • Add a impl From<u64> for JSValue, that can be used to cast u64 to BigInt.
  • Add JSValue::as_u64(&self) -> Option<u64> to cast BigInt to u64.

Alternatives

This solution aligns closely to current implementation of primitives handling in JSValue. I am not aware of alternative implementation ideas, or if this can be built on top of wasm-bindgen.

@lucacasonato lucacasonato changed the title Better support for u64 through js_sys::BigInt and JSValue::as_u64() Better support for u64 through JSValue::as_u64() and u64::into() -> JsValue Nov 8, 2020
@ghost
Copy link

ghost commented Feb 25, 2021

Signed i64 variants would be great too!

@trevyn
Copy link
Contributor

trevyn commented Sep 3, 2021

Also should be closable now that #2629 is merged.

@RReverser
Copy link
Member

I don't see anything similar to JsValue::as_u64 (a way to get u64/i64 from BigInt) in the linked PR, only the opposite conversions.

Am I missing some API? If not, perhaps worth reopening this issue.

@RReverser
Copy link
Member

Ping, can we reopen this?

@alexcrichton alexcrichton reopened this Dec 20, 2021
RReverser added a commit to RReverser/wasm-bindgen that referenced this issue Sep 2, 2022
Adds a way to convert `JsValue` or a `BigInt` to `i64`/`u64`/`i128`/`u128` with type and range checks, returning the original `JsValue` otherwise.

This could be optimised a little bit further via more intrinsics, but it's good enough for the initial implementation, so leaving any optimisations for the future.

Fixes rustwasm#2350.
RReverser added a commit to RReverser/wasm-bindgen that referenced this issue Sep 2, 2022
Adds a way to convert `JsValue` or a `BigInt` to `i64`/`u64`/`i128`/`u128` with type and range checks, returning the original `JsValue` otherwise.

This could be optimised a little bit further via more intrinsics, but it's good enough for the initial implementation, so leaving any optimisations for the future.

Fixes rustwasm#2350.
RReverser added a commit to RReverser/wasm-bindgen that referenced this issue Sep 4, 2022
Adds a way to convert `JsValue` or a `BigInt` to `i64`/`u64`/`i128`/`u128` with type and range checks, returning the original `JsValue` otherwise.

This could be optimised a little bit further via more intrinsics, but it's good enough for the initial implementation, so leaving any optimisations for the future.

Fixes rustwasm#2350.
Liamolucko pushed a commit that referenced this issue Sep 4, 2022
Adds a way to convert `JsValue` or a `BigInt` to `i64`/`u64`/`i128`/`u128` with type and range checks, returning the original `JsValue` otherwise.

This could be optimised a little bit further via more intrinsics, but it's good enough for the initial implementation, so leaving any optimisations for the future.

Fixes #2350.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants