Skip to content

Commit

Permalink
Extract helper
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 4, 2022
1 parent 9aa0978 commit 12ffc2b
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/lib.rs
Expand Up @@ -886,18 +886,18 @@ macro_rules! big_numbers {
)*)
}

fn bigint_get_as_i64(v: &JsValue) -> Option<i64> {
unsafe { Option::from_abi(__wbindgen_bigint_get_as_i64(v.idx)) }
}

macro_rules! try_from_for_num64 {
($ty:ty) => {
impl TryFrom<JsValue> for $ty {
type Error = JsValue;

#[inline]
fn try_from(v: JsValue) -> Result<Self, JsValue> {
if let WasmOptionalI64 {
present: 1,
value: as_i64,
} = unsafe { __wbindgen_bigint_get_as_i64(v.idx) }
{
if let Some(as_i64) = bigint_get_as_i64(&v) {
// Reinterpret bits; ABI-wise this is safe to do and allows us to avoid
// having separate intrinsics per signed/unsigned types.
let as_self = as_i64 as Self;
Expand All @@ -924,13 +924,12 @@ macro_rules! try_from_for_num128 {
#[inline]
fn try_from(v: JsValue) -> Result<Self, JsValue> {
// Truncate the bigint to 64 bits, this will give us the lower part.
let lo = unsafe { __wbindgen_bigint_get_as_i64(v.idx) };
if lo.present == 0 {
let lo = match bigint_get_as_i64(&v) {
// The lower part must be interpreted as unsigned in both i128 and u128.
Some(lo) => lo as u64,
// Not a bigint.
return Err(v);
}
// The lower part must be interpreted as unsigned in both i128 and u128.
let lo = lo.value as u64;
None => return Err(v),
};
// Now we know it's a bigint, so we can safely use `>> 64n` without
// worrying about a JS exception on type mismatch.
let hi = v >> JsValue::from(64_u64);
Expand Down

0 comments on commit 12ffc2b

Please sign in to comment.