7.4 Operations on Iterator Objects
See Common Iteration Interfaces (
7.4.1 Iterator Records
An Iterator Record is a next
method.
Iterator Records have the fields listed in
Field Name | Value | Meaning |
---|---|---|
[[Iterator]] | an Object | An object that conforms to the Iterator or AsyncIterator interface. |
[[NextMethod]] |
an |
The next method of the [[Iterator]] object.
|
[[Done]] | a Boolean | Whether the iterator has been closed. |
7.4.2 GetIteratorFromMethod ( obj, method )
The abstract operation GetIteratorFromMethod takes arguments obj (an
- Let iterator be ?
Call (method, obj). - If iterator
is not an Object , throw aTypeError exception. - Let nextMethod be ?
Get (iterator,"next" ). - Let iteratorRecord be the
Iterator Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]:false }. - Return iteratorRecord.
7.4.3 GetIterator ( obj, kind )
The abstract operation GetIterator takes arguments obj (an
- If kind is
async , then- Let method be ?
GetMethod (obj,@@asyncIterator ). - If method is
undefined , then- Let syncMethod be ?
GetMethod (obj,@@iterator ). - If syncMethod is
undefined , throw aTypeError exception. - Let syncIteratorRecord be ?
GetIteratorFromMethod (obj, syncMethod). - Return
CreateAsyncFromSyncIterator (syncIteratorRecord).
- Let syncMethod be ?
- Let method be ?
- Else,
- Let method be ?
GetMethod (obj,@@iterator ).
- Let method be ?
- If method is
undefined , throw aTypeError exception. - Return ?
GetIteratorFromMethod (obj, method).
7.4.4 IteratorNext ( iteratorRecord [ , value ] )
The abstract operation IteratorNext takes argument iteratorRecord (an
- If value is not present, then
- Let result be ?
Call (iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]).
- Let result be ?
- Else,
- Let result be ?
Call (iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « value »).
- Let result be ?
- If result
is not an Object , throw aTypeError exception. - Return result.
7.4.5 IteratorComplete ( iterResult )
The abstract operation IteratorComplete takes argument iterResult (an Object) and returns either a
7.4.6 IteratorValue ( iterResult )
The abstract operation IteratorValue takes argument iterResult (an Object) and returns either a
- Return ?
Get (iterResult,"value" ).
7.4.7 IteratorStep ( iteratorRecord )
The abstract operation IteratorStep takes argument iteratorRecord (an
- Let result be ?
IteratorNext (iteratorRecord). - Let done be ?
IteratorComplete (result). - If done is
true , returnfalse . - Return result.
7.4.8 IteratorStepValue ( iteratorRecord )
The abstract operation IteratorStepValue takes argument iteratorRecord (an
- Let result be
Completion (IteratorNext (iteratorRecord)). - If result is a
throw completion , then- Set iteratorRecord.[[Done]] to
true . - Return ? result.
- Set iteratorRecord.[[Done]] to
- Set result to ! result.
- Let done be
Completion (IteratorComplete (result)). - If done is a
throw completion , then- Set iteratorRecord.[[Done]] to
true . - Return ? done.
- Set iteratorRecord.[[Done]] to
- Set done to ! done.
- If done is
true , then- Set iteratorRecord.[[Done]] to
true . - Return
done .
- Set iteratorRecord.[[Done]] to
- Let value be
Completion (Get (result,"value" )). - If value is a
throw completion , then- Set iteratorRecord.[[Done]] to
true .
- Set iteratorRecord.[[Done]] to
- Return ? value.
7.4.9 IteratorClose ( iteratorRecord, completion )
The abstract operation IteratorClose takes arguments iteratorRecord (an
Assert : iteratorRecord.[[Iterator]]is an Object .- Let iterator be iteratorRecord.[[Iterator]].
- Let innerResult be
Completion (GetMethod (iterator,"return" )). - If innerResult is a
normal completion , then- Let return be innerResult.[[Value]].
- If return is
undefined , return ? completion. - Set innerResult to
Completion (Call (return, iterator)).
- If completion is a
throw completion , return ? completion. - If innerResult is a
throw completion , return ? innerResult. - If innerResult.[[Value]]
is not an Object , throw aTypeError exception. - Return ? completion.
7.4.10 IfAbruptCloseIterator ( value, iteratorRecord )
IfAbruptCloseIterator is a shorthand for a sequence of algorithm steps that use an
IfAbruptCloseIterator (value, iteratorRecord).
means the same thing as:
Assert : value is aCompletion Record .- If value is an
abrupt completion , return ?IteratorClose (iteratorRecord, value). - Else, set value to ! value.
7.4.11 AsyncIteratorClose ( iteratorRecord, completion )
The abstract operation AsyncIteratorClose takes arguments iteratorRecord (an
Assert : iteratorRecord.[[Iterator]]is an Object .- Let iterator be iteratorRecord.[[Iterator]].
- Let innerResult be
Completion (GetMethod (iterator,"return" )). - If innerResult is a
normal completion , then- Let return be innerResult.[[Value]].
- If return is
undefined , return ? completion. - Set innerResult to
Completion (Call (return, iterator)). - If innerResult is a
normal completion , set innerResult toCompletion (Await (innerResult.[[Value]])).
- If completion is a
throw completion , return ? completion. - If innerResult is a
throw completion , return ? innerResult. - If innerResult.[[Value]]
is not an Object , throw aTypeError exception. - Return ? completion.
7.4.12 CreateIterResultObject ( value, done )
The abstract operation CreateIterResultObject takes arguments value (an
- Let obj be
OrdinaryObjectCreate (%Object.prototype% ). - Perform !
CreateDataPropertyOrThrow (obj,"value" , value). - Perform !
CreateDataPropertyOrThrow (obj,"done" , done). - Return obj.
7.4.13 CreateListIteratorRecord ( list )
The abstract operation CreateListIteratorRecord takes argument list (a next
method returns the successive elements of list. It performs the following steps when called:
- Let closure be a new
Abstract Closure with no parameters that captures list and performs the following steps when called:- For each element E of list, do
- Perform ?
GeneratorYield (CreateIterResultObject (E,false )).
- Perform ?
- Return
NormalCompletion (undefined ).
- For each element E of list, do
- Let iterator be
CreateIteratorFromClosure (closure,empty ,%IteratorPrototype% ). - Return the
Iterator Record { [[Iterator]]: iterator, [[NextMethod]]: %GeneratorFunction.prototype.prototype.next%, [[Done]]:false }.
The list iterator object is never directly accessible to ECMAScript code.
7.4.14 IteratorToList ( iteratorRecord )
The abstract operation IteratorToList takes argument iteratorRecord (an
- Let values be a new empty
List . - Repeat,
- Let next be ?
IteratorStepValue (iteratorRecord). - If next is
done , then- Return values.
- Append next to values.
- Let next be ?