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 <info@bnoordhuis.nl>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
pull/20186/merge
Gabriel Schulhof 2018-04-20 22:27:43 -04:00
parent ce4c8c823c
commit c1078c4a3b
2 changed files with 24 additions and 0 deletions

View File

@ -2292,6 +2292,13 @@ static void DLOpen(const FunctionCallbackInfo<Value>& 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),

View File

@ -15,3 +15,20 @@ extern "C" NODE_MODULE_EXPORT void INITIALIZER(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context) {
NODE_SET_METHOD(exports, "hello", Method);
}
static void FakeInit(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> 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)