-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
lib,src: iterate module requests of a module wrap in JS #52058
Merged
Merged
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
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 |
---|---|---|
|
@@ -2,17 +2,21 @@ | |
|
||
const assert = require('internal/assert'); | ||
const { | ||
Array, | ||
ArrayIsArray, | ||
ArrayPrototypeForEach, | ||
ArrayPrototypeIndexOf, | ||
ArrayPrototypeMap, | ||
ArrayPrototypeSome, | ||
ObjectDefineProperty, | ||
ObjectFreeze, | ||
ObjectGetPrototypeOf, | ||
ObjectPrototypeHasOwnProperty, | ||
ObjectSetPrototypeOf, | ||
PromiseResolve, | ||
PromisePrototypeThen, | ||
ReflectApply, | ||
SafePromiseAllReturnVoid, | ||
SafePromiseAllReturnArrayLike, | ||
Symbol, | ||
SymbolToStringTag, | ||
TypeError, | ||
|
@@ -294,44 +298,62 @@ class SourceTextModule extends Module { | |
importModuleDynamically, | ||
}); | ||
|
||
this[kLink] = async (linker) => { | ||
this.#statusOverride = 'linking'; | ||
this[kDependencySpecifiers] = undefined; | ||
} | ||
|
||
const promises = this[kWrap].link(async (identifier, attributes) => { | ||
const module = await linker(identifier, this, { attributes, assert: attributes }); | ||
if (!isModule(module)) { | ||
throw new ERR_VM_MODULE_NOT_MODULE(); | ||
} | ||
if (module.context !== this.context) { | ||
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT(); | ||
} | ||
if (module.status === 'errored') { | ||
throw new ERR_VM_MODULE_LINK_FAILURE(`request for '${identifier}' resolved to an errored module`, module.error); | ||
} | ||
if (module.status === 'unlinked') { | ||
await module[kLink](linker); | ||
} | ||
return module[kWrap]; | ||
async [kLink](linker) { | ||
this.#statusOverride = 'linking'; | ||
|
||
const moduleRequests = this[kWrap].getModuleRequests(); | ||
// Iterates the module requests and links with the linker. | ||
// Specifiers should be aligned with the moduleRequests array in order. | ||
const specifiers = Array(moduleRequests.length); | ||
const modulePromises = Array(moduleRequests.length); | ||
// Iterates with index to avoid calling into userspace with `Symbol.iterator`. | ||
for (let idx = 0; idx < moduleRequests.length; idx++) { | ||
const { specifier, attributes } = moduleRequests[idx]; | ||
|
||
const linkerResult = linker(specifier, this, { | ||
attributes, | ||
assert: attributes, | ||
}); | ||
const modulePromise = PromisePrototypeThen( | ||
PromiseResolve(linkerResult), async (module) => { | ||
if (!isModule(module)) { | ||
throw new ERR_VM_MODULE_NOT_MODULE(); | ||
} | ||
if (module.context !== this.context) { | ||
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT(); | ||
} | ||
if (module.status === 'errored') { | ||
throw new ERR_VM_MODULE_LINK_FAILURE(`request for '${specifier}' resolved to an errored module`, module.error); | ||
} | ||
if (module.status === 'unlinked') { | ||
await module[kLink](linker); | ||
} | ||
return module[kWrap]; | ||
}); | ||
modulePromises[idx] = modulePromise; | ||
specifiers[idx] = specifier; | ||
} | ||
|
||
try { | ||
if (promises !== undefined) { | ||
await SafePromiseAllReturnVoid(promises); | ||
} | ||
} catch (e) { | ||
this.#error = e; | ||
throw e; | ||
} finally { | ||
this.#statusOverride = undefined; | ||
} | ||
}; | ||
|
||
this[kDependencySpecifiers] = undefined; | ||
try { | ||
const modules = await SafePromiseAllReturnArrayLike(modulePromises); | ||
this[kWrap].link(specifiers, modules); | ||
} catch (e) { | ||
this.#error = e; | ||
throw e; | ||
} finally { | ||
this.#statusOverride = undefined; | ||
} | ||
} | ||
|
||
get dependencySpecifiers() { | ||
validateInternalField(this, kDependencySpecifiers, 'SourceTextModule'); | ||
this[kDependencySpecifiers] ??= ObjectFreeze(this[kWrap].getStaticDependencySpecifiers()); | ||
// TODO(legendecas): add a new getter to expose the import attributes as the value type | ||
// of [[RequestedModules]] is changed in https://tc39.es/proposal-import-attributes/#table-cyclic-module-fields. | ||
this[kDependencySpecifiers] ??= ObjectFreeze( | ||
ArrayPrototypeMap(this[kWrap].getModuleRequests(), (request) => request.specifier)); | ||
return this[kDependencySpecifiers]; | ||
} | ||
|
||
|
@@ -393,10 +415,10 @@ class SyntheticModule extends Module { | |
context, | ||
identifier, | ||
}); | ||
} | ||
|
||
this[kLink] = () => this[kWrap].link(() => { | ||
assert.fail('link callback should not be called'); | ||
}); | ||
[kLink]() { | ||
/** nothing to do for synthetic modules */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't need to be solved in this PR but it seems weird that we provide the link method for SyntheticModule at all.... |
||
} | ||
|
||
setExport(name, value) { | ||
|
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
Oops, something went wrong.
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.
This leads to holey arrays, it might still be better to just use ArrayPrototyprPush since this is in a hot path (I think ArrayPrototypePush can be optimized properly the last time I checked).
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.
Do you have a figure for it? As far as I can tell with the latest V8 version, using a microbenchmark to allocate a 20-length array with the two patterns, the result is outstanding that pre-allocating could be ~40% faster: https://jsbench.me/9tlvebajbe/1.
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 problem with holey arrays come more from the access instead of the creation, since V8 needs to defend against things like this:
Array.prototype[1] = 1;
let a = Array(2);
a[1]; // 1
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.
(Also, it's not blocking, I am fine landing with holey arrays, since it's probably still not that much work compared to all the other things the resolution is doing..)
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.
Thanks for bringing this up. I think in this particular case the array is not accessed repetitively so I'd find pre-allocation would be a good solution here.