mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
node-api,src: fix module registration in MSVC C++
PR-URL: https://github.com/nodejs/node/pull/42459 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
This commit is contained in:
parent
646e057680
commit
44fdf953ba
@ -827,11 +827,13 @@ extern "C" NODE_EXTERN void node_module_register(void* mod);
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma section(".CRT$XCU", read)
|
||||
#define NODE_C_CTOR(fn) \
|
||||
NODE_CTOR_PREFIX void __cdecl fn(void); \
|
||||
__declspec(dllexport, allocate(".CRT$XCU")) \
|
||||
void (__cdecl*fn ## _)(void) = fn; \
|
||||
namespace { \
|
||||
struct fn##_ { \
|
||||
fn##_() { fn(); }; \
|
||||
} fn##_v_; \
|
||||
} \
|
||||
NODE_CTOR_PREFIX void __cdecl fn(void)
|
||||
#else
|
||||
#define NODE_C_CTOR(fn) \
|
||||
|
@ -44,12 +44,29 @@ typedef struct napi_module {
|
||||
#define NAPI_MODULE_VERSION 1
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#if defined(__cplusplus)
|
||||
#define NAPI_C_CTOR(fn) \
|
||||
static void __cdecl fn(void); \
|
||||
namespace { \
|
||||
struct fn##_ { \
|
||||
fn##_() { fn(); } \
|
||||
} fn##_v_; \
|
||||
} \
|
||||
static void __cdecl fn(void)
|
||||
#else // !defined(__cplusplus)
|
||||
#pragma section(".CRT$XCU", read)
|
||||
// The NAPI_C_CTOR macro defines a function fn that is called during CRT
|
||||
// initialization.
|
||||
// C does not support dynamic initialization of static variables and this code
|
||||
// simulates C++ behavior. Exporting the function pointer prevents it from being
|
||||
// optimized. See for details:
|
||||
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170
|
||||
#define NAPI_C_CTOR(fn) \
|
||||
static void __cdecl fn(void); \
|
||||
__declspec(dllexport, allocate(".CRT$XCU")) void(__cdecl * fn##_)(void) = \
|
||||
fn; \
|
||||
static void __cdecl fn(void)
|
||||
#endif // defined(__cplusplus)
|
||||
#else
|
||||
#define NAPI_C_CTOR(fn) \
|
||||
static void fn(void) __attribute__((constructor)); \
|
||||
|
@ -62,6 +62,9 @@
|
||||
#define DECLARE_NODE_API_GETTER(name, func) \
|
||||
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }
|
||||
|
||||
#define DECLARE_NODE_API_PROPERTY_VALUE(name, value) \
|
||||
{ (name), NULL, NULL, NULL, NULL, (value), napi_default, NULL }
|
||||
|
||||
void add_returned_status(napi_env env,
|
||||
const char* key,
|
||||
napi_value object,
|
||||
|
8
test/node-api/test_init_order/binding.gyp
Normal file
8
test/node-api/test_init_order/binding.gyp
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "test_init_order",
|
||||
"sources": [ "test_init_order.cc" ]
|
||||
}
|
||||
]
|
||||
}
|
10
test/node-api/test_init_order/test.js
Normal file
10
test/node-api/test_init_order/test.js
Normal file
@ -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');
|
52
test/node-api/test_init_order/test_init_order.cc
Normal file
52
test/node-api/test_init_order/test_init_order.cc
Normal file
@ -0,0 +1,52 @@
|
||||
#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, std::size(descriptors), descriptors));
|
||||
|
||||
return exports;
|
||||
}
|
||||
EXTERN_C_END
|
||||
|
||||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
|
Loading…
Reference in New Issue
Block a user