Stanisław Czech 272334c69f
fix(napi): update logic for handling signed integers (#2356)
* Fix from functions for BigInt

Allow proper parsing of negative integers in those functions.

Fixes the pannic when calling:
BigInt::from(i128::MIN);

* Fix get_i128 and get_i64 for BigInt

Update the getter logic to take sign into account.

for get_i128 and get_u128 make lossless value consistent with the documentation
(documentation described different behavior then the code logic)

* Extend tests for BigInt types

Add more test cases for negative i64 and i128 to test the created changes

* Simplify the to napiValue logic for 128 bit types

Update the logic to handle negative values properly

Refactor to_napi_value and create_bigint_from_*128 to use the same logic
instead of copping mostly the same code 4 times

* Fix specification for deserialize object test

For unknown reason the binary specification was holding different value than spec file
2024-11-18 23:12:11 +08:00

50 lines
1.5 KiB
TypeScript

import test from 'ava'
import { napiVersion } from '../napi-version'
const bindings = require('../../index.node')
test('should create bigints', (t) => {
if (napiVersion >= 6) {
t.is(bindings.testCreateBigintFromI64(), BigInt('9223372036854775807'))
t.is(bindings.testCreateBigintFromMinI64(), BigInt('-9223372036854775808'))
t.is(bindings.testCreateBigintFromNegativeI64(), BigInt('-10'))
t.is(bindings.testCreateBigintFromU64(), BigInt('18446744073709551615'))
t.is(
bindings.testCreateBigintFromI128(),
BigInt('170141183460469231731687303715884105727'),
)
t.is(
bindings.testCreateBigintFromMinI128(),
BigInt('-170141183460469231731687303715884105728'),
)
t.is(
bindings.testCreateBigintFromNegativeI128(),
BigInt('-10'),
)
t.is(
bindings.testCreateBigintFromU128(),
BigInt('340282366920938463463374607431768211455'),
)
t.is(
bindings.testCreateBigintFromWords(),
BigInt('-340282366920938463463374607431768211455'),
)
} else {
t.is(bindings.testCreateBigintFromI64, undefined)
}
})
test('should get integers from bigints', (t) => {
if (napiVersion >= 6) {
t.is(bindings.testGetBigintI64(BigInt('-123')), -123)
t.is(bindings.testGetBigintU64(BigInt(123)), 123)
t.deepEqual(bindings.testGetBigintWords(), [
BigInt('9223372036854775807'),
BigInt('9223372036854775807'),
])
} else {
t.is(bindings.testGetBigintI64, undefined)
}
})