Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e304c42
test: add unhandled rejection guard
babygoat Nov 23, 2017
887dcb9
test: remove literals that obscure assert messages
Trott Dec 13, 2017
bba1442
n-api: fix memory leak in napi_async_destroy()
Dec 17, 2017
bcbb7af
test: remove ambiguous error messages from test_error
nadrane Dec 21, 2017
6bf3769
doc: updates examples to use NULL
mhdawson Jan 5, 2018
f81330f
n-api: throw RangeError in napi_create_dataview() with invalid range
romandev Dec 26, 2017
6d23c24
n-api: expose n-api version in process.versions
mhdawson Jan 9, 2018
40ce083
doc: napi: fix unbalanced emphasis
ofrobots Jan 12, 2018
621117e
doc: napi: make header style consistent
ofrobots Jan 12, 2018
111678b
n-api: throw RangeError napi_create_typedarray()
romandev Jan 8, 2018
94618a5
test: fixed typos in napi test
furstenheim Jan 14, 2018
375d043
doc: remove uannecessary Require
mhdawson Jan 16, 2018
f7fb0c3
timers: allow Immediates to be unrefed
apapirovski Jan 13, 2018
23fd460
test: refactor addons-napi/test_exception/test.js
Trott Jan 24, 2018
021e4a4
n-api: change assert ok check to notStrictEqual.
nbdaaron Jan 27, 2018
bf96235
test: show pending exception error in napi tests
blairwilcox Jan 27, 2018
17862cd
n-api: implement wrapping using private properties
Jan 23, 2018
29bbf0c
n-api: wrap control flow macro in do/while
bnoordhuis Feb 2, 2018
6619a1b
doc: remove usage of you in n-api doc
mhdawson Feb 2, 2018
8923ad5
doc: small typo in n-api.md
iSkore Feb 4, 2018
ed8ac32
n-api: add methods to open/close callback scope
mhdawson Dec 13, 2017
01b28fa
n-api: remove extra reference from test
Feb 3, 2018
d2f0672
doc: fix typo in n-api.md
vsemozhetbyt Feb 6, 2018
9c20b87
test: improve error message output
bshankar Feb 1, 2018
ef50323
test: convert new tests to use error types
jackhorton Feb 5, 2018
c4592e6
doc: fix exporting a function example
Feb 8, 2018
5ab4d31
doc: mark NAPI_AUTO_LENGTH as code
tniessen Feb 10, 2018
a1df466
test: remove unnecessary timer
cjihrig Feb 11, 2018
eb85cd8
n-api: fix object test
Feb 27, 2018
2b3207d
doc: fix n-api asynchronous threading docs
ebickle Mar 1, 2018
7a80a8d
n-api: update reference test
Mar 2, 2018
de2d3a6
n-api: update documentation
Mar 1, 2018
a92796e
n-api: resolve promise in test
Mar 8, 2018
2cfd4fe
n-api,test: add a new.target test to addons-napi
boingoing Jan 17, 2018
4a0fc8a
n-api,test: add int64 bounds tests
kfarnung Mar 12, 2018
ff968b7
doc: fix typos on n-api
shama Mar 16, 2018
01790d4
n-api: add missing exception checking
mhdawson Mar 15, 2018
a19b829
n-api: separate out async_hooks test
Mar 16, 2018
9b1b810
n-api: add napi_fatal_exception
mafintosh Mar 14, 2018
3a50835
n-api: re-write test_make_callback
Mar 19, 2018
b25dec8
doc: Add a missing comma
leaves4j Mar 23, 2018
c33daa4
n-api: bump version of n-api supported
mhdawson Mar 20, 2018
65b06de
n-api: ensure in-module exceptions are propagated
Mar 22, 2018
e3a2737
n-api: back up env before finalize
Apr 1, 2018
d73e802
n-api: add more `int64_t` tests
kfarnung Mar 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
n-api: add more int64_t tests
* Updated tests for `Number` and `int32_t`
* Added new tests for `int64_t`
* Updated N-API `int64_t` behavior to return zero for all non-finite
  numbers
* Clarified the documentation for these calls.

