diff --git a/test/addons-napi/test_number/test.js b/test/addons-napi/test_number/test.js index e92c6d0cd29951..11cf6f6b7478a6 100644 --- a/test/addons-napi/test_number/test.js +++ b/test/addons-napi/test_number/test.js @@ -45,3 +45,10 @@ 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)); + +// 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)); diff --git a/test/addons-napi/test_number/test_number.c b/test/addons-napi/test_number/test_number.c index 3707f1ee57d713..a1a70950083324 100644 --- a/test/addons-napi/test_number/test_number.c +++ b/test/addons-napi/test_number/test_number.c @@ -45,10 +45,33 @@ napi_value TestInt32Truncation(napi_env env, napi_callback_info info) { return output; } +napi_value TestInt64Truncation(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); + + napi_valuetype valuetype0; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); + + NAPI_ASSERT(env, valuetype0 == napi_number, + "Wrong type of arguments. Expects a number as first argument."); + + int64_t input; + NAPI_CALL(env, napi_get_value_int64(env, args[0], &input)); + + napi_value output; + NAPI_CALL(env, napi_create_int64(env, input, &output)); + + return output; +} + napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Test", Test), DECLARE_NAPI_PROPERTY("TestInt32Truncation", TestInt32Truncation), + DECLARE_NAPI_PROPERTY("TestInt64Truncation", TestInt64Truncation), }; NAPI_CALL(env, napi_define_properties(