15.5 Generator Function Definitions
Syntax
The syntactic context immediately following yield
requires use of the
15.5.1 Static Semantics: Early Errors
-
It is a Syntax Error if
HasDirectSuper ofGeneratorMethod istrue . -
It is a Syntax Error if
UniqueFormalParameters Contains YieldExpression istrue . -
It is a Syntax Error if
FunctionBodyContainsUseStrict ofGeneratorBody istrue andIsSimpleParameterList ofUniqueFormalParameters isfalse . -
It is a Syntax Error if any element of the
BoundNames ofUniqueFormalParameters also occurs in theLexicallyDeclaredNames ofGeneratorBody .
-
If the
source text matched by FormalParameters isstrict mode code , the Early Error rules for are applied.UniqueFormalParameters : FormalParameters -
If
BindingIdentifier is present and thesource text matched by BindingIdentifier isstrict mode code , it is a Syntax Error if theStringValue ofBindingIdentifier is either"eval" or"arguments" . -
It is a Syntax Error if
FunctionBodyContainsUseStrict ofGeneratorBody istrue andIsSimpleParameterList ofFormalParameters isfalse . -
It is a Syntax Error if any element of the
BoundNames ofFormalParameters also occurs in theLexicallyDeclaredNames ofGeneratorBody . -
It is a Syntax Error if
FormalParameters Contains YieldExpression istrue . -
It is a Syntax Error if
FormalParameters Contains SuperProperty istrue . -
It is a Syntax Error if
GeneratorBody Contains SuperProperty istrue . -
It is a Syntax Error if
FormalParameters Contains SuperCall istrue . -
It is a Syntax Error if
GeneratorBody Contains SuperCall istrue .
15.5.2 Runtime Semantics: EvaluateGeneratorBody
The
- Perform ?
FunctionDeclarationInstantiation (functionObject, argumentsList). - Let G be ?
OrdinaryCreateFromConstructor (functionObject,"%GeneratorFunction.prototype.prototype%" , « [[GeneratorState]], [[GeneratorContext]], [[GeneratorBrand]] »). - Set G.[[GeneratorBrand]] to
empty . - Perform
GeneratorStart (G,FunctionBody ). - Return
Completion Record { [[Type]]:return , [[Value]]: G, [[Target]]:empty }.
15.5.3 Runtime Semantics: InstantiateGeneratorFunctionObject
The
- Let name be
StringValue ofBindingIdentifier . - Let sourceText be the
source text matched by GeneratorDeclaration . - Let F be
OrdinaryFunctionCreate (%GeneratorFunction.prototype% , sourceText,FormalParameters ,GeneratorBody ,non-lexical-this , env, privateEnv). - Perform
SetFunctionName (F, name). - Let prototype be
OrdinaryObjectCreate (%GeneratorFunction.prototype.prototype% ). - Perform !
DefinePropertyOrThrow (F,"prototype" , PropertyDescriptor { [[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Return F.
- Let sourceText be the
source text matched by GeneratorDeclaration . - Let F be
OrdinaryFunctionCreate (%GeneratorFunction.prototype% , sourceText,FormalParameters ,GeneratorBody ,non-lexical-this , env, privateEnv). - Perform
SetFunctionName (F,"default" ). - Let prototype be
OrdinaryObjectCreate (%GeneratorFunction.prototype.prototype% ). - Perform !
DefinePropertyOrThrow (F,"prototype" , PropertyDescriptor { [[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Return F.
An anonymous export default
declaration, and its function code is therefore always
15.5.4 Runtime Semantics: InstantiateGeneratorFunctionExpression
The
- If name is not present, set name to
"" . - Let env be the LexicalEnvironment of the
running execution context . - Let privateEnv be the
running execution context 's PrivateEnvironment. - Let sourceText be the
source text matched by GeneratorExpression . - Let closure be
OrdinaryFunctionCreate (%GeneratorFunction.prototype% , sourceText,FormalParameters ,GeneratorBody ,non-lexical-this , env, privateEnv). - Perform
SetFunctionName (closure, name). - Let prototype be
OrdinaryObjectCreate (%GeneratorFunction.prototype.prototype% ). - Perform !
DefinePropertyOrThrow (closure,"prototype" , PropertyDescriptor { [[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Return closure.
Assert : name is not present.- Set name to
StringValue ofBindingIdentifier . - Let outerEnv be the
running execution context 's LexicalEnvironment. - Let funcEnv be
NewDeclarativeEnvironment (outerEnv). - Perform ! funcEnv.CreateImmutableBinding(name,
false ). - Let privateEnv be the
running execution context 's PrivateEnvironment. - Let sourceText be the
source text matched by GeneratorExpression . - Let closure be
OrdinaryFunctionCreate (%GeneratorFunction.prototype% , sourceText,FormalParameters ,GeneratorBody ,non-lexical-this , funcEnv, privateEnv). - Perform
SetFunctionName (closure, name). - Let prototype be
OrdinaryObjectCreate (%GeneratorFunction.prototype.prototype% ). - Perform !
DefinePropertyOrThrow (closure,"prototype" , PropertyDescriptor { [[Value]]: prototype, [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }). - Perform ! funcEnv.InitializeBinding(name, closure).
- Return closure.
The
15.5.5 Runtime Semantics: Evaluation
- Return ?
Yield (undefined ).
- Let exprRef be ?
Evaluation ofAssignmentExpression . - Let value be ?
GetValue (exprRef). - Return ?
Yield (value).
- Let generatorKind be
GetGeneratorKind (). - Let exprRef be ?
Evaluation ofAssignmentExpression . - Let value be ?
GetValue (exprRef). - Let iteratorRecord be ?
GetIterator (value, generatorKind). - Let iterator be iteratorRecord.[[Iterator]].
- Let received be
NormalCompletion (undefined ). - Repeat,
- If received is a
normal completion , then- Let innerResult be ?
Call (iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « received.[[Value]] »). - If generatorKind is
async , set innerResult to ?Await (innerResult). - If innerResult
is not an Object , throw aTypeError exception. - Let done be ?
IteratorComplete (innerResult). - If done is
true , then- Return ?
IteratorValue (innerResult).
- Return ?
- If generatorKind is
async , set received toCompletion (AsyncGeneratorYield (?IteratorValue (innerResult))). - Else, set received to
Completion (GeneratorYield (innerResult)).
- Let innerResult be ?
- Else if received is a
throw completion , then- Let throw be ?
GetMethod (iterator,"throw" ). - If throw is not
undefined , then- Let innerResult be ?
Call (throw, iterator, « received.[[Value]] »). - If generatorKind is
async , set innerResult to ?Await (innerResult). - NOTE: Exceptions from the inner iterator
throw
method are propagated.Normal completions from an innerthrow
method are processed similarly to an innernext
. - If innerResult
is not an Object , throw aTypeError exception. - Let done be ?
IteratorComplete (innerResult). - If done is
true , then- Return ?
IteratorValue (innerResult).
- Return ?
- If generatorKind is
async , set received toCompletion (AsyncGeneratorYield (?IteratorValue (innerResult))). - Else, set received to
Completion (GeneratorYield (innerResult)).
- Let innerResult be ?
- Else,
- NOTE: If iterator does not have a
throw
method, this throw is going to terminate theyield*
loop. But first we need to give iterator a chance to clean up. - Let closeCompletion be
Completion Record { [[Type]]:normal , [[Value]]:empty , [[Target]]:empty }. - If generatorKind is
async , perform ?AsyncIteratorClose (iteratorRecord, closeCompletion). - Else, perform ?
IteratorClose (iteratorRecord, closeCompletion). - NOTE: The next step throws a
TypeError to indicate that there was ayield*
protocol violation: iterator does not have athrow
method. - Throw a
TypeError exception.
- NOTE: If iterator does not have a
- Let throw be ?
- Else,
Assert : received is areturn completion .- Let return be ?
GetMethod (iterator,"return" ). - If return is
undefined , then- Set value to received.[[Value]].
- If generatorKind is
async , then- Set value to ?
Await (value).
- Set value to ?
- Return
Completion Record { [[Type]]:return , [[Value]]: value, [[Target]]:empty }.
- Let innerReturnResult be ?
Call (return, iterator, « received.[[Value]] »). - If generatorKind is
async , set innerReturnResult to ?Await (innerReturnResult). - If innerReturnResult
is not an Object , throw aTypeError exception. - Let done be ?
IteratorComplete (innerReturnResult). - If done is
true , then- Set value to ?
IteratorValue (innerReturnResult). - Return
Completion Record { [[Type]]:return , [[Value]]: value, [[Target]]:empty }.
- Set value to ?
- If generatorKind is
async , set received toCompletion (AsyncGeneratorYield (?IteratorValue (innerReturnResult))). - Else, set received to
Completion (GeneratorYield (innerReturnResult)).
- If received is a