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 BigInt conversions #3049

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions crates/cli-support/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,21 @@ intrinsics! {
#[symbol = "__wbindgen_number_new"]
#[signature = fn(F64) -> Externref]
NumberNew,
#[symbol = "__wbindgen_bigint_new"]
#[symbol = "__wbindgen_bigint_from_str"]
#[signature = fn(ref_string()) -> Externref]
BigIntNew,
BigIntFromStr,
#[symbol = "__wbindgen_bigint_from_i64"]
#[signature = fn(I64) -> Externref]
BigIntFromI64,
#[symbol = "__wbindgen_bigint_from_u64"]
#[signature = fn(U64) -> Externref]
BigIntFromU64,
#[symbol = "__wbindgen_bigint_from_i128"]
#[signature = fn(I64, U64) -> Externref]
BigIntFromI128,
#[symbol = "__wbindgen_bigint_from_u128"]
#[signature = fn(U64, U64) -> Externref]
BigIntFromU128,
#[symbol = "__wbindgen_string_new"]
#[signature = fn(ref_string()) -> Externref]
StringNew,
Expand Down
12 changes: 11 additions & 1 deletion crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3323,11 +3323,21 @@ impl<'a> Context<'a> {
args[0].clone()
}

Intrinsic::BigIntNew => {
Intrinsic::BigIntFromStr => {
assert_eq!(args.len(), 1);
format!("BigInt({})", args[0])
}

Intrinsic::BigIntFromI64 | Intrinsic::BigIntFromU64 => {
assert_eq!(args.len(), 1);
args[0].clone()
}

Intrinsic::BigIntFromI128 | Intrinsic::BigIntFromU128 => {
assert_eq!(args.len(), 2);
format!("{} << BigInt(64) | {}", args[0], args[1])
RReverser marked this conversation as resolved.
Show resolved Hide resolved
}

Intrinsic::StringNew => {
assert_eq!(args.len(), 1);
args[0].clone()
Expand Down
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl JsValue {
/// allocated large integer) and returns a handle to the JS version of it.
#[inline]
pub fn bigint_from_str(s: &str) -> JsValue {
unsafe { JsValue::_new(__wbindgen_bigint_new(s.as_ptr(), s.len())) }
unsafe { JsValue::_new(__wbindgen_bigint_from_str(s.as_ptr(), s.len())) }
}

/// Creates a new JS value which is a boolean.
Expand Down Expand Up @@ -868,7 +868,7 @@ macro_rules! numbers {
numbers! { i8 u8 i16 u16 i32 u32 f32 f64 }

macro_rules! big_numbers {
($($n:ident)*) => ($(
(|$arg:ident|, $($n:ident = $handle:expr,)*) => ($(
impl PartialEq<$n> for JsValue {
#[inline]
fn eq(&self, other: &$n) -> bool {
Expand All @@ -878,14 +878,20 @@ macro_rules! big_numbers {

impl From<$n> for JsValue {
#[inline]
fn from(n: $n) -> JsValue {
JsValue::bigint_from_str(&n.to_string())
fn from($arg: $n) -> JsValue {
unsafe { JsValue::_new($handle) }
}
}
)*)
}

big_numbers! { i64 u64 i128 u128 }
big_numbers! {
|n|,
i64 = __wbindgen_bigint_from_i64(n),
u64 = __wbindgen_bigint_from_u64(n),
i128 = __wbindgen_bigint_from_i128((n >> 64) as i64, n as u64),
u128 = __wbindgen_bigint_from_u128((n >> 64) as u64, n as u64),
}

// `usize` and `isize` have to be treated a bit specially, because we know that
// they're 32-bit but the compiler conservatively assumes they might be bigger.
Expand Down Expand Up @@ -926,7 +932,11 @@ externs! {

fn __wbindgen_string_new(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_number_new(f: f64) -> u32;
fn __wbindgen_bigint_new(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_bigint_from_str(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_bigint_from_i64(n: i64) -> u32;
fn __wbindgen_bigint_from_u64(n: u64) -> u32;
fn __wbindgen_bigint_from_i128(hi: i64, lo: u64) -> u32;
fn __wbindgen_bigint_from_u128(hi: u64, lo: u64) -> u32;
RReverser marked this conversation as resolved.
Show resolved Hide resolved
fn __wbindgen_symbol_named_new(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_symbol_anonymous_new() -> u32;

Expand Down
36 changes: 26 additions & 10 deletions tests/wasm/u64.js → tests/wasm/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@ exports.js_works = () => {
assert.strictEqual(wasm.i64_min(), BigInt('-9223372036854775808'));
assert.strictEqual(wasm.u64_max(), BigInt('18446744073709551615'));

assert.strictEqual(wasm.i64_rust_identity(BigInt('0')), BigInt('0'));
assert.strictEqual(wasm.i64_rust_identity(BigInt('1')), BigInt('1'));
assert.strictEqual(wasm.i64_rust_identity(BigInt('-1')), BigInt('-1'));
assert.strictEqual(wasm.u64_rust_identity(BigInt('0')), BigInt('0'));
assert.strictEqual(wasm.u64_rust_identity(BigInt('1')), BigInt('1'));
assert.strictEqual(wasm.u64_rust_identity(BigInt('1') << BigInt('64')), BigInt('0'));

const u64_max = BigInt('18446744073709551615');
const i64_min = BigInt('-9223372036854775808');
assert.strictEqual(wasm.i64_rust_identity(i64_min), i64_min);
assert.strictEqual(wasm.u64_rust_identity(u64_max), u64_max);
const u64_max = BigInt('18446744073709551615');

const identityTestI64Values = [
BigInt('0'),
BigInt('1'),
BigInt('-1'),
i64_min,
];
for (const value of identityTestI64Values) {
assert.strictEqual(wasm.i64_rust_identity(value), value);
assert.strictEqual(wasm.i64_jsvalue_identity(value), value);
}

const identityTestU64Values = [
BigInt('0'),
BigInt('1'),
u64_max,
];
for (const value of identityTestU64Values) {
assert.strictEqual(wasm.u64_rust_identity(value), value);
assert.strictEqual(wasm.u64_jsvalue_identity(value), value);
}

assert.strictEqual(wasm.u64_rust_identity(BigInt('1') << BigInt('64')), BigInt('0'));
assert.strictEqual(wasm.i128_min_jsvalue(), BigInt('-170141183460469231731687303715884105728'));
assert.strictEqual(wasm.u128_max_jsvalue(), BigInt('340282366920938463463374607431768211455'));

assert.deepStrictEqual(wasm.u64_slice([]), new BigUint64Array());
assert.deepStrictEqual(wasm.i64_slice([]), new BigInt64Array());
Expand Down
22 changes: 21 additions & 1 deletion tests/wasm/u64.rs → tests/wasm/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/u64.js")]
#[wasm_bindgen(module = "tests/wasm/bigint.js")]
extern "C" {
fn i64_js_identity(a: i64) -> i64;
fn u64_js_identity(a: u64) -> u64;
Expand Down Expand Up @@ -53,6 +53,26 @@ pub fn u64_rust_identity(a: u64) -> u64 {
u64_js_identity(a)
}

#[wasm_bindgen]
pub fn i64_jsvalue_identity(a: i64) -> JsValue {
JsValue::from(a)
}

#[wasm_bindgen]
pub fn u64_jsvalue_identity(a: u64) -> JsValue {
JsValue::from(a)
}

#[wasm_bindgen]
pub fn i128_min_jsvalue() -> JsValue {
JsValue::from(i128::min_value())
}

#[wasm_bindgen]
pub fn u128_max_jsvalue() -> JsValue {
JsValue::from(u128::max_value())
}

#[wasm_bindgen]
pub fn i64_slice(a: &[i64]) -> Vec<i64> {
a.to_vec()
Expand Down
2 changes: 1 addition & 1 deletion tests/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use wasm_bindgen::prelude::*;

pub mod api;
pub mod arg_names;
pub mod bigint;
pub mod char;
pub mod classes;
pub mod closures;
Expand Down Expand Up @@ -43,7 +44,6 @@ pub mod simple;
pub mod slice;
pub mod structural;
pub mod truthy_falsy;
pub mod u64;
pub mod usize;
pub mod validate_prt;
pub mod variadic;
Expand Down