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

Commit f5c5be3

Browse files
committed
n-api,test: add int64 test and fix JSRT
Fixed a couple build warnings as well. PR-URL: #496 Reviewed-By: Taylor Woll <[email protected]> Reviewed-By: Jimmy Thomson <[email protected]> Reviewed-By: Hitesh Kanwathirtha <[email protected]>
1 parent 37ef3a9 commit f5c5be3

3 files changed

Lines changed: 46 additions & 33 deletions

File tree

src/node_api_jsrt.cc

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ namespace v8impl {
109109

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

112-
napi_env JsEnvFromV8Isolate(v8::Isolate* isolate) {
113-
return reinterpret_cast<napi_env>(isolate);
114-
}
115-
116112
v8::Isolate* V8IsolateFromJsEnv(napi_env e) {
117113
return reinterpret_cast<v8::Isolate*>(e);
118114
}
@@ -137,26 +133,6 @@ class EscapableHandleScopeWrapper {
137133
v8::EscapableHandleScope scope;
138134
};
139135

140-
napi_handle_scope JsHandleScopeFromV8HandleScope(HandleScopeWrapper* s) {
141-
return reinterpret_cast<napi_handle_scope>(s);
142-
}
143-
144-
HandleScopeWrapper* V8HandleScopeFromJsHandleScope(napi_handle_scope s) {
145-
return reinterpret_cast<HandleScopeWrapper*>(s);
146-
}
147-
148-
napi_escapable_handle_scope
149-
JsEscapableHandleScopeFromV8EscapableHandleScope(
150-
EscapableHandleScopeWrapper* s) {
151-
return reinterpret_cast<napi_escapable_handle_scope>(s);
152-
}
153-
154-
EscapableHandleScopeWrapper*
155-
V8EscapableHandleScopeFromJsEscapableHandleScope(
156-
napi_escapable_handle_scope s) {
157-
return reinterpret_cast<EscapableHandleScopeWrapper*>(s);
158-
}
159-
160136
//=== Conversion between V8 Handles and napi_value ========================
161137

162138
// This is assuming v8::Local<> will always be implemented with a single
@@ -552,9 +528,6 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,
552528

553529
// Registers a NAPI module.
554530
void napi_module_register(napi_module* mod) {
555-
// NAPI modules always work with the current node version.
556-
int module_version = NODE_MODULE_VERSION;
557-
558531
node::node_module* nm = new node::node_module {
559532
-1,
560533
mod->nm_flags,
@@ -651,14 +624,16 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location,
651624
size_t message_len) {
652625
const char* location_string = location;
653626
const char* message_string = message;
654-
if (location_len != -1) {
627+
628+
if (location_len != NAPI_AUTO_LENGTH) {
655629
char* location_nullterminated = static_cast<char*>(
656630
malloc((location_len + 1) * sizeof(char)));
657631
strncpy(location_nullterminated, location, location_len);
658632
location_nullterminated[location_len] = 0;
659633
location_string = location_nullterminated;
660634
}
661-
if (message_len != -1) {
635+
636+
if (message_len != NAPI_AUTO_LENGTH) {
662637
char* message_nullterminated = static_cast<char*>(
663638
malloc((message_len + 1) * sizeof(char)));
664639
strncpy(message_nullterminated, message, message_len);
@@ -688,7 +663,7 @@ napi_status napi_create_function(napi_env env,
688663
if (utf8name != nullptr) {
689664
CHECK_JSRT(JsCreateString(
690665
utf8name,
691-
length == -1 ? strlen(utf8name) : length,
666+
length == NAPI_AUTO_LENGTH ? strlen(utf8name) : length,
692667
&name));
693668
}
694669

@@ -1512,9 +1487,17 @@ napi_status napi_get_value_uint32(napi_env env,
15121487
napi_status napi_get_value_int64(napi_env env, napi_value v, int64_t* result) {
15131488
CHECK_ARG(result);
15141489
JsValueRef value = reinterpret_cast<JsValueRef>(v);
1515-
int valueInt;
1516-
CHECK_JSRT_EXPECTED(JsNumberToInt(value, &valueInt), napi_number_expected);
1517-
*result = static_cast<int64_t>(valueInt);
1490+
1491+
double valueDouble;
1492+
CHECK_JSRT_EXPECTED(JsNumberToDouble(value, &valueDouble),
1493+
napi_number_expected);
1494+
1495+
if (std::isnan(valueDouble)) {
1496+
*result = 0;
1497+
} else {
1498+
*result = static_cast<int64_t>(valueDouble);
1499+
}
1500+
15181501
return napi_ok;
15191502
}
15201503

test/addons-napi/test_number/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ assert.strictEqual(1, test_number.TestInt32Truncation(4294967297));
4545
assert.strictEqual(0, test_number.TestInt32Truncation(4294967296));
4646
assert.strictEqual(-1, test_number.TestInt32Truncation(4294967295));
4747
assert.strictEqual(3, test_number.TestInt32Truncation(4294967296 * 5 + 3));
48+
49+
// validate that the boundaries of safe integer can be passed through
50+
// successfully
51+
assert.strictEqual(Number.MAX_SAFE_INTEGER,
52+
test_number.TestInt64Truncation(Number.MAX_SAFE_INTEGER));
53+
assert.strictEqual(Number.MIN_SAFE_INTEGER,
54+
test_number.TestInt64Truncation(Number.MIN_SAFE_INTEGER));

test/addons-napi/test_number/test_number.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,33 @@ napi_value TestInt32Truncation(napi_env env, napi_callback_info info) {
4545
return output;
4646
}
4747

48+
napi_value TestInt64Truncation(napi_env env, napi_callback_info info) {
49+
size_t argc = 1;
50+
napi_value args[1];
51+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
52+
53+
NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
54+
55+
napi_valuetype valuetype0;
56+
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
57+
58+
NAPI_ASSERT(env, valuetype0 == napi_number,
59+
"Wrong type of arguments. Expects a number as first argument.");
60+
61+
int64_t input;
62+
NAPI_CALL(env, napi_get_value_int64(env, args[0], &input));
63+
64+
napi_value output;
65+
NAPI_CALL(env, napi_create_int64(env, input, &output));
66+
67+
return output;
68+
}
69+
4870
napi_value Init(napi_env env, napi_value exports) {
4971
napi_property_descriptor descriptors[] = {
5072
DECLARE_NAPI_PROPERTY("Test", Test),
5173
DECLARE_NAPI_PROPERTY("TestInt32Truncation", TestInt32Truncation),
74+
DECLARE_NAPI_PROPERTY("TestInt64Truncation", TestInt64Truncation),
5275
};
5376

5477
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)