1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var CreateIteratorFromClosure = require('./CreateIteratorFromClosure');
|
---|
6 | var IteratorCloseAll = require('./IteratorCloseAll');
|
---|
7 | var IteratorStep = require('es-abstract/2024/IteratorStep');
|
---|
8 | var IteratorStepValue = require('es-abstract/2024/IteratorStepValue');
|
---|
9 | var NormalCompletion = require('es-abstract/2024/NormalCompletion');
|
---|
10 | var ThrowCompletion = require('es-abstract/2024/ThrowCompletion');
|
---|
11 |
|
---|
12 | var isAbstractClosure = require('es-abstract/helpers/isAbstractClosure');
|
---|
13 | var IsArray = require('es-abstract/helpers/IsArray');
|
---|
14 | var isIteratorRecord = require('es-abstract/helpers/records/iterator-record');
|
---|
15 | var some = require('es-abstract/helpers/some');
|
---|
16 |
|
---|
17 | var callBound = require('call-bind/callBound');
|
---|
18 |
|
---|
19 | var $indexOf = callBound('Array.prototype.indexOf');
|
---|
20 | var $slice = callBound('Array.prototype.slice');
|
---|
21 | var $splice = callBound('Array.prototype.splice');
|
---|
22 |
|
---|
23 | var iterHelperProto = require('../IteratorHelperPrototype');
|
---|
24 |
|
---|
25 | var SLOT = require('internal-slot');
|
---|
26 |
|
---|
27 | // https://tc39.es/proposal-joint-iteration/#sec-IteratorZip
|
---|
28 |
|
---|
29 | module.exports = function IteratorZip(iters, mode, padding, finishResults) {
|
---|
30 | if (!IsArray(iters) || !some(iters, isIteratorRecord)) {
|
---|
31 | throw new $TypeError('`iters` must be a List of IteratorRecords');
|
---|
32 | }
|
---|
33 |
|
---|
34 | if (mode !== 'shortest' && mode !== 'longest' && mode !== 'strict') {
|
---|
35 | throw new $TypeError('`mode` must be one of "shortest", "longest", or "strict"');
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (!IsArray(padding)) {
|
---|
39 | throw new $TypeError('`padding` must be a List');
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (!isAbstractClosure(finishResults)) {
|
---|
43 | throw new $TypeError('`finishResults` must be an Abstract Closure');
|
---|
44 | }
|
---|
45 |
|
---|
46 | var iterCount = iters.length; // step 1
|
---|
47 |
|
---|
48 | var openIters = $slice(iters); // step 2
|
---|
49 |
|
---|
50 | var sentinel = {};
|
---|
51 | var closure = function () {
|
---|
52 | if (iterCount === 0) {
|
---|
53 | // 1. If iterCount = 0, return ReturnCompletion(undefined).
|
---|
54 | return sentinel; // step 1
|
---|
55 | }
|
---|
56 | // while (true) {
|
---|
57 | { // eslint-disable-line no-lone-blocks
|
---|
58 | var results = []; // step 3.b.i
|
---|
59 | if (openIters.length === 0) {
|
---|
60 | throw new $TypeError('Assertion failed: `openIters` is empty'); // step 3.b.ii
|
---|
61 | }
|
---|
62 | for (var i = 0; i < iterCount; ++i) { // step 3.b.iii
|
---|
63 | // for (var i = 0; i < iterCount; i += 1) { // step 3.b.iii
|
---|
64 | var result;
|
---|
65 |
|
---|
66 | var iter = iters[i];
|
---|
67 | if (iter === null) { // step 3.b.iii.1
|
---|
68 | if (mode !== 'longest') {
|
---|
69 | throw new $TypeError('Assertion failed: `mode` is not "longest"'); // step 3.b.iii.1.a
|
---|
70 | }
|
---|
71 | result = padding[i]; // step 3.b.iii.1.b
|
---|
72 | } else { // step 2
|
---|
73 | try {
|
---|
74 | result = IteratorStepValue(iter); // step 3.b.iii.2.a, 3.b.iii.2.c
|
---|
75 | } catch (e) { // step 3.b.iii.2.b
|
---|
76 | $splice(openIters, $indexOf(openIters, iter), 1); // step 3.b.iii.2.b.i
|
---|
77 | return IteratorCloseAll(openIters, ThrowCompletion(e)); // step 3.b.iii.2.b.ii
|
---|
78 | }
|
---|
79 | if (iter['[[Done]]']) { // step 3.b.iii.2.d
|
---|
80 | $splice(openIters, $indexOf(openIters, iter), 1); // step 3.b.iii.2.d.i
|
---|
81 | if (mode === 'shortest') { // step 3.b.iii.2.d.ii
|
---|
82 | IteratorCloseAll(openIters, NormalCompletion(undefined)); // step 3.b.iii.2.d.ii.i
|
---|
83 | return sentinel;
|
---|
84 | } else if (mode === 'strict') { // step 3.b.iii.2.d.iii
|
---|
85 | if (i !== 0) { // step 3.b.iii.2.d.iii.i
|
---|
86 | return IteratorCloseAll(
|
---|
87 | openIters,
|
---|
88 | ThrowCompletion(new $TypeError('Assertion failed: `i` is not 0'))
|
---|
89 | ); // step 3.b.iii.2.d.iii.i.i
|
---|
90 | }
|
---|
91 | for (var k = 1; k < iterCount; k += 1) { // step 3.b.iii.2.d.iii.ii
|
---|
92 | if (iters[k] === null) {
|
---|
93 | throw new $TypeError('Assertion failed: `iters[k]` is `null`'); // step 3.b.iii.2.d.iii.ii.i
|
---|
94 | }
|
---|
95 | try {
|
---|
96 | result = IteratorStep(iters[k]); // step 3.b.iii.2.d.iii.ii.ii, 3.b.iii.2.d.iii.ii.iii.ii.iv
|
---|
97 | } catch (e) { // step 3.b.iii.2.d.iii.ii.iii
|
---|
98 | return IteratorCloseAll(openIters, ThrowCompletion(e)); // step 3.b.iii.2.d.iii.ii.iii.ii
|
---|
99 | }
|
---|
100 |
|
---|
101 | // if (open === false) { // step 3.b.iii.2.d.iii.ii.v
|
---|
102 | if (iters[k]['[[Done]]']) { // step 3.b.iii.2.d.iii.ii.v
|
---|
103 | $splice(openIters, $indexOf(openIters, iters[k]), 1); // step 3.b.iii.2.d.iii.ii.v.i
|
---|
104 | } else { // step 3.b.iii.2.d.iii.ii.vi
|
---|
105 | return IteratorCloseAll(
|
---|
106 | openIters,
|
---|
107 | ThrowCompletion(new $TypeError('Assertion failed: `open` is not `false`'))
|
---|
108 | ); // step 3.b.iii.2.d.iii.ii.vi.i
|
---|
109 | }
|
---|
110 | }
|
---|
111 | } else { // step 3.b.iii.2.d.iv
|
---|
112 | if (mode !== 'longest') {
|
---|
113 | throw new $TypeError('Assertion failed: `mode` is not "longest"'); // step 3.b.iii.2.d.iv.i
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (openIters.length === 0) {
|
---|
117 | return sentinel; // ReturnCompletion(undefined); // step 3.b.iii.2.d.iv.ii
|
---|
118 | }
|
---|
119 |
|
---|
120 | // eslint-disable-next-line no-param-reassign
|
---|
121 | iters[i] = null; // step 3.b.iii.2.d.iv.iii
|
---|
122 | // i += 1;
|
---|
123 |
|
---|
124 | result = padding[i]; // step 3.b.iii.2.d.iv.iv
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | results[results.length] = result; // step 3.b.iii.3
|
---|
130 |
|
---|
131 | // 5. Let completion be Completion(Yield(results)).
|
---|
132 | // 6. If completion is an abrupt completion, then
|
---|
133 | // 1. Return ? IteratorCloseAll(openIters, completion).
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | return finishResults(results); // step 3.b.iv
|
---|
138 | };
|
---|
139 | SLOT.set(closure, '[[Sentinel]]', sentinel); // for the userland implementation
|
---|
140 | SLOT.set(closure, '[[CloseIfAbrupt]]', finishResults); // for the userland implementation
|
---|
141 |
|
---|
142 | var gen = CreateIteratorFromClosure(closure, 'Iterator Helper', iterHelperProto, ['[[UnderlyingIterators]]']); // step 4
|
---|
143 |
|
---|
144 | SLOT.set(gen, '[[UnderlyingIterators]]', openIters); // step 5
|
---|
145 |
|
---|
146 | return gen; // step 6
|
---|
147 | };
|
---|