-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node-api,src: fix module registration in MSVC C++
PR-URL: #42459 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Backport-PR-URL: #43293
- Loading branch information
1 parent
b8a8c83
commit 31ee218
Showing
6 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_init_order", | ||
"sources": [ "test_init_order.cc" ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
// This test verifies that C++ static variable dynamic initialization is called | ||
// correctly and does not interfere with the module initialization. | ||
const common = require('../../common'); | ||
const test_init_order = require(`./build/${common.buildType}/test_init_order`); | ||
const assert = require('assert'); | ||
|
||
assert.strictEqual(test_init_order.cppIntValue, 42); | ||
assert.strictEqual(test_init_order.cppStringValue, '123'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <node_api.h> | ||
#include <memory> | ||
#include <string> | ||
#include "../../js-native-api/common.h" | ||
|
||
// This test verifies that use of the NAPI_MODULE in C++ code does not | ||
// interfere with the C++ dynamic static initializers. | ||
|
||
namespace { | ||
|
||
// This class uses dynamic static initializers for the test. | ||
// In production code developers must avoid dynamic static initializers because | ||
// they affect the start up time. They must prefer static initialization such as | ||
// use of constexpr functions or classes with constexpr constructors. E.g. | ||
// instead of using std::string, it is preferrable to use const char[], or | ||
// constexpr std::string_view starting with C++17, or even constexpr | ||
// std::string starting with C++20. | ||
struct MyClass { | ||
static const std::unique_ptr<int> valueHolder; | ||
static const std::string testString; | ||
}; | ||
|
||
const std::unique_ptr<int> MyClass::valueHolder = | ||
std::unique_ptr<int>(new int(42)); | ||
// NOLINTNEXTLINE(runtime/string) | ||
const std::string MyClass::testString = std::string("123"); | ||
|
||
} // namespace | ||
|
||
EXTERN_C_START | ||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_value cppIntValue, cppStringValue; | ||
NODE_API_CALL(env, | ||
napi_create_int32(env, *MyClass::valueHolder, &cppIntValue)); | ||
NODE_API_CALL( | ||
env, | ||
napi_create_string_utf8( | ||
env, MyClass::testString.c_str(), NAPI_AUTO_LENGTH, &cppStringValue)); | ||
|
||
napi_property_descriptor descriptors[] = { | ||
DECLARE_NODE_API_PROPERTY_VALUE("cppIntValue", cppIntValue), | ||
DECLARE_NODE_API_PROPERTY_VALUE("cppStringValue", cppStringValue)}; | ||
|
||
NODE_API_CALL( | ||
env, | ||
napi_define_properties(env, | ||
exports, | ||
sizeof(descriptors) / sizeof(descriptors[0]), | ||
descriptors)); | ||
|
||
return exports; | ||
} | ||
EXTERN_C_END | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |