Skip to content

Commit 4241577

Browse files
committed
test: test doc'd napi_get_value_int32 behaviour
We chose to document this in the docs as there are different possible behaviours. Adding this test to validate that all vm implementations do it the same way. PR-URL: #12633 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent bda34bd commit 4241577

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

test/addons-napi/test_number/test.js

+8
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ assert.strictEqual(num7, test_number.Test(num7));
3737

3838
const num8 = Number.NEGATIVE_INFINITY;
3939
assert.strictEqual(num8, test_number.Test(num8));
40+
41+
42+
// validate documented behaviour when value is retrieved
43+
// as 32 bit integer with napi_get_value_int32
44+
assert.strictEqual(1, test_number.TestInt32Truncation(4294967297));
45+
assert.strictEqual(0, test_number.TestInt32Truncation(4294967296));
46+
assert.strictEqual(-1, test_number.TestInt32Truncation(4294967295));
47+
assert.strictEqual(3, test_number.TestInt32Truncation(4294967296 * 5 + 3));

test/addons-napi/test_number/test_number.c

+23
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,32 @@ napi_value Test(napi_env env, napi_callback_info info) {
2323
return output;
2424
}
2525

26+
napi_value TestInt32Truncation(napi_env env, napi_callback_info info) {
27+
size_t argc = 1;
28+
napi_value args[1];
29+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
30+
31+
NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
32+
33+
napi_valuetype valuetype0;
34+
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
35+
36+
NAPI_ASSERT(env, valuetype0 == napi_number,
37+
"Wrong type of arguments. Expects a number as first argument.");
38+
39+
int32_t input;
40+
NAPI_CALL(env, napi_get_value_int32(env, args[0], &input));
41+
42+
napi_value output;
43+
NAPI_CALL(env, napi_create_number(env, input, &output));
44+
45+
return output;
46+
}
47+
2648
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
2749
napi_property_descriptor descriptors[] = {
2850
DECLARE_NAPI_PROPERTY("Test", Test),
51+
DECLARE_NAPI_PROPERTY("TestInt32Truncation", TestInt32Truncation),
2952
};
3053

3154
NAPI_CALL_RETURN_VOID(env, napi_define_properties(

0 commit comments

Comments
 (0)