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 super
unless the source text containingsuper
is eval code that is being processed by adirect eval . Additionalearly error rules forsuper
withindirect 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. |
[[LoadedModules]] |
a |
A map from the specifier strings imported by this script to the resolved |
[[HostDefined]] |
anything (default value is |
Field reserved for use by |
16.1.5 ParseScript ( sourceText, realm, hostDefined )
The abstract operation ParseScript takes arguments sourceText (
- Let script be
ParseText (sourceText,Script ). - If script is a
List of errors, return script. - Return
Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: script, [[LoadedModules]]: « », [[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 (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
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 is a
normal completion , then- Set result to
Completion (Evaluation of script). - If result is a
normal completion and result.[[Value]] isempty , then- Set result to
NormalCompletion (undefined ).
- Set result to
- 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 not either a
VariableDeclaration , aForBinding , or 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 declaredFunctionNames does not contain fn, 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 not either a
- Let declaredVarNames be a new empty
List . - For each element d of varDeclarations, do
- If d is either a
VariableDeclaration , aForBinding , or aBindingIdentifier , then- For each String vn of the
BoundNames of d, do- If declaredFunctionNames does not contain vn, then
- Let vnDefinable be ? env.CanDeclareGlobalVar(vn).
- If vnDefinable is
false , throw aTypeError exception. - If declaredVarNames does not contain vn, then
- Append vn to declaredVarNames.
- If declaredFunctionNames does not contain vn, then
- For each String vn of the
- If d is either 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 ? env.CreateImmutableBinding(dn,
- Else,
- Perform ? env.CreateMutableBinding(dn,
false ).
- Perform ? env.CreateMutableBinding(dn,
- 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 ? env.CreateGlobalVarBinding(vn,
- Return
unused .
Unlike explicit var or function declarations, properties that are directly created on the