Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
c09f3dc
doc: put release script specifics in details
MylesBorins Sep 18, 2020
2a4ae09
doc: update crypto.createSecretKey accepted types
panva Sep 17, 2020
68ea7f5
module: fix crash on multiline named cjs imports
ctavan Sep 18, 2020
9d91842
tools,doc: upgrade dependencies
aduh95 Sep 17, 2020
c5d27e1
tools,doc: enforce alphabetical order for md refs
aduh95 Sep 17, 2020
ec6b78a
doc: add `socket.readyState`
clarkkozak Sep 18, 2020
0c4540b
doc: fix heading space bug in assert.md
tlhunter Sep 23, 2020
7f35573
tools: ignore build folder when checking links
Sep 23, 2020
1893449
doc: revise dependency redirection text in policy.md
Trott Sep 20, 2020
b4514d4
doc: replace "this guide" link text with guide title
Trott Sep 21, 2020
b9d767c
doc: change type of child_process.signalCode to string
Sep 16, 2020
ef0d2ef
doc: move package config docs to separate page
aduh95 Aug 7, 2020
ab7d0e9
meta: update module pages in CODEOWNERS
aduh95 Aug 26, 2020
1d1ce1f
doc: document support for package.json fields
aduh95 Aug 7, 2020
5057318
module: exports pattern support
guybedford Aug 12, 2020
d7282c0
doc: edit subpath export patterns introduction
Trott Sep 18, 2020
dd53036
doc: fixup lutimes metadata
addaleax Sep 24, 2020
a8d3a7f
doc: put landing specifics in details tag
Trott Sep 25, 2020
0a847ca
doc: update napi_make_callback documentation
Flarna Sep 23, 2020
6dc6dad
doc: avoid referring to C array size
tniessen Sep 22, 2020
482ce6c
doc: improve N-API string-to-native doc
Sep 23, 2020
5da5d41
doc: refine require/import conditions constraints
guybedford Sep 23, 2020
353a567
deps: upgrade to c-ares v1.16.1
codebytere Sep 24, 2020
0d8eaa3
src: allow N-API addon in `AddLinkedBinding()`
addaleax Sep 22, 2020
1e1cb94
src: fix incorrect SIGSEGV handling in NODE_USE_V8_WASM_TRAP_HANDLER
korniltsev Sep 21, 2020
d7c28c9
test,child_process: add tests for signalCode value
Trott Sep 24, 2020
1758ac8
doc: added version 7 to N-API version matrix
NickNaso Sep 23, 2020
87dfed0
doc: add gpg key export directions to releases doc
danielleadams Sep 22, 2020
19b95a7
deps: upgrade to libuv 1.40.0
cjihrig Sep 25, 2020
f2635b3
test: replace annonymous functions with arrow
PoojaDurgad Aug 25, 2020
857e321
doc: set encoding to hex before piping hash
vbarzana Sep 25, 2020
86ac749
doc: add history entry for breaking destroy() change
kanongil Sep 24, 2020
0f4ecaa
repl: standardize Control key indications
Trott Sep 19, 2020
010173a
doc: edit n-api.md for minor improvements
Trott Sep 26, 2020
f551f52
module: named exports for CJS via static analysis
guybedford May 15, 2020
80eb221
tools: update ESLint to 7.10.0
cjihrig Sep 26, 2020
70ad69b
doc: outline when origin is set to unhandledRejection
mlarcher Sep 22, 2020
99a79e3
fs: fix fs.promises.writeFile with typed arrays
targos Sep 27, 2020
46a4154
doc: packages docs feedback
guybedford Sep 27, 2020
02db136
doc: alphabetize error list
Trott Sep 15, 2020
2e10616
doc: remove http2 non-link anchor tags
Trott Sep 12, 2020
c048607
2020-09-29, Version 14.13.0 (Current)
MylesBorins Sep 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
src: allow N-API addon in AddLinkedBinding()
`AddLinkedBinding()` can be used to load old-style Node.js addons, but
currently not N-API addons. There’s no good reason not to support
N-API addons as well, so add that.

PR-URL: #35301
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
  • Loading branch information
addaleax authored and MylesBorins committed Sep 29, 2020
commit 0d8eaa3942f289874ed8c5d2a9468ba9c9ec45c8
4 changes: 4 additions & 0 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ void AddLinkedBinding(Environment* env, const node_module& mod) {
prev_head->nm_link = &env->extra_linked_bindings()->back();
}

void AddLinkedBinding(Environment* env, const napi_module& mod) {
AddLinkedBinding(env, napi_module_to_node_module(&mod));
}

