-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
src: minor c++ refactors to module_wrap #15515
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
Conversation
src/module_wrap.cc
Outdated
@@ -27,15 +27,13 @@ using v8::Maybe; | |||
using v8::MaybeLocal; | |||
using v8::Module; | |||
using v8::Object; | |||
using v8::Persistent; | |||
using v8::Promise; | |||
using v8::ScriptCompiler; | |||
using v8::ScriptOrigin; | |||
using v8::String; | |||
using v8::Value; | |||
|
|||
static const char* EXTENSIONS[] = {".mjs", ".js", ".json", ".node"}; |
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.
Since you're here, can you make this static const char *const
? The pointed-to strings are const now but the array itself is not.
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.
done.
src/module_wrap.cc
Outdated
std::vector<ModuleWrap*>* same_hash = module_map_[module->GetIdentityHash()]; | ||
auto it = std::find(same_hash->begin(), same_hash->end(), this); | ||
Isolate* isolate = Isolate::GetCurrent(); | ||
Environment* env = Environment::GetCurrent(isolate); |
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.
ModuleWrap
is a BaseObject
, right? You can just use env()
here. Same goes for the constructor: env()->isolate()
. There are probably quite a few more places that could be updated.
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.
Yes, done.
src/module_wrap.cc
Outdated
if (it != same_hash.end()) { | ||
same_hash.erase(it); | ||
if (same_hash.empty()) | ||
env->module_map.erase(module->GetIdentityHash()); | ||
} |
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.
Correct me if I'm wrong but if you used a std::unordered_multimap
, you could drop the extra indirection of the std::vector
. You could then simply look up env->module_map.equal_range(module->GetIdentityHash())
.
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.
Yes, good idea – done.
@@ -244,7 +243,7 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context, | |||
} | |||
|
|||
Local<Promise> resolve_promise = | |||
dependent->resolve_cache_[specifier_std]->Get(iso); | |||
dependent->resolve_cache_[specifier_std].Get(iso); |
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.
This habit of using iso
when the rest of the code base consistently uses isolate
... urgh.
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.
whats the problem? / there are not style docs saying to use isolate
?
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.
That doesn't mean we shouldn't try to keep things consistent.
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.
@Fishrock123 I agree consistency is nice, but every pull request I do seems to have style errors and there is no guide; I will continue to bring this up as long as I see comments like "urgh." for innocent mistakes.
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.
Have to agree with @bmeck here. There really ought to be a style guide that makes these things more obvious.
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.
Not everything needs to be spelled out. When you start making changes to a code base you're not familiar with, you first peek around to get a feel for the house style. 10 minutes of browsing would show that we use isolate
everywhere and iso
nowhere.
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.
@bnoordhuis you have even in the past called PRs of mine stupid. That line of reasoning and interactions like the one above make me want things spelled out to avoid such behavior.
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.
If it's any consolation to you, I'm also disappointed none of the reviewers called it out.
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.
It is not. Mistakes are made by everyone and we should work together, not bring pessimism into things if it can be avoided.
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.
Fwiw I was pretty sure I pushed commits to the original PR that did that particular style change, because I knew it wasn’t obvious and I think @bmeck said he preferred that to an endless list of style nits? These probably just got lost during some force push or sth.
LGTM once @bnoordhuis' comments are addressed |
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.
Seems fine to me, though my C++ isn't as strong as others so I am mostly deferring to them for structure choices.
f7e0884
to
569daf0
Compare
@bnoordhuis Updated, PTAL |
|
1c171c1
to
e9ff842
Compare
- Move `module_map` to `Environment` instead of having it be global state - `std::map` → `std::unordered_map` - Remove one level of indirection for the map values - Clean up empty vectors in `module_map` - Call `Reset()` on all persistent handles in `resolve_cache_` - Add a missing `HandleScope` to `ModuleWrap::~ModuleWrap()` PR-URL: nodejs#15515 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
e9ff842
to
e66a05d
Compare
Landed in 150b8f7 |
- Move `module_map` to `Environment` instead of having it be global state - `std::map` → `std::unordered_map` - Remove one level of indirection for the map values - Clean up empty vectors in `module_map` - Call `Reset()` on all persistent handles in `resolve_cache_` - Add a missing `HandleScope` to `ModuleWrap::~ModuleWrap()` PR-URL: #15515 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
- Move `module_map` to `Environment` instead of having it be global state - `std::map` → `std::unordered_map` - Remove one level of indirection for the map values - Clean up empty vectors in `module_map` - Call `Reset()` on all persistent handles in `resolve_cache_` - Add a missing `HandleScope` to `ModuleWrap::~ModuleWrap()` PR-URL: nodejs/node#15515 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
- Move `module_map` to `Environment` instead of having it be global state - `std::map` → `std::unordered_map` - Remove one level of indirection for the map values - Clean up empty vectors in `module_map` - Call `Reset()` on all persistent handles in `resolve_cache_` - Add a missing `HandleScope` to `ModuleWrap::~ModuleWrap()` PR-URL: #15515 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
This does not land cleanly in LTS. Please feel free to manually backport by following the guide. Please also feel free to replace do-not-land if it is being backported |
module_map
toEnvironment
instead of having it be global statestd::map
→std::unordered_map
module_map
Reset()
on all persistent handles inresolve_cache_
HandleScope
toModuleWrap::~ModuleWrap()
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
src/module_wrap