Skip to content

Commit 7273e06

Browse files
committed
cherry pick changes from PR #46319
1 parent 10d248f commit 7273e06

File tree

4 files changed

+61
-146
lines changed

4 files changed

+61
-146
lines changed

src/node_api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ static void napi_module_register_cb(v8::Local<v8::Object> exports,
631631
module,
632632
context,
633633
napi_mod->nm_register_func,
634-
static_cast<int32_t>(napi_mod->nm_api_version));
634+
NAPI_DEFAULT_MODULE_API_VERSION);
635635
}
636636

637637
void napi_module_register_by_symbol(v8::Local<v8::Object> exports,

src/node_api.h

Lines changed: 14 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -30,190 +30,61 @@ struct uv_loop_s; // Forward declaration.
3030

3131
typedef napi_value(NAPI_CDECL* napi_addon_register_func)(napi_env env,
3232
napi_value exports);
33-
34-
#ifndef NAPI_EXPERIMENTAL
35-
36-
typedef struct napi_module {
37-
int nm_version;
38-
unsigned int nm_flags;
39-
const char* nm_filename;
40-
napi_addon_register_func nm_register_func;
41-
const char* nm_modname;
42-
void* nm_priv;
43-
void* reserved[4];
44-
} napi_module;
45-
46-
#else
47-
4833
typedef int32_t(NAPI_CDECL* napi_addon_get_api_version_func)();
4934

35+
// Used by deprecated registration method napi_module_register.
5036
typedef struct napi_module {
5137
int nm_version;
5238
unsigned int nm_flags;
5339
const char* nm_filename;
5440
napi_addon_register_func nm_register_func;
5541
const char* nm_modname;
5642
void* nm_priv;
57-
uintptr_t nm_api_version;
58-
void* reserved[3];
43+
void* reserved[4];
5944
} napi_module;
6045

61-
#endif // NAPI_EXPERIMENTAL
62-
6346
#define NAPI_MODULE_VERSION 1
6447

65-
#if defined(_MSC_VER)
66-
#if defined(__cplusplus)
67-
#define NAPI_C_CTOR(fn) \
68-
static void NAPI_CDECL fn(void); \
69-
namespace { \
70-
struct fn##_ { \
71-
fn##_() { fn(); } \
72-
} fn##_v_; \
73-
} \
74-
static void NAPI_CDECL fn(void)
75-
#else // !defined(__cplusplus)
76-
#pragma section(".CRT$XCU", read)
77-
// The NAPI_C_CTOR macro defines a function fn that is called during CRT
78-
// initialization.
79-
// C does not support dynamic initialization of static variables and this code
80-
// simulates C++ behavior. Exporting the function pointer prevents it from being
81-
// optimized. See for details:
82-
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170
83-
#define NAPI_C_CTOR(fn) \
84-
static void NAPI_CDECL fn(void); \
85-
__declspec(dllexport, allocate(".CRT$XCU")) void(NAPI_CDECL * fn##_)(void) = \
86-
fn; \
87-
static void NAPI_CDECL fn(void)
88-
#endif // defined(__cplusplus)
89-
#else
90-
#define NAPI_C_CTOR(fn) \
91-
static void fn(void) __attribute__((constructor)); \
92-
static void fn(void)
93-
#endif
94-
95-
#ifndef NAPI_EXPERIMENTAL
96-
97-
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
98-
EXTERN_C_START \
99-
static napi_module _module = { \
100-
NAPI_MODULE_VERSION, \
101-
flags, \
102-
__FILE__, \
103-
regfunc, \
104-
#modname, \
105-
priv, \
106-
{0}, \
107-
}; \
108-
NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); } \
109-
EXTERN_C_END
110-
11148
#define NAPI_MODULE_INITIALIZER_X(base, version) \
11249
NAPI_MODULE_INITIALIZER_X_HELPER(base, version)
11350
#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version
11451

11552
#ifdef __wasm32__
116-
#define NAPI_WASM_INITIALIZER \
117-
NAPI_MODULE_INITIALIZER_X(napi_register_wasm_v, NAPI_MODULE_VERSION)
118-
#define NAPI_MODULE(modname, regfunc) \
119-
EXTERN_C_START \
120-
NAPI_MODULE_EXPORT napi_value NAPI_WASM_INITIALIZER(napi_env env, \
121-
napi_value exports) { \
122-
return regfunc(env, exports); \
123-
} \
124-
EXTERN_C_END
53+
#define NAPI_MODULE_INITIALIZER_BASE napi_register_wasm_v
12554
#else
126-
#define NAPI_MODULE(modname, regfunc) \
127-
NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage)
128-
#endif
129-
13055
#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v
56+
#endif
13157

132-
#define NAPI_MODULE_INITIALIZER \
133-
NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, NAPI_MODULE_VERSION)
134-
135-
#define NAPI_MODULE_INIT() \
136-
EXTERN_C_START \
137-
NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
138-
napi_value exports); \
139-
EXTERN_C_END \
140-
NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER) \
141-
napi_value NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports)
142-
143-
#else
144-
145-
#define NAPI_MODULE_XV(modname, regfunc, api_version, priv, flags) \
146-
EXTERN_C_START \
147-
static napi_module _module = { \
148-
NAPI_MODULE_VERSION, \
149-
flags, \
150-
__FILE__, \
151-
regfunc, \
152-
#modname, \
153-
priv, \
154-
(uintptr_t)api_version, \
155-
{0}, \
156-
}; \
157-
NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); } \
158-
EXTERN_C_END
159-
160-
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
161-
NAPI_MODULE_XV(modname, regfunc, NAPI_VERSION, priv, flags)
162-
163-
// Not used. For backward compatibility only.
164-
#define NAPI_MODULE_INITIALIZER_X(base, version) \
165-
NAPI_MODULE_INITIALIZER_X_HELPER(base, version)
166-
#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version
167-
168-
#define NAPI_CONCAT_HELPER(text1, text2) text1##text2
169-
#define NAPI_CONCAT(text1, text2) NAPI_CONCAT_HELPER(text1, text2)
170-
171-
#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v
17258
#define NAPI_MODULE_GET_API_VERSION_BASE napi_module_get_api_version_v
17359