void AddLinkedBinding(Environment* env,
const char* name,
addon_context_register_func fn,
Expand Down
4 changes: 4 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
// Forward-declare libuv loop
struct uv_loop_s;

struct napi_module;

// Forward-declare these functions now to stop MSVS from becoming
// terminally confused when it's done in node_internals.h
namespace node {
Expand Down Expand Up @@ -824,6 +826,8 @@ extern "C" NODE_EXTERN void node_module_register(void* mod);
// In each variant, the registration function needs to be usable at least for
// the time during which the Environment exists.
NODE_EXTERN void AddLinkedBinding(Environment* env, const node_module& mod);
NODE_EXTERN void AddLinkedBinding(Environment* env,
const struct napi_module& mod);
NODE_EXTERN void AddLinkedBinding(Environment* env,
const char* name,
addon_context_register_func fn,
Expand Down
17 changes: 12 additions & 5 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ static void napi_module_register_cb(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
napi_module_register_by_symbol(exports, module, context,
static_cast<napi_module*>(priv)->nm_register_func);
static_cast<const napi_module*>(priv)->nm_register_func);
}

void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
Expand Down Expand Up @@ -480,19 +480,26 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
}
}

// Registers a NAPI module.
void napi_module_register(napi_module* mod) {
node::node_module* nm = new node::node_module {
namespace node {
node_module napi_module_to_node_module(const napi_module* mod) {
return {
-1,
mod->nm_flags | NM_F_DELETEME,
nullptr,
mod->nm_filename,
nullptr,
napi_module_register_cb,
mod->nm_modname,
mod, // priv
const_cast<napi_module*>(mod), // priv
nullptr,
};
}
} // namespace node

// Registers a NAPI module.
void napi_module_register(napi_module* mod) {
node::node_module* nm = new node::node_module(
node::napi_module_to_node_module(mod));
node::node_module_register(nm);
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct uv_loop_s; // Forward declaration.
typedef napi_value (*napi_addon_register_func)(napi_env env,
napi_value exports);

typedef struct {
typedef struct napi_module {
int nm_version;
unsigned int nm_flags;
const char* nm_filename;
Expand Down
2 changes: 2 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ namespace fs {
std::string Basename(const std::string& str, const std::string& extension);
} // namespace fs

node_module napi_module_to_node_module(const napi_module* mod);

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
109 changes: 108 additions & 1 deletion test/cctest/test_linked_binding.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "node_test_fixture.h"
#include "node_internals.h" // RunBootstrapping()
#include "node_api.h"

void InitializeBinding(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
Expand Down Expand Up @@ -83,3 +83,110 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingTest) {
CHECK_EQ(strcmp(*utf8val, "value"), 0);
CHECK_EQ(calls, 1);
}

napi_value InitializeLocalNapiBinding(napi_env env, napi_value exports) {
napi_value key, value;
CHECK_EQ(
napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &key), napi_ok);
CHECK_EQ(
napi_create_string_utf8(env, "world", NAPI_AUTO_LENGTH, &value), napi_ok);
CHECK_EQ(napi_set_property(env, exports, key, value), napi_ok);
return nullptr;
}

static napi_module local_linked_napi = {
NAPI_MODULE_VERSION,
node::ModuleFlags::kLinked,
nullptr,
InitializeLocalNapiBinding,
"local_linked_napi",
nullptr,
{0},
};

TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiTest) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env {handle_scope, argv};

AddLinkedBinding(*test_env, local_linked_napi);

v8::Local<v8::Context> context = isolate_->GetCurrentContext();

const char* run_script =
"process._linkedBinding('local_linked_napi').hello";
v8::Local<v8::Script> script = v8::Script::Compile(
context,
v8::String::NewFromOneByte(isolate_,
reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
v8::String::Utf8Value utf8val(isolate_, completion_value);
CHECK_NOT_NULL(*utf8val);
CHECK_EQ(strcmp(*utf8val, "world"), 0);
}

napi_value NapiLinkedWithInstanceData(napi_env env, napi_value exports) {
int* instance_data = new int(0);
CHECK_EQ(
napi_set_instance_data(
env,
instance_data,
[](napi_env env, void* data, void* hint) {
++*static_cast<int*>(data);
}, nullptr),
napi_ok);

napi_value key, value;
CHECK_EQ(
napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &key), napi_ok);
CHECK_EQ(
napi_create_external(env, instance_data, nullptr, nullptr, &value),
napi_ok);
CHECK_EQ(napi_set_property(env, exports, key, value), napi_ok);
return nullptr;
}

static napi_module local_linked_napi_id = {
NAPI_MODULE_VERSION,
node::ModuleFlags::kLinked,
nullptr,
NapiLinkedWithInstanceData,
"local_linked_napi_id",
nullptr,
{0},
};

TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiInstanceDataTest) {
const v8::HandleScope handle_scope(isolate_);
int* instance_data = nullptr;

{
const Argv argv;
Env test_env {handle_scope, argv};

AddLinkedBinding(*test_env, local_linked_napi_id);

v8::Local<v8::Context> context = isolate_->GetCurrentContext();

const char* run_script =
"process._linkedBinding('local_linked_napi_id').hello";
v8::Local<v8::Script> script = v8::Script::Compile(
context,
v8::String::NewFromOneByte(isolate_,
reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value =
script->Run(context).ToLocalChecked();
CHECK(completion_value->IsExternal());
instance_data = static_cast<int*>(
completion_value.As<v8::External>()->Value());
CHECK_NE(instance_data, nullptr);
CHECK_EQ(*instance_data, 0);
}

CHECK_EQ(*instance_data, 1);
delete instance_data;
}