-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
addons: allow multiple requires of the same addon #19731
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <node_api.h> | ||
#include "../common.h" | ||
#include <string.h> | ||
|
||
napi_value Method(napi_env env, napi_callback_info info) { | ||
napi_value world; | ||
const char* str = "world"; | ||
size_t str_len = strlen(str); | ||
NAPI_CALL(env, napi_create_string_utf8(env, str, str_len, &world)); | ||
return world; | ||
} | ||
|
||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("hello", Method); | ||
NAPI_CALL(env, napi_define_properties(env, exports, 1, &desc)); | ||
return exports; | ||
} | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "binding", | ||
"sources": [ "binding.c" ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
|
||
const bindingPath = require.resolve(`./build/${common.buildType}/binding`); | ||
|
||
require(bindingPath); | ||
delete require.cache[bindingPath]; | ||
require(bindingPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <node.h> | ||
#include <v8.h> | ||
|
||
void Method(const v8::FunctionCallbackInfo<v8::Value>& args) { | ||
v8::Isolate* isolate = args.GetIsolate(); | ||
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world")); | ||
} | ||
|
||
void define(v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value> module, | ||
v8::Local<v8::Context> context) { | ||
NODE_SET_METHOD(exports, "hello", Method); | ||
} | ||
|
||
NODE_MODULE(binding, &define); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], | ||
'sources': [ 'binding.cc' ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
|
||
const bindingPath = require.resolve(`./build/${common.buildType}/binding`); | ||
|
||
require(bindingPath); | ||
delete require.cache[bindingPath]; | ||
require(bindingPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need a stronger key because this also breaks those projects which use two different versions of the same module. So, like,
The distinction between the two modules with identical
nm_modname
is the path from which they are loaded. So we should save the absolute path of the module we are about to load to a global variable declared next tomodpending
right before callingdlib.Open()
, and then use that path as the key here.So, like
static node_module* modpending; +std::string modpending_filename;
and then
in
node_module_register()
, and+modpending_filename = std::string(*filename); bool is_opened = dlib.Open();
in
DLOpen()
.In general, I have reservations about attempting to duplicate what the library loader is doing, because that code has very likely been refined to a very high degree over the course of the decades.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are absolutely right, the basename is not unambiguous enough.
I can change it as you suggest - in fact we don't even need to save the pending filename, we can just he the pending
node_module *
as we do today and use its file name as the key in thestd::unordered_map
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frutiger we must also ensure that two different file names which refer to the same file in reality are treated correctly. I'm not sure to what extent the JS side of
require()
takes care of resolving files in this way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frutiger it might be worth also taking a look at the JS side, and maybe adding some more resolution steps to the portion of the code path following the decision to load a native module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frutiger I guess we should at least follow symlinks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not actually sure just following symlinks is enough, nor do I think we would be able to do a good job of maintaining compatibility of this logic with the loader logic in JS. Perhaps we could hash the contents of the file itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, the JS loader logic would cause the resulting call to
dlopen()
to always return a new module, and run the constructors. However, this means that we will have reproduceddlopen()
's decision as to whether to load a library or increment the refcount of an existing one. I'm pretty sure we haven't done that.The decision made by
dlopen()
as to whether to load a new library, thereby running its constructors, or incrementing the reference count of an existing library, thereby not running its constructors, is what we're trying to replicate here. The code for that decision is very old, very well-vetted, and may even be platform-dependent. This is why I said I was reluctant to attempt to reproduce that code.In an ideal world we'd have
dlreopen()
or theRTLD_REOPEN
flag which would do exactly what we need. But alas.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess @bnoordhuis' modification whereby it falls back to looking for a well-known symbol is a way to kick-start the addon if it fails to register. But, like you said, this won't work for existing modules. sigh