1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var AdvanceStringIndex = require('es-abstract/2024/AdvanceStringIndex');
|
---|
6 | var Call = require('es-abstract/2024/Call');
|
---|
7 | var CompletionRecord = require('es-abstract/2024/CompletionRecord');
|
---|
8 | var CreateIteratorFromClosure = require('../aos/CreateIteratorFromClosure');
|
---|
9 | var GetIteratorDirect = require('../aos/GetIteratorDirect');
|
---|
10 | var GetMethod = require('es-abstract/2024/GetMethod');
|
---|
11 | var IsArray = require('es-abstract/2024/IsArray');
|
---|
12 | var IteratorCloseAll = require('../aos/IteratorCloseAll');
|
---|
13 | var IteratorStepValue = require('es-abstract/2024/IteratorStepValue');
|
---|
14 | var ThrowCompletion = require('es-abstract/2024/ThrowCompletion');
|
---|
15 | var Type = require('es-abstract/2024/Type');
|
---|
16 |
|
---|
17 | var forEach = require('es-abstract/helpers/forEach');
|
---|
18 | var getIteratorMethod = require('es-abstract/helpers/getIteratorMethod');
|
---|
19 |
|
---|
20 | var callBound = require('call-bind/callBound');
|
---|
21 | var $indexOf = callBound('Array.prototype.indexOf');
|
---|
22 | var $splice = callBound('Array.prototype.splice');
|
---|
23 |
|
---|
24 | var iterHelperProto = require('../IteratorHelperPrototype');
|
---|
25 |
|
---|
26 | var SLOT = require('internal-slot');
|
---|
27 |
|
---|
28 | module.exports = function concat() {
|
---|
29 | if (this instanceof concat) {
|
---|
30 | throw new $TypeError('`Iterator.concat` is not a constructor');
|
---|
31 | }
|
---|
32 |
|
---|
33 | var iterables = []; // step 1
|
---|
34 |
|
---|
35 | forEach(arguments, function (item) { // step 2
|
---|
36 | if (Type(item) !== 'Object') {
|
---|
37 | throw new $TypeError('`Iterator.concat` requires all arguments to be objects'); // step 2.1
|
---|
38 | }
|
---|
39 | // var method = GetMethod(item, Symbol.iterator); // step 2.2
|
---|
40 | var method = getIteratorMethod(
|
---|
41 | {
|
---|
42 | AdvanceStringIndex: AdvanceStringIndex,
|
---|
43 | GetMethod: GetMethod,
|
---|
44 | IsArray: IsArray
|
---|
45 | },
|
---|
46 | item
|
---|
47 | );
|
---|
48 | if (typeof method === 'undefined') {
|
---|
49 | throw new $TypeError('`Iterator.concat` requires all arguments to be iterable'); // step 2.3
|
---|
50 | }
|
---|
51 | iterables[iterables.length] = { '[[OpenMethod]]': method, '[[Iterable]]': item }; // step 2.4
|
---|
52 | });
|
---|
53 |
|
---|
54 | var openIters = []; // step 3
|
---|
55 | var sentinel = {};
|
---|
56 | var closeIfAbrupt = function (abruptCompletion) {
|
---|
57 | if (!(abruptCompletion instanceof CompletionRecord)) {
|
---|
58 | throw new $TypeError('`abruptCompletion` must be a Completion Record');
|
---|
59 | }
|
---|
60 | if (openIters.length > 0) {
|
---|
61 | IteratorCloseAll(
|
---|
62 | openIters,
|
---|
63 | abruptCompletion
|
---|
64 | );
|
---|
65 | }
|
---|
66 | };
|
---|
67 |
|
---|
68 | var index = 0;
|
---|
69 | var closure = function () { // step 4
|
---|
70 | if (index < iterables.length) {
|
---|
71 | // forEach(iterables, function (iterable) { // step 4.a
|
---|
72 | var iteratorRecord;
|
---|
73 | if (openIters.length === 0) {
|
---|
74 | var iterable = iterables[index];
|
---|
75 | var iter = Call(iterable['[[OpenMethod]]'], iterable['[[Iterable]]']); // step 4.a.i
|
---|
76 | if (Type(iter) !== 'Object') {
|
---|
77 | closeIfAbrupt(ThrowCompletion(new $TypeError('???'))); // step 4.a.ii
|
---|
78 | }
|
---|
79 | iteratorRecord = GetIteratorDirect(iter); // step 4.a.iii
|
---|
80 | iteratorRecord.obj = iterable['[[Iterable]]'];
|
---|
81 | openIters[openIters.length] = iteratorRecord; // step 4.a.iv
|
---|
82 | } else {
|
---|
83 | iteratorRecord = openIters[0];
|
---|
84 | }
|
---|
85 |
|
---|
86 | // var innerAlive = true; // step 4.a.v
|
---|
87 | // while (innerAlive) { // step 4.a.vi
|
---|
88 | { // eslint-disable-line no-lone-blocks
|
---|
89 | // step 4.a.vi.3.a
|
---|
90 | var innerValue;
|
---|
91 | try {
|
---|
92 | innerValue = IteratorStepValue(iteratorRecord); // step 5.b.ix.4.a
|
---|
93 | } catch (e) {
|
---|
94 | // innerAlive = false;
|
---|
95 | $splice(openIters, $indexOf(openIters, iteratorRecord), 1); // step 4.a.vi.2.a
|
---|
96 | index += 1;
|
---|
97 | closeIfAbrupt(ThrowCompletion(e)); // step 4.a.vi.3.b
|
---|
98 | }
|
---|
99 | if (iteratorRecord['[[Done]]']) {
|
---|
100 | // innerAlive = false;
|
---|
101 | $splice(openIters, $indexOf(openIters, iteratorRecord), 1);
|
---|
102 | index += 1;
|
---|
103 | return closure();
|
---|
104 | }
|
---|
105 | return innerValue; // // step 4.a.vi.3.a
|
---|
106 | }
|
---|
107 | // });
|
---|
108 | }
|
---|
109 |
|
---|
110 | // return ReturnCompletion(undefined); // step 4.b
|
---|
111 | return sentinel;
|
---|
112 | };
|
---|
113 | SLOT.set(closure, '[[Sentinel]]', sentinel); // for the userland implementation
|
---|
114 | SLOT.set(closure, '[[CloseIfAbrupt]]', closeIfAbrupt); // for the userland implementation
|
---|
115 |
|
---|
116 | var gen = CreateIteratorFromClosure(closure, 'Iterator Helper', iterHelperProto, ['[[UnderlyingIterators]]']); // step 5
|
---|
117 | SLOT.set(gen, '[[UnderlyingIterators]]', []); // step 6
|
---|
118 | return gen; // step 7
|
---|
119 | };
|
---|