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

Commit 2b4f308

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 <tawoll@ntdev.microsoft.com> Reviewed-By: Jimmy Thomson <jithomso@microsoft.com> Reviewed-By: Hitesh Kanwathirtha <hiteshk@microsoft.com>
1 parent d67ed11 commit 2b4f308

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

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,
@@ -652,14 +625,16 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location,
652625
size_t message_len) {
653626
const char* location_string = location;
654627
const char* message_string = message;
655-
if (location_len != -1) {
628+
629+
if (location_len != NAPI_AUTO_LENGTH) {
656630
char* location_nullterminated = static_cast<char*>(
657631
malloc((location_len + 1) * sizeof(char)));
658632
strncpy(location_nullterminated, location, location_len);
659633
location_nullterminated[location_len] = 0;
660634
location_string = location_nullterminated;
661635
}
662-
if (message_len != -1) {
636+
637+
if (message_len != NAPI_AUTO_LENGTH) {
663638
char* message_nullterminated = static_cast<char*>(
664639
malloc((message_len + 1) * sizeof(char)));
665640
strncpy(message_nullterminated, message, message_len);
@@ -689,7 +664,7 @@ napi_status napi_create_function(napi_env env,
689664
if (utf8name != nullptr) {
690665
CHECK_JSRT(JsCreateString(
691666
utf8name,
692-
length == -1 ? strlen(utf8name) : length,
667+
length == NAPI_AUTO_LENGTH ? strlen(utf8name) : length,
693668
&name));
694669
}
695670

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

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)