This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
spec.html
661 lines (615 loc) · 30.5 KB
/
spec.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<!DOCTYPE html>
<meta charset="utf-8">
<pre class="metadata">
title: Iterator Helpers
status: proposal
stage: 3
location: https://tc39.es/proposal-iterator-helpers
copyright: false
contributors: Gus Caplan
</pre>
<style>
ins.block {
font-weight: bold;
font-style: italic;
}
hr {
height: 0.25em;
background: #ccc;
border: 0;
margin: 2em 0;
}
.unicode-property-table {
table-layout: fixed;
width: 100%;
font-size: 80%;
}
.unicode-property-table ul {
padding-left: 0;
list-style: none;
}
#metadata-block {
margin: 4em 0;
padding: 10px;
border: 1px solid #ee8421;
}
#metadata-block h1 {
font-size: 1.5em;
margin-top: 0;
}
#metadata-block > ul {
list-style-type: none;
margin: 0; padding: 0;
}
</style>
<div id="metadata-block">
<h1>Contributing to this Proposal</h1>
<p>This proposal is developed on GitHub with the help of the ECMAScript community. There are a number of ways to contribute to the development of this specification:</p>
<ul>
<li>GitHub Repository: <a href="https://github.com/tc39/proposal-iterator-helpers">https://github.com/tc39/proposal-iterator-helpers</a></li>
<li>Issues: <a href="https://github.com/tc39/proposal-iterator-helpers/issues">All Issues</a>, <a href="https://github.com/tc39/proposal-iterator-helpers/issues/new">File a New Issue</a></li>
<li>Pull Requests: <a href="https://github.com/tc39/proposal-iterator-helpers/pulls">All Pull Requests</a>, <a href="https://github.com/tc39/proposal-iterator-helpers/pulls/new">Create a New Pull Request</a></li>
<!-- <li>Test Suite: <a href="https://github.com/tc39/test262">Test262</a></li> -->
<li>
<p>Community:</p>
<ul>
<li>Discourse: <a href="https://es.discourse.group">https://es.discourse.group</a></li>
<li>Chat: <a href="https://github.com/tc39/how-we-work/blob/HEAD/matrix-guide.md">Matrix</a></li>
</ul>
</li>
</ul>
</div>
<emu-clause id="sec-well-known-intrinsic-objects">
<h1>Well-Known Intrinsic Objects</h1>
<emu-table id="table-7" caption="Well-Known Intrinsic Objects">
<table>
<tr>
<th>
Intrinsic Name
</th>
<th>
Global Name
</th>
<th>
ECMAScript Language Association
</th>
</tr>
<tr>
<td>
<ins>%Iterator%</ins>
</td>
<td>
<ins>`Iterator`</ins>
</td>
<td>
<ins>The `Iterator` constructor (<emu-xref href="#sec-iterator-constructor"></emu-xref>)</ins>
</td>
</tr>
<tr>
<td>
%IteratorPrototype%
</td>
<td>
<ins>`Iterator.prototype`</ins>
</td>
<td>
An object that all standard built-in iterator objects indirectly inherit from
<p><ins>The initial value of the *"prototype"* data property of %Iterator%; i.e., %Iterator.prototype%</ins></p>
</td>
</tr>
</table>
</emu-table>
</emu-clause>
<emu-clause id="sec-abstract-operations">
<h1>Abstract Operations</h1>
<emu-clause id="sec-SetterThatIgnoresPrototypeProperties" type="abstract operation">
<h1>
SetterThatIgnoresPrototypeProperties (
_this_: an ECMAScript language value,
_home_: an Object,
_p_: a property key,
_v_: an ECMAScript language value,
): either a normal completion containing ~unused~ or a throw completion
</h1>
<dl class="header">
</dl>
<emu-alg>
1. If _this_ is not an Object, then
1. Throw a *TypeError* exception.
1. If _this_ is _home_, then
1. NOTE: Throwing here emulates assignment to a non-writable data property on the _home_ object in strict mode code.
1. Throw a *TypeError* exception.
1. Let _desc_ be ? _this_.[[GetOwnProperty]](_p_).
1. If _desc_ is *undefined*, then
1. Perform ? CreateDataPropertyOrThrow(_this_, _p_, _v_).
1. Else,
1. Perform ? Set(_this_, _p_, _v_, *true*).
1. Return ~unused~.
</emu-alg>
</emu-clause>
<emu-clause id="sec-operations-on-iterator-objects">
<h1>Operations on Iterator Objects</h1>
<emu-clause id="sec-getiteratordirect" type="abstract operation">
<h1>
GetIteratorDirect (
_obj_: an Object,
): either a normal completion containing an Iterator Record or a throw completion
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _nextMethod_ be ? Get(_obj_, `"next"`).
1. Let _iteratorRecord_ be Record { [[Iterator]]: _obj_, [[NextMethod]]: _nextMethod_, [[Done]]: *false* }.
1. Return _iteratorRecord_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-getiteratorflattenable" type="abstract operation">
<h1>
GetIteratorFlattenable (
_obj_: an ECMAScript language value,
_stringHandling_: ~iterate-strings~ or ~reject-strings~,
): either a normal completion containing an Iterator Record or a throw completion
</h1>
<dl class="header">
</dl>
<emu-alg>
1. If _obj_ is not an Object, then
1. If _stringHandling_ is ~reject-strings~ or _obj_ is not a String, throw a *TypeError* exception.
1. Let _method_ be ? GetMethod(_obj_, @@iterator).
1. If _method_ is *undefined*, then
1. Let _iterator_ be _obj_.
1. Else,
1. Let _iterator_ be ? Call(_method_, _obj_).
1. If _iterator_ is not an Object, throw a *TypeError* exception.
1. Return ? GetIteratorDirect(_iterator_).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-control-abstraction-objects">
<h1>Control Abstraction Objects</h1>
<emu-clause id="sec-iteration">
<h1>Iteration</h1>
<emu-clause id="sec-iterator-objects">
<h1>Iterator Objects</h1>
<emu-clause id="sec-iterator-constructor">
<h1>The Iterator Constructor</h1>
<p>The <dfn>Iterator</dfn> constructor:</p>
<ul>
<li>is <dfn>%Iterator%</dfn>.</li>
<li>is the initial value of the *"Iterator"* property of the global object.</li>
<li>is designed to be subclassable. It may be used as the value of an *extends* clause of a class definition.</li>
</ul>
<emu-clause id="sec-iterator">
<h1>Iterator ( )</h1>
<p>When the `Iterator` function is called, the following steps are taken:</p>
<emu-alg>
1. If NewTarget is *undefined* or the active function object, throw a *TypeError* exception.
1. Return ? OrdinaryCreateFromConstructor(NewTarget, *"%Iterator.prototype%"*).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-properties-of-the-iterator-constructor">
<h1>Properties of the Iterator Constructor</h1>
<emu-clause id="sec-iterator.prototype">
<h1>Iterator.prototype</h1>
<p>The initial value of Iterator.prototype is %Iterator.prototype%.</p>
<p>This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *false* }.</p>
</emu-clause>
<emu-clause id="sec-iterator.from">
<h1>Iterator.from ( _O_ )</h1>
<emu-alg>
1. Let _iteratorRecord_ be ? GetIteratorFlattenable(_O_, ~iterate-strings~).
1. Let _hasInstance_ be ? OrdinaryHasInstance(%Iterator%, _iteratorRecord_.[[Iterator]]).
1. If _hasInstance_ is *true*, then
1. Return _iteratorRecord_.[[Iterator]].
1. Let _wrapper_ be OrdinaryObjectCreate(%WrapForValidIteratorPrototype%, « [[Iterated]] »).
1. Set _wrapper_.[[Iterated]] to _iteratorRecord_.
1. Return _wrapper_.
</emu-alg>
<emu-clause id="sec-wrapforvaliditeratorprototype-object">
<h1>The %WrapForValidIteratorPrototype% Object</h1>
<p>The <dfn>%WrapForValidIteratorPrototype%</dfn> object:</p>
<ul>
<li>has a [[Prototype]] internal slot whose value is %Iterator.prototype%.</li>
</ul>
<emu-clause id="sec-wrapforvaliditeratorprototype.next">
<h1>%WrapForValidIteratorPrototype%.next ( )</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. Perform ? RequireInternalSlot(_O_, [[Iterated]]).
1. Let _iteratorRecord_ be _O_.[[Iterated]].
1. Return ? Call(_iteratorRecord_.[[NextMethod]], _iteratorRecord_.[[Iterator]]).
</emu-alg>
</emu-clause>
<emu-clause id="sec-wrapforvaliditeratorprototype.return">
<h1>%WrapForValidIteratorPrototype%.return ( )</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. Perform ? RequireInternalSlot(_O_, [[Iterated]]).
1. Let _iterator_ be _O_.[[Iterated]].[[Iterator]].
1. Assert: _iterator_ is an Object.
1. Let _returnMethod_ be ? GetMethod(_iterator_, *"return"*).
1. If _returnMethod_ is *undefined*, then
1. Return CreateIterResultObject(*undefined*, *true*).
1. Return ? Call(_returnMethod_, _iterator_).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-iterator-helper-objects">
<h1>Iterator Helper Objects</h1>
<p>An Iterator Helper object is an ordinary object that represents a lazy transformation of some specific source iterator object. There is not a named constructor for Iterator Helper objects. Instead, Iterator Helper objects are created by calling certain methods of Iterator instance objects.</p>
<emu-clause id="sec-%iteratorhelperprototype%-object">
<h1>The %IteratorHelperPrototype% Object</h1>
<p>The <dfn>%IteratorHelperPrototype%</dfn> object:</p>
<ul>
<li>has properties that are inherited by all Iterator Helper Objects.</li>
<li>is an ordinary object.</li>
<li>has a [[Prototype]] internal slot whose value is %Iterator.prototype%.</li>
<li>has the following properties:</li>
</ul>
<emu-clause id="sec-%iteratorhelperprototype%.next">
<h1>%IteratorHelperPrototype%.next ( )</h1>
<emu-alg>
1. Return ? GeneratorResume(*this* value, *undefined*, *"Iterator Helper"*).
</emu-alg>
</emu-clause>
<emu-clause id="sec-%iteratorhelperprototype%.return">
<h1>%IteratorHelperPrototype%.return ( )</h1>
<emu-alg>
1. Let _O_ be *this* value.
1. Perform ? RequireInternalSlot(_O_, [[UnderlyingIterator]]).
1. Assert: _O_ has a [[GeneratorState]] slot.
1. If _O_.[[GeneratorState]] is ~suspended-start~, then
1. Set _O_.[[GeneratorState]] to ~completed~.
1. NOTE: Once a generator enters the completed state it never leaves it and its associated execution context is never resumed. Any execution state associated with _O_ can be discarded at this point.
1. Perform ? IteratorClose(_O_.[[UnderlyingIterator]], NormalCompletion(~unused~)).
1. Return CreateIterResultObject(*undefined*, *true*).
1. Let _C_ be Completion { [[Type]]: ~return~, [[Value]]: *undefined*, [[Target]]: ~empty~ }.
1. Return ? GeneratorResumeAbrupt(_O_, _C_, *"Iterator Helper"*).
</emu-alg>
</emu-clause>
<emu-clause id="sec-%iteratorhelperprototype%-@@tostringtag">
<h1>%IteratorHelperPrototype% [ @@toStringTag ]</h1>
<p>The initial value of the @@toStringTag property is the String value *"Iterator Helper"*.</p>
<p>This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *true* }.</p>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="sec-iteratorprototype">
<h1>Iterator.prototype</h1>
<p>The <dfn>Iterator prototype object</dfn>:</p>
<ul>
<li>is <dfn>%Iterator.prototype%</dfn>.</li>
<li>has a [[Prototype]] internal slot whose value is %Object.prototype%.</li>
<li>is an ordinary object.</li>
</ul>
<emu-clause id="sec-iteratorprototype.constructor">
<h1>Iterator.prototype.constructor</h1>
<p>`Iterator.prototype.constructor` is an accessor property with attributes { [[Enumerable]]: *false*, [[Configurable]]: *true* }. The [[Get]] and [[Set]] attributes are defined as follows:</p>
<emu-clause id="sec-get-iteratorprototype-constructor">
<h1>get Iterator.prototype.constructor</h1>
<p>The value of the [[Get]] attribute is a built-in function that requires no arguments. It performs the following steps when called:</p>
<emu-alg>
1. Return %Iterator%.
</emu-alg>
</emu-clause>
<emu-clause id="sec-set-iteratorprototype-constructor">
<h1>set Iterator.prototype.constructor</h1>
<p>The value of the [[Set]] attribute is a built-in function that takes an argument _v_. It performs the following steps when called:</p>
<emu-alg>
1. Perform ? SetterThatIgnoresPrototypeProperties(*this* value, %Iterator.prototype%, *"constructor"*, _v_).
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-note>
<p>Unlike the *"constructor"* property on most built-in prototypes, for web-compatibility reasons this property must be an accessor.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-iteratorprototype.map">
<h1>Iterator.prototype.map ( _mapper_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *undefined*.
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_mapped_, _iterated_).
1. Let _completion_ be Completion(Yield(_mapped_)).
1. IfAbruptCloseIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Let _result_ be CreateIteratorFromClosure(_closure_, *"Iterator Helper"*, %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
1. Set _result_.[[UnderlyingIterator]] to _iterated_.
1. Return _result_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.filter">
<h1>Iterator.prototype.filter ( _predicate_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _predicate_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *undefined*.
1. Let _selected_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_selected_, _iterated_).
1. If ToBoolean(_selected_) is *true*, then
1. Let _completion_ be Completion(Yield(_value_)).
1. IfAbruptCloseIterator(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Let _result_ be CreateIteratorFromClosure(_closure_, *"Iterator Helper"*, %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
1. Set _result_.[[UnderlyingIterator]] to _iterated_.
1. Return _result_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.take">
<h1>Iterator.prototype.take ( _limit_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. Let _numLimit_ be ? ToNumber(_limit_).
1. If _numLimit_ is *NaN*, throw a *RangeError* exception.
1. Let _integerLimit_ be ! ToIntegerOrInfinity(_numLimit_).
1. If _integerLimit_ < 0, throw a *RangeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _integerLimit_ and performs the following steps when called:
1. Let _remaining_ be _integerLimit_.
1. Repeat,
1. If _remaining_ is 0, then
1. Return ? IteratorClose(_iterated_, NormalCompletion(*undefined*)).
1. If _remaining_ is not +∞, then
1. Set _remaining_ to _remaining_ - 1.
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *undefined*.
1. Let _completion_ be Completion(Yield(_value_)).
1. IfAbruptCloseIterator(_completion_, _iterated_).
1. Let _result_ be CreateIteratorFromClosure(_closure_, *"Iterator Helper"*, %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
1. Set _result_.[[UnderlyingIterator]] to _iterated_.
1. Return _result_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.drop">
<h1>Iterator.prototype.drop ( _limit_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. Let _numLimit_ be ? ToNumber(_limit_).
1. If _numLimit_ is *NaN*, throw a *RangeError* exception.
1. Let _integerLimit_ be ! ToIntegerOrInfinity(_numLimit_).
1. If _integerLimit_ < 0, throw a *RangeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _integerLimit_ and performs the following steps when called:
1. Let _remaining_ be _integerLimit_.
1. Repeat, while _remaining_ > 0,
1. If _remaining_ is not +∞, then
1. Set _remaining_ to _remaining_ - 1.
1. Let _next_ be ? IteratorStep(_iterated_).
1. If _next_ is *false*, return *undefined*.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *undefined*.
1. Let _completion_ be Completion(Yield(_value_)).
1. IfAbruptCloseIterator(_completion_, _iterated_).
1. Let _result_ be CreateIteratorFromClosure(_closure_, *"Iterator Helper"*, %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
1. Set _result_.[[UnderlyingIterator]] to _iterated_.
1. Return _result_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.flatmap">
<h1>Iterator.prototype.flatMap ( _mapper_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_mapper_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _mapper_ and performs the following steps when called:
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *undefined*.
1. Let _mapped_ be Completion(Call(_mapper_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_mapped_, _iterated_).
1. Let _innerIterator_ be Completion(GetIteratorFlattenable(_mapped_, ~reject-strings~)).
1. IfAbruptCloseIterator(_innerIterator_, _iterated_).
1. Let _innerAlive_ be *true*.
1. Repeat, while _innerAlive_ is *true*,
1. Let _innerValue_ be Completion(IteratorStepValue(_innerIterator_)).
1. IfAbruptCloseIterator(_innerValue_, _iterated_).
1. If _innerValue_ is ~done~, then
1. Set _innerAlive_ to *false*.
1. Else,
1. Let _completion_ be Completion(Yield(_innerValue_)).
1. If _completion_ is an abrupt completion, then
1. Let _backupCompletion_ be Completion(IteratorClose(_innerIterator_, _completion_)).
1. IfAbruptCloseIterator(_backupCompletion_, _iterated_).
1. Return ? IteratorClose(_completion_, _iterated_).
1. Set _counter_ to _counter_ + 1.
1. Let _result_ be CreateIteratorFromClosure(_closure_, *"Iterator Helper"*, %IteratorHelperPrototype%, « [[UnderlyingIterator]] »).
1. Set _result_.[[UnderlyingIterator]] to _iterated_.
1. Return _result_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.reduce">
<h1>Iterator.prototype.reduce ( _reducer_ [ , _initialValue_ ] )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_reducer_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. If _initialValue_ is not present, then
1. Let _accumulator_ be ? IteratorStepValue(_iterated_).
1. If _accumulator_ is ~done~, throw a *TypeError* exception.
1. Let _counter_ be 1.
1. Else,
1. Let _accumulator_ be _initialValue_.
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return _accumulator_.
1. Let _result_ be Completion(Call(_reducer_, *undefined*, « _accumulator_, _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. Set _accumulator_ to _result_.[[Value]].
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.toarray">
<h1>Iterator.prototype.toArray ( )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _items_ be a new empty List.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return CreateArrayFromList(_items_).
1. Append _value_ to _items_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.foreach">
<h1>Iterator.prototype.forEach ( _fn_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_fn_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *undefined*.
1. Let _result_ be Completion(Call(_fn_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.some">
<h1>Iterator.prototype.some ( _predicate_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *false*.
1. Let _result_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? IteratorClose(_iterated_, NormalCompletion(*true*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.every">
<h1>Iterator.prototype.every ( _predicate_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *true*.
1. Let _result_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *false*, return ? IteratorClose(_iterated_, NormalCompletion(*false*)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype.find">
<h1>Iterator.prototype.find ( _predicate_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _counter_ be 0.
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, return *undefined*.
1. Let _result_ be Completion(Call(_predicate_, *undefined*, « _value_, 𝔽(_counter_) »)).
1. IfAbruptCloseIterator(_result_, _iterated_).
1. If ToBoolean(_result_) is *true*, return ? IteratorClose(_iterated_, NormalCompletion(_value_)).
1. Set _counter_ to _counter_ + 1.
</emu-alg>
</emu-clause>
<emu-clause id="sec-iteratorprototype-@@tostringtag">
<h1>Iterator.prototype [ @@toStringTag ]</h1>
<p>`Iterator.prototype[@@toStringTag]` is an accessor property with attributes { [[Enumerable]]: *false*, [[Configurable]]: *true* }. The [[Get]] and [[Set]] attributes are defined as follows:</p>
<emu-clause id="sec-get-iteratorprototype-@@tostringtag">
<h1>get Iterator.prototype [ @@toStringTag ]</h1>
<p>The value of the [[Get]] attribute is a built-in function that requires no arguments. It performs the following steps when called:</p>
<emu-alg>
1. Return *"Iterator"*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-set-iteratorprototype-@@tostringtag">
<h1>set Iterator.prototype [ @@toStringTag ]</h1>
<p>The value of the [[Set]] attribute is a built-in function that takes an argument _v_. It performs the following steps when called:</p>
<emu-alg>
1. Perform ? SetterThatIgnoresPrototypeProperties(*this* value, %Iterator.prototype%, %Symbol.toStringTag%, _v_).
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-note>
<p>Unlike the @@toStringTag property on most built-in prototypes, for web-compatibility reasons this property must be an accessor.</p>
</emu-note>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="updated-abstract-operations">
<h1>Updated Abstract Operations</h1>
<emu-clause id="sec-createiteratorfromclosure" type="abstract operation">
<h1>
CreateIteratorFromClosure (
_closure_: an Abstract Closure with no parameters,
_generatorBrand_: a String or ~empty~,
_generatorPrototype_: an Object,
<ins>optional _extraSlots_: a List of names of internal slots,</ins>
): a Generator
</h1>
<dl class="header">
</dl>
<emu-alg>
1. NOTE: _closure_ can contain uses of the Yield operation to yield an IteratorResult object.
1. <ins>If _extraSlots_ is not present, set _extraSlots_ to a new empty List.</ins>
1. Let _internalSlotsList_ be <ins>the list-concatenation of _extraSlots_ and</ins> « [[GeneratorState]], [[GeneratorContext]], [[GeneratorBrand]] ».
1. Let _generator_ be OrdinaryObjectCreate(_generatorPrototype_, _internalSlotsList_).
1. Set _generator_.[[GeneratorBrand]] to _generatorBrand_.
1. Set _generator_.[[GeneratorState]] to *undefined*.
1. Let _callerContext_ be the running execution context.
1. Let _calleeContext_ be a new execution context.
1. Set the Function of _calleeContext_ to *null*.
1. Set the Realm of _calleeContext_ to the current Realm Record.
1. Set the ScriptOrModule of _calleeContext_ to _callerContext_'s ScriptOrModule.
1. If _callerContext_ is not already suspended, suspend _callerContext_.
1. Push _calleeContext_ onto the execution context stack; _calleeContext_ is now the running execution context.
1. Perform GeneratorStart(_generator_, _closure_).
1. Remove _calleeContext_ from the execution context stack and restore _callerContext_ as the running execution context.
1. Return _generator_.
</emu-alg>
</emu-clause>
</emu-clause>