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