Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

addaleax
Copy link
Member

@addaleax addaleax commented Sep 21, 2017

  • Move module_map to Environment instead of having it be global state
  • std::mapstd::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()
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

src/module_wrap

@addaleax addaleax added the esm Issues and PRs related to the ECMAScript Modules implementation. label Sep 21, 2017
@addaleax addaleax requested a review from bmeck September 21, 2017 00:05
@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Sep 21, 2017
@@ -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"};
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

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);
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done.

if (it != same_hash.end()) {
same_hash.erase(it);
if (same_hash.empty())
env->module_map.erase(module->GetIdentityHash());
}
Copy link
Member

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()).

Copy link
Member Author

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);
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

@bnoordhuis bnoordhuis Sep 21, 2017

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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.

@jasnell
Copy link
Member

jasnell commented Sep 21, 2017

LGTM once @bnoordhuis' comments are addressed

Copy link
Member

@bmeck bmeck left a 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.

@addaleax
Copy link
Member Author

@bnoordhuis Updated, PTAL

@addaleax
Copy link
Member Author

addaleax commented Sep 22, 2017

- 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]>
@addaleax
Copy link
Member Author

Landed in 150b8f7

@addaleax addaleax closed this Sep 23, 2017
@addaleax addaleax deleted the module-refactor branch September 23, 2017 15:01
addaleax added a commit that referenced this pull request Sep 23, 2017
- 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]>
addaleax added a commit to addaleax/ayo that referenced this pull request Sep 23, 2017
- 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]>
jasnell pushed a commit that referenced this pull request Sep 25, 2017
- 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]>
@MylesBorins
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants