16 ECMAScript Language: Scripts and Modules
16.1 Scripts
Syntax
16.1.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
LexicallyDeclaredNames ofScriptBody contains any duplicate entries. -
It is a Syntax Error if any element of the
LexicallyDeclaredNames ofScriptBody also occurs in theVarDeclaredNames ofScriptBody .
-
It is a Syntax Error if
StatementList Contains superunless the source text containingsuperis eval code that is being processed by adirect eval . Additionalearly error rules forsuperwithindirect eval are defined in19.2.1.1 . -
It is a Syntax Error if
StatementList Contains NewTarget unless the source text containingNewTarget is eval code that is being processed by adirect eval . Additionalearly error rules forNewTarget indirect eval are defined in19.2.1.1 . -
It is a Syntax Error if
ContainsDuplicateLabels ofStatementList with argument « » istrue . -
It is a Syntax Error if
ContainsUndefinedBreakTarget ofStatementList with argument « » istrue . -
It is a Syntax Error if
ContainsUndefinedContinueTarget ofStatementList with arguments « » and « » istrue . -
It is a Syntax Error if
AllPrivateIdentifiersValid ofStatementList with argument « » isfalse unless the source text containingScriptBody is eval code that is being processed by adirect eval .
16.1.2 Static Semantics: IsStrict
The
- If
ScriptBody is present and theDirective Prologue ofScriptBody contains aUse Strict Directive , returntrue ; otherwise, returnfalse .
16.1.3 Runtime Semantics: Evaluation
- Return
undefined .
16.1.4 Script Records
A Script Record encapsulates information about a script being evaluated. Each script record contains the fields listed in
| Field Name | Value Type | Meaning |
|---|---|---|
| [[Realm]] |
a |
The |
| [[ECMAScriptCode]] |
a |
The result of parsing the source text of this script using |
| [[HostDefined]] |
anything (default value is |
Field reserved for use by |
16.1.5 ParseScript ( sourceText, realm, hostDefined )
The abstract operation ParseScript takes arguments sourceText (ECMAScript source text), realm, and hostDefined and returns a
- Let script be
ParseText (sourceText,Script ). - If script is a
List of errors, return script. - Return
Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: script, [[HostDefined]]: hostDefined }.
An implementation may parse script source text and analyse it for Early Error conditions prior to evaluation of ParseScript for that script source text. However, the reporting of any errors must be deferred until the point where this specification actually performs ParseScript upon that source text.
16.1.6 ScriptEvaluation ( scriptRecord )
The abstract operation ScriptEvaluation takes argument scriptRecord and returns either a
- Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
- Let scriptContext be a new ECMAScript code
execution context . - Set the Function of scriptContext to
null . - Set the
Realm of scriptContext to scriptRecord.[[Realm]]. - Set the ScriptOrModule of scriptContext to scriptRecord.
- Set the VariableEnvironment of scriptContext to globalEnv.
- Set the LexicalEnvironment of scriptContext to globalEnv.
- Set the PrivateEnvironment of scriptContext to
null . - Suspend the currently
running execution context . - Push scriptContext onto the
execution context stack ; scriptContext is now therunning execution context . - Let script be scriptRecord.[[ECMAScriptCode]].
- Let result be
Completion (GlobalDeclarationInstantiation (script, globalEnv)). - If result.[[Type]] is
normal , then- Set result to the result of evaluating script.
- If result.[[Type]] is
normal and result.[[Value]] isempty , then- Set result to
NormalCompletion (undefined ).
- Set result to
- Suspend scriptContext and remove it from the
execution context stack . Assert : Theexecution context stack is not empty.- Resume the context that is now on the top of the
execution context stack as therunning execution context . - Return ? result.
16.1.7 GlobalDeclarationInstantiation ( script, env )
The abstract operation GlobalDeclarationInstantiation takes arguments script (a
When an
It performs the following steps when called:
- Let lexNames be the
LexicallyDeclaredNames of script. - Let varNames be the
VarDeclaredNames of script. - For each element name of lexNames, do
- If env.HasVarDeclaration(name) is
true , throw aSyntaxError exception. - If env.HasLexicalDeclaration(name) is
true , throw aSyntaxError exception. - Let hasRestrictedGlobal be ? env.HasRestrictedGlobalProperty(name).
- If hasRestrictedGlobal is
true , throw aSyntaxError exception.
- If env.HasVarDeclaration(name) is
- For each element name of varNames, do
- If env.HasLexicalDeclaration(name) is
true , throw aSyntaxError exception.
- If env.HasLexicalDeclaration(name) is
- Let varDeclarations be the
VarScopedDeclarations of script. - Let functionsToInitialize be a new empty
List . - Let declaredFunctionNames be a new empty
List . - For each element d of varDeclarations, in reverse
List order, do- If d is neither a
VariableDeclaration nor aForBinding nor aBindingIdentifier , thenAssert : d is either aFunctionDeclaration , aGeneratorDeclaration , anAsyncFunctionDeclaration , or anAsyncGeneratorDeclaration .- NOTE: If there are multiple function declarations for the same name, the last declaration is used.
- Let fn be the sole element of the
BoundNames of d. - If fn is not an element of declaredFunctionNames, then
- Let fnDefinable be ? env.CanDeclareGlobalFunction(fn).
- If fnDefinable is
false , throw aTypeError exception. - Append fn to declaredFunctionNames.
- Insert d as the first element of functionsToInitialize.
- If d is neither a
- Let declaredVarNames be a new empty
List . - For each element d of varDeclarations, do
- If d is a
VariableDeclaration , aForBinding , or aBindingIdentifier , then- For each String vn of the
BoundNames of d, do- If vn is not an element of declaredFunctionNames, then
- Let vnDefinable be ? env.CanDeclareGlobalVar(vn).
- If vnDefinable is
false , throw aTypeError exception. - If vn is not an element of declaredVarNames, then
- Append vn to declaredVarNames.
- If vn is not an element of declaredFunctionNames, then
- For each String vn of the
- If d is a
- NOTE: No abnormal terminations occur after this algorithm step if the
global object is anordinary object . However, if theglobal object is aProxy exotic object it may exhibit behaviours that cause abnormal terminations in some of the following steps. - NOTE: Annex
B.3.2.2 adds additional steps at this point. - Let lexDeclarations be the
LexicallyScopedDeclarations of script. - Let privateEnv be
null . - For each element d of lexDeclarations, do
- NOTE: Lexically declared names are only instantiated here but not initialized.
- For each element dn of the
BoundNames of d, do- If
IsConstantDeclaration of d istrue , then- Perform ?
env.CreateImmutableBinding (dn,true ).
- Perform ?
- Else,
- Perform ?
env.CreateMutableBinding (dn,false ).
- Perform ?
- If
- For each
Parse Node f of functionsToInitialize, do- Let fn be the sole element of the
BoundNames of f. - Let fo be
InstantiateFunctionObject of f with arguments env and privateEnv. - Perform ?
env.CreateGlobalFunctionBinding (fn, fo,false ).
- Let fn be the sole element of the
- For each String vn of declaredVarNames, do
- Perform ?
env.CreateGlobalVarBinding (vn,false ).
- Perform ?
- Return
unused .
Unlike explicit var or function declarations, properties that are directly created on the
16.2 Modules
Syntax
16.2.1 Module Semantics
16.2.1.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
LexicallyDeclaredNames ofModuleItemList contains any duplicate entries. -
It is a Syntax Error if any element of the
LexicallyDeclaredNames ofModuleItemList also occurs in theVarDeclaredNames ofModuleItemList . -
It is a Syntax Error if the
ExportedNames ofModuleItemList contains any duplicate entries. -
It is a Syntax Error if any element of the
ExportedBindings ofModuleItemList does not also occur in either theVarDeclaredNames ofModuleItemList , or theLexicallyDeclaredNames ofModuleItemList . -
It is a Syntax Error if
ModuleItemList Contains super. -
It is a Syntax Error if
ModuleItemList Contains NewTarget . -
It is a Syntax Error if
ContainsDuplicateLabels ofModuleItemList with argument « » istrue . -
It is a Syntax Error if
ContainsUndefinedBreakTarget ofModuleItemList with argument « » istrue . -
It is a Syntax Error if
ContainsUndefinedContinueTarget ofModuleItemList with arguments « » and « » istrue . -
It is a Syntax Error if
AllPrivateIdentifiersValid ofModuleItemList with argument « » isfalse .
The duplicate export default
- It is a Syntax Error if
IsStringWellFormedUnicode (theSV ofStringLiteral ) isfalse .
16.2.1.2 Static Semantics: ImportedLocalNames ( importEntries )
The abstract operation ImportedLocalNames takes argument importEntries (a
- Let localNames be a new empty
List . - For each
ImportEntry Record i of importEntries, do- Append i.[[LocalName]] to localNames.
- Return localNames.
16.2.1.3 Static Semantics: ModuleRequests
The
- Return a new empty
List .
- Return ModuleRequests of
ModuleItem .
- Let moduleNames be ModuleRequests of
ModuleItemList . - Let additionalNames be ModuleRequests of
ModuleItem . - Append to moduleNames each element of additionalNames that is not already an element of moduleNames.
- Return moduleNames.
- Return a new empty
List .
- Return ModuleRequests of
FromClause .
- Return a
List whose sole element is theSV ofStringLiteral .
- Return the ModuleRequests of
FromClause .
- Return a new empty
List .
16.2.1.4 Abstract Module Records
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]] |
a |
The |
| [[Environment]] |
a |
The |
| [[Namespace]] |
an Object or |
The Module Namespace Object ( |
| [[HostDefined]] |
anything (default value is |
Field reserved for use by |
| 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]]: Module Record, [[BindingName]]: String | 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 |
| Evaluate() |
Returns a promise for the evaluation of this module and its dependencies, resolving on successful evaluation or if it has already been evaluated successfully, and rejecting for an evaluation error or if it has already been evaluated unsuccessfully. If the promise is rejected, Link must have completed successfully prior to invoking this method. |
16.2.1.5 Cyclic Module Records
A Cyclic Module Record is used to represent information about a module that can participate in dependency cycles with other modules that are subclasses of the
In addition to the fields defined in
| Field Name | Value Type | Meaning |
|---|---|---|
| [[Status]] |
|
Initially |
| [[EvaluationError]] |
an |
A |
| [[DFSIndex]] |
an |
Auxiliary field used during Link and Evaluate only. If [[Status]] is |
| [[DFSAncestorIndex]] |
an |
Auxiliary field used during Link and Evaluate only. If [[Status]] is |
| [[RequestedModules]] |
a |
A |
| [[CycleRoot]] |
a |
The first visited module of the cycle, the root DFS ancestor of the strongly connected component. For a module not in a cycle this would be the module itself. Once Evaluate has completed, a module's [[DFSAncestorIndex]] is equal to the [[DFSIndex]] of its [[CycleRoot]]. |
| [[HasTLA]] | a Boolean |
Whether this module is individually asynchronous (for example, if it's a |
| [[AsyncEvaluation]] | a Boolean |
Whether this module is either itself asynchronous or has an asynchronous dependency. Note: The order in which this field is set is used to order queued executions, see |
| [[TopLevelCapability]] |
a |
If this module is the [[CycleRoot]] of some cycle, and Evaluate() was called on some module in that cycle, this field contains the |
| [[AsyncParentModules]] |
a |
If this module or a dependency has [[HasTLA]] |
| [[PendingAsyncDependencies]] |
an |
If this module has any asynchronous dependencies, this tracks the number of asynchronous dependency modules remaining to execute for this module. A module with asynchronous dependencies will be executed when this field reaches 0 and there are no execution errors. |
In addition to the methods defined in
| Method | Purpose |
|---|---|
| InitializeEnvironment() |
Initialize the |
| ExecuteModule( [ promiseCapability ] ) |
Evaluate the module's code within its |
16.2.1.5.1 Link ( )
The Link concrete method of a
Assert : module.[[Status]] is notlinking orevaluating .- Let stack be a new empty
List . - Let result be
Completion (InnerModuleLinking (module, stack, 0)). - If result is an
abrupt completion , then- For each
Cyclic Module Record m of stack, doAssert : m.[[Status]] islinking .- Set m.[[Status]] to
unlinked .
Assert : module.[[Status]] isunlinked .- Return ? result.
- For each
Assert : module.[[Status]] islinked ,evaluating-async , orevaluated .Assert : stack is empty.- Return
unused .
16.2.1.5.1.1 InnerModuleLinking ( module, stack, index )
The abstract operation InnerModuleLinking takes arguments module (a
- If module is not a
Cyclic Module Record , then- Perform ? module.Link().
- Return index.
- If module.[[Status]] is
linking ,linked ,evaluating-async , orevaluated , then- Return index.
Assert : module.[[Status]] isunlinked .- Set module.[[Status]] to
linking . - Set module.[[DFSIndex]] to index.
- Set module.[[DFSAncestorIndex]] to index.
- Set index to index + 1.
- Append module to stack.
- For each String required of module.[[RequestedModules]], do
- Let requiredModule be ?
HostResolveImportedModule (module, required). - Set index to ? InnerModuleLinking(requiredModule, stack, index).
- If requiredModule is a
Cyclic Module Record , thenAssert : requiredModule.[[Status]] is eitherlinking ,linked ,evaluating-async , orevaluated .Assert : requiredModule.[[Status]] islinking if and only if requiredModule is in stack.- If requiredModule.[[Status]] is
linking , then- Set module.[[DFSAncestorIndex]] to
min (module.[[DFSAncestorIndex]], requiredModule.[[DFSAncestorIndex]]).
- Set module.[[DFSAncestorIndex]] to
- Let requiredModule be ?
- Perform ? module.InitializeEnvironment().
Assert : module occurs exactly once in stack.Assert : module.[[DFSAncestorIndex]] ≤ module.[[DFSIndex]].- If module.[[DFSAncestorIndex]] = module.[[DFSIndex]], then
- Let done be
false . - Repeat, while done is
false ,- Let requiredModule be the last element in stack.
- Remove the last element of stack.
Assert : requiredModule is aCyclic Module Record .- Set requiredModule.[[Status]] to
linked . - If requiredModule and module are the same
Module Record , set done totrue .
- Let done be
- Return index.
16.2.1.5.2 Evaluate ( )
The Evaluate concrete method of a
Assert : This call to Evaluate is not happening at the same time as another call to Evaluate within thesurrounding agent .Assert : module.[[Status]] islinked ,evaluating-async , orevaluated .- If module.[[Status]] is
evaluating-async orevaluated , set module to module.[[CycleRoot]]. - If module.[[TopLevelCapability]] is not
empty , then- Return module.[[TopLevelCapability]].[[Promise]].
- Let stack be a new empty
List . - Let capability be !
NewPromiseCapability (%Promise% ). - Set module.[[TopLevelCapability]] to capability.
- Let result be
Completion (InnerModuleEvaluation (module, stack, 0)). - If result is an
abrupt completion , then- For each
Cyclic Module Record m of stack, doAssert : m.[[Status]] isevaluating .- Set m.[[Status]] to
evaluated . - Set m.[[EvaluationError]] to result.
Assert : module.[[Status]] isevaluated .Assert : module.[[EvaluationError]] is result.- Perform !
Call (capability.[[Reject]],undefined , « result.[[Value]] »).
- For each
- Else,
- Return capability.[[Promise]].
16.2.1.5.2.1 InnerModuleEvaluation ( module, stack, index )
The abstract operation InnerModuleEvaluation takes arguments module (a
- If module is not a
Cyclic Module Record , then- Let promise be ! module.Evaluate().
Assert : promise.[[PromiseState]] is notpending .- If promise.[[PromiseState]] is
rejected , then- Return
ThrowCompletion (promise.[[PromiseResult]]).
- Return
- Return index.
- If module.[[Status]] is
evaluating-async orevaluated , then- If module.[[EvaluationError]] is
empty , return index. - Otherwise, return ? module.[[EvaluationError]].
- If module.[[EvaluationError]] is
- If module.[[Status]] is
evaluating , return index. Assert : module.[[Status]] islinked .- Set module.[[Status]] to
evaluating . - Set module.[[DFSIndex]] to index.
- Set module.[[DFSAncestorIndex]] to index.
- Set module.[[PendingAsyncDependencies]] to 0.
- Set index to index + 1.
- Append module to stack.
- For each String required of module.[[RequestedModules]], do
- Let requiredModule be !
HostResolveImportedModule (module, required). - NOTE: Link must be completed successfully prior to invoking this method, so every requested module is guaranteed to resolve successfully.
- Set index to ? InnerModuleEvaluation(requiredModule, stack, index).
- If requiredModule is a
Cyclic Module Record , thenAssert : requiredModule.[[Status]] is eitherevaluating ,evaluating-async , orevaluated .Assert : requiredModule.[[Status]] isevaluating if and only if requiredModule is in stack.- If requiredModule.[[Status]] is
evaluating , then- Set module.[[DFSAncestorIndex]] to
min (module.[[DFSAncestorIndex]], requiredModule.[[DFSAncestorIndex]]).
- Set module.[[DFSAncestorIndex]] to
- Else,
- Set requiredModule to requiredModule.[[CycleRoot]].
Assert : requiredModule.[[Status]] isevaluating-async orevaluated .- If requiredModule.[[EvaluationError]] is not
empty , return ? requiredModule.[[EvaluationError]].
- If requiredModule.[[AsyncEvaluation]] is
true , then- Set module.[[PendingAsyncDependencies]] to module.[[PendingAsyncDependencies]] + 1.
- Append module to requiredModule.[[AsyncParentModules]].
- Let requiredModule be !
- If module.[[PendingAsyncDependencies]] > 0 or module.[[HasTLA]] is
true , thenAssert : module.[[AsyncEvaluation]] isfalse and was never previously set totrue .- Set module.[[AsyncEvaluation]] to
true . - NOTE: The order in which module records have their [[AsyncEvaluation]] fields transition to
true is significant. (See16.2.1.5.2.4 .) - If module.[[PendingAsyncDependencies]] is 0, perform
ExecuteAsyncModule (module).
- Otherwise, perform ?
module.ExecuteModule() . Assert : module occurs exactly once in stack.Assert : module.[[DFSAncestorIndex]] ≤ module.[[DFSIndex]].- If module.[[DFSAncestorIndex]] = module.[[DFSIndex]], then
- Let done be
false . - Repeat, while done is
false ,- Let requiredModule be the last element in stack.
- Remove the last element of stack.
Assert : requiredModule is aCyclic Module Record .- If requiredModule.[[AsyncEvaluation]] is
false , set requiredModule.[[Status]] toevaluated . - Otherwise, set requiredModule.[[Status]] to
evaluating-async . - If requiredModule and module are the same
Module Record , set done totrue . - Set requiredModule.[[CycleRoot]] to module.
- Let done be
- Return index.
A module is
Any modules depending on a module of an asynchronous cycle when that cycle is not
16.2.1.5.2.2 ExecuteAsyncModule ( module )
The abstract operation ExecuteAsyncModule takes argument module (a
Assert : module.[[Status]] isevaluating orevaluating-async .Assert : module.[[HasTLA]] istrue .- Let capability be !
NewPromiseCapability (%Promise% ). - Let fulfilledClosure be a new
Abstract Closure with no parameters that captures module and performs the following steps when called:- Perform
AsyncModuleExecutionFulfilled (module). - Return
undefined .
- Perform
- Let onFulfilled be
CreateBuiltinFunction (fulfilledClosure, 0,"" , « »). - Let rejectedClosure be a new
Abstract Closure with parameters (error) that captures module and performs the following steps when called:- Perform
AsyncModuleExecutionRejected (module, error). - Return
undefined .
- Perform
- Let onRejected be
CreateBuiltinFunction (rejectedClosure, 0,"" , « »). - Perform
PerformPromiseThen (capability.[[Promise]], onFulfilled, onRejected). - Perform !
module.ExecuteModule (capability). - Return
unused .
16.2.1.5.2.3 GatherAvailableAncestors ( module, execList )
The abstract operation GatherAvailableAncestors takes arguments module (a
- For each
Cyclic Module Record m of module.[[AsyncParentModules]], do- If execList does not contain m and m.[[CycleRoot]].[[EvaluationError]] is
empty , thenAssert : m.[[Status]] isevaluating-async .Assert : m.[[EvaluationError]] isempty .Assert : m.[[AsyncEvaluation]] istrue .Assert : m.[[PendingAsyncDependencies]] > 0.- Set m.[[PendingAsyncDependencies]] to m.[[PendingAsyncDependencies]] - 1.
- If m.[[PendingAsyncDependencies]] = 0, then
- Append m to execList.
- If m.[[HasTLA]] is
false , perform GatherAvailableAncestors(m, execList).
- If execList does not contain m and m.[[CycleRoot]].[[EvaluationError]] is
- Return
unused .
When an asynchronous execution for a root module is fulfilled, this function determines the list of modules which are able to synchronously execute together on this completion, populating them in execList.
16.2.1.5.2.4 AsyncModuleExecutionFulfilled ( module )
The abstract operation AsyncModuleExecutionFulfilled takes argument module (a
- If module.[[Status]] is
evaluated , thenAssert : module.[[EvaluationError]] is notempty .- Return
unused .
Assert : module.[[Status]] isevaluating-async .Assert : module.[[AsyncEvaluation]] istrue .Assert : module.[[EvaluationError]] isempty .- Set module.[[AsyncEvaluation]] to
false . - Set module.[[Status]] to
evaluated . - If module.[[TopLevelCapability]] is not
empty , then - Let execList be a new empty
List . - Perform
GatherAvailableAncestors (module, execList). - Let sortedExecList be a
List whose elements are the elements of execList, in the order in which they had their [[AsyncEvaluation]] fields set totrue inInnerModuleEvaluation . Assert : All elements of sortedExecList have their [[AsyncEvaluation]] field set totrue , [[PendingAsyncDependencies]] field set to 0, and [[EvaluationError]] field set toempty .- For each
Cyclic Module Record m of sortedExecList, do- If m.[[Status]] is
evaluated , thenAssert : m.[[EvaluationError]] is notempty .
- Else if m.[[HasTLA]] is
true , then- Perform
ExecuteAsyncModule (m).
- Perform
- Else,
- Let result be
m.ExecuteModule() . - If result is an
abrupt completion , then- Perform
AsyncModuleExecutionRejected (m, result.[[Value]]).
- Perform
- Else,
- Let result be
- If m.[[Status]] is
- Return
unused .
16.2.1.5.2.5 AsyncModuleExecutionRejected ( module, error )
The abstract operation AsyncModuleExecutionRejected takes arguments module (a
- If module.[[Status]] is
evaluated , thenAssert : module.[[EvaluationError]] is notempty .- Return
unused .
Assert : module.[[Status]] isevaluating-async .Assert : module.[[AsyncEvaluation]] istrue .Assert : module.[[EvaluationError]] isempty .- Set module.[[EvaluationError]] to
ThrowCompletion (error). - Set module.[[Status]] to
evaluated . - For each
Cyclic Module Record m of module.[[AsyncParentModules]], do- Perform AsyncModuleExecutionRejected(m, error).
- If module.[[TopLevelCapability]] is not
empty , then - Return
unused .
16.2.1.5.3 Example Cyclic Module Record Graphs
This non-normative section gives a series of examples of the linking and evaluation of a few common module graphs, with a specific focus on how errors can occur.
First consider the following simple module graph:
Let's first assume that there are no error conditions. When a
Consider then cases involving linking errors. If
Finally, consider a case involving evaluation errors. If
The difference here between linking and evaluation errors is due to how evaluation must be only performed once, as it can cause side effects; it is thus important to remember whether evaluation has already been performed, even if unsuccessfully. (In the error case, it makes sense to also remember the exception because otherwise subsequent Evaluate() calls would have to synthesize a new one.) Linking, on the other hand, is side-effect-free, and thus even if it fails, it can be retried at a later time with no issues.
Now consider a different type of error condition:
In this scenario, module A declares a dependency on some other module, but no
Now, consider a module graph with a cycle:
Here we assume that the entry point is module A, so that the
An analogous story occurs for the evaluation phase of a cyclic module graph, in the success case.
Now consider a case where A has an linking error; for example, it tries to import a binding from C that does not exist. In that case, the above steps still occur, including the early return from the second call to
Alternatively, consider a case where A has an evaluation error; for example, its source code throws an exception. In that case, the evaluation-time analog of the above steps still occurs, including the early return from the second call to await through the whole dependency graph through the
Lastly, consider a module graph with a cycle, where all modules complete asynchronously:
Linking happens as before, and all modules end up with [[Status]] set to
Calling A.Evaluate() calls
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] |
|---|---|---|---|---|---|---|
| A | 0 | 0 | « » | 2 (B and C) | ||
| B | 1 | 0 | « A » | 1 (D) | ||
| C | 2 | 0 | « A » | 2 (D and E) | ||
| D | 3 | 0 | « B, C » | 0 | ||
| E | 4 | 4 | « C » | 0 |
Let us assume that E finishes executing first. When that happens,
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] |
|---|---|---|---|---|---|---|
| C | 2 | 0 | « A » | 1 (D) | ||
| E | 4 | 4 | « C » | 0 |
D is next to finish (as it was the only module that was still executing). When that happens, await). The fields of the updated modules are as given in
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] |
|---|---|---|---|---|---|---|
| B | 1 | 0 | « A » | 0 | ||
| C | 2 | 0 | « A » | 0 | ||
| D | 3 | 0 | « B, C » | 0 |
Let us assume that C finishes executing next. When that happens,
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] |
|---|---|---|---|---|---|---|
| A | 0 | 0 | « » | 1 (B) | ||
| C | 2 | 0 | « A » | 0 |
Then, B finishes executing. When that happens,
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] |
|---|---|---|---|---|---|---|
| A | 0 | 0 | « » | 0 | ||
| B | 1 | 0 | « A » | 0 |
Finally, A finishes executing. When that happens,
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] |
|---|---|---|---|---|---|---|
| A | 0 | 0 | « » | 0 |
Alternatively, consider a failure case where C fails execution and returns an error before B has finished executing. When that happens,
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] | [[EvaluationError]] |
|---|---|---|---|---|---|---|---|
| A | 0 | 0 | « » | 1 (B) | |||
| C | 2 | 1 | « A » | 0 | C's evaluation error |
A will be rejected with the same error as C since C will call
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] | [[EvaluationError]] |
|---|---|---|---|---|---|---|---|
| A | 0 | 0 | « » | 0 | C's |
Then, B finishes executing without an error. When that happens,
| Module | [[DFSIndex]] | [[DFSAncestorIndex]] | [[Status]] | [[AsyncEvaluation]] | [[AsyncParentModules]] | [[PendingAsyncDependencies]] | [[EvaluationError]] |
|---|---|---|---|---|---|---|---|
| A | 0 | 0 | « » | 0 | C's |
||
| B | 1 | 0 | « A » | 0 |
16.2.1.6 Source Text Module Records
A Source Text Module Record is used to represent information about a module that was defined from ECMAScript source text (
A
In addition to the fields defined in
| Field Name | Value Type | Meaning |
|---|---|---|
| [[ECMAScriptCode]] |
a |
The result of parsing the source text of this module using |
| [[Context]] |
an ECMAScript |
The |
| [[ImportMeta]] | an Object |
An object exposed through the import.meta meta property. It is |
| [[ImportEntries]] |
a |
A |
| [[LocalExportEntries]] |
a |
A |
| [[IndirectExportEntries]] |
a |
A export * as namespace declarations.
|
| [[StarExportEntries]] |
a |
A export * declarations that occur within the module, not including export * as namespace declarations.
|
An ImportEntry Record is a
| Field Name | Value Type | Meaning |
|---|---|---|
| [[ModuleRequest]] | a String |
String value of the |
| [[ImportName]] |
a String or |
The name under which the desired binding is exported by the module identified by [[ModuleRequest]]. The value |
| [[LocalName]] | a String | The name that is used to locally access the imported value from within the importing module. |
| Import Statement Form | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] |
|---|---|---|---|
import v from "mod";
|
|
|
|
import * as ns from "mod";
|
|
|
|
import {x} from "mod";
|
|
|
|
import {x as v} from "mod";
|
|
|
|
import "mod";
|
An |
||
An ExportEntry Record is a
| Field Name | Value Type | Meaning |
|---|---|---|
| [[ExportName]] |
a String or |
The name used to export this binding by this module. |
| [[ModuleRequest]] |
a String or |
The String value of the |
| [[ImportName]] |
a String, |
The name under which the desired binding is exported by the module identified by [[ModuleRequest]]. export * as ns from "mod" declarations. export * from "mod" declarations.
|
| [[LocalName]] |
a String or |
The name that is used to locally access the exported value from within the importing module. |
| Export Statement Form | [[ExportName]] | [[ModuleRequest]] | [[ImportName]] | [[LocalName]] |
|---|---|---|---|---|
export var v;
|
|
|
|
|
export default function f() {}
|
|
|
|
|
export default function () {}
|
|
|
|
|
export default 42;
|
|
|
|
|
export {x};
|
|
|
|
|
export {v as x};
|
|
|
|
|
export {x} from "mod";
|
|
|
|
|
export {v as x} from "mod";
|
|
|
|
|
export * from "mod";
|
|
|
|
|
export * as ns from "mod";
|
|
|
|
|
The following definitions specify the required concrete methods and other
16.2.1.6.1 ParseModule ( sourceText, realm, hostDefined )
The abstract operation ParseModule takes arguments sourceText (ECMAScript source text), realm, and hostDefined and returns a
- Let body be
ParseText (sourceText,Module ). - If body is a
List of errors, return body. - Let requestedModules be the
ModuleRequests of body. - Let importEntries be
ImportEntries of body. - Let importedBoundNames be
ImportedLocalNames (importEntries). - Let indirectExportEntries be a new empty
List . - Let localExportEntries be a new empty
List . - Let starExportEntries be a new empty
List . - Let exportEntries be
ExportEntries of body. - For each
ExportEntry Record ee of exportEntries, do- If ee.[[ModuleRequest]] is
null , then- If ee.[[LocalName]] is not an element of importedBoundNames, then
- Append ee to localExportEntries.
- Else,
- Let ie be the element of importEntries whose [[LocalName]] is the same as ee.[[LocalName]].
- If ie.[[ImportName]] is
namespace-object , then- NOTE: This is a re-export of an imported module namespace object.
- Append ee to localExportEntries.
- Else,
- NOTE: This is a re-export of a single name.
- Append the
ExportEntry Record { [[ModuleRequest]]: ie.[[ModuleRequest]], [[ImportName]]: ie.[[ImportName]], [[LocalName]]:null , [[ExportName]]: ee.[[ExportName]] } to indirectExportEntries.
- If ee.[[LocalName]] is not an element of importedBoundNames, then
- Else if ee.[[ImportName]] is
all-but-default , thenAssert : ee.[[ExportName]] isnull .- Append ee to starExportEntries.
- Else,
- Append ee to indirectExportEntries.
- If ee.[[ModuleRequest]] is
- Let async be body
Contains await. - Return
Source Text Module Record { [[Realm]]: realm, [[Environment]]:empty , [[Namespace]]:empty , [[CycleRoot]]:empty , [[HasTLA]]: async, [[AsyncEvaluation]]:false , [[TopLevelCapability]]:empty , [[AsyncParentModules]]: « », [[PendingAsyncDependencies]]:empty , [[Status]]:unlinked , [[EvaluationError]]:empty , [[HostDefined]]: hostDefined, [[ECMAScriptCode]]: body, [[Context]]:empty , [[ImportMeta]]:empty , [[RequestedModules]]: requestedModules, [[ImportEntries]]: importEntries, [[LocalExportEntries]]: localExportEntries, [[IndirectExportEntries]]: indirectExportEntries, [[StarExportEntries]]: starExportEntries, [[DFSIndex]]:empty , [[DFSAncestorIndex]]:empty }.
An implementation may parse module source text and analyse it for Early Error conditions prior to the evaluation of ParseModule for that module source text. However, the reporting of any errors must be deferred until the point where this specification actually performs ParseModule upon that source text.
16.2.1.6.2 GetExportedNames ( [ exportStarSet ] )
The GetExportedNames concrete method of a
- If exportStarSet is not present, set exportStarSet to a new empty
List . - If exportStarSet contains module, then
- Append module to exportStarSet.
- Let exportedNames be a new empty
List . - For each
ExportEntry Record e of module.[[LocalExportEntries]], doAssert : module provides the direct binding for this export.- Append e.[[ExportName]] to exportedNames.
- For each
ExportEntry Record e of module.[[IndirectExportEntries]], doAssert : module imports a specific binding for this export.- Append e.[[ExportName]] to exportedNames.
- For each
ExportEntry Record e of module.[[StarExportEntries]], do- Let requestedModule be ?
HostResolveImportedModule (module, e.[[ModuleRequest]]). - Let starNames be ? requestedModule.GetExportedNames(exportStarSet).
- For each element n of starNames, do
- If
SameValue (n,"default" ) isfalse , then- If n is not an element of exportedNames, then
- Append n to exportedNames.
- If n is not an element of exportedNames, then
- If
- Let requestedModule be ?
- Return exportedNames.
GetExportedNames does not filter out or throw an exception for names that have ambiguous star export bindings.
16.2.1.6.3 ResolveExport ( exportName [ , resolveSet ] )
The ResolveExport concrete method of a
ResolveExport attempts to resolve an imported binding to the actual defining module and local binding name. The defining module may be the module represented by the
If a defining module is found, a
It performs the following steps when called:
- If resolveSet is not present, set resolveSet to a new empty
List . - For each
Record { [[Module]], [[ExportName]] } r of resolveSet, do- If module and r.[[Module]] are the same
Module Record andSameValue (exportName, r.[[ExportName]]) istrue , thenAssert : This is a circular import request.- Return
null .
- If module and r.[[Module]] are the same
- Append the
Record { [[Module]]: module, [[ExportName]]: exportName } to resolveSet. - For each
ExportEntry Record e of module.[[LocalExportEntries]], do- If
SameValue (exportName, e.[[ExportName]]) istrue , thenAssert : module provides the direct binding for this export.- Return
ResolvedBinding Record { [[Module]]: module, [[BindingName]]: e.[[LocalName]] }.
- If
- For each
ExportEntry Record e of module.[[IndirectExportEntries]], do- If
SameValue (exportName, e.[[ExportName]]) istrue , then- Let importedModule be ?
HostResolveImportedModule (module, e.[[ModuleRequest]]). - If e.[[ImportName]] is
all , thenAssert : module does not provide the direct binding for this export.- Return
ResolvedBinding Record { [[Module]]: importedModule, [[BindingName]]:namespace }.
- Else,
Assert : module imports a specific binding for this export.- Return ? importedModule.ResolveExport(e.[[ImportName]], resolveSet).
- Let importedModule be ?
- If
- If
SameValue (exportName,"default" ) istrue , thenAssert : Adefaultexport was not explicitly defined by this module.- Return
null . - NOTE: A
defaultexport cannot be provided by anexport * from "mod"declaration.
- Let starResolution be
null . - For each
ExportEntry Record e of module.[[StarExportEntries]], do- Let importedModule be ?
HostResolveImportedModule (module, e.[[ModuleRequest]]). - Let resolution be ? importedModule.ResolveExport(exportName, resolveSet).
- If resolution is
ambiguous , returnambiguous . - If resolution is not
null , thenAssert : resolution is aResolvedBinding Record .- If starResolution is
null , set starResolution to resolution. - Else,
Assert : There is more than one*import that includes the requested name.- If resolution.[[Module]] and starResolution.[[Module]] are not the same
Module Record , returnambiguous . - If resolution.[[BindingName]] is
namespace and starResolution.[[BindingName]] is notnamespace , or if resolution.[[BindingName]] is notnamespace and starResolution.[[BindingName]] isnamespace , returnambiguous . - If resolution.[[BindingName]] is a String, starResolution.[[BindingName]] is a String, and
SameValue (resolution.[[BindingName]], starResolution.[[BindingName]]) isfalse , returnambiguous .
- Let importedModule be ?
- Return starResolution.
16.2.1.6.4 InitializeEnvironment ( )
The InitializeEnvironment concrete method of a
- For each
ExportEntry Record e of module.[[IndirectExportEntries]], do- Let resolution be ? module.ResolveExport(e.[[ExportName]]).
- If resolution is
null orambiguous , throw aSyntaxError exception. Assert : resolution is aResolvedBinding Record .
Assert : All named exports from module are resolvable.- Let realm be module.[[Realm]].
Assert : realm is notundefined .- Let env be
NewModuleEnvironment (realm.[[GlobalEnv]]). - Set module.[[Environment]] to env.
- For each
ImportEntry Record in of module.[[ImportEntries]], do- Let importedModule be !
HostResolveImportedModule (module, in.[[ModuleRequest]]). - NOTE: The above call cannot fail because imported module requests are a subset of module.[[RequestedModules]], and these have been resolved earlier in this algorithm.
- If in.[[ImportName]] is
namespace-object , then- Let namespace be ?
GetModuleNamespace (importedModule). - Perform ! env.CreateImmutableBinding(in.[[LocalName]],
true ). - Perform ! env.InitializeBinding(in.[[LocalName]], namespace).
- Let namespace be ?
- Else,
- Let resolution be ? importedModule.ResolveExport(in.[[ImportName]]).
- If resolution is
null orambiguous , throw aSyntaxError exception. - If resolution.[[BindingName]] is
namespace , then- Let namespace be ?
GetModuleNamespace (resolution.[[Module]]). - Perform ! env.CreateImmutableBinding(in.[[LocalName]],
true ). - Perform ! env.InitializeBinding(in.[[LocalName]], namespace).
- Let namespace be ?
- Else,
- Perform env.CreateImportBinding(in.[[LocalName]], resolution.[[Module]], resolution.[[BindingName]]).
- Let importedModule be !
- Let moduleContext be a new ECMAScript code
execution context . - Set the Function of moduleContext to
null . Assert : module.[[Realm]] is notundefined .- Set the
Realm of moduleContext to module.[[Realm]]. - Set the ScriptOrModule of moduleContext to module.
- Set the VariableEnvironment of moduleContext to module.[[Environment]].
- Set the LexicalEnvironment of moduleContext to module.[[Environment]].
- Set the PrivateEnvironment of moduleContext to
null . - Set module.[[Context]] to moduleContext.
- Push moduleContext onto the
execution context stack ; moduleContext is now therunning execution context . - Let code be module.[[ECMAScriptCode]].
- Let varDeclarations be the
VarScopedDeclarations of code. - Let declaredVarNames be a new empty
List . - For each element d of varDeclarations, do
- For each element dn of the
BoundNames of d, do- If dn is not an element of declaredVarNames, then
- Perform ! env.CreateMutableBinding(dn,
false ). - Perform ! env.InitializeBinding(dn,
undefined ). - Append dn to declaredVarNames.
- Perform ! env.CreateMutableBinding(dn,
- If dn is not an element of declaredVarNames, then
- For each element dn of the
- Let lexDeclarations be the
LexicallyScopedDeclarations of code. - Let privateEnv be
null . - For each element d of lexDeclarations, do
- For each element dn of the
BoundNames of d, do- If
IsConstantDeclaration of d istrue , then- Perform ! env.CreateImmutableBinding(dn,
true ).
- Perform ! env.CreateImmutableBinding(dn,
- Else,
- Perform ! env.CreateMutableBinding(dn,
false ).
- Perform ! env.CreateMutableBinding(dn,
- If d is a
FunctionDeclaration , aGeneratorDeclaration , anAsyncFunctionDeclaration , or anAsyncGeneratorDeclaration , then- Let fo be
InstantiateFunctionObject of d with arguments env and privateEnv. - Perform ! env.InitializeBinding(dn, fo).
- Let fo be
- If
- For each element dn of the
- Remove moduleContext from the
execution context stack . - Return
unused .
16.2.1.6.5 ExecuteModule ( [ capability ] )
The ExecuteModule concrete method of a
- Let moduleContext be a new ECMAScript code
execution context . - Set the Function of moduleContext to
null . - Set the
Realm of moduleContext to module.[[Realm]]. - Set the ScriptOrModule of moduleContext to module.
Assert : module has been linked and declarations in its module environment have been instantiated.- Set the VariableEnvironment of moduleContext to module.[[Environment]].
- Set the LexicalEnvironment of moduleContext to module.[[Environment]].
- Suspend the currently
running execution context . - If module.[[HasTLA]] is
false , thenAssert : capability is not present.- Push moduleContext onto the
execution context stack ; moduleContext is now therunning execution context . - Let result be the result of evaluating module.[[ECMAScriptCode]].
- Suspend moduleContext and remove it from the
execution context stack . - Resume the context that is now on the top of the
execution context stack as therunning execution context . - If result is an
abrupt completion , then- Return ? result.
- Else,
Assert : capability is aPromiseCapability Record .- Perform
AsyncBlockStart (capability, module.[[ECMAScriptCode]], moduleContext).
- Return
unused .
16.2.1.7 HostResolveImportedModule ( referencingScriptOrModule, specifier )
The import()
An example of when referencingScriptOrModule can be
<button type="button" onclick="import('./foo.mjs')">Click me</button>
there will be no import()
An implementation of HostResolveImportedModule must conform to the following requirements:
-
If the returned
Completion Record is anormal completion , it must be anormal completion containing an instance of a concrete subclass ofModule Record . -
If a
Module Record corresponding to the pair referencingScriptOrModule, specifier does not exist or cannot be created, an exception must be thrown. -
Each time this operation is called with a specific referencingScriptOrModule, specifier pair as arguments it must return the same
Module Record instance if it completes normally.
Multiple different referencingScriptOrModule, specifier pairs may map to the same
16.2.1.8 HostImportModuleDynamically ( referencingScriptOrModule, specifier, promiseCapability )
The import()
An implementation of HostImportModuleDynamically must conform to the following requirements:
-
It must return
unused . Success or failure must instead be signaled as discussed below. -
The
host environment must conform to one of the two following sets of requirements:- Success path
-
- At some future time, the
host environment must performFinishDynamicImport (referencingScriptOrModule, specifier, promiseCapability, promise), where promise is a Promise resolved withundefined . - Any subsequent call to
HostResolveImportedModule afterFinishDynamicImport has completed, given the arguments referencingScriptOrModule and specifier, must return anormal completion containing a module which has already been evaluated, i.e. whose Evaluate concrete method has already been called and returned anormal completion .
- At some future time, the
- Failure path
-
- At some future time, the
host environment must performFinishDynamicImport (referencingScriptOrModule, specifier, promiseCapability, promise), where promise is a Promise rejected with an error representing the cause of failure.
- At some future time, the
-
If the
host environment takes the success path once for a given referencingScriptOrModule, specifier pair, it must always do so for subsequent calls. -
The operation must not call promiseCapability.[[Resolve]] or promiseCapability.[[Reject]], but instead must treat promiseCapability as an opaque identifying value to be passed through to
FinishDynamicImport .
The actual process performed is
16.2.1.9 FinishDynamicImport ( referencingScriptOrModule, specifier, promiseCapability, innerPromise )
The abstract operation FinishDynamicImport takes arguments referencingScriptOrModule, specifier, promiseCapability (a import()
- Let fulfilledClosure be a new
Abstract Closure with parameters (result) that captures referencingScriptOrModule, specifier, and promiseCapability and performs the following steps when called:Assert : result isundefined .- Let moduleRecord be !
HostResolveImportedModule (referencingScriptOrModule, specifier). Assert : Evaluate has already been invoked on moduleRecord and successfully completed.- Let namespace be
Completion (GetModuleNamespace (moduleRecord)). - If namespace is an
abrupt completion , then- Perform !
Call (promiseCapability.[[Reject]],undefined , « namespace.[[Value]] »).
- Perform !
- Else,
- Perform !
Call (promiseCapability.[[Resolve]],undefined , « namespace.[[Value]] »).
- Perform !
- Return
unused .
- Let onFulfilled be
CreateBuiltinFunction (fulfilledClosure, 0,"" , « »). - Let rejectedClosure be a new
Abstract Closure with parameters (error) that captures promiseCapability and performs the following steps when called:- Perform !
Call (promiseCapability.[[Reject]],undefined , « error »). - Return
unused .
- Perform !
- Let onRejected be
CreateBuiltinFunction (rejectedClosure, 0,"" , « »). - Perform
PerformPromiseThen (innerPromise, onFulfilled, onRejected). - Return
unused .
16.2.1.10 GetModuleNamespace ( module )
The abstract operation GetModuleNamespace takes argument module (an instance of a concrete subclass of
Assert : If module is aCyclic Module Record , then module.[[Status]] is notunlinked .- Let namespace be module.[[Namespace]].
- If namespace is
empty , then- Let exportedNames be ? module.GetExportedNames().
- Let unambiguousNames be a new empty
List . - For each element name of exportedNames, do
- Let resolution be ? module.ResolveExport(name).
- If resolution is a
ResolvedBinding Record , append name to unambiguousNames.
- Set namespace to
ModuleNamespaceCreate (module, unambiguousNames).
- Return namespace.
The only way GetModuleNamespace can throw is via one of the triggered
16.2.1.11 Runtime Semantics: Evaluation
- Return
undefined .
- Let result be the result of evaluating
ModuleItemList . - If result.[[Type]] is
normal and result.[[Value]] isempty , then- Return
undefined .
- Return
- Return ? result.
- Let sl be the result of evaluating
ModuleItemList . ReturnIfAbrupt (sl).- Let s be the result of evaluating
ModuleItem . - Return ?
UpdateEmpty (s, sl).
The value of a
- Return
empty .
16.2.2 Imports
Syntax
16.2.2.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
BoundNames ofImportDeclaration contains any duplicate entries.
16.2.2.2 Static Semantics: ImportEntries
The
- Return a new empty
List .
- Let entries1 be ImportEntries of
ModuleItemList . - Let entries2 be ImportEntries of
ModuleItem . - Return the
list-concatenation of entries1 and entries2.
- Return a new empty
List .
- Let module be the sole element of
ModuleRequests ofFromClause . - Return
ImportEntriesForModule ofImportClause with argument module.
- Return a new empty
List .
16.2.2.3 Static Semantics: ImportEntriesForModule
The
- Let entries1 be ImportEntriesForModule of
ImportedDefaultBinding with argument module. - Let entries2 be ImportEntriesForModule of
NameSpaceImport with argument module. - Return the
list-concatenation of entries1 and entries2.
- Let entries1 be ImportEntriesForModule of
ImportedDefaultBinding with argument module. - Let entries2 be ImportEntriesForModule of
NamedImports with argument module. - Return the
list-concatenation of entries1 and entries2.
- Let localName be the sole element of
BoundNames ofImportedBinding . - Let defaultEntry be the
ImportEntry Record { [[ModuleRequest]]: module, [[ImportName]]:"default" , [[LocalName]]: localName }. - Return « defaultEntry ».
- Let localName be the
StringValue ofImportedBinding . - Let entry be the
ImportEntry Record { [[ModuleRequest]]: module, [[ImportName]]:namespace-object , [[LocalName]]: localName }. - Return « entry ».
- Return a new empty
List .
- Let specs1 be the ImportEntriesForModule of
ImportsList with argument module. - Let specs2 be the ImportEntriesForModule of
ImportSpecifier with argument module. - Return the
list-concatenation of specs1 and specs2.
- Let localName be the sole element of
BoundNames ofImportedBinding . - Let entry be the
ImportEntry Record { [[ModuleRequest]]: module, [[ImportName]]: localName, [[LocalName]]: localName }. - Return « entry ».
- Let importName be the
StringValue ofModuleExportName . - Let localName be the
StringValue ofImportedBinding . - Let entry be the
ImportEntry Record { [[ModuleRequest]]: module, [[ImportName]]: importName, [[LocalName]]: localName }. - Return « entry ».
16.2.3 Exports
Syntax
16.2.3.1 Static Semantics: Early Errors
-
It is a Syntax Error if
ReferencedBindings ofNamedExports contains anyStringLiteral s. -
For each
IdentifierName n inReferencedBindings ofNamedExports : It is a Syntax Error ifStringValue of n is aReservedWord or if theStringValue of n is one of:"implements" ,"interface" ,"let" ,"package" ,"private" ,"protected" ,"public" , or"static" .
The above rule means that each
16.2.3.2 Static Semantics: ExportedBindings
The
ExportedBindings are the locally bound names that are explicitly associated with a
It is defined piecewise over the following productions:
- Let names1 be ExportedBindings of
ModuleItemList . - Let names2 be ExportedBindings of
ModuleItem . - Return the
list-concatenation of names1 and names2.
- Return a new empty
List .
- Return a new empty
List .
- Return the ExportedBindings of
NamedExports .
- Return the
BoundNames ofVariableStatement .
- Return the
BoundNames ofDeclaration .
- Return the
BoundNames of thisExportDeclaration .
- Return a new empty
List .
- Let names1 be the ExportedBindings of
ExportsList . - Let names2 be the ExportedBindings of
ExportSpecifier . - Return the
list-concatenation of names1 and names2.
- Return a
List whose sole element is theStringValue ofModuleExportName .
- Return a
List whose sole element is theStringValue of the firstModuleExportName .
16.2.3.3 Static Semantics: ExportedNames
The
ExportedNames are the externally visible names that a
It is defined piecewise over the following productions:
- Let names1 be ExportedNames of
ModuleItemList . - Let names2 be ExportedNames of
ModuleItem . - Return the
list-concatenation of names1 and names2.
- Return the ExportedNames of
ExportDeclaration .
- Return a new empty
List .
- Return the ExportedNames of
ExportFromClause .
- Return a new empty
List .
- Return a
List whose sole element is theStringValue ofModuleExportName .
- Return the ExportedNames of
NamedExports .
- Return the
BoundNames ofVariableStatement .
- Return the
BoundNames ofDeclaration .
- Return «
"default" ».
- Return a new empty
List .
- Let names1 be the ExportedNames of
ExportsList . - Let names2 be the ExportedNames of
ExportSpecifier . - Return the
list-concatenation of names1 and names2.
- Return a
List whose sole element is theStringValue ofModuleExportName .
- Return a
List whose sole element is theStringValue of the secondModuleExportName .
16.2.3.4 Static Semantics: ExportEntries
The
- Return a new empty
List .
- Let entries1 be ExportEntries of
ModuleItemList . - Let entries2 be ExportEntries of
ModuleItem . - Return the
list-concatenation of entries1 and entries2.
- Return a new empty
List .
- Let module be the sole element of
ModuleRequests ofFromClause . - Return
ExportEntriesForModule ofExportFromClause with argument module.
- Return
ExportEntriesForModule ofNamedExports with argumentnull .
- Let entries be a new empty
List . - Let names be the
BoundNames ofVariableStatement . - For each element name of names, do
- Append the
ExportEntry Record { [[ModuleRequest]]:null , [[ImportName]]:null , [[LocalName]]: name, [[ExportName]]: name } to entries.
- Append the
- Return entries.
- Let entries be a new empty
List . - Let names be the
BoundNames ofDeclaration . - For each element name of names, do
- Append the
ExportEntry Record { [[ModuleRequest]]:null , [[ImportName]]:null , [[LocalName]]: name, [[ExportName]]: name } to entries.
- Append the
- Return entries.
- Let names be
BoundNames ofHoistableDeclaration . - Let localName be the sole element of names.
- Return a
List whose sole element is theExportEntry Record { [[ModuleRequest]]:null , [[ImportName]]:null , [[LocalName]]: localName, [[ExportName]]:"default" }.
- Let names be
BoundNames ofClassDeclaration . - Let localName be the sole element of names.
- Return a
List whose sole element is theExportEntry Record { [[ModuleRequest]]:null , [[ImportName]]:null , [[LocalName]]: localName, [[ExportName]]:"default" }.
- Let entry be the
ExportEntry Record { [[ModuleRequest]]:null , [[ImportName]]:null , [[LocalName]]:"*default*" , [[ExportName]]:"default" }. - Return « entry ».
16.2.3.5 Static Semantics: ExportEntriesForModule
The
- Let entry be the
ExportEntry Record { [[ModuleRequest]]: module, [[ImportName]]:all-but-default , [[LocalName]]:null , [[ExportName]]:null }. - Return « entry ».
- Let exportName be the
StringValue ofModuleExportName . - Let entry be the
ExportEntry Record { [[ModuleRequest]]: module, [[ImportName]]:all , [[LocalName]]:null , [[ExportName]]: exportName }. - Return « entry ».
- Return a new empty
List .
- Let specs1 be the ExportEntriesForModule of
ExportsList with argument module. - Let specs2 be the ExportEntriesForModule of
ExportSpecifier with argument module. - Return the
list-concatenation of specs1 and specs2.
- Let sourceName be the
StringValue ofModuleExportName . - If module is
null , then- Let localName be sourceName.
- Let importName be
null .
- Else,
- Let localName be
null . - Let importName be sourceName.
- Let localName be
- Return a
List whose sole element is theExportEntry Record { [[ModuleRequest]]: module, [[ImportName]]: importName, [[LocalName]]: localName, [[ExportName]]: sourceName }.
- Let sourceName be the
StringValue of the firstModuleExportName . - Let exportName be the
StringValue of the secondModuleExportName . - If module is
null , then- Let localName be sourceName.
- Let importName be
null .
- Else,
- Let localName be
null . - Let importName be sourceName.
- Let localName be
- Return a
List whose sole element is theExportEntry Record { [[ModuleRequest]]: module, [[ImportName]]: importName, [[LocalName]]: localName, [[ExportName]]: exportName }.
16.2.3.6 Static Semantics: ReferencedBindings
The
- Return a new empty
List .
- Let names1 be the ReferencedBindings of
ExportsList . - Let names2 be the ReferencedBindings of
ExportSpecifier . - Return the
list-concatenation of names1 and names2.
- Return the ReferencedBindings of the first
ModuleExportName .
- Return a
List whose sole element is theIdentifierName .
- Return a
List whose sole element is theStringLiteral .
16.2.3.7 Runtime Semantics: Evaluation
- Return
empty .
- Return the result of evaluating
VariableStatement .
- Return the result of evaluating
Declaration .
- Return the result of evaluating
HoistableDeclaration .
- Let value be ?
BindingClassDeclarationEvaluation ofClassDeclaration . - Let className be the sole element of
BoundNames ofClassDeclaration . - If className is
"*default*" , then- Let env be the
running execution context 's LexicalEnvironment. - Perform ?
InitializeBoundName ("*default*" , value, env).
- Let env be the
- Return
empty .
- If
IsAnonymousFunctionDefinition (AssignmentExpression ) istrue , then- Let value be ?
NamedEvaluation ofAssignmentExpression with argument"default" .
- Let value be ?
- Else,
- Let rhs be the result of evaluating
AssignmentExpression . - Let value be ?
GetValue (rhs).
- Let rhs be the result of evaluating
- Let env be the
running execution context 's LexicalEnvironment. - Perform ?
InitializeBoundName ("*default*" , value, env). - Return
empty .