-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
vm: sync-ify SourceTextModule linkage #59000
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
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
2262e98
to
af4a107
Compare
Split `module.link(linker)` into two synchronous step `sourceTextModule.linkRequestedModules()` and `sourceTextModule.instantiate()`. This allows creating vm modules and resolving the dependencies in a complete synchronous procedure. This also makes `syntheticModule.link()` redundant. The link step for a SyntheticModule is no-op and is already taken care in the constructor by initializing the binding slots with the given export names.
af4a107
to
9c18d11
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #59000 +/- ##
==========================================
+ Coverage 90.04% 90.07% +0.03%
==========================================
Files 641 641
Lines 188846 188898 +52
Branches 37039 37052 +13
==========================================
+ Hits 170040 170151 +111
+ Misses 11504 11453 -51
+ Partials 7302 7294 -8
🚀 New features to boost your workflow:
|
The order of the `modules` array should respect the order of | ||
[`sourceTextModule.moduleRequests`][]. |
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.
The order of the `modules` array should respect the order of | |
[`sourceTextModule.moduleRequests`][]. | |
The order of the module instances in the `modules` array should correspond to the order of | |
[`sourceTextModule.moduleRequests`][] being resolved. |
@@ -369,6 +371,37 @@ class SourceTextModule extends Module { | |||
} | |||
} | |||
|
|||
linkRequestedModules(modules) { | |||
validateThisInternalField(this, kWrap, 'SyntheticModule'); |
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.
validateThisInternalField(this, kWrap, 'SyntheticModule'); | |
validateThisInternalField(this, kWrap, 'SourceTextModule'); |
} | ||
return module[kWrap]; | ||
}); | ||
this[kWrap].link(moduleWraps); |
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 wonder if the separation of two methods is actually necessary? This just stashes the wraps into an internal map. It seems fine to just merge all this into instantiate()
and only do the conversion when instantiation happens. Users can maintain an array themselves before calling instantiate()
and that doesn't seem to make a whole lot of difference - the map is not really visible to them anyway. In return the API would look simpler and users can be more flexible in how they maintain that array e.g. fill them in a non-consecutive way, or modify them as much as needed.
On a related note, I don't think the resolve cache even strictly has to be in C++ land. The current way of managing the cache using a v8::Global may be leak prone since V8 doesn't necessary know that a specific Module wrap is the only one that holds a bunch others alive - it just sees a bunch of v8::Global "out of nowhere" because V8's GC can't trace what that map is doing in C++ obviously. That might be the cause of #50113 (or that was my conclusion ~2 years ago: #50113 (comment)). Moving the cache to JS land might make it go away since then V8 would be able to understand all the inter-module-wrap references. And if it's in JS land, the linkRequestedModules()
isn't strictly necessary because there might be better ways to interact with the cache. It looks more like a design that's specifically tailored to how we currently manage this cache, which is more of a thing that just happened but is not necessarily optimal design-wise.
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 necessary to split to link
and instantiate
to support cyclic module requests. Please have a look at test/parallel/test-vm-module-instantiate.js
.
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.
The resolve cache will be cleared immediately when instantiate
is invoked. It is only intended to be easily referenced in the ResolveModuleCallback
from V8. If a SourceTextModule
is correctly invoked with instantiate
after linkRequestedModules
, the resolve cache should not keep the modules alive indefinitely.
Lines 615 to 616 in e3e739d
// clear resolve cache on instantiate | |
obj->resolve_cache_.clear(); |
For #50113, I think an OOM can be reproduced without link
or evaluate
: #50113 (comment).
Split
module.link(linker)
into two synchronous stepsourceTextModule.linkRequestedModules()
andsourceTextModule.instantiate()
. This allows creating vm modules andresolving the dependencies in a complete synchronous procedure.
This also makes
syntheticModule.link()
redundant. The link step for aSyntheticModule is no-op and is already taken care in the constructor
by initializing the binding slots with the given export names.
This also enables (static) source phase import in
vm.SourceTextModule
s.Refs: #37648