PR-URL: #19402
Refs: nodejs/node-chakracore#500
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
kfarnung authored and Gabriel Schulhof committed Apr 12, 2018
commit d73e802c52ea0b746d7f7403490ad08ec528addf
27 changes: 20 additions & 7 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1808,13 +1808,17 @@ napi_status napi_get_value_int32(napi_env env,
- `[out] result`: C int32 primitive equivalent of the given JavaScript Number.

Returns `napi_ok` if the API succeeded. If a non-number `napi_value`
is passed in `napi_number_expected .
is passed in `napi_number_expected`.

This API returns the C int32 primitive equivalent
of the given JavaScript Number. If the number exceeds the range of the
32 bit integer, then the result is truncated to the equivalent of the
bottom 32 bits. This can result in a large positive number becoming
a negative number if the value is > 2^31 -1.
of the given JavaScript Number.

If the number exceeds the range of the 32 bit integer, then the result is
truncated to the equivalent of the bottom 32 bits. This can result in a large
positive number becoming a negative number if the value is > 2^31 -1.

Non-finite number values (NaN, positive infinity, or negative infinity) set the
result to zero.

#### napi_get_value_int64
<!-- YAML
Expand All @@ -1833,8 +1837,17 @@ napi_status napi_get_value_int64(napi_env env,
Returns `napi_ok` if the API succeeded. If a non-number `napi_value`
is passed in it returns `napi_number_expected`.

This API returns the C int64 primitive equivalent of the given
JavaScript Number.
This API returns the C int64 primitive equivalent of the given JavaScript
Number.

Number values outside the range of
[`Number.MIN_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.min_safe_integer)
-(2^53 - 1) -
[`Number.MAX_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.max_safe_integer)
(2^53 - 1) will lose precision.

Non-finite number values (NaN, positive infinity, or negative infinity) set the
result to zero.

#### napi_get_value_string_latin1
<!-- YAML
Expand Down
11 changes: 6 additions & 5 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2197,15 +2197,16 @@ napi_status napi_get_value_int64(napi_env env,

RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected);

// v8::Value::IntegerValue() converts NaN to INT64_MIN, inconsistent with
// v8::Value::Int32Value() that converts NaN to 0. So special-case NaN here.
// v8::Value::IntegerValue() converts NaN, +Inf, and -Inf to INT64_MIN,
// inconsistent with v8::Value::Int32Value() which converts those values to 0.
// Special-case all non-finite values to match that behavior.
double doubleValue = val.As<v8::Number>()->Value();
if (std::isnan(doubleValue)) {
*result = 0;
} else {
if (std::isfinite(doubleValue)) {
// Empty context: https://github.com/nodejs/node/issues/14379
v8::Local<v8::Context> context;
*result = val->IntegerValue(context).FromJust();
} else {
*result = 0;
}

return napi_clear_last_error(env);
Expand Down
135 changes: 99 additions & 36 deletions test/addons-napi/test_number/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,113 @@ const test_number = require(`./build/${common.buildType}/test_number`);


// testing api calls for number
assert.strictEqual(0, test_number.Test(0));
assert.strictEqual(1, test_number.Test(1));
assert.strictEqual(-1, test_number.Test(-1));
assert.strictEqual(100, test_number.Test(100));
assert.strictEqual(2121, test_number.Test(2121));
assert.strictEqual(-1233, test_number.Test(-1233));
assert.strictEqual(986583, test_number.Test(986583));
assert.strictEqual(-976675, test_number.Test(-976675));
function testNumber(num) {
assert.strictEqual(num, test_number.Test(num));
}

const num1 = 98765432213456789876546896323445679887645323232436587988766545658;
assert.strictEqual(num1, test_number.Test(num1));
testNumber(0);
testNumber(-0);
testNumber(1);
testNumber(-1);
testNumber(100);
testNumber(2121);
testNumber(-1233);
testNumber(986583);
testNumber(-976675);

const num2 = -4350987086545760976737453646576078997096876957864353245245769809;
assert.strictEqual(num2, test_number.Test(num2));
testNumber(
98765432213456789876546896323445679887645323232436587988766545658);
testNumber(
-4350987086545760976737453646576078997096876957864353245245769809);
testNumber(Number.MIN_SAFE_INTEGER);
testNumber(Number.MAX_SAFE_INTEGER);
testNumber(Number.MAX_SAFE_INTEGER + 10);

const num3 = Number.MAX_SAFE_INTEGER;
assert.strictEqual(num3, test_number.Test(num3));
testNumber(Number.MIN_VALUE);
testNumber(Number.MAX_VALUE);
testNumber(Number.MAX_VALUE + 10);

const num4 = Number.MAX_SAFE_INTEGER + 10;
assert.strictEqual(num4, test_number.Test(num4));
testNumber(Number.POSITIVE_INFINITY);
testNumber(Number.NEGATIVE_INFINITY);
assert(Object.is(NaN, test_number.Test(NaN)));

const num5 = Number.MAX_VALUE;
assert.strictEqual(num5, test_number.Test(num5));
// validate documented behavior when value is retrieved as 32-bit integer with
// `napi_get_value_int32`
function testInt32(input, expected = input) {
assert.strictEqual(expected, test_number.TestInt32Truncation(input));
}

const num6 = Number.MAX_VALUE + 10;
assert.strictEqual(num6, test_number.Test(num6));
// Test zero
testInt32(0.0, 0);
testInt32(-0.0, 0);

const num7 = Number.POSITIVE_INFINITY;
assert.strictEqual(num7, test_number.Test(num7));
// Test min/max int32 range
testInt32(-Math.pow(2, 31));
testInt32(Math.pow(2, 31) - 1);

const num8 = Number.NEGATIVE_INFINITY;
assert.strictEqual(num8, test_number.Test(num8));
// Test overflow scenarios
testInt32(4294967297, 1);
testInt32(4294967296, 0);
testInt32(4294967295, -1);
testInt32(4294967296 * 5 + 3, 3);

// Test min/max safe integer range
testInt32(Number.MIN_SAFE_INTEGER, 1);
testInt32(Number.MAX_SAFE_INTEGER, -1);

// validate documented behaviour when value is retrieved
// as 32 bit integer with napi_get_value_int32
assert.strictEqual(1, test_number.TestInt32Truncation(4294967297));
assert.strictEqual(0, test_number.TestInt32Truncation(4294967296));
assert.strictEqual(-1, test_number.TestInt32Truncation(4294967295));
assert.strictEqual(3, test_number.TestInt32Truncation(4294967296 * 5 + 3));
// Test within int64_t range (with precision loss)
testInt32(-Math.pow(2, 63) + (Math.pow(2, 9) + 1), 1024);
testInt32(Math.pow(2, 63) - (Math.pow(2, 9) + 1), -1024);

// validate that the boundaries of safe integer can be passed through
// successfully
assert.strictEqual(Number.MAX_SAFE_INTEGER,
test_number.TestInt64Truncation(Number.MAX_SAFE_INTEGER));
assert.strictEqual(Number.MIN_SAFE_INTEGER,
test_number.TestInt64Truncation(Number.MIN_SAFE_INTEGER));
// Test min/max double value
testInt32(-Number.MIN_VALUE, 0);
testInt32(Number.MIN_VALUE, 0);
testInt32(-Number.MAX_VALUE, 0);
testInt32(Number.MAX_VALUE, 0);

// Test outside int64_t range
testInt32(-Math.pow(2, 63) + (Math.pow(2, 9)), 0);
testInt32(Math.pow(2, 63) - (Math.pow(2, 9)), 0);

// Test non-finite numbers
testInt32(Number.POSITIVE_INFINITY, 0);
testInt32(Number.NEGATIVE_INFINITY, 0);
testInt32(Number.NaN, 0);

// validate documented behavior when value is retrieved as 64-bit integer with
// `napi_get_value_int64`
function testInt64(input, expected = input) {
assert.strictEqual(expected, test_number.TestInt64Truncation(input));
}

// Both V8 and ChakraCore return a sentinel value of `0x8000000000000000` when
// the conversion goes out of range, but V8 treats it as unsigned in some cases.
const RANGEERROR_POSITIVE = Math.pow(2, 63);
const RANGEERROR_NEGATIVE = -Math.pow(2, 63);

// Test zero
testInt64(0.0, 0);
testInt64(-0.0, 0);

// Test min/max safe integer range
testInt64(Number.MIN_SAFE_INTEGER);
testInt64(Number.MAX_SAFE_INTEGER);

// Test within int64_t range (with precision loss)
testInt64(-Math.pow(2, 63) + (Math.pow(2, 9) + 1));
testInt64(Math.pow(2, 63) - (Math.pow(2, 9) + 1));

// Test min/max double value
testInt64(-Number.MIN_VALUE, 0);
testInt64(Number.MIN_VALUE, 0);
testInt64(-Number.MAX_VALUE, RANGEERROR_NEGATIVE);
testInt64(Number.MAX_VALUE, RANGEERROR_POSITIVE);

// Test outside int64_t range
testInt64(-Math.pow(2, 63) + (Math.pow(2, 9)), RANGEERROR_NEGATIVE);
testInt64(Math.pow(2, 63) - (Math.pow(2, 9)), RANGEERROR_POSITIVE);

// Test non-finite numbers
testInt64(Number.POSITIVE_INFINITY, 0);
testInt64(Number.NEGATIVE_INFINITY, 0);
testInt64(Number.NaN, 0);