diff --git a/crates/napi/src/bindgen_runtime/js_values/bigint.rs b/crates/napi/src/bindgen_runtime/js_values/bigint.rs index 67256488..5261e137 100644 --- a/crates/napi/src/bindgen_runtime/js_values/bigint.rs +++ b/crates/napi/src/bindgen_runtime/js_values/bigint.rs @@ -205,6 +205,15 @@ impl ToNapiValue for isize { } } +impl From for BigInt { + fn from(val: i64) -> Self { + BigInt { + sign_bit: val < 0, + words: vec![val as u64], + } + } +} + impl From for BigInt { fn from(val: u64) -> Self { BigInt { @@ -213,3 +222,24 @@ impl From for BigInt { } } } + +impl From 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 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() }, + } + } +} diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index d104c6fb..76385fc9 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -34,6 +34,8 @@ Generated by [AVA](https://avajs.dev). export function createBigInt(): bigint␊ export function createBigIntI64(): bigint␊ export function bigintGetU64AsString(bi: bigint): string␊ + export function bigintFromI64(): bigint␊ + export function bigintFromI128(): bigint␊ export function getCwd(callback: (arg0: string) => 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␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 22ff5dba..375b4dee 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index b6f38072..1747067c 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -114,6 +114,8 @@ import { plusOne, Width, captureErrorInCallback, + bigintFromI128, + bigintFromI64, } from '../' test('export const', (t) => { @@ -679,6 +681,11 @@ BigIntTest('js mod test', (t) => { 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 Napi4Test('call thread safe function', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 70132707..aab94c85 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -24,6 +24,8 @@ export function bigintAdd(a: bigint, b: bigint): bigint export function createBigInt(): bigint export function createBigIntI64(): bigint export function bigintGetU64AsString(bi: bigint): string +export function bigintFromI64(): bigint +export function bigintFromI128(): bigint export function getCwd(callback: (arg0: string) => 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 diff --git a/examples/napi/src/bigint.rs b/examples/napi/src/bigint.rs index 6962c76e..b6e30b67 100644 --- a/examples/napi/src/bigint.rs +++ b/examples/napi/src/bigint.rs @@ -22,3 +22,13 @@ fn create_big_int_i64() -> i64n { pub fn bigint_get_u64_as_string(bi: BigInt) -> 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) +}