14 ECMAScript Language: Functions and Classes
14.1 Function Definitions
Syntax
14.1.1 Directive Prologues and the Use Strict Directive
A Directive Prologue is the longest sequence of
A Use Strict Directive is an "use strict" or 'use strict'. A
A
The
14.1.2 Static Semantics: Early Errors
-
If the source code matching this production is strict code, the Early Error rules for
are applied.StrictFormalParameters : FormalParameters -
If the source code matching this production is strict code, it is a Syntax Error if
BindingIdentifier is theIdentifierName evalor theIdentifierName arguments. -
It is a Syntax Error if ContainsUseStrict of
FunctionBody istrue and IsSimpleParameterList ofFormalParameters isfalse . -
It is a Syntax Error if any element of the BoundNames of
FormalParameters also occurs in the LexicallyDeclaredNames ofFunctionBody . -
It is a Syntax Error if
FormalParameters ContainsSuperProperty istrue . -
It is a Syntax Error if
FunctionBody ContainsSuperProperty istrue . -
It is a Syntax Error if
FormalParameters ContainsSuperCall istrue . -
It is a Syntax Error if
FunctionBody ContainsSuperCall istrue .
The LexicallyDeclaredNames of a
-
It is a Syntax Error if BoundNames of
FormalParameters contains any duplicate elements.
-
It is a Syntax Error if IsSimpleParameterList of
FormalParameterList isfalse and BoundNames ofFormalParameterList contains any duplicate elements.
Multiple occurrences of the same
-
It is a Syntax Error if the LexicallyDeclaredNames of
FunctionStatementList contains any duplicate entries. -
It is a Syntax Error if any element of the LexicallyDeclaredNames of
FunctionStatementList also occurs in the VarDeclaredNames ofFunctionStatementList . -
It is a Syntax Error if ContainsDuplicateLabels of
FunctionStatementList with argument « » istrue . -
It is a Syntax Error if ContainsUndefinedBreakTarget of
FunctionStatementList with argument « » istrue . -
It is a Syntax Error if ContainsUndefinedContinueTarget of
FunctionStatementList with arguments « » and « » istrue .
14.1.3 Static Semantics: BoundNames
- Return the BoundNames of
BindingIdentifier .
- Return «
"*default*"».
"*default*" is used within this specification as a synthetic name for hoistable anonymous functions that are defined using export declarations.
- Return a new empty
List .
- Let names be BoundNames of
FormalsList . - Append to names the BoundNames of
FunctionRestParameter . - Return names.
- Let names be BoundNames of
FormalsList . - Append to names the elements of BoundNames of
FormalParameter . - Return names.
14.1.4 Static Semantics: Contains
With parameter symbol.
- Return
false .
Static semantic rules that depend upon substructure generally do not look into function definitions.
14.1.5 Static Semantics: ContainsExpression
- Return
false .
- Return ContainsExpression of
FunctionRestParameter .
- If ContainsExpression of
FormalsList istrue , returntrue . - Return ContainsExpression of
FunctionRestParameter .
- If ContainsExpression of
FormalsList istrue , returntrue . - Return ContainsExpression of
FormalParameter .
14.1.6 Static Semantics: ContainsUseStrict
- If the
Directive Prologue ofFunctionStatementList contains aUse Strict Directive , returntrue ; otherwise, returnfalse .
14.1.7 Static Semantics: ExpectedArgumentCount
- Return 0.
- Return 0.
- Return the ExpectedArgumentCount of
FormalsList .
The ExpectedArgumentCount of a
- If HasInitializer of
FormalParameter istrue , return 0. - Return 1.
- Let count be the ExpectedArgumentCount of
FormalsList . - If HasInitializer of
FormalsList istrue or HasInitializer ofFormalParameter istrue , return count. - Return count+1.
14.1.8 Static Semantics: HasInitializer
- If HasInitializer of
FormalsList istrue , returntrue . - Return HasInitializer of
FormalParameter .
14.1.9 Static Semantics: HasName
- Return
false .
- Return
true .
14.1.10 Static Semantics: IsAnonymousFunctionDefinition ( production )
The abstract operation IsAnonymousFunctionDefinition determines if its argument is a function definition that does not bind a name. The argument production is the result of parsing an
- If IsFunctionDefinition of production is
false , returnfalse . - Let hasName be the result of HasName of production.
- If hasName is
true , returnfalse . - Return
true .
14.1.11 Static Semantics: IsConstantDeclaration
- Return
false .
14.1.12 Static Semantics: IsFunctionDefinition
- Return
true .
14.1.13 Static Semantics: IsSimpleParameterList
- Return
true .
- Return
false .
- Return
false .
- If IsSimpleParameterList of
FormalsList isfalse , returnfalse . - Return IsSimpleParameterList of
FormalParameter .
- Return IsSimpleParameterList of
BindingElement .
14.1.14 Static Semantics: LexicallyDeclaredNames
- Return a new empty
List .
- Return TopLevelLexicallyDeclaredNames of
StatementList .
14.1.15 Static Semantics: LexicallyScopedDeclarations
- Return a new empty
List .
- Return the TopLevelLexicallyScopedDeclarations of
StatementList .
14.1.16 Static Semantics: VarDeclaredNames
- Return a new empty
List .
- Return TopLevelVarDeclaredNames of
StatementList .
14.1.17 Static Semantics: VarScopedDeclarations
- Return a new empty
List .
- Return the TopLevelVarScopedDeclarations of
StatementList .
14.1.18 Runtime Semantics: EvaluateBody
With parameter functionObject.
- Return the result of evaluating
FunctionStatementList .
14.1.19 Runtime Semantics: IteratorBindingInitialization
With parameters iteratorRecord and environment.
When
- Return
NormalCompletion (empty ).
- Let restIndex be the result of performing IteratorBindingInitialization for
FormalsList using iteratorRecord and environment as the arguments. ReturnIfAbrupt (restIndex).- Return the result of performing IteratorBindingInitialization for
FunctionRestParameter using iteratorRecord and environment as the arguments.
- Let status be the result of performing IteratorBindingInitialization for
FormalsList using iteratorRecord and environment as the arguments. ReturnIfAbrupt (status).- Return the result of performing IteratorBindingInitialization for
FormalParameter using iteratorRecord and environment as the arguments.
- If ContainsExpression of
BindingElement isfalse , return the result of performing IteratorBindingInitialization forBindingElement using iteratorRecord and environment as the arguments. - Let currentContext be the
running execution context . - Let originalEnv be the VariableEnvironment of currentContext.
- Assert: The VariableEnvironment and LexicalEnvironment of currentContext are the same.
- Assert: environment and originalEnv are the same.
- Let paramVarEnv be
NewDeclarativeEnvironment (originalEnv). - Set the VariableEnvironment of currentContext to paramVarEnv.
- Set the LexicalEnvironment of currentContext to paramVarEnv.
- Let result be the result of performing IteratorBindingInitialization for
BindingElement using iteratorRecord and environment as the arguments. - Set the VariableEnvironment of currentContext to originalEnv.
- Set the LexicalEnvironment of currentContext to originalEnv.
- Return result.
The new
- If ContainsExpression of
BindingRestElement isfalse , return the result of performing IteratorBindingInitialization forBindingRestElement using iteratorRecord and environment as the arguments. - Let currentContext be the
running execution context . - Let originalEnv be the VariableEnvironment of currentContext.
- Assert: The VariableEnvironment and LexicalEnvironment of currentContext are the same.
- Assert: environment and originalEnv are the same.
- Let paramVarEnv be
NewDeclarativeEnvironment (originalEnv). - Set the VariableEnvironment of currentContext to paramVarEnv.
- Set the LexicalEnvironment of currentContext to paramVarEnv.
- Let result be the result of performing IteratorBindingInitialization for
BindingRestElement using iteratorRecord and environment as the arguments. - Set the VariableEnvironment of currentContext to originalEnv.
- Set the LexicalEnvironment of currentContext to originalEnv.
- Return result.
The new
14.1.20 Runtime Semantics: InstantiateFunctionObject
With parameter scope.
- If the function code for
FunctionDeclaration isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let name be StringValue of
BindingIdentifier . - Let F be
FunctionCreate (Normal ,FormalParameters ,FunctionBody , scope, strict). - Perform
MakeConstructor (F). - Perform
SetFunctionName (F, name). - Return F.
- If the function code for
FunctionDeclaration isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let F be
FunctionCreate (Normal ,FormalParameters ,FunctionBody , scope, strict). - Perform
MakeConstructor (F). - Perform
SetFunctionName (F,"default"). - Return F.
An anonymous export default declaration.
14.1.21 Runtime Semantics: Evaluation
- Return
NormalCompletion (empty ).
An alternative semantics is provided in
- Return
NormalCompletion (empty ).
- If the function code for
FunctionExpression isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the LexicalEnvironment of the
running execution context . - Let closure be
FunctionCreate (Normal ,FormalParameters ,FunctionBody , scope, strict). - Perform
MakeConstructor (closure). - Return closure.
- If the function code for
FunctionExpression isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the
running execution context 's LexicalEnvironment. - Let funcEnv be
NewDeclarativeEnvironment (scope). - Let envRec be funcEnv's
EnvironmentRecord . - Let name be StringValue of
BindingIdentifier . - Perform envRec.CreateImmutableBinding(name,
false ). - Let closure be
FunctionCreate (Normal ,FormalParameters ,FunctionBody , funcEnv, strict). - Perform
MakeConstructor (closure). - Perform
SetFunctionName (closure, name). - Perform envRec.InitializeBinding(name, closure).
- Return closure.
The
A prototype property is automatically created for every function defined using a
- Return
NormalCompletion (undefined ).
14.2 Arrow Function Definitions
Syntax
Supplemental Syntax
When the production
is recognized the following grammar is used to refine the interpretation of
14.2.1 Static Semantics: Early Errors
-
It is a Syntax Error if
ArrowParameters ContainsYieldExpression istrue . -
It is a Syntax Error if ContainsUseStrict of
ConciseBody istrue and IsSimpleParameterList ofArrowParameters isfalse . -
It is a Syntax Error if any element of the BoundNames of
ArrowParameters also occurs in the LexicallyDeclaredNames ofConciseBody .
-
If the [Yield] grammar parameter is present on
ArrowParameters , it is a Syntax Error if the lexical token sequence matched byCoverParenthesizedExpressionAndArrowParameterList cannot be parsed with no tokens left over using[?Yield] ArrowFormalParameters as the goal symbol.[Yield] -
If the [Yield] grammar parameter is not present on
ArrowParameters , it is a Syntax Error if the lexical token sequence matched byCoverParenthesizedExpressionAndArrowParameterList cannot be parsed with no tokens left over using[?Yield] ArrowFormalParameters as the goal symbol. -
All early errors rules for
ArrowFormalParameters and its derived productions also apply to CoveredFormalsList ofCoverParenthesizedExpressionAndArrowParameterList .[?Yield]
14.2.2 Static Semantics: BoundNames
- Let formals be CoveredFormalsList of
CoverParenthesizedExpressionAndArrowParameterList . - Return the BoundNames of formals.
14.2.3 Static Semantics: Contains
With parameter symbol.
- If symbol is not one of
NewTarget ,SuperProperty ,SuperCall ,superorthis, returnfalse . - If
ArrowParameters Contains symbol istrue , returntrue . - Return
ConciseBody Contains symbol.
Normally, Contains does not look inside most function forms. However, Contains is used to detect new.target, this, and super usage within an
- Let formals be CoveredFormalsList of
CoverParenthesizedExpressionAndArrowParameterList . - Return formals Contains symbol.
14.2.4 Static Semantics: ContainsExpression
- Return
false .
14.2.5 Static Semantics: ContainsUseStrict
- Return
false .
14.2.6 Static Semantics: ExpectedArgumentCount
- Return 1.
14.2.7 Static Semantics: HasName
- Return
false .
14.2.8 Static Semantics: IsSimpleParameterList
- Return
true .
- Let formals be CoveredFormalsList of
CoverParenthesizedExpressionAndArrowParameterList . - Return IsSimpleParameterList of formals.
14.2.9 Static Semantics: CoveredFormalsList
- Return this
ArrowParameters .
- If the [Yield] grammar parameter is present for
CoverParenthesizedExpressionAndArrowParameterList , return the result of parsing the lexical token stream matched by[Yield] CoverParenthesizedExpressionAndArrowParameterList using[Yield] ArrowFormalParameters as the goal symbol.[Yield] - If the [Yield] grammar parameter is not present for
CoverParenthesizedExpressionAndArrowParameterList , return the result of parsing the lexical token stream matched by[Yield] CoverParenthesizedExpressionAndArrowParameterList usingArrowFormalParameters as the goal symbol.
14.2.10 Static Semantics: LexicallyDeclaredNames
14.2.11 Static Semantics: LexicallyScopedDeclarations
14.2.12 Static Semantics: VarDeclaredNames
14.2.13 Static Semantics: VarScopedDeclarations
14.2.14 Runtime Semantics: IteratorBindingInitialization
With parameters iteratorRecord and environment.
When
- Assert: iteratorRecord.[[Done]] is
false . - Let next be
IteratorStep (iteratorRecord.[[Iterator]]). - If next is an
abrupt completion , set iteratorRecord.[[Done]] totrue . ReturnIfAbrupt (next).- If next is
false , set iteratorRecord.[[Done]] totrue . - Else,
- Let v be
IteratorValue (next). - If v is an
abrupt completion , set iteratorRecord.[[Done]] totrue . ReturnIfAbrupt (v).
- Let v be
- If iteratorRecord.[[Done]] is
true , let v beundefined . - Return the result of performing BindingInitialization for
BindingIdentifier using v and environment as the arguments.
14.2.15 Runtime Semantics: EvaluateBody
With parameter functionObject.
- Let exprRef be the result of evaluating
AssignmentExpression . - Let exprValue be ?
GetValue (exprRef). - Return
Completion {[[Type]]:return , [[Value]]: exprValue, [[Target]]:empty }.
14.2.16 Runtime Semantics: Evaluation
- If the function code for this
ArrowFunction isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the LexicalEnvironment of the
running execution context . - Let parameters be CoveredFormalsList of
ArrowParameters . - Let closure be
FunctionCreate (Arrow , parameters,ConciseBody , scope, strict). - Return closure.
An arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an super, the function object created in step 4 is not made into a method by performing super is always contained within a non-super is accessible via the scope that is captured by the function object of the
14.3 Method Definitions
Syntax
14.3.1 Static Semantics: Early Errors
-
It is a Syntax Error if ContainsUseStrict of
FunctionBody istrue and IsSimpleParameterList ofStrictFormalParameters isfalse . -
It is a Syntax Error if any element of the BoundNames of
StrictFormalParameters also occurs in the LexicallyDeclaredNames ofFunctionBody .
-
It is a Syntax Error if BoundNames of
PropertySetParameterList contains any duplicate elements. -
It is a Syntax Error if ContainsUseStrict of
FunctionBody istrue and IsSimpleParameterList ofPropertySetParameterList isfalse . -
It is a Syntax Error if any element of the BoundNames of
PropertySetParameterList also occurs in the LexicallyDeclaredNames ofFunctionBody .
14.3.2 Static Semantics: ComputedPropertyContains
With parameter symbol.
- Return the result of ComputedPropertyContains for
PropertyName with argument symbol.
14.3.3 Static Semantics: ExpectedArgumentCount
- If HasInitializer of
FormalParameter istrue , return 0. - Return 1.
14.3.4 Static Semantics: HasComputedPropertyKey
- Return IsComputedPropertyKey of
PropertyName .
14.3.5 Static Semantics: HasDirectSuper
- If
StrictFormalParameters ContainsSuperCall istrue , returntrue . - Return
FunctionBody ContainsSuperCall .
- Return
FunctionBody ContainsSuperCall .
- If
PropertySetParameterList ContainsSuperCall istrue , returntrue . - Return
FunctionBody ContainsSuperCall .
14.3.6 Static Semantics: PropName
- Return PropName of
PropertyName .
14.3.7 Static Semantics: SpecialMethod
- Return
false .
- Return
true .
14.3.8 Runtime Semantics: DefineMethod
With parameters object and optional parameter functionPrototype.
- Let propKey be the result of evaluating
PropertyName . ReturnIfAbrupt (propKey).- If the function code for this
MethodDefinition isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the
running execution context 's LexicalEnvironment. - If functionPrototype was passed as a parameter, let kind be
Normal ; otherwise let kind beMethod . - Let closure be
FunctionCreate (kind,StrictFormalParameters ,FunctionBody , scope, strict). If functionPrototype was passed as a parameter, then pass its value as the prototype optional argument ofFunctionCreate . - Perform
MakeMethod (closure, object). - Return the
Record {[[Key]]: propKey, [[Closure]]: closure}.
14.3.9 Runtime Semantics: PropertyDefinitionEvaluation
With parameters object and enumerable.
- Let methodDef be DefineMethod of
MethodDefinition with argument object. ReturnIfAbrupt (methodDef).- Perform
SetFunctionName (methodDef.[[Closure]], methodDef.[[Key]]). - Let desc be the PropertyDescriptor{[[Value]]: methodDef.[[Closure]], [[Writable]]:
true , [[Enumerable]]: enumerable, [[Configurable]]:true }. - Return ?
DefinePropertyOrThrow (object, methodDef.[[Key]], desc).
See
- Let propKey be the result of evaluating
PropertyName . ReturnIfAbrupt (propKey).- If the function code for this
MethodDefinition isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the
running execution context 's LexicalEnvironment. - Let formalParameterList be the production
.FormalParameters : [empty] - Let closure be
FunctionCreate (Method , formalParameterList,FunctionBody , scope, strict). - Perform
MakeMethod (closure, object). - Perform
SetFunctionName (closure, propKey,"get"). - Let desc be the PropertyDescriptor{[[Get]]: closure, [[Enumerable]]: enumerable, [[Configurable]]:
true }. - Return ?
DefinePropertyOrThrow (object, propKey, desc).
- Let propKey be the result of evaluating
PropertyName . ReturnIfAbrupt (propKey).- If the function code for this
MethodDefinition isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the
running execution context 's LexicalEnvironment. - Let closure be
FunctionCreate (Method ,PropertySetParameterList ,FunctionBody , scope, strict). - Perform
MakeMethod (closure, object). - Perform
SetFunctionName (closure, propKey,"set"). - Let desc be the PropertyDescriptor{[[Set]]: closure, [[Enumerable]]: enumerable, [[Configurable]]:
true }. - Return ?
DefinePropertyOrThrow (object, propKey, desc).
14.4 Generator Function Definitions
Syntax
The syntactic context immediately following yield requires use of the
Abstract operations relating to generator objects are defined in
14.4.1 Static Semantics: Early Errors
-
It is a Syntax Error if HasDirectSuper of
GeneratorMethod istrue . -
It is a Syntax Error if
StrictFormalParameters ContainsYieldExpression istrue . -
It is a Syntax Error if ContainsUseStrict of
GeneratorBody istrue and IsSimpleParameterList ofStrictFormalParameters isfalse . -
It is a Syntax Error if any element of the BoundNames of
StrictFormalParameters also occurs in the LexicallyDeclaredNames ofGeneratorBody .
-
If the source code matching this production is strict code, the Early Error rules for
are applied.StrictFormalParameters : FormalParameters -
If the source code matching this production is strict code, it is a Syntax Error if
BindingIdentifier is theIdentifierName evalor theIdentifierName arguments. -
It is a Syntax Error if ContainsUseStrict of
GeneratorBody istrue and IsSimpleParameterList ofFormalParameters isfalse . -
It is a Syntax Error if any element of the BoundNames of
FormalParameters also occurs in the LexicallyDeclaredNames ofGeneratorBody . -
It is a Syntax Error if
FormalParameters ContainsYieldExpression istrue . -
It is a Syntax Error if
FormalParameters ContainsSuperProperty istrue . -
It is a Syntax Error if
GeneratorBody ContainsSuperProperty istrue . -
It is a Syntax Error if
FormalParameters ContainsSuperCall istrue . -
It is a Syntax Error if
GeneratorBody ContainsSuperCall istrue .
14.4.2 Static Semantics: BoundNames
- Return the BoundNames of
BindingIdentifier .
- Return «
"*default*"».
"*default*" is used within this specification as a synthetic name for hoistable anonymous functions that are defined using export declarations.
14.4.3 Static Semantics: ComputedPropertyContains
With parameter symbol.
- Return the result of ComputedPropertyContains for
PropertyName with argument symbol.
14.4.4 Static Semantics: Contains
With parameter symbol.
- Return
false .
Static semantic rules that depend upon substructure generally do not look into function definitions.
14.4.5 Static Semantics: HasComputedPropertyKey
- Return IsComputedPropertyKey of
PropertyName .
14.4.6 Static Semantics: HasDirectSuper
- If
StrictFormalParameters ContainsSuperCall istrue , returntrue . - Return
GeneratorBody ContainsSuperCall .
14.4.7 Static Semantics: HasName
- Return
false .
- Return
true .
14.4.8 Static Semantics: IsConstantDeclaration
- Return
false .
14.4.9 Static Semantics: IsFunctionDefinition
- Return
true .
14.4.10 Static Semantics: PropName
- Return PropName of
PropertyName .
14.4.11 Runtime Semantics: EvaluateBody
With parameter functionObject.
- Let G be ?
OrdinaryCreateFromConstructor (functionObject,"%GeneratorPrototype%", « [[GeneratorState]], [[GeneratorContext]] »). - Perform
GeneratorStart (G,FunctionBody ). - Return
Completion {[[Type]]:return , [[Value]]: G, [[Target]]:empty }.
14.4.12 Runtime Semantics: InstantiateFunctionObject
With parameter scope.
- If the function code for
GeneratorDeclaration isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let name be StringValue of
BindingIdentifier . - Let F be
GeneratorFunctionCreate (Normal ,FormalParameters ,GeneratorBody , scope, strict). - Let prototype be
ObjectCreate (%GeneratorPrototype% ). - Perform
DefinePropertyOrThrow (F,"prototype", PropertyDescriptor{[[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Perform
SetFunctionName (F, name). - Return F.
- If the function code for
GeneratorDeclaration isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let F be
GeneratorFunctionCreate (Normal ,FormalParameters ,GeneratorBody , scope, strict). - Let prototype be
ObjectCreate (%GeneratorPrototype% ). - Perform
DefinePropertyOrThrow (F,"prototype", PropertyDescriptor{[[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Perform
SetFunctionName (F,"default"). - Return F.
An anonymous export default declaration.
14.4.13 Runtime Semantics: PropertyDefinitionEvaluation
With parameter object and enumerable.
- Let propKey be the result of evaluating
PropertyName . ReturnIfAbrupt (propKey).- If the function code for this
GeneratorMethod isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the
running execution context 's LexicalEnvironment. - Let closure be
GeneratorFunctionCreate (Method ,StrictFormalParameters ,GeneratorBody , scope, strict). - Perform
MakeMethod (closure, object). - Let prototype be
ObjectCreate (%GeneratorPrototype% ). - Perform
DefinePropertyOrThrow (closure,"prototype", PropertyDescriptor{[[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Perform
SetFunctionName (closure, propKey). - Let desc be the PropertyDescriptor{[[Value]]: closure, [[Writable]]:
true , [[Enumerable]]: enumerable, [[Configurable]]:true }. - Return ?
DefinePropertyOrThrow (object, propKey, desc).
14.4.14 Runtime Semantics: Evaluation
- If the function code for this
GeneratorExpression isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the LexicalEnvironment of the
running execution context . - Let closure be
GeneratorFunctionCreate (Normal ,FormalParameters ,GeneratorBody , scope, strict). - Let prototype be
ObjectCreate (%GeneratorPrototype% ). - Perform
DefinePropertyOrThrow (closure,"prototype", PropertyDescriptor{[[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Return closure.
- If the function code for this
GeneratorExpression isstrict mode code , let strict betrue . Otherwise let strict befalse . - Let scope be the
running execution context 's LexicalEnvironment. - Let funcEnv be
NewDeclarativeEnvironment (scope). - Let envRec be funcEnv's
EnvironmentRecord . - Let name be StringValue of
BindingIdentifier . - Perform envRec.CreateImmutableBinding(name,
false ). - Let closure be
GeneratorFunctionCreate (Normal ,FormalParameters ,GeneratorBody , funcEnv, strict). - Let prototype be
ObjectCreate (%GeneratorPrototype% ). - Perform
DefinePropertyOrThrow (closure,"prototype", PropertyDescriptor{[[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Perform
SetFunctionName (closure, name). - Perform envRec.InitializeBinding(name, closure).
- Return closure.
The
- Return ?
GeneratorYield (CreateIterResultObject (undefined ,false )).
- Let exprRef be the result of evaluating
AssignmentExpression . - Let value be ?
GetValue (exprRef). - Return ?
GeneratorYield (CreateIterResultObject (value,false )).
- Let exprRef be the result of evaluating
AssignmentExpression . - Let value be ?
GetValue (exprRef). - Let iterator be ?
GetIterator (value). - Let received be
NormalCompletion (undefined ). - Repeat
- If received.[[Type]] is
normal , then- Let innerResult be ?
IteratorNext (iterator, received.[[Value]]). - Let done be ?
IteratorComplete (innerResult). - If done is
true , then- Return ?
IteratorValue (innerResult).
- Return ?
- Let received be
GeneratorYield (innerResult).
- Let innerResult be ?
- Else if received.[[Type]] is
throw , then- Let throw be ?
GetMethod (iterator,"throw"). - If throw is not
undefined , then- Let innerResult be ?
Call (throw, iterator, « received.[[Value]] »). - NOTE: Exceptions from the inner iterator
throwmethod are propagated. Normal completions from an innerthrowmethod are processed similarly to an innernext. - If
Type (innerResult) is not Object, throw aTypeError exception. - Let done be ?
IteratorComplete (innerResult). - If done is
true , then- Return ?
IteratorValue (innerResult).
- Return ?
- Let received be
GeneratorYield (innerResult).
- Let innerResult be ?
- Else,
- NOTE: If iterator does not have a
throwmethod, this throw is going to terminate theyield*loop. But first we need to give iterator a chance to clean up. - Perform ?
IteratorClose (iterator,Completion {[[Type]]:normal , [[Value]]:empty , [[Target]]:empty }). - NOTE: The next step throws a
TypeError to indicate that there was ayield*protocol violation: iterator does not have athrowmethod. - Throw a
TypeError exception.
- NOTE: If iterator does not have a
- Let throw be ?
- Else,
- Assert: received.[[Type]] is
return . - Let return be ?
GetMethod (iterator,"return"). - If return is
undefined , returnCompletion (received). - Let innerReturnResult be ?
Call (return, iterator, « received.[[Value]] »). - If
Type (innerReturnResult) is not Object, throw aTypeError exception. - Let done be ?
IteratorComplete (innerReturnResult). - If done is
true , then- Let value be ?
IteratorValue (innerReturnResult). - Return
Completion {[[Type]]:return , [[Value]]: value, [[Target]]:empty }.
- Let value be ?
- Let received be
GeneratorYield (innerReturnResult).
- Assert: received.[[Type]] is
- If received.[[Type]] is
14.5 Class Definitions
Syntax
A class definition is always strict code.
14.5.1 Static Semantics: Early Errors
-
It is a Syntax Error if
ClassHeritage is not present and the following algorithm evaluates totrue :- Let constructor be ConstructorMethod of
ClassBody . - If constructor is
empty , returnfalse . - Return HasDirectSuper of constructor.
- Let constructor be ConstructorMethod of
-
It is a Syntax Error if PrototypePropertyNameList of
ClassElementList contains more than one occurrence of"constructor".
-
It is a Syntax Error if PropName of
MethodDefinition is not"constructor"and HasDirectSuper ofMethodDefinition istrue . -
It is a Syntax Error if PropName of
MethodDefinition is"constructor"and SpecialMethod ofMethodDefinition istrue .
-
It is a Syntax Error if HasDirectSuper of
MethodDefinition istrue . -
It is a Syntax Error if PropName of
MethodDefinition is"prototype".
14.5.2 Static Semantics: BoundNames
- Return the BoundNames of
BindingIdentifier .
- Return «
"*default*"».
14.5.3 Static Semantics: ConstructorMethod
- If
ClassElement is the production , returnClassElement : ; empty . - If IsStatic of
ClassElement istrue , returnempty . - If PropName of
ClassElement is not"constructor", returnempty . - Return
ClassElement .
- Let head be ConstructorMethod of
ClassElementList . - If head is not
empty , return head. - If
ClassElement is the production , returnClassElement : ; empty . - If IsStatic of
ClassElement istrue , returnempty . - If PropName of
ClassElement is not"constructor", returnempty . - Return
ClassElement .
Early Error rules ensure that there is only one method definition named "constructor" and that it is not an accessor property or generator definition.
14.5.4 Static Semantics: Contains
With parameter symbol.
- If symbol is
ClassBody , returntrue . - If symbol is
ClassHeritage , then- If
ClassHeritage is present, returntrue ; otherwise returnfalse .
- If
- Let inHeritage be
ClassHeritage Contains symbol. - If inHeritage is
true , returntrue . - Return the result of ComputedPropertyContains for
ClassBody with argument symbol.
Static semantic rules that depend upon substructure generally do not look into class bodies except for
14.5.5 Static Semantics: ComputedPropertyContains
With parameter symbol.
- Let inList be the result of ComputedPropertyContains for
ClassElementList with argument symbol. - If inList is
true , returntrue . - Return the result of ComputedPropertyContains for
ClassElement with argument symbol.
- Return the result of ComputedPropertyContains for
MethodDefinition with argument symbol.
- Return the result of ComputedPropertyContains for
MethodDefinition with argument symbol.
- Return
false .
14.5.6 Static Semantics: HasName
- Return
false .
- Return
true .
14.5.7 Static Semantics: IsConstantDeclaration
- Return
false .
14.5.8 Static Semantics: IsFunctionDefinition
- Return
true .
14.5.9 Static Semantics: IsStatic
- Return
false .
- Return
true .
- Return
false .
14.5.10 Static Semantics: NonConstructorMethodDefinitions
- Let list be NonConstructorMethodDefinitions of
ClassElementList . - If
ClassElement is the production , return list.ClassElement : ; - If IsStatic of
ClassElement isfalse and PropName ofClassElement is"constructor", return list. - Append
ClassElement to the end of list. - Return list.
14.5.11 Static Semantics: PrototypePropertyNameList
- Let list be PrototypePropertyNameList of
ClassElementList . - If PropName of
ClassElement isempty , return list. - If IsStatic of
ClassElement istrue , return list. - Append PropName of
ClassElement to the end of list. - Return list.
14.5.12 Static Semantics: PropName
- Return
empty .
14.5.13 Static Semantics: StaticPropertyNameList
- Let list be StaticPropertyNameList of
ClassElementList . - If PropName of
ClassElement isempty , return list. - If IsStatic of
ClassElement isfalse , return list. - Append PropName of
ClassElement to the end of list. - Return list.
14.5.14 Runtime Semantics: ClassDefinitionEvaluation
With parameter className.
- Let lex be the LexicalEnvironment of the
running execution context . - Let classScope be
NewDeclarativeEnvironment (lex). - Let classScopeEnvRec be classScope's
EnvironmentRecord . - If className is not
undefined , then- Perform classScopeEnvRec.CreateImmutableBinding(className,
true ).
- Perform classScopeEnvRec.CreateImmutableBinding(className,
- If
ClassHeritage_opt is not present, then- Let protoParent be the intrinsic object
%ObjectPrototype% . - Let constructorParent be the intrinsic object
%FunctionPrototype% .
- Let protoParent be the intrinsic object
- Else,
- Set the
running execution context 's LexicalEnvironment to classScope. - Let superclass be the result of evaluating
ClassHeritage . - Set the
running execution context 's LexicalEnvironment to lex. ReturnIfAbrupt (superclass).- If superclass is
null , then- Let protoParent be
null . - Let constructorParent be the intrinsic object
%FunctionPrototype% .
- Let protoParent be
- Else if
IsConstructor (superclass) isfalse , throw aTypeError exception. - Else,
- Set the
- Let proto be
ObjectCreate (protoParent). - If
ClassBody_opt is not present, let constructor beempty . - Else, let constructor be ConstructorMethod of
ClassBody . - If constructor is
empty , then- If
ClassHeritage_opt is present, then- Let constructor be the result of parsing the source text
using the syntactic grammar with the goal symbolconstructor(... args){ super (...args);}MethodDefinition .
- Let constructor be the result of parsing the source text
- Else,
- Let constructor be the result of parsing the source text
using the syntactic grammar with the goal symbolconstructor( ){ }MethodDefinition .
- Let constructor be the result of parsing the source text
- If
- Set the
running execution context 's LexicalEnvironment to classScope. - Let constructorInfo be the result of performing DefineMethod for constructor with arguments proto and constructorParent as the optional functionPrototype argument.
- Assert: constructorInfo is not an
abrupt completion . - Let F be constructorInfo.[[Closure]].
- If
ClassHeritage_opt is present, set F's [[ConstructorKind]] internal slot to"derived". - Perform
MakeConstructor (F,false , proto). - Perform
MakeClassConstructor (F). - Perform
CreateMethodProperty (proto,"constructor", F). - If
ClassBody_opt is not present, let methods be a new emptyList . - Else, let methods be NonConstructorMethodDefinitions of
ClassBody . - For each
ClassElement m in order from methods- If IsStatic of m is
false , then- Let status be the result of performing PropertyDefinitionEvaluation for m with arguments proto and
false .
- Let status be the result of performing PropertyDefinitionEvaluation for m with arguments proto and
- Else,
- Let status be the result of performing PropertyDefinitionEvaluation for m with arguments F and
false .
- Let status be the result of performing PropertyDefinitionEvaluation for m with arguments F and
- If status is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to lex. - Return
Completion (status).
- Set the
- If IsStatic of m is
- Set the
running execution context 's LexicalEnvironment to lex. - If className is not
undefined , then- Perform classScopeEnvRec.InitializeBinding(className, F).
- Return F.
14.5.15 Runtime Semantics: BindingClassDeclarationEvaluation
- Let className be StringValue of
BindingIdentifier . - Let value be the result of ClassDefinitionEvaluation of
ClassTail with argument className. ReturnIfAbrupt (value).- Let hasNameProperty be ?
HasOwnProperty (value,"name"). - If hasNameProperty is
false , performSetFunctionName (value, className). - Let env be the
running execution context 's LexicalEnvironment. - Perform ?
InitializeBoundName (className, value, env). - Return value.
- Return the result of ClassDefinitionEvaluation of
ClassTail with argumentundefined .
14.5.16 Runtime Semantics: Evaluation
- Let status be the result of BindingClassDeclarationEvaluation of this
ClassDeclaration . ReturnIfAbrupt (status).- Return
NormalCompletion (empty ).
- If
BindingIdentifier_opt is not present, let className beundefined . - Else, let className be StringValue of
BindingIdentifier . - Let value be the result of ClassDefinitionEvaluation of
ClassTail with argument className. ReturnIfAbrupt (value).- If className is not
undefined , then- Let hasNameProperty be ?
HasOwnProperty (value,"name"). - If hasNameProperty is
false , then- Perform
SetFunctionName (value, className).
- Perform
- Let hasNameProperty be ?
- Return
NormalCompletion (value).
If the class definition included a name static method then that method is not over-written with a name data property for the class name.
14.6 Tail Position Calls
14.6.1 Static Semantics: IsInTailPosition(nonterminal)
The abstract operation IsInTailPosition with argument nonterminal performs the following steps:
- Assert: nonterminal is a parsed grammar production.
- If the source code matching nonterminal is not strict code, return
false . - If nonterminal is not contained within a
FunctionBody orConciseBody , returnfalse . - Let body be the
FunctionBody orConciseBody that most closely contains nonterminal. - If body is the
FunctionBody of aGeneratorBody , returnfalse . - Return the result of HasProductionInTailPosition of body with argument nonterminal.
Tail Position calls are only defined in
14.6.2 Static Semantics: HasProductionInTailPosition
With parameter nonterminal.
nonterminal is a parsed grammar production that represents a specific range of source text. When the following algorithms compare nonterminal to other grammar symbols they are testing whether the same source text was matched by both symbols.
14.6.2.1 Statement Rules
- Return HasProductionInTailPosition of
AssignmentExpression with argument nonterminal.
- Let has be HasProductionInTailPosition of
StatementList with argument nonterminal. - If has is
true , returntrue . - Return HasProductionInTailPosition of
StatementListItem with argument nonterminal.
- Return
false .
- Let has be HasProductionInTailPosition of the first
Statement with argument nonterminal. - If has is
true , returntrue . - Return HasProductionInTailPosition of the second
Statement with argument nonterminal.
- Return HasProductionInTailPosition of
Statement with argument nonterminal.
- Return HasProductionInTailPosition of
LabelledItem with argument nonterminal.
- Return HasProductionInTailPosition of
Expression with argument nonterminal.
- Return HasProductionInTailPosition of
CaseBlock with argument nonterminal.
- Let has be
false . - If the first
CaseClauses is present, let has be HasProductionInTailPosition of the firstCaseClauses with argument nonterminal. - If has is
true , returntrue . - Let has be HasProductionInTailPosition of the
DefaultClause with argument nonterminal. - If has is
true , returntrue . - If the second
CaseClauses is present, let has be HasProductionInTailPosition of the secondCaseClauses with argument nonterminal. - Return has.
- Let has be HasProductionInTailPosition of
CaseClauses with argument nonterminal. - If has is
true , returntrue . - Return HasProductionInTailPosition of
CaseClause with argument nonterminal.
- If
StatementList is present, return HasProductionInTailPosition ofStatementList with argument nonterminal. - Return
false .
- Return HasProductionInTailPosition of
Catch with argument nonterminal.
- Return HasProductionInTailPosition of
Finally with argument nonterminal.
- Return HasProductionInTailPosition of
Block with argument nonterminal.
14.6.2.2 Expression Rules
- Return
false .
- Return HasProductionInTailPosition of
AssignmentExpression with argument nonterminal.
- Let has be HasProductionInTailPosition of the first
AssignmentExpression with argument nonterminal. - If has is
true , returntrue . - Return HasProductionInTailPosition of the second
AssignmentExpression with argument nonterminal.
- Return HasProductionInTailPosition of
BitwiseORExpression with argument nonterminal.
- Return HasProductionInTailPosition of
LogicalANDExpression with argument nonterminal.
- If this
CallExpression is nonterminal, returntrue . - Return
false .
- If this
MemberExpression is nonterminal, returntrue . - Return
false .
- Let expr be CoveredParenthesizedExpression of
CoverParenthesizedExpressionAndArrowParameterList . - Return HasProductionInTailPosition of expr with argument nonterminal.
- Return HasProductionInTailPosition of
Expression with argument nonterminal.
14.6.3 Runtime Semantics: PrepareForTailCall ( )
The abstract operation PrepareForTailCall performs the following steps:
- Let leafContext be the
running execution context . - Suspend leafContext.
- Pop leafContext from the
execution context stack . Theexecution context now on the top of the stack becomes therunning execution context . - Assert: leafContext has no further use. It will never be activated as the
running execution context .
A tail position call must either release any transient internal resources associated with the currently executing function
For example, a tail position call should only grow an implementation's activation record stack by the amount that the size of the target function's activation record exceeds the size of the calling function's activation record. If the target function's activation record is smaller, then the total size of the stack should decrease.