Skip to content

Commit

Permalink
js-sys: Fix BigInt::from(usize) and BigInt::from(isize) (rustwasm…
Browse files Browse the repository at this point in the history
…#3056)

* js-sys: Fix `BigInt::from(usize)` and `BigInt::from(isize)`

Fixes rustwasm#3055

This changes `isize` and `usize` to be converted to `BigInt` in the same way as `i32`/`u32`, `BigInt(JsValue::from(n))`, rather than `JsValue::from(n).unchecked_into()`. The latter is now wrong since as of rustwasm#2978 that `JsValue::from` returns a `Number`, not a `BigInt`.

* Add a regression test

* fmt
  • Loading branch information
Liamolucko committed Sep 2, 2022
1 parent c890dc3 commit f75a3f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ macro_rules! bigint_from {
}
)*)
}
bigint_from!(i8 u8 i16 u16 i32 u32);
bigint_from!(i8 u8 i16 u16 i32 u32 isize usize);

macro_rules! bigint_from_big {
($($x:ident)*) => ($(
Expand All @@ -1070,7 +1070,7 @@ macro_rules! bigint_from_big {
}
)*)
}
bigint_from_big!(i64 u64 i128 u128 isize usize);
bigint_from_big!(i64 u64 i128 u128);

impl PartialEq<Number> for BigInt {
#[inline]
Expand Down
43 changes: 43 additions & 0 deletions crates/js-sys/tests/wasm/BigInt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use js_sys::BigInt;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::wasm_bindgen_test;

/// `assert_eq!`, but the arguments are converted to `JsValue`s.
#[track_caller]
fn assert_jsvalue_eq(a: impl Into<JsValue>, b: impl Into<JsValue>) {
assert_eq!(a.into(), b.into());
}

#[wasm_bindgen_test]
fn from() {
// Test that all the `From` impls work properly.
assert_jsvalue_eq(BigInt::from(1u8), 1u64);
assert_jsvalue_eq(BigInt::from(1u16), 1u64);
assert_jsvalue_eq(BigInt::from(1u32), 1u64);
assert_jsvalue_eq(BigInt::from(1u64), 1u64);
assert_jsvalue_eq(BigInt::from(1u128), 1u64);
assert_jsvalue_eq(BigInt::from(1usize), 1u64);
assert_jsvalue_eq(BigInt::from(-3i8), -3i64);
assert_jsvalue_eq(BigInt::from(-3i16), -3i64);
assert_jsvalue_eq(BigInt::from(-3i32), -3i64);
assert_jsvalue_eq(BigInt::from(-3i64), -3i64);
assert_jsvalue_eq(BigInt::from(-3i128), -3i64);
assert_jsvalue_eq(BigInt::from(-3isize), -3i64);
}

#[wasm_bindgen_test]
fn eq() {
// Test that all the `Eq` impls work properly.
assert_eq!(BigInt::from(1u64), 1u8);
assert_eq!(BigInt::from(1u64), 1u16);
assert_eq!(BigInt::from(1u64), 1u32);
assert_eq!(BigInt::from(1u64), 1u64);
assert_eq!(BigInt::from(1u64), 1u128);
assert_eq!(BigInt::from(1u64), 1usize);
assert_eq!(BigInt::from(-3i64), -3i8);
assert_eq!(BigInt::from(-3i64), -3i16);
assert_eq!(BigInt::from(-3i64), -3i32);
assert_eq!(BigInt::from(-3i64), -3i64);
assert_eq!(BigInt::from(-3i64), -3i128);
assert_eq!(BigInt::from(-3i64), -3isize);
}
1 change: 1 addition & 0 deletions crates/js-sys/tests/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub mod Array;
pub mod ArrayBuffer;
pub mod ArrayIterator;
pub mod BigInt;
pub mod Boolean;
pub mod DataView;
pub mod Date;
Expand Down

0 comments on commit f75a3f8

Please sign in to comment.