Proposal to add a mechanism for enabling a more extensive standard library in JavaScript via a collection of built in modules. With this infrastructure in place it will be possible to start iterating on standard library features as additional modules.
This Standard defines the ECMAScript 2021 general-purpose programming language.
...
A Module Record encapsulates structural information about the imports and exports of a single module. This information is used to link the imports and exports of sets of connected modules. A Module Record includes four fields that are only used when evaluating a module.
For specification purposes Module Record values are values of the
Module Record defines the fields listed in
Field Name | Value Type | Meaning |
---|---|---|
[[Realm]] |
|
The |
[[Environment]] |
module |
The |
[[Namespace]] |
Object | |
The Module Namespace Object ( |
[[HostDefined]] |
Any, default value is |
Field reserved for use by host environments that need to associate additional information with a module. |
Method | Purpose |
---|---|
GetExportedNames([exportStarSet]) | Return a list of all names that are either directly or indirectly exported from this module. |
ResolveExport(exportName [, resolveSet]) |
Return the binding of a name exported by this module. Bindings are represented by a ResolvedBinding Record, of the form { [[Module]]: Each time this operation is called with a specific exportName, resolveSet pair as arguments it must return the same result if it completes normally. |
Link() |
Prepare the module for evaluation by transitively resolving all module dependencies and creating a module |
Evaluate() |
If this module has already been evaluated successfully, return Link must have completed successfully prior to invoking this method. |
A Synthetic Module Record is used to represent information about a module that is defined by specifications. Its exports are derived from a pair of lists, of string keys and of ECMAScript values. The set of exported names is static, and determined at creation time (as an argument to
In addition to the fields defined in
Field Name | Value Type | Meaning |
---|---|---|
[[ExportNames]] | A | |
[[IsFrozen]] | Boolean | If |
[[EvaluationSteps]] | An abstract operation | An abstract operation that will be performed upon evaluation of the module, taking the |
The abstract operation CreateSyntheticModule creates a
The following are the concrete methods for
The Freeze concrete method of a
It performs the following steps:
The GetExportedNames concrete method of a
It performs the following steps:
The ResolveExport concrete method of a
It performs the following steps:
The abstract operation SetModuleExport can be used to set or change the exported value for a pre-established export of a
The Instantiate concrete method of a
It performs the following steps:
The Evaluate concrete method of a
It performs the following steps:
This non-normative section shows how one could define a few different Synthetic Module Records.
The following algorithm, given an object object, creates a
"default"
», "default"
, module.[[HostDefined]]).The following algorithm creates a "add"
, which provides a built-in
"add"
», "add"
, adderFunction).An example adder function is an anonymous built-in function. When called with arguments arg1 and arg2, the following steps are taken:
HostHasBuiltinModule is a host-defined abstract operation that provides a means check for a module registered with import()
The implementation of
Multiple different referencingScriptOrModule, specifier pairs may map to the same
HostCreateBuiltinModule is a host-defined abstract operation that adds a module defined by the
The implementation of HostCreateBuiltinModule must conform to the following requirements:
HostResolveBuiltinModule is a host-defined abstract operation that provides the import()
It is possible for a host to implement this abstract operation with
The implementation of HostResolveBuiltinModule must conform to the following requirements:
Multiple different referencingScriptOrModule, specifier pairs may map to the same
HostResolveImportedModule is a host-defined abstract operation that provides the concrete import()
An example of when referencingScriptOrModule can be
<button type="button" onclick="import('./foo.mjs')">Click me</button>
there will be no active script or module at the time the import()
The implementation of HostResolveImportedModule must conform to the following requirements:
Multiple different referencingScriptOrModule, specifier pairs may map to the same
HostImportModuleDynamically is a host-defined abstract operation that performs any necessary setup work in order to make available the module corresponding to the import()
The implementation of HostImportModuleDynamically must conform to the following requirements:
The actual process performed is host-defined, but typically consists of performing whatever I/O operations are necessary to allow
The abstract operation FinishDynamicImport takes arguments referencingScriptOrModule, specifier, promiseCapability (a PromiseCapability import()
The abstract operation GetModuleNamespace takes argument module. It retrieves the Module Namespace Object representing module's exports, lazily creating it the first time it was requested, and storing it in module.[[Namespace]] for future retrieval. It performs the following steps when called:
The only way GetModuleNamespace can throw is via one of the triggered
The value of a
The Standard Library contains a set of modules builtin within the implementation. These builtin library modules may reside within the ECMAScript language implementation or they may be part of the containing host.
Importing a library follows the same syntax as importing any other type of module. The difference is the format of the js:
. Host environments can define additional
A Map of provided builtin modules is included in the
Field Name | Value | Meaning |
---|---|---|
[[Identifier]] | String | The name used to refer to this Builtin Library. This must be unique in the Map of builtin library modules. |
[[SyntheticModuleRecord]] | ModuleRecord | The module record for builtin library. |
[[HostDefined]] |
Any, default value is |
Field reserved for use by host environments that need to associate additional information with this Library. |
A Loader provides the means of resolving and loading builtin modules stored in an implementation defined way. There can be multiple loaders, each responsible for resolving and loading builtin modules. Multiple loaders can resolve the modules specified with the same ModuleSpecifier. A loader must implement the methods described in
The following additional fields are provided on the
Field Name | Value | Meaning |
---|---|---|
[[BuiltinModuleMap]] |
A |
Each Identifier is the same identifier of the associated |
[[Loader]] |
The ECMAScript language |
The |
A Builtin Module Resolution Record is used to represent information during the loading of a module built in to the host. Resolution and loading of these modules occurs locally.
Builtin Module Records are specified in
Field Name | Value Type | Meaning |
---|---|---|
[[Status]] | String |
Initially "uninitialized" . Transitions to "resolving" , "resolved" , "loading" , "loaded" (in that order) as the module progresses throughout the loading process.
|
[[ResolvingLoaders]] |
|
The list of all Loaders that can resolve and load this module. The list is sorted from highest priority to lowest. |
Builtin Module Resolution Records are used with the methods specified in
Method | Purpose |
---|---|
ResolveModuleIdentifier() | Determine if an imported can resolve a module. |
LoadModuleIdentifier() | Load a module from the current importer. |
The ResolveModuleIdentifier concrete method implements the module resolution part of the Builtin Module import process.
Each
If the "resolved"
and adds the current Loader to the front of the [[ResolvingLoaders]] list.
This abstract method performs the following steps:
"resolving "
or "resolved"
."js:"
, then"js:"
."resolved"
.The LoadModuleIdentifier concrete method implements the module loading part of the Builtin Module import process.
Each
If the "resolved"
and adds the current Loader to the front of the [[ResolvingLoaders]] list.
This abstract method performs the following steps:
"loading"
."js:"
."js:"
.Importing a Builtin Module involves resolving where the module can be loaded from and then actually loading the module. One or more Loaders can participate in the importing process.
A
ImportBuiltinModule is the abstract operation that returns the concrete
This abstract operation performs the following steps.
"uninitialized"
, [[ResolvingLoaders]]: "[ ]"
}....
See
See
See
See
See
The BuiltinModule object:
new
operator.When the export
function is called with arguments moduleSpecifier, and exports, the following steps are taken:
When the freezeModule
function is called with arguments moduleSpecifier, the following steps are taken:
When the freezeAllModules
function is called, the following steps are taken:
When the hasModule
function is called with arguments moduleSpecifier the following steps are taken:
When the export
function is called with arguments moduleSpecifier, the following steps are taken:
HostCallJobCallback(...)
HostEnqueueFinalizationRegistryCleanupJob(...)
HostEnqueuePromiseJob(...)
HostFinalizeImportMeta(...)
HostGetImportMetaProperties(...)
HostHasSourceTextAvailable(...)
HostMakeJobCallback(...)