fix(napi): missing From implementation for Bigint (#1440)
This commit is contained in:
parent
f14799f0b7
commit
46f08ee6dd
6 changed files with 51 additions and 0 deletions
|
@ -205,6 +205,15 @@ impl ToNapiValue for isize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<i64> for BigInt {
|
||||||
|
fn from(val: i64) -> Self {
|
||||||
|
BigInt {
|
||||||
|
sign_bit: val < 0,
|
||||||
|
words: vec![val as u64],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<u64> for BigInt {
|
impl From<u64> for BigInt {
|
||||||
fn from(val: u64) -> Self {
|
fn from(val: u64) -> Self {
|
||||||
BigInt {
|
BigInt {
|
||||||
|
@ -213,3 +222,24 @@ impl From<u64> for BigInt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<i128> for BigInt {
|
||||||
|
fn from(val: i128) -> Self {
|
||||||
|
let sign_bit = val < 0;
|
||||||
|
let words = (if sign_bit { -val } else { val }).to_ne_bytes();
|
||||||
|
BigInt {
|
||||||
|
sign_bit,
|
||||||
|
words: unsafe { std::slice::from_raw_parts(words.as_ptr() as *mut _, 2).to_vec() },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u128> for BigInt {
|
||||||
|
fn from(val: u128) -> Self {
|
||||||
|
let words = val.to_ne_bytes();
|
||||||
|
BigInt {
|
||||||
|
sign_bit: false,
|
||||||
|
words: unsafe { std::slice::from_raw_parts(words.as_ptr() as *mut _, 2).to_vec() },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -34,6 +34,8 @@ Generated by [AVA](https://avajs.dev).
|
||||||
export function createBigInt(): bigint␊
|
export function createBigInt(): bigint␊
|
||||||
export function createBigIntI64(): bigint␊
|
export function createBigIntI64(): bigint␊
|
||||||
export function bigintGetU64AsString(bi: bigint): string␊
|
export function bigintGetU64AsString(bi: bigint): string␊
|
||||||
|
export function bigintFromI64(): bigint␊
|
||||||
|
export function bigintFromI128(): bigint␊
|
||||||
export function getCwd(callback: (arg0: string) => void): void␊
|
export function getCwd(callback: (arg0: string) => void): void␊
|
||||||
export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void␊
|
export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void␊
|
||||||
export function optionStart(callback: (arg0: string | undefined | null, arg1: string) => void): void␊
|
export function optionStart(callback: (arg0: string | undefined | null, arg1: string) => void): void␊
|
||||||
|
|
Binary file not shown.
|
@ -114,6 +114,8 @@ import {
|
||||||
plusOne,
|
plusOne,
|
||||||
Width,
|
Width,
|
||||||
captureErrorInCallback,
|
captureErrorInCallback,
|
||||||
|
bigintFromI128,
|
||||||
|
bigintFromI64,
|
||||||
} from '../'
|
} from '../'
|
||||||
|
|
||||||
test('export const', (t) => {
|
test('export const', (t) => {
|
||||||
|
@ -679,6 +681,11 @@ BigIntTest('js mod test', (t) => {
|
||||||
t.is(xx3.digest(), BigInt('1116'))
|
t.is(xx3.digest(), BigInt('1116'))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
BigIntTest('from i128 i64', (t) => {
|
||||||
|
t.is(bigintFromI64(), BigInt('100'))
|
||||||
|
t.is(bigintFromI128(), BigInt('-100'))
|
||||||
|
})
|
||||||
|
|
||||||
const Napi4Test = Number(process.versions.napi) >= 4 ? test : test.skip
|
const Napi4Test = Number(process.versions.napi) >= 4 ? test : test.skip
|
||||||
|
|
||||||
Napi4Test('call thread safe function', (t) => {
|
Napi4Test('call thread safe function', (t) => {
|
||||||
|
|
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -24,6 +24,8 @@ export function bigintAdd(a: bigint, b: bigint): bigint
|
||||||
export function createBigInt(): bigint
|
export function createBigInt(): bigint
|
||||||
export function createBigIntI64(): bigint
|
export function createBigIntI64(): bigint
|
||||||
export function bigintGetU64AsString(bi: bigint): string
|
export function bigintGetU64AsString(bi: bigint): string
|
||||||
|
export function bigintFromI64(): bigint
|
||||||
|
export function bigintFromI128(): bigint
|
||||||
export function getCwd(callback: (arg0: string) => void): void
|
export function getCwd(callback: (arg0: string) => void): void
|
||||||
export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void
|
export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void
|
||||||
export function optionStart(callback: (arg0: string | undefined | null, arg1: string) => void): void
|
export function optionStart(callback: (arg0: string | undefined | null, arg1: string) => void): void
|
||||||
|
|
|
@ -22,3 +22,13 @@ fn create_big_int_i64() -> i64n {
|
||||||
pub fn bigint_get_u64_as_string(bi: BigInt) -> String {
|
pub fn bigint_get_u64_as_string(bi: BigInt) -> String {
|
||||||
bi.get_u64().1.to_string()
|
bi.get_u64().1.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn bigint_from_i64() -> BigInt {
|
||||||
|
BigInt::from(100i64)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
pub fn bigint_from_i128() -> BigInt {
|
||||||
|
BigInt::from(-100i128)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue