Skip to content

Commit f75a3f8

Browse files
authored
js-sys: Fix BigInt::from(usize) and BigInt::from(isize) (#3056)
* js-sys: Fix `BigInt::from(usize)` and `BigInt::from(isize)` Fixes #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 #2978 that `JsValue::from` returns a `Number`, not a `BigInt`. * Add a regression test * fmt
1 parent c890dc3 commit f75a3f8

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

crates/js-sys/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ macro_rules! bigint_from {
10511051
}
10521052
)*)
10531053
}
1054-
bigint_from!(i8 u8 i16 u16 i32 u32);
1054+
bigint_from!(i8 u8 i16 u16 i32 u32 isize usize);
10551055

10561056
macro_rules! bigint_from_big {
10571057
($($x:ident)*) => ($(
@@ -1070,7 +1070,7 @@ macro_rules! bigint_from_big {
10701070
}
10711071
)*)
10721072
}
1073-
bigint_from_big!(i64 u64 i128 u128 isize usize);
1073+
bigint_from_big!(i64 u64 i128 u128);
10741074

10751075
impl PartialEq<Number> for BigInt {
10761076
#[inline]

crates/js-sys/tests/wasm/BigInt.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use js_sys::BigInt;
2+
use wasm_bindgen::prelude::*;
3+
use wasm_bindgen_test::wasm_bindgen_test;
4+
5+
/// `assert_eq!`, but the arguments are converted to `JsValue`s.
6+
#[track_caller]
7+
fn assert_jsvalue_eq(a: impl Into<JsValue>, b: impl Into<JsValue>) {
8+
assert_eq!(a.into(), b.into());
9+
}
10+
11+
#[wasm_bindgen_test]
12+
fn from() {
13+
// Test that all the `From` impls work properly.
14+
assert_jsvalue_eq(BigInt::from(1u8), 1u64);
15+
assert_jsvalue_eq(BigInt::from(1u16), 1u64);
16+
assert_jsvalue_eq(BigInt::from(1u32), 1u64);
17+
assert_jsvalue_eq(BigInt::from(1u64), 1u64);
18+
assert_jsvalue_eq(BigInt::from(1u128), 1u64);
19+
assert_jsvalue_eq(BigInt::from(1usize), 1u64);
20+
assert_jsvalue_eq(BigInt::from(-3i8), -3i64);
21+
assert_jsvalue_eq(BigInt::from(-3i16), -3i64);
22+
assert_jsvalue_eq(BigInt::from(-3i32), -3i64);
23+
assert_jsvalue_eq(BigInt::from(-3i64), -3i64);
24+
assert_jsvalue_eq(BigInt::from(-3i128), -3i64);
25+
assert_jsvalue_eq(BigInt::from(-3isize), -3i64);
26+
}
27+
28+
#[wasm_bindgen_test]
29+
fn eq() {
30+
// Test that all the `Eq` impls work properly.
31+
assert_eq!(BigInt::from(1u64), 1u8);
32+
assert_eq!(BigInt::from(1u64), 1u16);
33+
assert_eq!(BigInt::from(1u64), 1u32);
34+
assert_eq!(BigInt::from(1u64), 1u64);
35+
assert_eq!(BigInt::from(1u64), 1u128);
36+
assert_eq!(BigInt::from(1u64), 1usize);
37+
assert_eq!(BigInt::from(-3i64), -3i8);
38+
assert_eq!(BigInt::from(-3i64), -3i16);
39+
assert_eq!(BigInt::from(-3i64), -3i32);
40+
assert_eq!(BigInt::from(-3i64), -3i64);
41+
assert_eq!(BigInt::from(-3i64), -3i128);
42+
assert_eq!(BigInt::from(-3i64), -3isize);
43+
}

crates/js-sys/tests/wasm/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
pub mod Array;
55
pub mod ArrayBuffer;
66
pub mod ArrayIterator;
7+
pub mod BigInt;
78
pub mod Boolean;
89
pub mod DataView;
910
pub mod Date;

0 commit comments

Comments
 (0)