From 07e9df476c2c11d419286933da4204733163e74a Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Mon, 11 Sep 2017 09:57:38 -0400 Subject: [PATCH 01/10] n-api: add optional string length parameters --- src/node_api.cc | 10 +++++++--- src/node_api.h | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 724d5c3ee96701..3e25f408149d5a 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -933,12 +933,15 @@ napi_status napi_get_last_error_info(napi_env env, } NAPI_NO_RETURN void napi_fatal_error(const char* location, - const char* message) { + size_t location_len, + const char* message, + size_t message_len) { node::FatalError(location, message); } napi_status napi_create_function(napi_env env, const char* utf8name, + size_t length, napi_callback cb, void* callback_data, napi_value* result) { @@ -965,7 +968,7 @@ napi_status napi_create_function(napi_env env, if (utf8name != nullptr) { v8::Local name_string; - CHECK_NEW_FROM_UTF8(env, name_string, utf8name); + CHECK_NEW_FROM_UTF8(env, name_string, utf8name, length); return_value->SetName(name_string); } @@ -976,6 +979,7 @@ napi_status napi_create_function(napi_env env, napi_status napi_define_class(napi_env env, const char* utf8name, + size_t length, napi_callback constructor, void* callback_data, size_t property_count, @@ -997,7 +1001,7 @@ napi_status napi_define_class(napi_env env, isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata); v8::Local name_string; - CHECK_NEW_FROM_UTF8(env, name_string, utf8name); + CHECK_NEW_FROM_UTF8(env, name_string, utf8name, length); tpl->SetClassName(name_string); size_t static_property_count = 0; diff --git a/src/node_api.h b/src/node_api.h index e1a40983a9907d..82f2e1900e2fe4 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -109,7 +109,9 @@ napi_get_last_error_info(napi_env env, const napi_extended_error_info** result); NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, - const char* message); + size_t location_len, + const char* message, + size_t message_len); // Getters for defined singletons NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result); @@ -154,6 +156,7 @@ NAPI_EXTERN napi_status napi_create_symbol(napi_env env, napi_value* result); NAPI_EXTERN napi_status napi_create_function(napi_env env, const char* utf8name, + size_t length, napi_callback cb, void* data, napi_value* result); @@ -336,6 +339,7 @@ NAPI_EXTERN napi_status napi_get_new_target(napi_env env, NAPI_EXTERN napi_status napi_define_class(napi_env env, const char* utf8name, + size_t length, napi_callback constructor, void* data, size_t property_count, From 53c4e81dd02da10bf53962e001f7297beac4aa57 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Mon, 11 Sep 2017 10:04:57 -0400 Subject: [PATCH 02/10] napi: update doc for additional string length parameter --- doc/api/n-api.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 00bba55a4c9e3d..50e71d7b572d43 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -552,11 +552,18 @@ thrown to immediately terminate the process. added: v8.2.0 --> ```C -NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message); +NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, + size_t location_len, + const char* message, + size_t message_len); ``` - `[in] location`: Optional location at which the error occurred. +- `[in] location_len`: The length of the location in bytes, or -1 if it is +null-terminated. - `[in] message`: The message associated with the error. +- `[in] message_len`: The length of the message in bytes, or -1 if it is +null-terminated. The function call does not return, the process will be terminated. @@ -1248,6 +1255,7 @@ added: v8.0.0 ```C napi_status napi_create_function(napi_env env, const char* utf8name, + size_t length, napi_callback cb, void* data, napi_value* result) @@ -1256,6 +1264,8 @@ napi_status napi_create_function(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] utf8name`: A string representing the name of the function encoded as UTF8. +- `[in] length`: The length of the utf8name in bytes, or -1 if it is +null-terminated. - `[in] cb`: A function pointer to the native function to be invoked when the created function is invoked from JavaScript. - `[in] data`: Optional arbitrary context data to be passed into the native @@ -3026,6 +3036,7 @@ added: v8.0.0 ```C napi_status napi_define_class(napi_env env, const char* utf8name, + size_t length, napi_callback constructor, void* data, size_t property_count, @@ -3037,6 +3048,8 @@ napi_status napi_define_class(napi_env env, - `[in] utf8name`: Name of the JavaScript constructor function; this is not required to be the same as the C++ class name, though it is recommended for clarity. + - `[in] length`: The length of the utf8name in bytes, or -1 if it is +null-terminated. - `[in] constructor`: Callback function that handles constructing instances of the class. (This should be a static method on the class, not an actual C++ constructor function.) From 7a7dc8b2f701bd86aea79ecaf149e6623d7f1dd1 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Mon, 11 Sep 2017 10:28:59 -0400 Subject: [PATCH 03/10] n-api: fix build errors --- src/node_api.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 3e25f408149d5a..1c9e712c74320f 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -968,7 +968,7 @@ napi_status napi_create_function(napi_env env, if (utf8name != nullptr) { v8::Local name_string; - CHECK_NEW_FROM_UTF8(env, name_string, utf8name, length); + CHECK_NEW_FROM_UTF8_LEN(env, name_string, utf8name, length); return_value->SetName(name_string); } @@ -1001,7 +1001,7 @@ napi_status napi_define_class(napi_env env, isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata); v8::Local name_string; - CHECK_NEW_FROM_UTF8(env, name_string, utf8name, length); + CHECK_NEW_FROM_UTF8_LEN(env, name_string, utf8name, length); tpl->SetClassName(name_string); size_t static_property_count = 0; From ef1af4add53c4e291271a840ac06558b9969bf0a Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Mon, 11 Sep 2017 10:29:45 -0400 Subject: [PATCH 04/10] test: update test for additional string length parameter --- test/addons-napi/5_function_factory/binding.c | 4 +--- test/addons-napi/6_object_wrap/myobject.cc | 1 + test/addons-napi/7_factory_wrap/myobject.cc | 2 +- test/addons-napi/8_passing_wrapped/myobject.cc | 2 +- test/addons-napi/test_constructor/test_constructor.c | 4 ++-- test/addons-napi/test_fatal/test_fatal.c | 2 +- test/addons-napi/test_function/test_function.c | 4 ++-- test/addons-napi/test_make_callback/binding.cc | 2 +- test/addons-napi/test_make_callback_recurse/binding.cc | 2 +- 9 files changed, 11 insertions(+), 12 deletions(-) diff --git a/test/addons-napi/5_function_factory/binding.c b/test/addons-napi/5_function_factory/binding.c index c8751f1fa918a7..95f12bfd151c0b 100644 --- a/test/addons-napi/5_function_factory/binding.c +++ b/test/addons-napi/5_function_factory/binding.c @@ -4,15 +4,13 @@ napi_value MyFunction(napi_env env, napi_callback_info info) { napi_value str; NAPI_CALL(env, napi_create_string_utf8(env, "hello world", -1, &str)); - return str; } napi_value CreateFunction(napi_env env, napi_callback_info info) { napi_value fn; NAPI_CALL(env, - napi_create_function(env, "theFunction", MyFunction, NULL, &fn)); - + napi_create_function(env, "theFunction", -1, MyFunction, NULL, &fn)); return fn; } diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index 90815253add651..5dec35d33a771b 100644 --- a/test/addons-napi/6_object_wrap/myobject.cc +++ b/test/addons-napi/6_object_wrap/myobject.cc @@ -24,6 +24,7 @@ void MyObject::Init(napi_env env, napi_value exports) { napi_value cons; NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons)); + napi_define_class(env, "MyObject", -1, New, nullptr, 3, properties, &cons)); NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, cons, 1, &constructor)); diff --git a/test/addons-napi/7_factory_wrap/myobject.cc b/test/addons-napi/7_factory_wrap/myobject.cc index c6d538a7cefbc6..d40f9a0d1a5656 100644 --- a/test/addons-napi/7_factory_wrap/myobject.cc +++ b/test/addons-napi/7_factory_wrap/myobject.cc @@ -22,7 +22,7 @@ napi_status MyObject::Init(napi_env env) { napi_value cons; status = - napi_define_class(env, "MyObject", New, nullptr, 1, properties, &cons); + napi_define_class(env, "MyObject", -1, New, nullptr, 1, properties, &cons); if (status != napi_ok) return status; status = napi_create_reference(env, cons, 1, &constructor); diff --git a/test/addons-napi/8_passing_wrapped/myobject.cc b/test/addons-napi/8_passing_wrapped/myobject.cc index 0c24d7696e66ec..20609b24286ce3 100644 --- a/test/addons-napi/8_passing_wrapped/myobject.cc +++ b/test/addons-napi/8_passing_wrapped/myobject.cc @@ -17,7 +17,7 @@ napi_status MyObject::Init(napi_env env) { napi_status status; napi_value cons; - status = napi_define_class(env, "MyObject", New, nullptr, 0, nullptr, &cons); + status = napi_define_class(env, "MyObject", -1, New, nullptr, 0, nullptr, &cons); if (status != napi_ok) return status; status = napi_create_reference(env, cons, 1, &constructor); diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 73de5ad2877740..759c2ea1903b24 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -77,8 +77,8 @@ napi_value Init(napi_env env, napi_value exports) { }; napi_value cons; - NAPI_CALL(env, napi_define_class(env, "MyObject", New, - NULL, sizeof(properties)/sizeof(*properties), properties, &cons)); + NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "MyObject", -1, New, + NULL, sizeof(properties)/sizeof(*properties), properties, &cons)); NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_)); diff --git a/test/addons-napi/test_fatal/test_fatal.c b/test/addons-napi/test_fatal/test_fatal.c index 78d791cc91f0a3..aa9698a64c55ea 100644 --- a/test/addons-napi/test_fatal/test_fatal.c +++ b/test/addons-napi/test_fatal/test_fatal.c @@ -2,7 +2,7 @@ #include "../common.h" napi_value Test(napi_env env, napi_callback_info info) { - napi_fatal_error("test_fatal::Test", "fatal message"); + napi_fatal_error("test_fatal::Test", -1, "fatal message", -1); return NULL; } diff --git a/test/addons-napi/test_function/test_function.c b/test/addons-napi/test_function/test_function.c index 76a8b05e036f32..78548752a8cb51 100644 --- a/test/addons-napi/test_function/test_function.c +++ b/test/addons-napi/test_function/test_function.c @@ -28,9 +28,9 @@ napi_value Test(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { napi_value fn; - NAPI_CALL(env, napi_create_function(env, NULL, Test, NULL, &fn)); + NAPI_CALL(env, napi_create_function(env, NULL, -1, Test, NULL, &fn)); NAPI_CALL(env, napi_set_named_property(env, exports, "Test", fn)); - return exports; +>>>>>>> test: update test for additional string length parameter } NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/test/addons-napi/test_make_callback/binding.cc b/test/addons-napi/test_make_callback/binding.cc index df2c4dc672ea47..8a6d94985c1962 100644 --- a/test/addons-napi/test_make_callback/binding.cc +++ b/test/addons-napi/test_make_callback/binding.cc @@ -45,7 +45,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { napi_value fn; - NAPI_CALL(env, napi_create_function(env, NULL, MakeCallback, NULL, &fn)); + NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn)); NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn)); return exports; } diff --git a/test/addons-napi/test_make_callback_recurse/binding.cc b/test/addons-napi/test_make_callback_recurse/binding.cc index 1e0c16c80ee664..212c9ec4022fa3 100644 --- a/test/addons-napi/test_make_callback_recurse/binding.cc +++ b/test/addons-napi/test_make_callback_recurse/binding.cc @@ -20,7 +20,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { napi_value fn; - NAPI_CALL(env, napi_create_function(env, NULL, MakeCallback, NULL, &fn)); + NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn)); NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn)); return exports; } From 8762a3c38f918e8c41cf9547c3e95152c05e358c Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Thu, 14 Sep 2017 22:27:47 -0400 Subject: [PATCH 05/10] Handle non-zero terminated string in napi_fatal_error --- src/node_api.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index 1c9e712c74320f..d2d76c318de5a7 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -936,7 +936,19 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location, size_t location_len, const char* message, size_t message_len) { - node::FatalError(location, message); + char* location_string = const_cast(location); + char* message_string = const_cast(message); + if (location_len != -1) { + location_string = (char*) malloc(location_len * sizeof(char) + 1); + strncpy(location_string, location, location_len); + location_string[location_len] = '\0'; + } + if (message_len != -1) { + message_string = (char*) malloc(message_len * sizeof(char) + 1); + strncpy(message_string, message, message_len); + message_string[message_len] = '\0'; + } + node::FatalError(location_string, message_string); } napi_status napi_create_function(napi_env env, From c0b202c5a18ac594f1ca2e2b2f981ad676da7bb0 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Thu, 14 Sep 2017 22:28:34 -0400 Subject: [PATCH 06/10] Add tests for non-zero termianted string input --- test/addons-napi/test_constructor/binding.gyp | 4 +++ test/addons-napi/test_constructor/test2.js | 8 ++++++ .../test_constructor/test_constructor_name.c | 25 +++++++++++++++++++ test/addons-napi/test_fatal/test2.js | 18 +++++++++++++ test/addons-napi/test_fatal/test_fatal.c | 6 +++++ test/addons-napi/test_function/test.js | 10 +++++--- .../addons-napi/test_function/test_function.c | 25 ++++++++++++++----- 7 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 test/addons-napi/test_constructor/test2.js create mode 100644 test/addons-napi/test_constructor/test_constructor_name.c create mode 100644 test/addons-napi/test_fatal/test2.js diff --git a/test/addons-napi/test_constructor/binding.gyp b/test/addons-napi/test_constructor/binding.gyp index 55140e7c379f02..1945a9fd5a711e 100644 --- a/test/addons-napi/test_constructor/binding.gyp +++ b/test/addons-napi/test_constructor/binding.gyp @@ -3,6 +3,10 @@ { "target_name": "test_constructor", "sources": [ "test_constructor.c" ] + }, + { + "target_name": "test_constructor_name", + "sources": [ "test_constructor_name.c" ] } ] } diff --git a/test/addons-napi/test_constructor/test2.js b/test/addons-napi/test_constructor/test2.js new file mode 100644 index 00000000000000..1f4a990e9897c9 --- /dev/null +++ b/test/addons-napi/test_constructor/test2.js @@ -0,0 +1,8 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); + +// Testing api calls for a constructor that defines properties +const TestConstructor = + require(`./build/${common.buildType}/test_constructor_name`); +assert.equal(TestConstructor.name, 'MyObject'); diff --git a/test/addons-napi/test_constructor/test_constructor_name.c b/test/addons-napi/test_constructor/test_constructor_name.c new file mode 100644 index 00000000000000..0e61892ece260e --- /dev/null +++ b/test/addons-napi/test_constructor/test_constructor_name.c @@ -0,0 +1,25 @@ +#include +#include "../common.h" + +napi_ref constructor_; + +napi_value New(napi_env env, napi_callback_info info) { + napi_value _this; + NAPI_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL)); + + return _this; +} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_value cons; + NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "MyObject_Extra", 8, New, + NULL, 0, NULL, &cons)); + + NAPI_CALL_RETURN_VOID(env, + napi_set_named_property(env, module, "exports", cons)); + + NAPI_CALL_RETURN_VOID(env, + napi_create_reference(env, cons, 1, &constructor_)); +} + +NAPI_MODULE(addon, Init) diff --git a/test/addons-napi/test_fatal/test2.js b/test/addons-napi/test_fatal/test2.js new file mode 100644 index 00000000000000..b9bde8f13016cc --- /dev/null +++ b/test/addons-napi/test_fatal/test2.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); +const child_process = require('child_process'); +const test_fatal = require(`./build/${common.buildType}/test_fatal`); + +// Test in a child process because the test code will trigger a fatal error +// that crashes the process. +if (process.argv[2] === 'child') { + test_fatal.TestStringLength(); + return; +} + +const p = child_process.spawnSync( + process.execPath, [ '--napi-modules', __filename, 'child' ]); +assert.ifError(p.error); +assert.ok(p.stderr.toString().includes( + 'FATAL ERROR: test_fatal::Test fatal message')); diff --git a/test/addons-napi/test_fatal/test_fatal.c b/test/addons-napi/test_fatal/test_fatal.c index aa9698a64c55ea..6d8fa6ff8613f2 100644 --- a/test/addons-napi/test_fatal/test_fatal.c +++ b/test/addons-napi/test_fatal/test_fatal.c @@ -6,9 +6,15 @@ napi_value Test(napi_env env, napi_callback_info info) { return NULL; } +napi_value TestStringLength(napi_env env, napi_callback_info info) { + napi_fatal_error("test_fatal::TestStringLength", 16, "fatal message", 13); + return NULL; +} + napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("Test", Test), + DECLARE_NAPI_PROPERTY("TestStringLength", TestStringLength), }; NAPI_CALL(env, napi_define_properties( diff --git a/test/addons-napi/test_function/test.js b/test/addons-napi/test_function/test.js index bdb9133adf9346..e18a5ff45e504e 100644 --- a/test/addons-napi/test_function/test.js +++ b/test/addons-napi/test_function/test.js @@ -9,20 +9,22 @@ const test_function = require(`./build/${common.buildType}/test_function`); function func1() { return 1; } -assert.strictEqual(test_function.Test(func1), 1); +assert.strictEqual(test_function.TestCall(func1), 1); function func2() { console.log('hello world!'); return null; } -assert.strictEqual(test_function.Test(func2), null); +assert.strictEqual(test_function.TestCall(func2), null); function func3(input) { return input + 1; } -assert.strictEqual(test_function.Test(func3, 1), 2); +assert.strictEqual(test_function.TestCall(func3, 1), 2); function func4(input) { return func3(input); } -assert.strictEqual(test_function.Test(func4, 1), 2); +assert.strictEqual(test_function.TestCall(func4, 1), 2); +assert.strictEqual(test_function.TestName.name, 'Name'); +assert.strictEqual(test_function.TestNameShort.name, 'Name_'); diff --git a/test/addons-napi/test_function/test_function.c b/test/addons-napi/test_function/test_function.c index 78548752a8cb51..749c0616f50028 100644 --- a/test/addons-napi/test_function/test_function.c +++ b/test/addons-napi/test_function/test_function.c @@ -1,7 +1,7 @@ #include #include "../common.h" -napi_value Test(napi_env env, napi_callback_info info) { +napi_value TestCallFunction(napi_env env, napi_callback_info info) { size_t argc = 10; napi_value args[10]; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); @@ -26,11 +26,24 @@ napi_value Test(napi_env env, napi_callback_info info) { return result; } -napi_value Init(napi_env env, napi_value exports) { - napi_value fn; - NAPI_CALL(env, napi_create_function(env, NULL, -1, Test, NULL, &fn)); - NAPI_CALL(env, napi_set_named_property(env, exports, "Test", fn)); ->>>>>>> test: update test for additional string length parameter +void TestFunctionName(napi_env env, napi_callback_info info) {} + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_value fn1; + NAPI_CALL(env, napi_create_function( + env, NULL, -1, TestCallFunction, NULL, &fn1)); + + napi_value fn2; + NAPI_CALL(env, napi_create_function( + env, "Name", -1, TestFunctionName, NULL, &fn2)); + + napi_value fn3; + NAPI_CALL(env, napi_create_function( + env, "Name_extra", 5, TestFunctionName, NULL, &fn3)); + + NAPI_CALL(env, napi_set_named_property(env, exports, "TestCall", fn1)); + NAPI_CALL(env, napi_set_named_property(env, exports, "TestName", fn2)); + NAPI_CALL(env, napi_set_named_property(env, exports, "TestNameShort", fn3)); } NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) From cf0f4fa0eb45bb8606f11d4603684fea504d23fd Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Fri, 15 Sep 2017 00:34:45 -0400 Subject: [PATCH 07/10] Fix errors after rebasing --- test/addons-napi/4_object_factory/binding.c | 2 +- test/addons-napi/5_function_factory/binding.c | 2 +- test/addons-napi/6_object_wrap/myobject.cc | 3 +-- test/addons-napi/7_factory_wrap/binding.cc | 2 +- .../test_constructor/test_constructor.c | 2 +- .../test_constructor/test_constructor_name.c | 14 ++++++-------- test/addons-napi/test_env_sharing/compare_env.c | 2 +- test/addons-napi/test_function/test.js | 1 + 8 files changed, 13 insertions(+), 15 deletions(-) diff --git a/test/addons-napi/4_object_factory/binding.c b/test/addons-napi/4_object_factory/binding.c index dedfd288f7ec0f..38b8ec8e1cab48 100644 --- a/test/addons-napi/4_object_factory/binding.c +++ b/test/addons-napi/4_object_factory/binding.c @@ -16,7 +16,7 @@ napi_value CreateObject(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, - napi_create_function(env, "exports", CreateObject, NULL, &exports)); + napi_create_function(env, "exports", -1, CreateObject, NULL, &exports)); return exports; } diff --git a/test/addons-napi/5_function_factory/binding.c b/test/addons-napi/5_function_factory/binding.c index 95f12bfd151c0b..8cc41f6aac5c3d 100644 --- a/test/addons-napi/5_function_factory/binding.c +++ b/test/addons-napi/5_function_factory/binding.c @@ -16,7 +16,7 @@ napi_value CreateFunction(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, - napi_create_function(env, "exports", CreateFunction, NULL, &exports)); + napi_create_function(env, "exports", -1, CreateFunction, NULL, &exports)); return exports; } diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index 5dec35d33a771b..af706274375512 100644 --- a/test/addons-napi/6_object_wrap/myobject.cc +++ b/test/addons-napi/6_object_wrap/myobject.cc @@ -23,8 +23,7 @@ void MyObject::Init(napi_env env, napi_value exports) { napi_value cons; NAPI_CALL_RETURN_VOID(env, - napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons)); - napi_define_class(env, "MyObject", -1, New, nullptr, 3, properties, &cons)); + napi_define_class(env, "MyObject", -1, New, nullptr, 3, properties, &cons)); NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, cons, 1, &constructor)); diff --git a/test/addons-napi/7_factory_wrap/binding.cc b/test/addons-napi/7_factory_wrap/binding.cc index 551bfef15dbc87..c8fca4d536e74c 100644 --- a/test/addons-napi/7_factory_wrap/binding.cc +++ b/test/addons-napi/7_factory_wrap/binding.cc @@ -16,7 +16,7 @@ napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, MyObject::Init(env)); NAPI_CALL(env, - napi_create_function(env, "exports", CreateObject, NULL, &exports)); + napi_create_function(env, "exports", -1, CreateObject, NULL, &exports)); return exports; } diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 759c2ea1903b24..526a19249c5736 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -78,7 +78,7 @@ napi_value Init(napi_env env, napi_value exports) { napi_value cons; NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "MyObject", -1, New, - NULL, sizeof(properties)/sizeof(*properties), properties, &cons)); + NULL, sizeof(properties)/sizeof(*properties), properties, &cons)); NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_)); diff --git a/test/addons-napi/test_constructor/test_constructor_name.c b/test/addons-napi/test_constructor/test_constructor_name.c index 0e61892ece260e..b178e80f49f489 100644 --- a/test/addons-napi/test_constructor/test_constructor_name.c +++ b/test/addons-napi/test_constructor/test_constructor_name.c @@ -10,16 +10,14 @@ napi_value New(napi_env env, napi_callback_info info) { return _this; } -void Init(napi_env env, napi_value exports, napi_value module, void* priv) { +napi_value Init(napi_env env, napi_value exports) { napi_value cons; - NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "MyObject_Extra", 8, New, - NULL, 0, NULL, &cons)); + NAPI_CALL(env, napi_define_class( + env, "MyObject_Extra", 8, New, NULL, 0, NULL, &cons)); - NAPI_CALL_RETURN_VOID(env, - napi_set_named_property(env, module, "exports", cons)); - - NAPI_CALL_RETURN_VOID(env, - napi_create_reference(env, cons, 1, &constructor_)); + NAPI_CALL(env, + napi_create_reference(env, cons, 1, &constructor_)); + return cons; } NAPI_MODULE(addon, Init) diff --git a/test/addons-napi/test_env_sharing/compare_env.c b/test/addons-napi/test_env_sharing/compare_env.c index b7e789ac499317..da984b68f53f3e 100644 --- a/test/addons-napi/test_env_sharing/compare_env.c +++ b/test/addons-napi/test_env_sharing/compare_env.c @@ -15,7 +15,7 @@ napi_value compare(napi_env env, napi_callback_info info) { } napi_value Init(napi_env env, napi_value exports) { - NAPI_CALL(env, napi_create_function(env, "exports", compare, NULL, &exports)); + NAPI_CALL(env, napi_create_function(env, "exports", -1, compare, NULL, &exports)); return exports; } diff --git a/test/addons-napi/test_function/test.js b/test/addons-napi/test_function/test.js index e18a5ff45e504e..752e9965b23039 100644 --- a/test/addons-napi/test_function/test.js +++ b/test/addons-napi/test_function/test.js @@ -26,5 +26,6 @@ function func4(input) { return func3(input); } assert.strictEqual(test_function.TestCall(func4, 1), 2); + assert.strictEqual(test_function.TestName.name, 'Name'); assert.strictEqual(test_function.TestNameShort.name, 'Name_'); From 90ef0517be1fa234228e76c843f86c814f9f2b1a Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Fri, 15 Sep 2017 10:14:32 -0400 Subject: [PATCH 08/10] Fix CI run fail --- test/addons-napi/test_constructor/test_constructor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 526a19249c5736..d742f2bcd1707b 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -77,7 +77,7 @@ napi_value Init(napi_env env, napi_value exports) { }; napi_value cons; - NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "MyObject", -1, New, + NAPI_CALL(env, napi_define_class(env, "MyObject", -1, New, NULL, sizeof(properties)/sizeof(*properties), properties, &cons)); NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_)); From b0175aeaef0cd4cbc8b6b4f62a05faaaf177797f Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Fri, 15 Sep 2017 10:35:22 -0400 Subject: [PATCH 09/10] Fix remaining rebasing difference --- doc/api/n-api.md | 2 +- test/addons-napi/test_function/test_function.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 50e71d7b572d43..42db6e3a9e9535 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -552,7 +552,7 @@ thrown to immediately terminate the process. added: v8.2.0 --> ```C -NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, +NAPI_NO_RETURN void napi_fatal_error(const char* location, size_t location_len, const char* message, size_t message_len); diff --git a/test/addons-napi/test_function/test_function.c b/test/addons-napi/test_function/test_function.c index 749c0616f50028..99a699bfea0c87 100644 --- a/test/addons-napi/test_function/test_function.c +++ b/test/addons-napi/test_function/test_function.c @@ -28,7 +28,7 @@ napi_value TestCallFunction(napi_env env, napi_callback_info info) { void TestFunctionName(napi_env env, napi_callback_info info) {} -void Init(napi_env env, napi_value exports, napi_value module, void* priv) { +napi_value Init(napi_env env, napi_value exports) { napi_value fn1; NAPI_CALL(env, napi_create_function( env, NULL, -1, TestCallFunction, NULL, &fn1)); @@ -44,6 +44,8 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { NAPI_CALL(env, napi_set_named_property(env, exports, "TestCall", fn1)); NAPI_CALL(env, napi_set_named_property(env, exports, "TestName", fn2)); NAPI_CALL(env, napi_set_named_property(env, exports, "TestNameShort", fn3)); + + return exports; } NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) From 8e7bf4730a804600ec1abe690557539d128c7b67 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Mon, 18 Sep 2017 11:34:27 -0400 Subject: [PATCH 10/10] Fix linter error --- src/node_api.cc | 6 ++++-- test/addons-napi/6_object_wrap/myobject.cc | 4 ++-- test/addons-napi/7_factory_wrap/myobject.cc | 4 ++-- test/addons-napi/8_passing_wrapped/myobject.cc | 3 ++- test/addons-napi/test_constructor/test2.js | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index d2d76c318de5a7..cedf5ad7a458eb 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -939,12 +939,14 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location, char* location_string = const_cast(location); char* message_string = const_cast(message); if (location_len != -1) { - location_string = (char*) malloc(location_len * sizeof(char) + 1); + location_string = reinterpret_cast( + malloc(location_len * sizeof(char) + 1)); strncpy(location_string, location, location_len); location_string[location_len] = '\0'; } if (message_len != -1) { - message_string = (char*) malloc(message_len * sizeof(char) + 1); + message_string = reinterpret_cast( + malloc(message_len * sizeof(char) + 1)); strncpy(message_string, message, message_len); message_string[message_len] = '\0'; } diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index af706274375512..aca91877d3a2ae 100644 --- a/test/addons-napi/6_object_wrap/myobject.cc +++ b/test/addons-napi/6_object_wrap/myobject.cc @@ -22,8 +22,8 @@ void MyObject::Init(napi_env env, napi_value exports) { }; napi_value cons; - NAPI_CALL_RETURN_VOID(env, - napi_define_class(env, "MyObject", -1, New, nullptr, 3, properties, &cons)); + NAPI_CALL_RETURN_VOID(env, napi_define_class( + env, "MyObject", -1, New, nullptr, 3, properties, &cons)); NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, cons, 1, &constructor)); diff --git a/test/addons-napi/7_factory_wrap/myobject.cc b/test/addons-napi/7_factory_wrap/myobject.cc index d40f9a0d1a5656..4e1d79c1febc17 100644 --- a/test/addons-napi/7_factory_wrap/myobject.cc +++ b/test/addons-napi/7_factory_wrap/myobject.cc @@ -21,8 +21,8 @@ napi_status MyObject::Init(napi_env env) { }; napi_value cons; - status = - napi_define_class(env, "MyObject", -1, New, nullptr, 1, properties, &cons); + status = napi_define_class( + env, "MyObject", -1, New, nullptr, 1, properties, &cons); if (status != napi_ok) return status; status = napi_create_reference(env, cons, 1, &constructor); diff --git a/test/addons-napi/8_passing_wrapped/myobject.cc b/test/addons-napi/8_passing_wrapped/myobject.cc index 20609b24286ce3..19cc7dd2a29493 100644 --- a/test/addons-napi/8_passing_wrapped/myobject.cc +++ b/test/addons-napi/8_passing_wrapped/myobject.cc @@ -17,7 +17,8 @@ napi_status MyObject::Init(napi_env env) { napi_status status; napi_value cons; - status = napi_define_class(env, "MyObject", -1, New, nullptr, 0, nullptr, &cons); + status = napi_define_class( + env, "MyObject", -1, New, nullptr, 0, nullptr, &cons); if (status != napi_ok) return status; status = napi_create_reference(env, cons, 1, &constructor); diff --git a/test/addons-napi/test_constructor/test2.js b/test/addons-napi/test_constructor/test2.js index 1f4a990e9897c9..64c03cbc684ac3 100644 --- a/test/addons-napi/test_constructor/test2.js +++ b/test/addons-napi/test_constructor/test2.js @@ -5,4 +5,4 @@ const assert = require('assert'); // Testing api calls for a constructor that defines properties const TestConstructor = require(`./build/${common.buildType}/test_constructor_name`); -assert.equal(TestConstructor.name, 'MyObject'); +assert.strictEqual(TestConstructor.name, 'MyObject');