1 #include <node_api.h> 2 3 // This test uses old module initialization style deprecated in current code. 4 // The goal is to see that all previously compiled code continues to work the 5 // same way as before. 6 // The test has a copy of previous macro definitions which are removed from 7 // the node_api.h file. 8 9 #if defined(_MSC_VER) 10 #if defined(__cplusplus) 11 #define NAPI_C_CTOR(fn) \ 12 static void NAPI_CDECL fn(void); \ 13 namespace { \ 14 struct fn##_ { \ 15 fn##_() { fn(); } \ 16 } fn##_v_; \ 17 } \ 18 static void NAPI_CDECL fn(void) 19 #else // !defined(__cplusplus) 20 #pragma section(".CRT$XCU", read) 21 // The NAPI_C_CTOR macro defines a function fn that is called during CRT 22 // initialization. 23 // C does not support dynamic initialization of static variables and this code 24 // simulates C++ behavior. Exporting the function pointer prevents it from being 25 // optimized. See for details: 26 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170 27 #define NAPI_C_CTOR(fn) \ 28 static void NAPI_CDECL fn(void); \ 29 __declspec(dllexport, allocate(".CRT$XCU")) void(NAPI_CDECL * fn##_)(void) = \ 30 fn; \ 31 static void NAPI_CDECL fn(void) 32 #endif // defined(__cplusplus) 33 #else 34 #define NAPI_C_CTOR(fn) \ 35 static void fn(void) __attribute__((constructor)); \ 36 static void fn(void) 37 #endif 38 39 #define NAPI_MODULE_TEST(modname, regfunc) \ 40 EXTERN_C_START \ 41 static napi_module _module = { \ 42 NAPI_MODULE_VERSION, \ 43 0, \ 44 __FILE__, \ 45 regfunc, \ 46 #modname, \ 47 NULL, \ 48 {0}, \ 49 }; \ 50 NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); } \ 51 EXTERN_C_END 52 53 NAPI_MODULE_TEST(NODE_GYP_MODULE_NAME, NULL) 54