17460
#define NAPI_MODULE_INITIALIZER \
175-
NAPI_CONCAT(NAPI_MODULE_INITIALIZER_BASE, NAPI_MODULE_VERSION)
61+
NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, NAPI_MODULE_VERSION)
17662

17763
#define NAPI_MODULE_GET_API_VERSION \
178-
NAPI_CONCAT(NAPI_MODULE_GET_API_VERSION_BASE, NAPI_MODULE_VERSION)
64+
NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_GET_API_VERSION_BASE, \
65+
NAPI_MODULE_VERSION)
17966

18067
#define NAPI_MODULE_INIT() \
18168
EXTERN_C_START \
182-
NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
183-
napi_value exports); \
18469
NAPI_MODULE_EXPORT int32_t NAPI_MODULE_GET_API_VERSION() { \
18570
return NAPI_VERSION; \
18671
} \
72+
NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
73+
napi_value exports); \
18774
EXTERN_C_END \
18875
napi_value NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports)
18976

190-
#ifdef __wasm32__
191-
192-
#define NAPI_WASM_INITIALIZER \
193-
NAPI_CONCAT(napi_register_wasm_v, NAPI_MODULE_VERSION)
194-
#define NAPI_WASM_GET_API_VERSION \
195-
NAPI_CONCAT(napi_wasm_get_api_version_v, NAPI_MODULE_VERSION)
196-
#define NAPI_MODULE(modname, regfunc) \
197-
EXTERN_C_START \
198-
NAPI_MODULE_EXPORT napi_value NAPI_WASM_INITIALIZER(napi_env env, \
199-
napi_value exports) { \
200-
return regfunc(env, exports); \
201-
} \
202-
NAPI_MODULE_EXPORT int32_t NAPI_WASM_GET_API_VERSION() { \
203-
return NAPI_VERSION; \
204-
} \
205-
EXTERN_C_END
206-
#else
207-
20877
#define NAPI_MODULE(modname, regfunc) \
20978
NAPI_MODULE_INIT() { return regfunc(env, exports); }
21079

211-
#endif // __wasm32__
212-
213-
#endif // NAPI_EXPERIMENTAL
80+
// Deprecated. Use NAPI_MODULE.
81+
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
82+
NAPI_MODULE(modname, regfunc)
21483

21584
EXTERN_C_START
21685

86+
// Deprecated. Replaced by symbol-based registration defined by NAPI_MODULE
87+
// and NAPI_MODULE_INIT macros.
21788
NAPI_EXTERN void NAPI_CDECL napi_module_register(napi_module* mod);
21889

21990
NAPI_EXTERN NAPI_NO_RETURN void NAPI_CDECL
Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
11
#include <node_api.h>
22

3-
NAPI_MODULE(NODE_GYP_MODULE_NAME, NULL)
3+
#if defined(_MSC_VER)
4+
#if defined(__cplusplus)
5+
#define NAPI_C_CTOR(fn) \
6+
static void NAPI_CDECL fn(void); \
7+
namespace { \
8+
struct fn##_ { \
9+
fn##_() { fn(); } \
10+
} fn##_v_; \
11+
} \
12+
static void NAPI_CDECL fn(void)
13+
#else // !defined(__cplusplus)
14+
#pragma section(".CRT$XCU", read)
15+
// The NAPI_C_CTOR macro defines a function fn that is called during CRT
16+
// initialization.
17+
// C does not support dynamic initialization of static variables and this code
18+
// simulates C++ behavior. Exporting the function pointer prevents it from being
19+
// optimized. See for details:
20+
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170
21+
#define NAPI_C_CTOR(fn) \
22+
static void NAPI_CDECL fn(void); \
23+
__declspec(dllexport, allocate(".CRT$XCU")) void(NAPI_CDECL * fn##_)(void) = \
24+
fn; \
25+
static void NAPI_CDECL fn(void)
26+
#endif // defined(__cplusplus)
27+
#else
28+
#define NAPI_C_CTOR(fn) \
29+
static void fn(void) __attribute__((constructor)); \
30+
static void fn(void)
31+
#endif
32+
33+
#define NAPI_MODULE_TEST(modname, regfunc) \
34+
EXTERN_C_START \
35+
static napi_module _module = { \
36+
NAPI_MODULE_VERSION, \
37+
0, \
38+
__FILE__, \
39+
regfunc, \
40+
#modname, \
41+
NULL, \
42+
{0}, \
43+
}; \
44+
NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); } \
45+
EXTERN_C_END
46+
47+
NAPI_MODULE_TEST(NODE_GYP_MODULE_NAME, NULL)

test/node-api/test_reference_obj_only/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async function runTests() {
5353
assert.throws(() => { addon.createRef(entry.value); },
5454
{
5555
name: 'Error',
56-
message: 'Invalid argument'
56+
message: 'Invalid argument',
5757
});
5858
}
5959
}

0 commit comments

Comments
 (0)