[ECMA-262 layering] Refactor import-related host hooks - #8253
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Hi!
I plan to propose a refactor to the import-related host hooks of ECMA-262, and this PR shows how it would affect HTML.
Currently, ECMA-262 has two host hooks:
HostResolveImportedModule(referrer, specifier), which synchronously returns the Module Record corresponding to the(referrer, specifier)pair;HostImportModuleDynamically(referrer, specifier), used for dynamicimport(), which asynchronously loads the Module Record corresponding to(referrer, specifier)and all its dependencies, calls.Link()/.Evaluate()on it and then "returns" to ECMA-262 by callingFinishDynamicImport.We are working on three ECMA-262 proposals related to modules, and each one of them has needs that are not satisfied by the existing layering:
Instead of adding multiple new host hooks and duplicating the graph fetching algorithm, we can:
HostLoadImportedModule(referrer, specifier), that loads a single Module Record and can copmlete asynchronously: it's an async version ofHostResolveImportedModule.HostLoadImportedModule, which is called recursively for each dependency.Module Records will expose a third method, other than
.Link()and.Evaluate():.LoadRequestedModules(), which is the one responsible of callingHostLoadImportedModule.The host algorithm to load/execute a top-level module (for example, using a
<script type="module">tag) will now look like this.module.LoadRequestedModules():HostLoadImportedModule(module, specifier):module.Link()module.Evaluate()The algorithm for dynamic import will look like this.
HostLoadImportedModule(referrer, specifier):module.LoadRequestedModules():HostLoadImportedModule(module, specifier):module.Link()module.Evaluate()For the Module Blocks proposal, the algorithm to import a module block will look like the above dynamic import algorithm, except that we skip step 1.
For the Import Reflection proposal, the algorithm to load an uninstantiated JS module will just call
HostLoadImportedModulewithout then calling the various Module Record methods.For the Compartments proposal, we will be able to use the new graph loading algorithm by just replacing calls to
HostLoadImportedModulewith calls to an user-provided function.Some more precise remarks about these HTML changes
The only observable change (observable from HTML, rather than users) is that when you import the same specifier twice from the same file, only the first one causes a call toHostLoadImportedModule(unless the first one fails):In practice this doesn't change much, becauseHostImportModuleDynamically/HostResolveImportedModulewere already required to return the same Module Record both times.We still call the host hook multiple times if the first dynamic import fails, because there was an explicit goal of allowing to retry failed dynamic imports (Normative: change idempotency for HostImportModuleDynamically tc39/ecma262#1645).EDIT: For now I reverted this change, since even if it's not observable by HTML it's observable in other places.
The first commit is a small refactor only within HTML (it doesn't affect layering) that makes it easier to move the graph traversal to ECMA-262. I believe that it should not have observable effects (I wrote why in the commit description), but if it does I'll find an alternative solution.
I had to add an
hostDefinedparameter toLoadRequestedModules, which ECMA-262 passes as-is toHostLoadImportedModule, so that HTML can propagate the correct requestdestination. I couldn't just store the destination on the referrer Module Record's[[HostDefined]]slot because it should only be propagated through static imports and not dynamic ones.On the ECMA-262 side, this refactor doesn't take into account import assertions, because they have not been merged to the main spec yet. I wrote these HTML changes assuming that
HostLoadImportedModulewill receive a{ [[Specifier]], [[Assertions]] }record instead of just the specifier, similarly to how the import assertions proposal updatedHostResolveImportedModuleto receive{ [[Specifier]], [[Assertions]] }.Links
/index.html ( diff )
/infrastructure.html ( diff )
/webappapis.html ( diff )