diff options
author | Vladimir Morozov <vmorozov@microsoft.com> | 2022-03-24 18:37:35 +0300 |
---|---|---|
committer | Michael Dawson <mdawson@devrus.com> | 2022-04-07 16:40:07 +0300 |
commit | 44fdf953ba435a46a3525bc877069044a3157e7d (patch) | |
tree | e7a1d71b0600d26c8bcc3d25dfcc43aa0646969e /src | |
parent | 646e057680dcf4e639c36b160d271af66a04b157 (diff) |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/node.h | 8 | ||||
-rw-r--r-- | src/node_api.h | 17 |
2 files changed, 22 insertions, 3 deletions
diff --git a/src/node.h b/src/node.h index b96b3ea1e49..dec8dd6805f 100644 --- a/src/node.h +++ b/src/node.h @@ -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) \ diff --git a/src/node_api.h b/src/node_api.h index e8e903b62a8..d95046676a6 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -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)); \ |