File: test_null_init.c

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (53 lines) | stat: -rw-r--r-- 3,013 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <node_api.h>

// This test uses old module initialization style deprecated in current code.
// The goal is to see that all previously compiled code continues to work the
// same way as before.
// The test has a copy of previous macro definitions which are removed from
// the node_api.h file.

#if defined(_MSC_VER)
#if defined(__cplusplus)
#define NAPI_C_CTOR(fn)                                                        \
  static void NAPI_CDECL fn(void);                                             \
  namespace {                                                                  \
  struct fn##_ {                                                               \
    fn##_() { fn(); }                                                          \
  } fn##_v_;                                                                   \
  }                                                                            \
  static void NAPI_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 NAPI_CDECL fn(void);                                             \
  __declspec(dllexport, allocate(".CRT$XCU")) void(NAPI_CDECL * fn##_)(void) = \
      fn;                                                                      \
  static void NAPI_CDECL fn(void)
#endif  // defined(__cplusplus)
#else
#define NAPI_C_CTOR(fn)                                                        \
  static void fn(void) __attribute__((constructor));                           \
  static void fn(void)
#endif

#define NAPI_MODULE_TEST(modname, regfunc)                                     \
  EXTERN_C_START                                                               \
  static napi_module _module = {                                               \
      NAPI_MODULE_VERSION,                                                     \
      0,                                                                       \
      __FILE__,                                                                \
      regfunc,                                                                 \
      #modname,                                                                \
      NULL,                                                                    \
      {0},                                                                     \
  };                                                                           \
  NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); }         \
  EXTERN_C_END

NAPI_MODULE_TEST(NODE_GYP_MODULE_NAME, NULL)