15 ECMAScript Language: Scripts and Modules
15.1 Scripts
Syntax
15.1.1 Static Semantics: Early Errors
-
It is a Syntax Error if the LexicallyDeclaredNames of
ScriptBody contains any duplicate entries. -
It is a Syntax Error if any element of the LexicallyDeclaredNames of
ScriptBody also occurs in the VarDeclaredNames ofScriptBody .
-
It is a Syntax Error if
StatementList Containssuperunless the source code containingsuperis eval code that is being processed by adirect eval that is contained in function code that is not the function code of anArrowFunction . -
It is a Syntax Error if
StatementList ContainsNewTarget unless the source code containingNewTarget is eval code that is being processed by adirect eval that is contained in function code that is not the function code of anArrowFunction . -
It is a Syntax Error if ContainsDuplicateLabels of
StatementList with argument « » istrue . -
It is a Syntax Error if ContainsUndefinedBreakTarget of
StatementList with argument « » istrue . -
It is a Syntax Error if ContainsUndefinedContinueTarget of
StatementList with arguments « » and « » istrue .
15.1.2 Static Semantics: IsStrict
- If the
Directive Prologue ofStatementList contains aUse Strict Directive , returntrue ; otherwise, returnfalse .
15.1.3 Static Semantics: LexicallyDeclaredNames
- Return TopLevelLexicallyDeclaredNames of
StatementList .
At the top level of a
15.1.4 Static Semantics: LexicallyScopedDeclarations
- Return TopLevelLexicallyScopedDeclarations of
StatementList .
15.1.5 Static Semantics: VarDeclaredNames
- Return TopLevelVarDeclaredNames of
StatementList .
15.1.6 Static Semantics: VarScopedDeclarations
- Return TopLevelVarScopedDeclarations of
StatementList .
15.1.7 Runtime Semantics: Evaluation
- Return
NormalCompletion (undefined ).
15.1.8 Script Records
A Script Record encapsulates information about a script being evaluated. Each script record contains the fields listed in
| Field Name |
Value |
Meaning |
|---|---|---|
| [[Realm]] |
|
The |
| [[Environment]] |
|
The |
| [[ECMAScriptCode]] | a parse result |
The result of parsing the source text of this module using |
| [[HostDefined]] |
Any, default value is |
Field reserved for use by host environments that need to associate additional information with a script. |
15.1.9 ParseScript ( sourceText, realm, hostDefined )
The abstract operation ParseScript with arguments sourceText, realm, and hostDefined creates a
- Assert: sourceText is an ECMAScript source text (see clause
10 ). - Parse sourceText using
Script as the goal symbol and analyze the parse result for any Early Error conditions. If the parse was successful and no early errors were found, let body be the resulting parse tree. Otherwise, let body be aList of one or moreSyntaxError orReferenceError objects representing the parsing errors and/or early errors. Parsing andearly error detection may be interweaved in an implementation dependent manner. If more than one parsing error orearly error is present, the number and ordering of error objects in the list is implementation dependent, but at least one must be present. - If body is a
List of errors, then return body. - Return
Script Record {[[Realm]]: realm, [[Environment]]:undefined , [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined}.
An implementation may parse script source text and analyze 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.
15.1.10 ScriptEvaluation ( scriptRecord )
- Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
- Let scriptCxt be a new ECMAScript code
execution context . - Set the Function of scriptCxt to
null . - Set the
Realm of scriptCxt to scriptRecord.[[Realm]]. - Set the ScriptOrModule of scriptCxt to scriptRecord.
- Set the VariableEnvironment of scriptCxt to globalEnv.
- Set the LexicalEnvironment of scriptCxt to globalEnv.
- Suspend the currently
running execution context . - Push scriptCxt on to the
execution context stack ; scriptCxt is now therunning execution context . - Let result be
GlobalDeclarationInstantiation (ScriptBody , globalEnv). - If result.[[Type]] is
normal , then- Let result be the result of evaluating
ScriptBody .
- Let result be the result of evaluating
- If result.[[Type]] is
normal and result.[[Value]] isempty , then- Let result be
NormalCompletion (undefined ).
- Let result be
- Suspend scriptCxt and remove it from the
execution context stack . - Assert: the
execution context stack is not empty. - Resume the context that is now on the top of the
execution context stack as therunning execution context . - Return
Completion (result).
15.1.11 Runtime Semantics: GlobalDeclarationInstantiation (script, env)
When an
GlobalDeclarationInstantiation is performed as follows using arguments script and env. script is the
- Let envRec be env's
EnvironmentRecord . - Assert: envRec is a global
Environment Record . - Let lexNames be the LexicallyDeclaredNames of script.
- Let varNames be the VarDeclaredNames of script.
- For each name in lexNames, do
- If envRec.HasVarDeclaration(name) is
true , throw aSyntaxError exception. - If envRec.HasLexicalDeclaration(name) is
true , throw aSyntaxError exception. - Let hasRestrictedGlobal be ? envRec.HasRestrictedGlobalProperty(name).
- If hasRestrictedGlobal is
true , throw aSyntaxError exception.
- If envRec.HasVarDeclaration(name) is
- For each name in varNames, do
- If envRec.HasLexicalDeclaration(name) is
true , throw aSyntaxError exception.
- If envRec.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 d in varDeclarations, in reverse list order do
- If d is neither a
VariableDeclaration or aForBinding , then- Assert: d is either a
FunctionDeclaration or aGeneratorDeclaration . - NOTE If there are multiple
FunctionDeclaration s 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 ? envRec.CanDeclareGlobalFunction(fn).
- If fnDefinable is
false , throw aTypeError exception. - Append fn to declaredFunctionNames.
- Insert d as the first element of functionsToInitialize.
- Assert: d is either a
- If d is neither a
- Let declaredVarNames be a new empty
List . - For each d in varDeclarations, do
- If d is a
VariableDeclaration or aForBinding , then- For each String vn in the BoundNames of d, do
- If vn is not an element of declaredFunctionNames, then
- Let vnDefinable be ? envRec.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 in the BoundNames of d, do
- If d is a
- NOTE: No abnormal terminations occur after this algorithm step if the
global object is an ordinary object. However, if theglobal object is a Proxy exotic object it may exhibit behaviours that cause abnormal terminations in some of the following steps. - NOTE: Annex
B.3.3.2 adds additional steps at this point. - Let lexDeclarations be the LexicallyScopedDeclarations of script.
- For each element d in 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 is
true , then- Perform ? envRec.CreateImmutableBinding(dn,
true ).
- Perform ? envRec.CreateImmutableBinding(dn,
- Else,
- Perform ? envRec.CreateMutableBinding(dn,
false ).
- Perform ? envRec.CreateMutableBinding(dn,
- If IsConstantDeclaration of d is
- For each production f in functionsToInitialize, do
- Let fn be the sole element of the BoundNames of f.
- Let fo be the result of performing InstantiateFunctionObject for f with argument env.
- Perform ? envRec.CreateGlobalFunctionBinding(fn, fo,
false ).
- For each String vn in declaredVarNames, in list order do
- Perform ? envRec.CreateGlobalVarBinding(vn,
false ).
- Perform ? envRec.CreateGlobalVarBinding(vn,
- Return
NormalCompletion (empty ).
Early errors specified in
Unlike explicit var or function declarations, properties that are directly created on the
15.1.12 Runtime Semantics: ScriptEvaluationJob ( sourceText, hostDefined )
The job ScriptEvaluationJob with parameters sourceText and hostDefined parses, validates, and evaluates sourceText as a
- Assert: sourceText is an ECMAScript source text (see clause
10 ). - Let realm be
the current Realm Record . - Let s be
ParseScript (sourceText, realm, hostDefined). - If s is a
List of errors, then- Perform
HostReportErrors (s). NextJob NormalCompletion (undefined ).
- Perform
- Let status be
ScriptEvaluation (s). NextJob Completion (status).
15.2 Modules
Syntax
15.2.1 Module Semantics
15.2.1.1 Static Semantics: Early Errors
-
It is a Syntax Error if the LexicallyDeclaredNames of
ModuleItemList contains any duplicate entries. -
It is a Syntax Error if any element of the LexicallyDeclaredNames of
ModuleItemList also occurs in the VarDeclaredNames ofModuleItemList . -
It is a Syntax Error if the ExportedNames of
ModuleItemList contains any duplicate entries. -
It is a Syntax Error if any element of the ExportedBindings of
ModuleItemList does not also occur in either the VarDeclaredNames ofModuleItemList , or the LexicallyDeclaredNames ofModuleItemList . -
It is a Syntax Error if
ModuleItemList containssuper. -
It is a Syntax Error if
ModuleItemList containsNewTarget . -
It is a Syntax Error if ContainsDuplicateLabels of
ModuleItemList with argument « » istrue . -
It is a Syntax Error if ContainsUndefinedBreakTarget of
ModuleItemList with argument « » istrue . -
It is a Syntax Error if ContainsUndefinedContinueTarget of
ModuleItemList with arguments « » and « » istrue .
The duplicate ExportedNames rule implies that multiple export default
15.2.1.2 Static Semantics: ContainsDuplicateLabels
With argument labelSet.
- Let hasDuplicates be ContainsDuplicateLabels of
ModuleItemList with argument labelSet. - If hasDuplicates is
true , returntrue . - Return ContainsDuplicateLabels of
ModuleItem with argument labelSet.
- Return
false .
15.2.1.3 Static Semantics: ContainsUndefinedBreakTarget
With argument labelSet.
- Let hasUndefinedLabels be ContainsUndefinedBreakTarget of
ModuleItemList with argument labelSet. - If hasUndefinedLabels is
true , returntrue . - Return ContainsUndefinedBreakTarget of
ModuleItem with argument labelSet.
- Return
false .
15.2.1.4 Static Semantics: ContainsUndefinedContinueTarget
With arguments iterationSet and labelSet.
- Let hasUndefinedLabels be ContainsUndefinedContinueTarget of
ModuleItemList with arguments iterationSet and « ». - If hasUndefinedLabels is
true , returntrue . - Return ContainsUndefinedContinueTarget of
ModuleItem with arguments iterationSet and « ».
- Return
false .
15.2.1.5 Static Semantics: ExportedBindings
ExportedBindings are the locally bound names that are explicitly associated with a
- Let names be ExportedBindings of
ModuleItemList . - Append to names the elements of the ExportedBindings of
ModuleItem . - Return names.
- Return a new empty
List .
15.2.1.6 Static Semantics: ExportedNames
ExportedNames are the externally visible names that a
- Let names be ExportedNames of
ModuleItemList . - Append to names the elements of the ExportedNames of
ModuleItem . - Return names.
- Return the ExportedNames of
ExportDeclaration .
- Return a new empty
List .
15.2.1.7 Static Semantics: ExportEntries
15.2.1.8 Static Semantics: ImportEntries
15.2.1.9 Static Semantics: ImportedLocalNames ( importEntries )
15.2.1.10 Static Semantics: ModuleRequests
- 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 .
15.2.1.11 Static Semantics: LexicallyDeclaredNames
The LexicallyDeclaredNames of a
- Let names be LexicallyDeclaredNames of
ModuleItemList . - Append to names the elements of the LexicallyDeclaredNames of
ModuleItem . - Return names.
- Return the BoundNames of
ImportDeclaration .
- If
ExportDeclaration isexportVariableStatement , return a new emptyList . - Return the BoundNames of
ExportDeclaration .
- Return LexicallyDeclaredNames of
StatementListItem .
At the top level of a
15.2.1.12 Static Semantics: LexicallyScopedDeclarations
- Return a new empty
List .
- Let declarations be LexicallyScopedDeclarations of
ModuleItemList . - Append to declarations the elements of the LexicallyScopedDeclarations of
ModuleItem . - Return declarations.
- Return a new empty
List .
15.2.1.13 Static Semantics: VarDeclaredNames
- Return a new empty
List .
- Let names be VarDeclaredNames of
ModuleItemList . - Append to names the elements of the VarDeclaredNames of
ModuleItem . - Return names.
- Return a new empty
List .
- If
ExportDeclaration isexportVariableStatement , return BoundNames ofExportDeclaration . - Return a new empty
List .
15.2.1.14 Static Semantics: VarScopedDeclarations
- Return a new empty
List .
- Let declarations be VarScopedDeclarations of
ModuleItemList . - Append to declarations the elements of the VarScopedDeclarations of
ModuleItem . - Return declarations.
- Return a new empty
List .
- If
ExportDeclaration isexportVariableStatement , return VarScopedDeclarations ofVariableStatement . - Return a new empty
List .
15.2.1.15 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 |
Meaning |
|---|---|---|
| [[Realm]] |
|
The |
| [[Environment]] |
|
The |
| [[Namespace]] |
Object | |
The Module Namespace Object ( |
| [[Evaluated]] | Boolean |
Initially |
| [[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, exportStarSet) |
Return the binding of a name exported by this module. Bindings are represented by a |
| ModuleDeclarationInstantiation() |
Transitively resolve all module dependencies and create a module |
| ModuleEvaluation() |
Do nothing if this module has already been evaluated. Otherwise, transitively evaluate all module dependences of this module and then evaluate this module. ModuleDeclarationInstantiation must be completed prior to invoking this method. |
15.2.1.16 Source Text Module Records
A Source Text Module Record is used to represent information about a module that was defined from ECMAScript source text (10) that was parsed using the goal symbol
In addition to the fields, defined in
| Field Name |
Value |
Meaning |
|---|---|---|
| [[ECMAScriptCode]] | a parse result |
The result of parsing the source text of this module using |
| [[RequestedModules]] |
|
A |
| [[ImportEntries]] |
|
A |
| [[LocalExportEntries]] |
|
A |
| [[IndirectExportEntries]] |
|
A |
| [[StarExportEntries]] |
|
A |
An ImportEntry
| Field Name |
Value |
Meaning |
|---|---|---|
| [[ModuleRequest]] | String |
String value of the |
| [[ImportName]] | String |
The name under which the desired binding is exported by the module identified by [[ModuleRequest]]. The value "*" indicates that the import request is for the target module's namespace object.
|
| [[LocalName]] | 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";
|
"mod"
|
"default"
|
"v"
|
import * as ns from "mod";
|
"mod"
|
"*"
|
"ns"
|
import {x} from "mod";
|
"mod"
|
"x"
|
"x"
|
import {x as v} from "mod";
|
"mod"
|
"x"
|
"v"
|
import "mod";
|
An ImportEntry |
||
An ExportEntry
| Field Name |
Value |
Meaning |
|---|---|---|
| [[ExportName]] | String | The name used to export this binding by this module. |
| [[ModuleRequest]] | String | null |
The String value of the |
| [[ImportName]] | String | null |
The name under which the desired binding is exported by the module identified by [[ModuleRequest]]. "*" indicates that the export request is for all exported bindings.
|
| [[LocalName]] | String | null |
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;
|
"v"
|
|
|
"v"
|
export default function f(){}
|
"default"
|
|
|
"f"
|
export default function(){}
|
"default"
|
|
|
"*default*"
|
export default 42;
|
"default"
|
|
|
"*default*"
|
export {x};
|
"x"
|
|
|
"x"
|
export {v as x};
|
"x"
|
|
|
"v"
|
export {x} from "mod";
|
"x"
|
"mod"
|
"x"
|
|
export {v as x} from "mod";
|
"x"
|
"mod"
|
"v"
|
|
export * from "mod";
|
|
"mod"
|
"*"
|
|
The following definitions specify the required concrete methods and other abstract operations for Source Text Module Records
15.2.1.16.1 ParseModule ( sourceText, realm, hostDefined )
The abstract operation ParseModule with arguments sourceText, realm, and hostDefined creates a
- Assert: sourceText is an ECMAScript source text (see clause
10 ). - Parse sourceText using
Module as the goal symbol and analyze the parse result for any Early Error conditions. If the parse was successful and no early errors were found, let body be the resulting parse tree. Otherwise, let body be aList of one or moreSyntaxError orReferenceError objects representing the parsing errors and/or early errors. Parsing andearly error detection may be interweaved in an implementation dependent manner. If more than one parsing error orearly error is present, the number and ordering of error objects in the list is implementation dependent, but at least one must be present. - If body is a
List of errors, then 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 record ee in 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
"*", then- Assert: this is a re-export of an imported module namespace object.
- Append ee to localExportEntries.
- Else, this is a re-export of a single name
- Append to indirectExportEntries the
Record {[[ModuleRequest]]: ie.[[ModuleRequest]], [[ImportName]]: ie.[[ImportName]], [[LocalName]]:null , [[ExportName]]: ee.[[ExportName]] }.
- Append to indirectExportEntries the
- If ee.[[LocalName]] is not an element of importedBoundNames, then
- Else, if ee.[[ImportName]] is
"*", then- Append ee to starExportEntries.
- Else,
- Append ee to indirectExportEntries.
- If ee.[[ModuleRequest]] is
- Return
Source Text Module Record {[[Realm]]: realm, [[Environment]]:undefined , [[HostDefined]]: hostDefined, [[Namespace]]:undefined , [[Evaluated]]:false , [[ECMAScriptCode]]: body, [[RequestedModules]]: requestedModules, [[ImportEntries]]: importEntries, [[LocalExportEntries]]: localExportEntries, [[StarExportEntries]]: starExportEntries, [[IndirectExportEntries]]: indirectExportEntries}.
An implementation may parse module source text and analyze 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.
15.2.1.16.2 GetExportedNames( exportStarSet ) Concrete Method
The GetExportedNames concrete method of a
- Let module be this
Source Text Module Record . - If exportStarSet contains module, then
- Assert: We've reached the starting point of an
import *circularity. - Return a new empty
List .
- Assert: We've reached the starting point of an
- Append module to exportStarSet.
- Let exportedNames be a new empty
List . - For each ExportEntry
Record e in module.[[LocalExportEntries]], do- Assert: module provides the direct binding for this export.
- Append e.[[ExportName]] to exportedNames.
- For each ExportEntry
Record e in module.[[IndirectExportEntries]], do- Assert: module imports a specific binding for this export.
- Append e.[[ExportName]] to exportedNames.
- For each ExportEntry
Record e in 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.
15.2.1.16.3 ResolveExport( exportName, resolveSet, exportStarSet ) Concrete Method
The ResolveExport concrete method of a
- Let module be this
Source Text Module Record . - For each
Record {[[Module]], [[ExportName]]} r in resolveSet, do:- If module and r.[[Module]] are the same
Module Record andSameValue (exportName, r.[[ExportName]]) istrue , then- Assert: 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 in module.[[LocalExportEntries]], do - For each ExportEntry
Record e in module.[[IndirectExportEntries]], do- If
SameValue (exportName, e.[[ExportName]]) istrue , then- Assert: module imports a specific binding for this export.
- Let importedModule be ?
HostResolveImportedModule (module, e.[[ModuleRequest]]). - Let indirectResolution be ? importedModule.ResolveExport(e.[[ImportName]], resolveSet, exportStarSet).
- If indirectResolution is not
null , return indirectResolution.
- If
- If
SameValue (exportName,"default") istrue , then- Assert: A
defaultexport was not explicitly defined by this module. - Throw a
SyntaxError exception. - NOTE A
defaultexport cannot be provided by anexport *.
- Assert: A
- If exportStarSet contains module, return
null . - Append module to exportStarSet.
- Let starResolution be
null . - For each ExportEntry
Record e in module.[[StarExportEntries]], do- Let importedModule be ?
HostResolveImportedModule (module, e.[[ModuleRequest]]). - Let resolution be ? importedModule.ResolveExport(exportName, resolveSet, exportStarSet).
- If resolution is
"ambiguous", return"ambiguous". - If resolution is not
null , then- If starResolution is
null , let starResolution be 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 orSameValue (resolution.[[BindingName]], starResolution.[[BindingName]]) isfalse , return"ambiguous".
- Assert: there is more than one
- If starResolution is
- Let importedModule be ?
- Return starResolution.
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 "ambiguous" is returned.
15.2.1.16.4 ModuleDeclarationInstantiation( ) Concrete Method
The ModuleDeclarationInstantiation concrete method of a
- Let module be this
Source Text Module Record . - Let realm be module.[[Realm]].
- Assert: realm is not
undefined . - Let code be module.[[ECMAScriptCode]].
- If module.[[Environment]] is not
undefined , returnNormalCompletion (empty ). - Let env be
NewModuleEnvironment (realm.[[GlobalEnv]]). - Set module.[[Environment]] to env.
- For each String required that is an element of module.[[RequestedModules]] do,
- NOTE: Before instantiating a module, all of the modules it requested must be available. An implementation may perform this test at any time prior to this point.
- Let requiredModule be ?
HostResolveImportedModule (module, required). - Perform ? requiredModule.ModuleDeclarationInstantiation().
- For each ExportEntry
Record e in module.[[IndirectExportEntries]], do- Let resolution be ? module.ResolveExport(e.[[ExportName]], « », « »).
- If resolution is
null or resolution is"ambiguous", throw aSyntaxError exception.
- Assert: all named exports from module are resolvable.
- Let envRec be env's
EnvironmentRecord . - For each ImportEntry
Record in in module.[[ImportEntries]], do- Let importedModule be ?
HostResolveImportedModule (module, in.[[ModuleRequest]]). - If in.[[ImportName]] is
"*", then- Let namespace be ?
GetModuleNamespace (importedModule). - Perform ! envRec.CreateImmutableBinding(in.[[LocalName]],
true ). - Call envRec.InitializeBinding(in.[[LocalName]], namespace).
- Let namespace be ?
- Else,
- Let resolution be ? importedModule.ResolveExport(in.[[ImportName]], « », « »).
- If resolution is
null or resolution is"ambiguous", throw aSyntaxError exception. - Call envRec.CreateImportBinding(in.[[LocalName]], resolution.[[Module]], resolution.[[BindingName]]).
- Let importedModule be ?
- Let varDeclarations be the VarScopedDeclarations of code.
- Let declaredVarNames be a new empty
List . - For each element d in varDeclarations do
- For each element dn of the BoundNames of d do
- If dn is not an element of declaredVarNames, then
- Perform ! envRec.CreateMutableBinding(dn,
false ). - Call envRec.InitializeBinding(dn,
undefined ). - Append dn to declaredVarNames.
- Perform ! envRec.CreateMutableBinding(dn,
- If dn is not an element of declaredVarNames, then
- For each element dn of the BoundNames of d do
- Let lexDeclarations be the LexicallyScopedDeclarations of code.
- For each element d in lexDeclarations do
- For each element dn of the BoundNames of d do
- If IsConstantDeclaration of d is
true , then- Perform ! envRec.CreateImmutableBinding(dn,
true ).
- Perform ! envRec.CreateImmutableBinding(dn,
- Else,
- Perform ! envRec.CreateMutableBinding(dn,
false ).
- Perform ! envRec.CreateMutableBinding(dn,
- If d is a
GeneratorDeclaration production or aFunctionDeclaration production, then- Let fo be the result of performing InstantiateFunctionObject for d with argument env.
- Call envRec.InitializeBinding(dn, fo).
- If IsConstantDeclaration of d is
- For each element dn of the BoundNames of d do
- Return
NormalCompletion (empty ).
15.2.1.16.5 ModuleEvaluation() Concrete Method
The ModuleEvaluation concrete method of a
- Let module be this
Source Text Module Record . - Assert: ModuleDeclarationInstantiation has already been invoked on module and successfully completed.
- Assert: module.[[Realm]] is not
undefined . - If module.[[Evaluated]] is
true , returnundefined . - Set module.[[Evaluated]] to
true . - For each String required that is an element of module.[[RequestedModules]] do,
- Let requiredModule be ?
HostResolveImportedModule (module, required). - Perform ? requiredModule.ModuleEvaluation().
- Let requiredModule be ?
- Let moduleCxt be a new ECMAScript code
execution context . - Set the Function of moduleCxt to
null . - Set the
Realm of moduleCxt to module.[[Realm]]. - Set the ScriptOrModule of moduleCxt to module.
- Assert: module has been linked and declarations in its
module environment have been instantiated. - Set the VariableEnvironment of moduleCxt to module.[[Environment]].
- Set the LexicalEnvironment of moduleCxt to module.[[Environment]].
- Suspend the currently
running execution context . - Push moduleCxt on to the
execution context stack ; moduleCxt is now therunning execution context . - Let result be the result of evaluating module.[[ECMAScriptCode]].
- Suspend moduleCxt 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 . - Return
Completion (result).
15.2.1.17 Runtime Semantics: HostResolveImportedModule (referencingModule, specifier )
HostResolveImportedModule is an implementation defined abstract operation that provides the concrete
The implementation of HostResolveImportedModule must conform to the following requirements:
-
The normal return value must be an instance of a concrete subclass of
Module Record . -
If a
Module Record corresponding to the pair referencingModule, specifier does not exist or cannot be created, an exception must be thrown. -
This operation must be idempotent if it completes normally. Each time it is called with a specific referencingModule, specifier pair as arguments it must return the same
Module Record instance.
Multiple different referencingModule, specifier pairs may map to the same
15.2.1.18 Runtime Semantics: GetModuleNamespace( module )
The abstract operation GetModuleNamespace called with argument module performs the following steps:
- Assert: module is an instance of a concrete subclass of
Module Record . - Let namespace be module.[[Namespace]].
- If namespace is
undefined , then- Let exportedNames be ? module.GetExportedNames(« »).
- Let unambiguousNames be a new empty
List . - For each name that is an element of exportedNames,
- Let resolution be ? module.ResolveExport(name, « », « »).
- If resolution is
null , throw aSyntaxError exception. - If resolution is not
"ambiguous", append name to unambiguousNames.
- Let namespace be
ModuleNamespaceCreate (module, unambiguousNames).
- Return namespace.
15.2.1.19 Runtime Semantics: TopLevelModuleEvaluationJob ( sourceText, hostDefined )
A TopLevelModuleEvaluationJob with parameters sourceText and hostDefined is a job that parses, validates, and evaluates sourceText as a
- Assert: sourceText is an ECMAScript source text (see clause
10 ). - Let realm be
the current Realm Record . - Let m be
ParseModule (sourceText, realm, hostDefined). - If m is a
List of errors, then- Perform
HostReportErrors (m). NextJob NormalCompletion (undefined ).
- Perform
- Let status be m.ModuleDeclarationInstantiation().
- If status is not an
abrupt completion , then- Assert: all dependencies of m have been transitively resolved and m is ready for evaluation.
- Let status be m.ModuleEvaluation().
NextJob Completion (status).
An implementation may parse a sourceText as a
15.2.1.20 Runtime Semantics: Evaluation
- Return
NormalCompletion (undefined ).
- Let result be the result of evaluating
ModuleItemList . - If result.[[Type]] is
normal and result.[[Value]] isempty , then- Return
NormalCompletion (undefined ).
- Return
- Return
Completion (result).
- Let sl be the result of evaluating
ModuleItemList . ReturnIfAbrupt (sl).- Let s be the result of evaluating
ModuleItem . - Return
Completion (UpdateEmpty (s, sl.[[Value]])).
The value of a
- Return
NormalCompletion (empty ).
15.2.2 Imports
Syntax
15.2.2.1 Static Semantics: Early Errors
-
It is a Syntax Error if the BoundNames of
ImportDeclaration contains any duplicate entries.
15.2.2.2 Static Semantics: BoundNames
- Return the BoundNames of
ImportClause .
- Return a new empty
List .
- Let names be the BoundNames of
ImportedDefaultBinding . - Append to names the elements of the BoundNames of
NameSpaceImport . - Return names.
- Let names be the BoundNames of
ImportedDefaultBinding . - Append to names the elements of the BoundNames of
NamedImports . - Return names.
- Return a new empty
List .
- Let names be the BoundNames of
ImportsList . - Append to names the elements of the BoundNames of
ImportSpecifier . - Return names.
- Return the BoundNames of
ImportedBinding .
15.2.2.3 Static Semantics: ImportEntries
- Let module be the sole element of ModuleRequests of
FromClause . - Return ImportEntriesForModule of
ImportClause with argument module.
- Return a new empty
List .
15.2.2.4 Static Semantics: ImportEntriesForModule
With parameter module.
- Let entries be ImportEntriesForModule of
ImportedDefaultBinding with argument module. - Append to entries the elements of the ImportEntriesForModule of
NameSpaceImport with argument module. - Return entries.
- Let entries be ImportEntriesForModule of
ImportedDefaultBinding with argument module. - Append to entries the elements of the ImportEntriesForModule of
NamedImports with argument module. - Return entries.
- Return a new empty
List .
- Let specs be the ImportEntriesForModule of
ImportsList with argument module. - Append to specs the elements of the ImportEntriesForModule of
ImportSpecifier with argument module. - Return specs.
15.2.2.5 Static Semantics: ModuleRequests
- Return ModuleRequests of
FromClause .
- Return a
List containing the StringValue ofStringLiteral .
15.2.3 Exports
Syntax
15.2.3.1 Static Semantics: Early Errors
-
For each
IdentifierName n in ReferencedBindings ofExportClause : It is a Syntax Error if StringValue of n is aReservedWord or if the StringValue of n is one of:"implements","interface","let","package","private","protected","public", or"static".
The above rule means that each ReferencedBindings of
15.2.3.2 Static Semantics: BoundNames
- Return a new empty
List .
- Return the BoundNames of
VariableStatement .
- Return the BoundNames of
Declaration .
- Let declarationNames be the BoundNames of
HoistableDeclaration . - If declarationNames does not include the element
"*default*", append"*default*"to declarationNames. - Return declarationNames.
- Let declarationNames be the BoundNames of
ClassDeclaration . - If declarationNames does not include the element
"*default*", append"*default*"to declarationNames. - Return declarationNames.
- Return «
"*default*"».
15.2.3.3 Static Semantics: ExportedBindings
- Return a new empty
List .
- Return the ExportedBindings of
ExportClause .
- Return the BoundNames of
VariableStatement .
- Return the BoundNames of
Declaration .
- Return the BoundNames of this
ExportDeclaration .
- Return a new empty
List .
- Let names be the ExportedBindings of
ExportsList . - Append to names the elements of the ExportedBindings of
ExportSpecifier . - Return names.
- Return a
List containing the StringValue ofIdentifierName .
- Return a
List containing the StringValue of the firstIdentifierName .
15.2.3.4 Static Semantics: ExportedNames
- Return a new empty
List .
- Return the ExportedNames of
ExportClause .
- Return the BoundNames of
VariableStatement .
- Return the BoundNames of
Declaration .
- Return «
"default"».
- Return a new empty
List .
- Let names be the ExportedNames of
ExportsList . - Append to names the elements of the ExportedNames of
ExportSpecifier . - Return names.
- Return a
List containing the StringValue ofIdentifierName .
- Return a
List containing the StringValue of the secondIdentifierName .
15.2.3.5 Static Semantics: ExportEntries
- Let module be the sole element of ModuleRequests of
FromClause . - Return ExportEntriesForModule of
ExportClause with argument module.
- Return ExportEntriesForModule of
ExportClause with argumentnull .
"*default*" is used within this specification as a synthetic name for anonymous default export values.
15.2.3.6 Static Semantics: ExportEntriesForModule
With parameter module.
- Return a new empty
List .
- Let specs be the ExportEntriesForModule of
ExportsList with argument module. - Append to specs the elements of the ExportEntriesForModule of
ExportSpecifier with argument module. - Return specs.
- Let sourceName be the StringValue of
IdentifierName . - 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 new
List containing theRecord {[[ModuleRequest]]: module, [[ImportName]]: importName, [[LocalName]]: localName, [[ExportName]]: sourceName }.
- Let sourceName be the StringValue of the first
IdentifierName . - Let exportName be the StringValue of the second
IdentifierName . - 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 new
List containing theRecord {[[ModuleRequest]]: module, [[ImportName]]: importName, [[LocalName]]: localName, [[ExportName]]: exportName }.
15.2.3.7 Static Semantics: IsConstantDeclaration
- Return
false .
It is not necessary to treat export default
15.2.3.8 Static Semantics: LexicallyScopedDeclarations
- Return a new empty
List .
- Return a new
List containing DeclarationPart ofDeclaration .
- Return a new
List containing DeclarationPart ofHoistableDeclaration .
- Return a new
List containingClassDeclaration .
- Return a new
List containing thisExportDeclaration .
15.2.3.9 Static Semantics: ModuleRequests
- Return the ModuleRequests of
FromClause .
- Return a new empty
List .
15.2.3.10 Static Semantics: ReferencedBindings
- Return a new empty
List .
- Let names be the ReferencedBindings of
ExportsList . - Append to names the elements of the ReferencedBindings of
ExportSpecifier . - Return names.
- Return a
List containing theIdentifierName .
- Return a
List containing the firstIdentifierName .
15.2.3.11 Runtime Semantics: Evaluation
- Return
NormalCompletion (empty ).
- Return the result of evaluating
VariableStatement .
- Return the result of evaluating
Declaration .
- Return the result of evaluating
HoistableDeclaration .
- Let value be the result of BindingClassDeclarationEvaluation of
ClassDeclaration . ReturnIfAbrupt (value).- Let className be the sole element of BoundNames of
ClassDeclaration . - If className is
"*default*", then- Let hasNameProperty be ?
HasOwnProperty (value,"name"). - If hasNameProperty is
false , performSetFunctionName (value,"default"). - Let env be the
running execution context 's LexicalEnvironment. - Perform ?
InitializeBoundName ("*default*", value, env).
- Let hasNameProperty be ?
- Return
NormalCompletion (empty ).
- Let rhs be the result of evaluating
AssignmentExpression . - Let value be ?
GetValue (rhs). - If
IsAnonymousFunctionDefinition (AssignmentExpression ) istrue , then- Let hasNameProperty be ?
HasOwnProperty (value,"name"). - If hasNameProperty is
false , performSetFunctionName (value,"default").
- Let hasNameProperty be ?
- Let env be the
running execution context 's LexicalEnvironment. - Perform ?
InitializeBoundName ("*default*", value, env). - Return
NormalCompletion (empty ).