From d3e37a44cb2fde4689dd7cae574d3abcd121500f Mon Sep 17 00:00:00 2001 From: messense Date: Fri, 4 Feb 2022 16:03:32 +0800 Subject: [PATCH] Fix BigInt issue with zero value Looks like 0 is a special case: https://github.com/nodejs/node/blob/4318b2348dbcd5003e0c4a14b5fe378cceec3c81/deps/v8/src/objects/bigint.cc#L1595-L1602 --- .../src/bindgen_runtime/js_values/bigint.rs | 3 +++ examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 2519 -> 2538 bytes examples/napi/__test__/values.spec.ts | 5 +++++ examples/napi/index.d.ts | 1 + examples/napi/src/bigint.rs | 5 +++++ 6 files changed, 15 insertions(+) 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 b3edb4fae0fd7734586c469a6acd499db8177487..dae368ecd269ef0664a38d1c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1>0%oV}n_Q!bRJpkE2t z^yUOz958T7c-FjDQ3xpdAjV7qUC$1HuXKN7=jq_@D0FEXPk~Q=B$yet;wOruAb8A+ z%(1`(ZeM5eSDN~CZd1O+#8b*1dFH|Hpox~LZu_v>0Xru$ilFVTsbR|=DUjMzNHvM# z1Jmvp=u0zs0)4ztVh2k@MfWg1(yHujpoA#uE{}t#RQB9*$8M3cT?u$i) zfU&(DI)xpTlVyy5$sz;u)4tOV)?h#oK)TXbTbrg4$-BraNQl|e(g(ZQE*;B2XlXTO z=+t!54t&P@D3a7+o%9l>VHfE2aF7Ah|KC1C`1U6xI}5u3Df#xQTR=D1c$2!K=ZU{c8wfgXRzoZYYv z!rk6TGNu!sbr}oyTFxt5MZ4x2wK1#00px!Vzb!d^L&gI+m{4{6|FJ5?pyM3acQVIw zs!rF2cXoF5huxe3Ecs5Ci;uLICOTqut#HTWelo%LD1qQa8~DFKeE<8mnQ#^C?t;cc zRMH`A-R+dBQAb8L3?2?tXaC&-jdVi{ahIdoBOVWO2$w@5y>@m-I zF-?>;C%f@kbf+3E{;pET-h4^y}uuB8OWI@j`q9bH{H|2ZqG>t6%UA8KkaPSy&l>q2cV73Z>LBm vtScx1&|;IL2__-2#&(;o?OcU6@!?^~yI{3V-!FZbeO&oJS|#CAuOI*bH7&5_ 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() +}