src: add public API for linked bindings · nodejs/node@a13f0a6 · GitHub
Skip to content

Commit a13f0a6

Browse files
addaleaxtargos
authored andcommitted
src: add public API for linked bindings
(Re-?)add a public API for creating linked bindings (access to `NM_F_LINKED` as a constant was previously removed in d6ac8a4), and add a test for the functionality. PR-URL: #26457 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Shelley Vohr <codebytere@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e531258 commit a13f0a6

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

node.gyp

Lines changed: 1 addition & 0 deletions

src/node.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ typedef void (*addon_context_register_func)(
445445
v8::Local<v8::Context> context,
446446
void* priv);
447447

448+
enum ModuleFlags {
449+
kLinked = 0x02
450+
};
451+
448452
struct node_module {
449453
int nm_version;
450454
unsigned int nm_flags;
@@ -532,6 +536,14 @@ extern "C" NODE_EXTERN void node_module_register(void* mod);
532536
/* NOLINTNEXTLINE (readability/null_usage) */ \
533537
NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
534538

539+
// Embedders can use this type of binding for statically linked native bindings.
540+
// It is used the same way addon bindings are used, except that linked bindings
541+
// can be accessed through `process._linkedBinding(modname)`.
542+
#define NODE_MODULE_LINKED(modname, regfunc) \
543+
/* NOLINTNEXTLINE (readability/null_usage) */ \
544+
NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, \
545+
node::ModuleFlags::kLinked)
546+
535547
/*
536548
* For backward compatibility in add-on modules.
537549
*/

src/node_binding.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ enum {
2121
NM_F_DELETEME = 1 << 3,
2222
};
2323

24+
// Make sure our internal values match the public API's values.
25+
static_assert(static_cast<int>(NM_F_LINKED) ==
26+
static_cast<int>(node::ModuleFlags::kLinked),
27+
"NM_F_LINKED != node::ModuleFlags::kLinked");
28+
2429
#define NODE_MODULE_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags) \
2530
static node::node_module _module = { \
2631
NODE_MODULE_VERSION, \

test/cctest/test_linked_binding.cc

Lines changed: 42 additions & 0 deletions

0 commit comments

Comments
 (0)