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