diff --git a/crates/napi/src/bindgen_runtime/js_values/bigint.rs b/crates/napi/src/bindgen_runtime/js_values/bigint.rs index 42427a8f..8a87764e 100644 --- a/crates/napi/src/bindgen_runtime/js_values/bigint.rs +++ b/crates/napi/src/bindgen_runtime/js_values/bigint.rs @@ -64,6 +64,9 @@ impl FromNapiValue for BigInt { words.set_len(word_count as usize); } + if word_count == 0 { + words = vec![0]; + } Ok(BigInt { sign_bit: sign_bit == 1, words, diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 5ff1e771..42f0cda7 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -31,6 +31,7 @@ Generated by [AVA](https://avajs.dev). export function bigintAdd(a: bigint, b: bigint): bigint␊ export function createBigInt(): bigint␊ export function createBigIntI64(): bigint␊ + export function bigintGetU64AsString(bi: bigint): string␊ 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 b3edb4fa..dae368ec 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 919e1367..16141148 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -43,6 +43,7 @@ import { bigintAdd, createBigInt, createBigIntI64, + bigintGetU64AsString, callThreadsafeFunction, threadsafeFunctionThrowError, asyncPlus100, @@ -423,6 +424,10 @@ BigIntTest('create BigInt i64', (t) => { t.is(createBigIntI64(), BigInt(100)) }) +BigIntTest('BigInt get_u64', (t) => { + t.is(bigintGetU64AsString(BigInt(0)), '0') +}) + BigIntTest('js mod test', (t) => { t.is(xxh64Alias(Buffer.from('hello world')), BigInt('1116')) t.is(xxh3.xxh3_64(Buffer.from('hello world')), BigInt('1116')) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 7c0e8945..21138f03 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -21,6 +21,7 @@ export function asyncMultiTwo(arg: number): Promise export function bigintAdd(a: bigint, b: bigint): bigint export function createBigInt(): bigint export function createBigIntI64(): bigint +export function bigintGetU64AsString(bi: bigint): string 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 80decf96..6962c76e 100644 --- a/examples/napi/src/bigint.rs +++ b/examples/napi/src/bigint.rs @@ -17,3 +17,8 @@ fn create_big_int() -> BigInt { fn create_big_int_i64() -> i64n { i64n(100) } + +#[napi] +pub fn bigint_get_u64_as_string(bi: BigInt) -> String { + bi.get_u64().1.to_string() +}