Fix BigInt issue with zero value

Looks like 0 is a special case: 4318b2348d/deps/v8/src/objects/bigint.cc (L1595-L1602)
This commit is contained in:
messense 2022-02-04 16:03:32 +08:00
parent ac25965ad3
commit d3e37a44cb
6 changed files with 15 additions and 0 deletions

View file

@ -64,6 +64,9 @@ impl FromNapiValue for BigInt {
words.set_len(word_count as usize); words.set_len(word_count as usize);
} }
if word_count == 0 {
words = vec![0];
}
Ok(BigInt { Ok(BigInt {
sign_bit: sign_bit == 1, sign_bit: sign_bit == 1,
words, words,

View file

@ -31,6 +31,7 @@ Generated by [AVA](https://avajs.dev).
export function bigintAdd(a: bigint, b: bigint): bigint␊ 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 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␊

View file

@ -43,6 +43,7 @@ import {
bigintAdd, bigintAdd,
createBigInt, createBigInt,
createBigIntI64, createBigIntI64,
bigintGetU64AsString,
callThreadsafeFunction, callThreadsafeFunction,
threadsafeFunctionThrowError, threadsafeFunctionThrowError,
asyncPlus100, asyncPlus100,
@ -423,6 +424,10 @@ BigIntTest('create BigInt i64', (t) => {
t.is(createBigIntI64(), BigInt(100)) t.is(createBigIntI64(), BigInt(100))
}) })
BigIntTest('BigInt get_u64', (t) => {
t.is(bigintGetU64AsString(BigInt(0)), '0')
})
BigIntTest('js mod test', (t) => { BigIntTest('js mod test', (t) => {
t.is(xxh64Alias(Buffer.from('hello world')), BigInt('1116')) t.is(xxh64Alias(Buffer.from('hello world')), BigInt('1116'))
t.is(xxh3.xxh3_64(Buffer.from('hello world')), BigInt('1116')) t.is(xxh3.xxh3_64(Buffer.from('hello world')), BigInt('1116'))

View file

@ -21,6 +21,7 @@ export function asyncMultiTwo(arg: number): Promise<number>
export function bigintAdd(a: bigint, b: bigint): bigint 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 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

View file

@ -17,3 +17,8 @@ fn create_big_int() -> BigInt {
fn create_big_int_i64() -> i64n { fn create_big_int_i64() -> i64n {
i64n(100) i64n(100)
} }
#[napi]
pub fn bigint_get_u64_as_string(bi: BigInt) -> String {
bi.get_u64().1.to_string()
}