Skip to content

Commit 6949543

Browse files
Gabriel Schulhofjasnell
authored andcommitted
src: cover extra load-via-special-symbol scenario
We need to look for a special symbol even if the module self-registers when the module self-registers with the wrong version. PR-URL: #20186 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 65e62e5 commit 6949543

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/node.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,13 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
22922292

22932293
// -1 is used for N-API modules
22942294
if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) {
2295+
// Even if the module did self-register, it may have done so with the wrong
2296+
// version. We must only give up after having checked to see if it has an
2297+
// appropriate initializer callback.
2298+
if (auto callback = GetInitializerCallback(&dlib)) {
2299+
callback(exports, module, context);
2300+
return;
2301+
}
22952302
char errmsg[1024];
22962303
snprintf(errmsg,
22972304
sizeof(errmsg),

test/addons/hello-world/binding.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,20 @@ extern "C" NODE_MODULE_EXPORT void INITIALIZER(v8::Local<v8::Object> exports,
1515
v8::Local<v8::Context> context) {
1616
NODE_SET_METHOD(exports, "hello", Method);
1717
}
18+
19+
static void FakeInit(v8::Local<v8::Object> exports,
20+
v8::Local<v8::Value> module,
21+
v8::Local<v8::Context> context) {
22+
auto isolate = context->GetIsolate();
23+
auto exception = v8::Exception::Error(v8::String::NewFromUtf8(isolate,
24+
"FakeInit should never run!", v8::NewStringType::kNormal)
25+
.ToLocalChecked());
26+
isolate->ThrowException(exception);
27+
}
28+
29+
// Define a Node.js module, but with the wrong version. Node.js should still be
30+
// able to load this module, multiple times even, because it exposes the
31+
// specially named initializer above.
32+
#undef NODE_MODULE_VERSION
33+
#define NODE_MODULE_VERSION 3
34+
NODE_MODULE(NODE_GYP_MODULE_NAME, FakeInit)

0 commit comments

Comments
 (0)