-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathbuiltin-iterator-zip.js
More file actions
210 lines (210 loc) · 7.76 KB
/
builtin-iterator-zip.js
File metadata and controls
210 lines (210 loc) · 7.76 KB
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
;(function(IteratorHelper, InternalError, TypeError, call, Symbol·iterator) {
function check(v, s) {
if (typeof v === "object" && v !== null) return
throw new TypeError(s)
}
function close(iter) {
try {
if (!iter) return
let method = iter.return
if (method) call(iter, method)
} catch (e) {
return e
}
}
function closeall(iters, count) {
let ex = undefined
for (let i = count; i-- > 0;) {
let iter = iters[i]
iters[i] = undefined
let e = close(iter)
if (!ex) ex = e
}
return ex
}
return function zip(iterables, options = undefined) {
check(iterables, "bad iterables")
if (options === undefined) options = {__proto__: null}
else check(options, "bad options")
let mode = options.mode
if (mode === undefined) mode = "shortest"
if (!(mode === "strict" || mode === "longest" || mode === "shortest"))
throw new TypeError("bad mode")
let padding = undefined
if (mode === "longest") {
padding = options.padding
if (padding !== undefined) check(padding, "bad padding")
}
let pads = []
let iters = []
let nexts = []
let count = 0
let paddingiter = undefined
let iterablesiter = iterables[Symbol·iterator]()
try {
let next = iterablesiter.next
for (;;) {
let item
try {
item = call(iterablesiter, next)
} catch (e) {
// *don't* close |iterablesiter| when .next() throws
iterablesiter = undefined
throw e
}
// order of .done and .value property
// access is observable and matters
if (item.done) break
let iter = item.value
check(iter, "bad iterator")
let method = iter[Symbol·iterator]
if (method) iter = call(iter, method) // iterable -> iterator
iters[count] = iter
nexts[count] = iter.next
count++
}
iterablesiter = undefined
if (padding) {
paddingiter = padding[Symbol·iterator]()
let next = paddingiter.next
let i = 0
let done = false
for (; i < count; i++) {
let value
try {
let item = call(paddingiter, next)
// order of .done and .value property
// access is observable and matters
done = item.done
value = item.value
} catch (e) {
// *don't* close |paddingiter| when .next()
// or .done or .value property access throws
paddingiter = undefined
throw e
}
if (done) break
pads[i] = value
}
// have to be careful to not double-close on exception
// exceptions from .return() should still bubble up though
let t = paddingiter
paddingiter = undefined
if (!done) {
let ex = close(t)
if (ex) throw ex
}
for (; i < count; i++) pads[i] = undefined
}
} catch (e) {
closeall(iters, count)
close(iterablesiter)
close(paddingiter)
throw e
}
// note: uses plain numbers for |state|, using
// constants grows the bytecode by about 200 bytes
let state = 0 // new, suspend, execute, complete
let alive = count
return {
__proto__: IteratorHelper,
// TODO(bnoordhuis) shows up as "at next (<null>:0:1)" in stack
// traces when iterator throws, should be "at next (native)"
next() {
switch (state) {
case 0: // new
case 1: // suspend
state = 2 // execute
break
case 2: // execute
throw new TypeError("running zipper")
case 3: // complete
return {value:undefined, done:true}
default:
throw new InternalError("bug")
}
let dones = 0
let values = 0
let results = []
for (let i = 0; i < count; i++) {
let iter = iters[i]
if (!iter) {
if (mode !== "longest") throw new InternalError("bug")
results[i] = pads[i]
continue
}
let result
try {
result = call(iter, nexts[i])
} catch (e) {
alive = 0
iters[i] = undefined
closeall(iters, count)
throw e
}
// order of .done and .value property
// access is observable and matters
if (!result.done) {
if (mode === "strict" && dones > 0) {
closeall(iters, count)
throw new TypeError("mismatched inputs")
}
results[i] = result.value
values++
continue
}
alive--
dones++
iters[i] = undefined
switch (mode) {
case "shortest":
let ex = closeall(iters, count)
if (ex) throw ex
state = 3 // complete
return {value:undefined, done:true}
case "longest":
if (alive < 1) {
state = 3 // complete
return {value:undefined, done:true}
}
results[i] = pads[i]
break
case "strict":
if (values > 0) {
closeall(iters, count)
throw new TypeError("mismatched inputs")
}
break
}
}
if (values === 0) {
state = 3 // complete
return {value:undefined, done:true}
}
state = 1 // suspend
return {value:results, done:false}
},
return() {
switch (state) {
case 0: // new
state = 3 // complete
break
case 1: // suspend
state = 2 // execute
break
case 2: // execute
throw new TypeError("running zipper")
case 3: // complete
return {value:undefined, done:true}
default:
throw new InternalError(`bug: state=${state}`)
}
// TODO skip when already closed because of earlier exception
let ex = closeall(iters, count)
if (ex) throw ex
state = 3 // complete
return {value:undefined, done:true}
},
}
}
})