Dynamic Modules allow non-source-text module records to be created and used by host environments.
In addition, they provide late definition of export bindings at the execution phase, to support named exports compatibility with legacy module systems.
1Dynamic Module Records
A Dynamic Module Record is used to represent information about a module that is defined programatically. Its fields contain digested information about the names that are exported by the module and its concrete methods use this digest to link, instantiate, and evaluate the module alongside other Abstract Module Records.
Dependency cycles with Source Text Module Records are avoided since these modules can only export, and not import from other Abstract Module Records.
Dynamic Module Records support late export binding, in that export names are only validated after execution.
In addition to the fields, defined in Table 37, Dynamic Module Records have the additional fields listed in Table 1. Each of these fields is initially set in CreateDynamicModule.
Table 1: Additional Fields of Dynamic Module Records
A completion of type throw representing the exception that occurred during evaluation. undefined if no exception occurred or if [[Status]] is not "evaluated".
1.1CreateDynamicModule ( realm, hostDefined )
This method would be expected to be called by the host when constructing a Module Record in HostResolveImportedModule.
The abstract operation CreateDynamicModule with arguments realm, and hostDefined creates a new Dynamic Module Record performing the following steps:
If module is not equal to nsModule or module.[[Status]] is not "evaluated", then
Return null.
Return module.[[exportNames]].
Note
The null return results in an error being thrown for star exports from dynamic modules, and creating an "unfinalized" namespace in the case of an uninstantiated dynamic namespace creation in cycles.
On success, Instantiate transitions this module's [[Status]] from "uninstantiated" to "instantiated". On failure, an exception is thrown and this module's [[Status]] remains "uninstantiated".
This abstract method performs the following steps:
Evaluate transitions this module's [[Status]] from "instantiated" to "evaluated".
If execution results in an exception, that exception is recorded in the [[EvaluationError]] field and rethrown by future invocations of Evaluate.
Evaluation of dynamic modules calls out to HostEvaluateDynamicModule, before finalising the export names of the dynamic modules. Any uninitialized export bindings throw a reference error at this stage.
This abstract method performs the following steps:
For each name that is an element of exportedNames, do
Let resolution be ? m.ResolveExport(name, « »).
If resolution is a ResolvedBinding Record, append name to unambiguousNames.
Let sortedExports be a new List containing the same values as the list unambiguousNames where the values are ordered as if an Array of the same values had been sorted using Array.prototype.sort using undefined as comparefn.
Set ns.[[Exports]] to sortedExports.
Set m.[[Status]] to "evaluated".
Assert: m.[[EvaluationError]] is undefined.
Return undefined.
1.6SetDynamicExportBinding ( name, value ) Concrete Method
The SetDynamicExportBinding concrete method of a Dynamic Module Record implements the corresponding Module Record abstract method for a string name and initialization value value.
After execution completion, only binding mutation is supported.
HostEvaluateDynamicModule is an implementation-defined abstract operation that performs programmatic execution of a Dynamic Module Record, dynamicModule.
The implementation of HostEvaluateDynamicModule must conform to the following requirements:
HostEvaluateDynamicModule will call the SetDynamicExportBinding concrete method on the dynamicModule to initialize the export bindings.
If there is an evaluation error, it must be thrown.
Note
HostEvaluateDynamicModule must not itself rely on checking what lexical bindings have already been initialized for the module. It is important that the bindings defined in evaluation are fully independent of what bindings are imported.