From c1078c4a3bf1a1b2e83d4ca5965b8f9fba26b166 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 20 Apr 2018 22:27:43 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/20186 Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung --- src/node.cc | 7 +++++++ test/addons/hello-world/binding.cc | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/node.cc b/src/node.cc index b4853091b9d..90defba40ba 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2292,6 +2292,13 @@ static void DLOpen(const FunctionCallbackInfo& args) { // -1 is used for N-API modules if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) { + // Even if the module did self-register, it may have done so with the wrong + // version. We must only give up after having checked to see if it has an + // appropriate initializer callback. + if (auto callback = GetInitializerCallback(&dlib)) { + callback(exports, module, context); + return; + } char errmsg[1024]; snprintf(errmsg, sizeof(errmsg), diff --git a/test/addons/hello-world/binding.cc b/test/addons/hello-world/binding.cc index ba6a22d7196..e267a3b2a76 100644 --- a/test/addons/hello-world/binding.cc +++ b/test/addons/hello-world/binding.cc @@ -15,3 +15,20 @@ extern "C" NODE_MODULE_EXPORT void INITIALIZER(v8::Local exports, v8::Local context) { NODE_SET_METHOD(exports, "hello", Method); } + +static void FakeInit(v8::Local exports, + v8::Local module, + v8::Local context) { + auto isolate = context->GetIsolate(); + auto exception = v8::Exception::Error(v8::String::NewFromUtf8(isolate, + "FakeInit should never run!", v8::NewStringType::kNormal) + .ToLocalChecked()); + isolate->ThrowException(exception); +} + +// Define a Node.js module, but with the wrong version. Node.js should still be +// able to load this module, multiple times even, because it exposes the +// specially named initializer above. +#undef NODE_MODULE_VERSION +#define NODE_MODULE_VERSION 3 +NODE_MODULE(NODE_GYP_MODULE_NAME, FakeInit)