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/node_api.h | |
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/node_api.h')
-rw-r--r-- | src/node_api.h | 17 |
1 files changed, 17 insertions, 0 deletions
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)); \ |