From f5c5be3c3c1153b02301f8706fc15cc45efab033 Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Mon, 12 Mar 2018 13:36:29 -0700 Subject: [PATCH] n-api,test: add int64 test and fix JSRT Fixed a couple build warnings as well. PR-URL: https://github.com/nodejs/node-chakracore/pull/496 Reviewed-By: Taylor Woll Reviewed-By: Jimmy Thomson Reviewed-By: Hitesh Kanwathirtha --- src/node_api_jsrt.cc | 49 +++++++--------------- test/addons-napi/test_number/test.js | 7 ++++ test/addons-napi/test_number/test_number.c | 23 ++++++++++ 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/node_api_jsrt.cc b/src/node_api_jsrt.cc index 95c37a49e93..d7f259e4573 100755 --- a/src/node_api_jsrt.cc +++ b/src/node_api_jsrt.cc @@ -109,10 +109,6 @@ namespace v8impl { //=== Conversion between V8 Isolate and napi_env ========================== -napi_env JsEnvFromV8Isolate(v8::Isolate* isolate) { - return reinterpret_cast(isolate); -} - v8::Isolate* V8IsolateFromJsEnv(napi_env e) { return reinterpret_cast(e); } @@ -137,26 +133,6 @@ class EscapableHandleScopeWrapper { v8::EscapableHandleScope scope; }; -napi_handle_scope JsHandleScopeFromV8HandleScope(HandleScopeWrapper* s) { - return reinterpret_cast(s); -} - -HandleScopeWrapper* V8HandleScopeFromJsHandleScope(napi_handle_scope s) { - return reinterpret_cast(s); -} - -napi_escapable_handle_scope - JsEscapableHandleScopeFromV8EscapableHandleScope( - EscapableHandleScopeWrapper* s) { - return reinterpret_cast(s); -} - -EscapableHandleScopeWrapper* - V8EscapableHandleScopeFromJsEscapableHandleScope( - napi_escapable_handle_scope s) { - return reinterpret_cast(s); -} - //=== Conversion between V8 Handles and napi_value ======================== // This is assuming v8::Local<> will always be implemented with a single @@ -552,9 +528,6 @@ void napi_module_register_cb(v8::Local exports, // Registers a NAPI module. void napi_module_register(napi_module* mod) { - // NAPI modules always work with the current node version. - int module_version = NODE_MODULE_VERSION; - node::node_module* nm = new node::node_module { -1, mod->nm_flags, @@ -651,14 +624,16 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location, size_t message_len) { const char* location_string = location; const char* message_string = message; - if (location_len != -1) { + + if (location_len != NAPI_AUTO_LENGTH) { char* location_nullterminated = static_cast( malloc((location_len + 1) * sizeof(char))); strncpy(location_nullterminated, location, location_len); location_nullterminated[location_len] = 0; location_string = location_nullterminated; } - if (message_len != -1) { + + if (message_len != NAPI_AUTO_LENGTH) { char* message_nullterminated = static_cast( malloc((message_len + 1) * sizeof(char))); strncpy(message_nullterminated, message, message_len); @@ -688,7 +663,7 @@ napi_status napi_create_function(napi_env env, if (utf8name != nullptr) { CHECK_JSRT(JsCreateString( utf8name, - length == -1 ? strlen(utf8name) : length, + length == NAPI_AUTO_LENGTH ? strlen(utf8name) : length, &name)); } @@ -1512,9 +1487,17 @@ napi_status napi_get_value_uint32(napi_env env, napi_status napi_get_value_int64(napi_env env, napi_value v, int64_t* result) { CHECK_ARG(result); JsValueRef value = reinterpret_cast(v); - int valueInt; - CHECK_JSRT_EXPECTED(JsNumberToInt(value, &valueInt), napi_number_expected); - *result = static_cast(valueInt); + + double valueDouble; + CHECK_JSRT_EXPECTED(JsNumberToDouble(value, &valueDouble), + napi_number_expected); + + if (std::isnan(valueDouble)) { + *result = 0; + } else { + *result = static_cast(valueDouble); + } + return napi_ok; } diff --git a/test/addons-napi/test_number/test.js b/test/addons-napi/test_number/test.js index b8ce6b7f995..f9d6eb6a6f3 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 3707f1ee57d..a1a70950083 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(