Skip to content

Commit 5e4f5e1

Browse files
vmorozmhdawson
authored andcommitted
node-api: deprecate napi_module_register
PR-URL: nodejs#46319 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent f523979 commit 5e4f5e1

5 files changed

Lines changed: 254 additions & 155 deletions

File tree

src/api/environment.cc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,9 @@ void AddLinkedBinding(Environment* env, const node_module& mod) {
843843
}
844844

845845
void AddLinkedBinding(Environment* env, const napi_module& mod) {
846-
AddLinkedBinding(env, napi_module_to_node_module(&mod));
846+
node_module node_mod = napi_module_to_node_module(&mod);
847+
node_mod.nm_flags = NM_F_LINKED;
848+
AddLinkedBinding(env, node_mod);
847849
}
848850

849851
void AddLinkedBinding(Environment* env,
@@ -864,6 +866,32 @@ void AddLinkedBinding(Environment* env,
864866
AddLinkedBinding(env, mod);
865867
}
866868

869+
void AddLinkedBinding(Environment* env,
870+
const char* name,
871+
napi_addon_register_func fn) {
872+
node_module mod = {
873+
-1,
874+
NM_F_LINKED,
875+
nullptr, // nm_dso_handle
876+
nullptr, // nm_filename
877+
nullptr, // nm_register_func
878+
[](v8::Local<v8::Object> exports,
879+
v8::Local<v8::Value> module,
880+
v8::Local<v8::Context> context,
881+
void* priv) {
882+
napi_module_register_by_symbol(
883+
exports,
884+
module,
885+
context,
886+
reinterpret_cast<napi_addon_register_func>(priv));
887+
},
888+
name,
889+
reinterpret_cast<void*>(fn),
890+
nullptr // nm_link
891+
};
892+
AddLinkedBinding(env, mod);
893+
}
894+
867895
static std::atomic<uint64_t> next_thread_id{0};
868896

869897
ThreadId AllocateEnvironmentThreadId() {

src/node.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
#include "v8-platform.h" // NOLINT(build/include_order)
7676
#include "node_version.h" // NODE_MODULE_VERSION
7777

78+
#define NAPI_EXPERIMENTAL
79+
#include "node_api.h"
80+
7881
#include <functional>
7982
#include <memory>
8083
#include <ostream>
@@ -121,8 +124,6 @@
121124
// Forward-declare libuv loop
122125
struct uv_loop_s;
123126

124-
struct napi_module;
125-
126127
// Forward-declare these functions now to stop MSVS from becoming
127128
// terminally confused when it's done in node_internals.h
128129
namespace node {
@@ -1235,6 +1236,9 @@ NODE_EXTERN void AddLinkedBinding(Environment* env,
12351236
const char* name,
12361237
addon_context_register_func fn,
12371238
void* priv);
1239+
NODE_EXTERN void AddLinkedBinding(Environment* env,
1240+
const char* name,
1241+
napi_addon_register_func fn);
12381242

12391243
/* Registers a callback with the passed-in Environment instance. The callback
12401244
* is called after the event loop exits, but before the VM is disposed.

src/node_api.h

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct uv_loop_s; // Forward declaration.
3131
typedef napi_value(NAPI_CDECL* napi_addon_register_func)(napi_env env,
3232
napi_value exports);
3333

34+
// Used by deprecated registration method napi_module_register.
3435
typedef struct napi_module {
3536
int nm_version;
3637
unsigned int nm_flags;
@@ -43,70 +44,15 @@ typedef struct napi_module {
4344

4445
#define NAPI_MODULE_VERSION 1
4546

46-
#if defined(_MSC_VER)
47-
#if defined(__cplusplus)
48-
#define NAPI_C_CTOR(fn) \
49-
static void NAPI_CDECL fn(void); \
50-
namespace { \
51-
struct fn##_ { \
52-
fn##_() { fn(); } \
53-
} fn##_v_; \
54-
} \
55-
static void NAPI_CDECL fn(void)
56-
#else // !defined(__cplusplus)
57-
#pragma section(".CRT$XCU", read)
58-
// The NAPI_C_CTOR macro defines a function fn that is called during CRT
59-
// initialization.
60-
// C does not support dynamic initialization of static variables and this code
61-
// simulates C++ behavior. Exporting the function pointer prevents it from being
62-
// optimized. See for details:
63-
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170
64-
#define NAPI_C_CTOR(fn) \
65-
static void NAPI_CDECL fn(void); \
66-
__declspec(dllexport, allocate(".CRT$XCU")) void(NAPI_CDECL * fn##_)(void) = \
67-
fn; \
68-
static void NAPI_CDECL fn(void)
69-
#endif // defined(__cplusplus)
70-
#else
71-
#define NAPI_C_CTOR(fn) \
72-
static void fn(void) __attribute__((constructor)); \
73-
static void fn(void)
74-
#endif
75-
76-
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
77-
EXTERN_C_START \
78-
static napi_module _module = { \
79-
NAPI_MODULE_VERSION, \
80-
flags, \
81-
__FILE__, \
82-
regfunc, \
83-
#modname, \
84-
priv, \
85-
{0}, \
86-
}; \
87-
NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); } \
88-
EXTERN_C_END
89-
9047
#define NAPI_MODULE_INITIALIZER_X(base, version) \
9148
NAPI_MODULE_INITIALIZER_X_HELPER(base, version)
9249
#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version
9350

9451
#ifdef __wasm32__
95-
#define NAPI_WASM_INITIALIZER \
96-
NAPI_MODULE_INITIALIZER_X(napi_register_wasm_v, NAPI_MODULE_VERSION)
97-
#define NAPI_MODULE(modname, regfunc) \
98-
EXTERN_C_START \
99-
NAPI_MODULE_EXPORT napi_value NAPI_WASM_INITIALIZER(napi_env env, \
100-
napi_value exports) { \
101-
return regfunc(env, exports); \
102-
} \
103-
EXTERN_C_END
52+
#define NAPI_MODULE_INITIALIZER_BASE napi_register_wasm_v
10453
#else
105-
#define NAPI_MODULE(modname, regfunc) \
106-
NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage)
107-
#endif
108-
10954
#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v
55+
#endif
11056

11157
#define NAPI_MODULE_INITIALIZER \
11258
NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, NAPI_MODULE_VERSION)
@@ -116,12 +62,24 @@ typedef struct napi_module {
11662
NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
11763
napi_value exports); \
11864
EXTERN_C_END \
119-
NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER) \
12065
napi_value NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports)
12166

67+
#define NAPI_MODULE(modname, regfunc) \
68+
NAPI_MODULE_INIT() { return regfunc(env, exports); }
69+
70+
// Deprecated. Use NAPI_MODULE.
71+
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
72+
NAPI_MODULE(modname, regfunc)
73+
12274
EXTERN_C_START
12375

124-
NAPI_EXTERN void NAPI_CDECL napi_module_register(napi_module* mod);
76+
// Deprecated. Replaced by symbol-based registration defined by NAPI_MODULE
77+
// and NAPI_MODULE_INIT macros.
78+
#if defined(__cplusplus) && __cplusplus >= 201402L
79+
[[deprecated]]
80+
#endif
81+
NAPI_EXTERN void NAPI_CDECL
82+
napi_module_register(napi_module* mod);
12583

12684
NAPI_EXTERN NAPI_NO_RETURN void NAPI_CDECL
12785
napi_fatal_error(const char* location,

0 commit comments

Comments
 (0)