Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
n-api,test: add int64 test and fix JSRT
Browse files Browse the repository at this point in the history
Fixed a couple build warnings as well.

PR-URL: #496
Reviewed-By: Taylor Woll <tawoll@ntdev.microsoft.com>
Reviewed-By: Jimmy Thomson <jithomso@microsoft.com>
Reviewed-By: Hitesh Kanwathirtha <hiteshk@microsoft.com>
  • Loading branch information
kfarnung committed Mar 13, 2018
1 parent 37ef3a9 commit f5c5be3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
49 changes: 16 additions & 33 deletions src/node_api_jsrt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ namespace v8impl {

//=== Conversion between V8 Isolate and napi_env ==========================

napi_env JsEnvFromV8Isolate(v8::Isolate* isolate) {
return reinterpret_cast<napi_env>(isolate);
}

v8::Isolate* V8IsolateFromJsEnv(napi_env e) {
return reinterpret_cast<v8::Isolate*>(e);
}
Expand All @@ -137,26 +133,6 @@ class EscapableHandleScopeWrapper {
v8::EscapableHandleScope scope;
};

napi_handle_scope JsHandleScopeFromV8HandleScope(HandleScopeWrapper* s) {
return reinterpret_cast<napi_handle_scope>(s);
}

HandleScopeWrapper* V8HandleScopeFromJsHandleScope(napi_handle_scope s) {
return reinterpret_cast<HandleScopeWrapper*>(s);
}

napi_escapable_handle_scope
JsEscapableHandleScopeFromV8EscapableHandleScope(
EscapableHandleScopeWrapper* s) {
return reinterpret_cast<napi_escapable_handle_scope>(s);
}

EscapableHandleScopeWrapper*
V8EscapableHandleScopeFromJsEscapableHandleScope(
napi_escapable_handle_scope s) {
return reinterpret_cast<EscapableHandleScopeWrapper*>(s);
}

//=== Conversion between V8 Handles and napi_value ========================

// This is assuming v8::Local<> will always be implemented with a single
Expand Down Expand Up @@ -552,9 +528,6 @@ void napi_module_register_cb(v8::Local<v8::Object> 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,
Expand Down Expand Up @@ -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<char*>(
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<char*>(
malloc((message_len + 1) * sizeof(char)));
strncpy(message_nullterminated, message, message_len);
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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<JsValueRef>(v);
int valueInt;
CHECK_JSRT_EXPECTED(JsNumberToInt(value, &valueInt), napi_number_expected);
*result = static_cast<int64_t>(valueInt);

double valueDouble;
CHECK_JSRT_EXPECTED(JsNumberToDouble(value, &valueDouble),
napi_number_expected);

if (std::isnan(valueDouble)) {
*result = 0;
} else {
*result = static_cast<int64_t>(valueDouble);
}

return napi_ok;
}

Expand Down
7 changes: 7 additions & 0 deletions test/addons-napi/test_number/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
23 changes: 23 additions & 0 deletions test/addons-napi/test_number/test_number.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f5c5be3

Please sign in to comment.