@opencode/codemode 2.2.5 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -1
- package/dist/codemode.d.ts +55 -4
- package/dist/codemode.js +63 -8
- package/dist/extension.d.ts +14 -9
- package/dist/extension.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interpreter/execute.d.ts +1 -1
- package/dist/interpreter/execute.js +16 -4
- package/dist/interpreter/extensions.d.ts +3 -3
- package/dist/interpreter/extensions.js +17 -11
- package/dist/interpreter/globals.js +10 -0
- package/dist/interpreter/interpreter.d.ts +11 -1
- package/dist/interpreter/interpreter.js +86 -2
- package/dist/interpreter/modules.d.ts +4 -0
- package/dist/interpreter/modules.js +33 -0
- package/dist/suspend-error.d.ts +23 -0
- package/dist/suspend-error.js +25 -0
- package/dist/tool-runtime.d.ts +4 -1
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +6 -0
- package/dist/vm/index.js +6 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +58 -0
- package/dist/vm/runtime/driver.js +184 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +80 -0
- package/dist/vm/runtime/execute.js +414 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +50 -0
- package/dist/vm/runtime/interpreter.js +94 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +87 -0
- package/dist/vm/snapshot/codec.js +751 -0
- package/dist/vm/snapshot/snapshot.d.ts +49 -0
- package/dist/vm/snapshot/snapshot.js +70 -0
- package/dist/vm/snapshot.d.ts +32 -0
- package/dist/vm/snapshot.js +306 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +225 -0
- package/dist/vm/vm.js +2931 -0
- package/package.json +6 -3
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
// The prelude's source (see prelude.ts): Code Mode JavaScript, parsed once per process. Only top-level function
|
|
2
|
+
// declarations; `$` names are the privileged helpers (helpers.ts). Keep `${` and backticks out of it.
|
|
3
|
+
export const code = String.raw `
|
|
4
|
+
function arrayFromIterable(items, mapper, thisArg) {
|
|
5
|
+
const result = []
|
|
6
|
+
let index = 0
|
|
7
|
+
for (const item of items) {
|
|
8
|
+
result[index] = mapper === undefined ? item : $Call(mapper, thisArg, item, index)
|
|
9
|
+
index++
|
|
10
|
+
}
|
|
11
|
+
return result
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Array.from over an array-like with a mapper: every index is read, holes included.
|
|
15
|
+
function arrayFromLike(source, length, mapper, thisArg) {
|
|
16
|
+
const result = new Array(length)
|
|
17
|
+
let index = 0
|
|
18
|
+
while (index < length) {
|
|
19
|
+
result[index] = $Call(mapper, thisArg, source[index], index)
|
|
20
|
+
index++
|
|
21
|
+
}
|
|
22
|
+
return result
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The callback methods Array and Uint8Array share. They fix the iteration length when they start and read elements
|
|
26
|
+
// live; they skip holes, but the find methods visit them as undefined. map and filter return the receiver's kind of
|
|
27
|
+
// collection.
|
|
28
|
+
function Array$prototype$map(callback, thisArg = undefined) {
|
|
29
|
+
return mapItems($Receiver(this, "Array", "Array.prototype.map"), callback, thisArg, "Array.map")
|
|
30
|
+
}
|
|
31
|
+
function Uint8Array$prototype$map(callback, thisArg = undefined) {
|
|
32
|
+
const bytes = $Receiver(this, "Uint8Array", "Uint8Array.prototype.map")
|
|
33
|
+
return $Bytes(mapItems(bytes, callback, thisArg, "Uint8Array.map"), Uint8Array.prototype, 0)
|
|
34
|
+
}
|
|
35
|
+
function mapItems(array, callback, thisArg, label) {
|
|
36
|
+
$Callback(callback, label)
|
|
37
|
+
const length = array.length
|
|
38
|
+
const result = new Array(length)
|
|
39
|
+
let index = 0
|
|
40
|
+
while (index < length) {
|
|
41
|
+
if (index in array) result[index] = $Call(callback, thisArg, array[index], index, array)
|
|
42
|
+
index++
|
|
43
|
+
}
|
|
44
|
+
return result
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function Array$prototype$filter(callback, thisArg = undefined) {
|
|
48
|
+
return filterItems($Receiver(this, "Array", "Array.prototype.filter"), callback, thisArg, "Array.filter")
|
|
49
|
+
}
|
|
50
|
+
function Uint8Array$prototype$filter(callback, thisArg = undefined) {
|
|
51
|
+
const bytes = $Receiver(this, "Uint8Array", "Uint8Array.prototype.filter")
|
|
52
|
+
return $Bytes(filterItems(bytes, callback, thisArg, "Uint8Array.filter"), Uint8Array.prototype, 0)
|
|
53
|
+
}
|
|
54
|
+
function filterItems(array, callback, thisArg, label) {
|
|
55
|
+
$Callback(callback, label)
|
|
56
|
+
const length = array.length
|
|
57
|
+
const result = []
|
|
58
|
+
let count = 0
|
|
59
|
+
let index = 0
|
|
60
|
+
let item
|
|
61
|
+
while (index < length) {
|
|
62
|
+
if (index in array) {
|
|
63
|
+
item = array[index]
|
|
64
|
+
if ($Call(callback, thisArg, item, index, array)) result[count++] = item
|
|
65
|
+
}
|
|
66
|
+
index++
|
|
67
|
+
}
|
|
68
|
+
return result
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function Array$prototype$forEach(callback, thisArg = undefined) {
|
|
72
|
+
forEachItem($Receiver(this, "Array", "Array.prototype.forEach"), callback, thisArg, "Array.forEach")
|
|
73
|
+
}
|
|
74
|
+
function Uint8Array$prototype$forEach(callback, thisArg = undefined) {
|
|
75
|
+
forEachItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.forEach"), callback, thisArg, "Uint8Array.forEach")
|
|
76
|
+
}
|
|
77
|
+
function forEachItem(array, callback, thisArg, label) {
|
|
78
|
+
$Callback(callback, label)
|
|
79
|
+
const length = array.length
|
|
80
|
+
let index = 0
|
|
81
|
+
while (index < length) {
|
|
82
|
+
if (index in array) $Call(callback, thisArg, array[index], index, array)
|
|
83
|
+
index++
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function Array$prototype$some(callback, thisArg = undefined) {
|
|
88
|
+
return testItems($Receiver(this, "Array", "Array.prototype.some"), callback, thisArg, "Array.some", false)
|
|
89
|
+
}
|
|
90
|
+
function Uint8Array$prototype$some(callback, thisArg = undefined) {
|
|
91
|
+
return testItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.some"), callback, thisArg, "Uint8Array.some", false)
|
|
92
|
+
}
|
|
93
|
+
function Array$prototype$every(callback, thisArg = undefined) {
|
|
94
|
+
return testItems($Receiver(this, "Array", "Array.prototype.every"), callback, thisArg, "Array.every", true)
|
|
95
|
+
}
|
|
96
|
+
function Uint8Array$prototype$every(callback, thisArg = undefined) {
|
|
97
|
+
return testItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.every"), callback, thisArg, "Uint8Array.every", true)
|
|
98
|
+
}
|
|
99
|
+
// some stops at the first truthy result, every at the first falsy one.
|
|
100
|
+
function testItems(array, callback, thisArg, label, every) {
|
|
101
|
+
$Callback(callback, label)
|
|
102
|
+
const length = array.length
|
|
103
|
+
let index = 0
|
|
104
|
+
while (index < length) {
|
|
105
|
+
if (index in array && !$Call(callback, thisArg, array[index], index, array) === every) return !every
|
|
106
|
+
index++
|
|
107
|
+
}
|
|
108
|
+
return every
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function Array$prototype$find(callback, thisArg = undefined) {
|
|
112
|
+
return findItem($Receiver(this, "Array", "Array.prototype.find"), callback, thisArg, "Array.find", false, false)
|
|
113
|
+
}
|
|
114
|
+
function Uint8Array$prototype$find(callback, thisArg = undefined) {
|
|
115
|
+
return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.find"), callback, thisArg, "Uint8Array.find", false, false)
|
|
116
|
+
}
|
|
117
|
+
function Array$prototype$findIndex(callback, thisArg = undefined) {
|
|
118
|
+
return findItem($Receiver(this, "Array", "Array.prototype.findIndex"), callback, thisArg, "Array.findIndex", false, true)
|
|
119
|
+
}
|
|
120
|
+
function Uint8Array$prototype$findIndex(callback, thisArg = undefined) {
|
|
121
|
+
return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.findIndex"), callback, thisArg, "Uint8Array.findIndex", false, true)
|
|
122
|
+
}
|
|
123
|
+
function Array$prototype$findLast(callback, thisArg = undefined) {
|
|
124
|
+
return findItem($Receiver(this, "Array", "Array.prototype.findLast"), callback, thisArg, "Array.findLast", true, false)
|
|
125
|
+
}
|
|
126
|
+
function Uint8Array$prototype$findLast(callback, thisArg = undefined) {
|
|
127
|
+
return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.findLast"), callback, thisArg, "Uint8Array.findLast", true, false)
|
|
128
|
+
}
|
|
129
|
+
function Array$prototype$findLastIndex(callback, thisArg = undefined) {
|
|
130
|
+
return findItem($Receiver(this, "Array", "Array.prototype.findLastIndex"), callback, thisArg, "Array.findLastIndex", true, true)
|
|
131
|
+
}
|
|
132
|
+
function Uint8Array$prototype$findLastIndex(callback, thisArg = undefined) {
|
|
133
|
+
return findItem($Receiver(this, "Uint8Array", "Uint8Array.prototype.findLastIndex"), callback, thisArg, "Uint8Array.findLastIndex", true, true)
|
|
134
|
+
}
|
|
135
|
+
function findItem(array, callback, thisArg, label, last, wantIndex) {
|
|
136
|
+
$Callback(callback, label)
|
|
137
|
+
const length = array.length
|
|
138
|
+
const step = last ? -1 : 1
|
|
139
|
+
let index = last ? length - 1 : 0
|
|
140
|
+
let item
|
|
141
|
+
while (index >= 0 && index < length) {
|
|
142
|
+
item = array[index]
|
|
143
|
+
if ($Call(callback, thisArg, item, index, array)) return wantIndex ? index : item
|
|
144
|
+
index += step
|
|
145
|
+
}
|
|
146
|
+
return wantIndex ? -1 : undefined
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// reduce and reduceRight take an initial value where the others take a thisArg.
|
|
150
|
+
function Array$prototype$reduce(callback, ...initial) {
|
|
151
|
+
return reduceItems($Receiver(this, "Array", "Array.prototype.reduce"), callback, initial, "Array.reduce", false)
|
|
152
|
+
}
|
|
153
|
+
function Uint8Array$prototype$reduce(callback, ...initial) {
|
|
154
|
+
return reduceItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.reduce"), callback, initial, "Uint8Array.reduce", false)
|
|
155
|
+
}
|
|
156
|
+
function Array$prototype$reduceRight(callback, ...initial) {
|
|
157
|
+
return reduceItems($Receiver(this, "Array", "Array.prototype.reduceRight"), callback, initial, "Array.reduceRight", true)
|
|
158
|
+
}
|
|
159
|
+
function Uint8Array$prototype$reduceRight(callback, ...initial) {
|
|
160
|
+
return reduceItems($Receiver(this, "Uint8Array", "Uint8Array.prototype.reduceRight"), callback, initial, "Uint8Array.reduceRight", true)
|
|
161
|
+
}
|
|
162
|
+
function reduceItems(array, callback, initial, label, right) {
|
|
163
|
+
$Callback(callback, label)
|
|
164
|
+
const length = array.length
|
|
165
|
+
const step = right ? -1 : 1
|
|
166
|
+
let index = right ? length - 1 : 0
|
|
167
|
+
let accumulator
|
|
168
|
+
if (initial.length > 0) accumulator = initial[0]
|
|
169
|
+
else {
|
|
170
|
+
while (index >= 0 && index < length && !(index in array)) index += step
|
|
171
|
+
if (index < 0 || index >= length) $Throw("TypeError", label + " of an empty array with no initial value.")
|
|
172
|
+
accumulator = array[index]
|
|
173
|
+
index += step
|
|
174
|
+
}
|
|
175
|
+
while (index >= 0 && index < length) {
|
|
176
|
+
if (index in array) accumulator = $Call(callback, undefined, accumulator, array[index], index, array)
|
|
177
|
+
index += step
|
|
178
|
+
}
|
|
179
|
+
return accumulator
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// forEach semantics: a returned array's holes are skipped, as flattening does.
|
|
183
|
+
function Array$prototype$flatMap(callback, thisArg = undefined) {
|
|
184
|
+
const array = $Receiver(this, "Array", "Array.prototype.flatMap")
|
|
185
|
+
$Callback(callback, "Array.flatMap")
|
|
186
|
+
const length = array.length
|
|
187
|
+
const result = []
|
|
188
|
+
let count = 0
|
|
189
|
+
let index = 0
|
|
190
|
+
let inner
|
|
191
|
+
let at
|
|
192
|
+
while (index < length) {
|
|
193
|
+
if (index in array) {
|
|
194
|
+
inner = $Call(callback, thisArg, array[index], index, array)
|
|
195
|
+
if ($IsArray(inner)) {
|
|
196
|
+
at = 0
|
|
197
|
+
while (at < inner.length) {
|
|
198
|
+
if (at in inner) result[count++] = inner[at]
|
|
199
|
+
at++
|
|
200
|
+
}
|
|
201
|
+
} else result[count++] = inner
|
|
202
|
+
}
|
|
203
|
+
index++
|
|
204
|
+
}
|
|
205
|
+
return result
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A sort with a comparator (see sortArray): a stable top-down merge sort that compares in the same order as the
|
|
209
|
+
// recursive one, running the merges (low, middle, high) the native planned in post-order over one working copy.
|
|
210
|
+
// Holes and undefined values sort last without being compared.
|
|
211
|
+
function sortWith(target, into, holes, comparator, work, undefs, merges) {
|
|
212
|
+
const merged = []
|
|
213
|
+
let merge = 0
|
|
214
|
+
let middle
|
|
215
|
+
let high
|
|
216
|
+
let left
|
|
217
|
+
let right
|
|
218
|
+
let count
|
|
219
|
+
let order
|
|
220
|
+
while (merge < merges.length) {
|
|
221
|
+
left = merges[merge]
|
|
222
|
+
middle = merges[merge + 1]
|
|
223
|
+
high = merges[merge + 2]
|
|
224
|
+
right = middle
|
|
225
|
+
count = 0
|
|
226
|
+
while (left < middle && right < high) {
|
|
227
|
+
order = +$Call(comparator, undefined, work[left], work[right])
|
|
228
|
+
// A NaN result counts as equal, which keeps the order stable.
|
|
229
|
+
if (order <= 0 || order !== order) {
|
|
230
|
+
merged[count] = work[left]
|
|
231
|
+
left++
|
|
232
|
+
} else {
|
|
233
|
+
merged[count] = work[right]
|
|
234
|
+
right++
|
|
235
|
+
}
|
|
236
|
+
count++
|
|
237
|
+
}
|
|
238
|
+
$MergeRest(work, merged, count, left, middle, right, high, merges[merge])
|
|
239
|
+
merge += 3
|
|
240
|
+
}
|
|
241
|
+
return $Sorted(target, into, holes, work, undefs)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// The default sort over more values than one native sort may take at once: native steps of bounded size (see
|
|
245
|
+
// $SortStep), so the run can pause between them like any loop.
|
|
246
|
+
function sortDefault(target, into, holes, values, undefs) {
|
|
247
|
+
const state = $SortState(values)
|
|
248
|
+
while ($SortStep(state)) {}
|
|
249
|
+
return $Sorted(target, into, holes, $SortResult(state, values), undefs)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function Array$prototype$toLocaleString() {
|
|
253
|
+
const array = $Receiver(this, "Array", "Array.prototype.toLocaleString")
|
|
254
|
+
let result = ""
|
|
255
|
+
let index = 0
|
|
256
|
+
let item
|
|
257
|
+
while (index < array.length) {
|
|
258
|
+
if (index > 0) result += ","
|
|
259
|
+
item = array[index]
|
|
260
|
+
if (item !== null && item !== undefined) result += String(item.toLocaleString())
|
|
261
|
+
index++
|
|
262
|
+
}
|
|
263
|
+
return result
|
|
264
|
+
}
|
|
265
|
+
// new Map, Set, WeakMap, and WeakSet from an iterable.
|
|
266
|
+
function fillCollection(target, items, kind) {
|
|
267
|
+
const pairs = kind === "Map" || kind === "WeakMap"
|
|
268
|
+
for (const item of items) {
|
|
269
|
+
if (!pairs) $Table("add", target, item)
|
|
270
|
+
else if (!$IsObject(item)) $Throw("TypeError", "new " + kind + "(...) expects [key, value] pairs as entry objects.")
|
|
271
|
+
else $Table("add", target, item[0], item[1])
|
|
272
|
+
}
|
|
273
|
+
return target
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function Map$groupBy(items, callback) {
|
|
277
|
+
return groupBy(items, callback, "Map")
|
|
278
|
+
}
|
|
279
|
+
function Object$groupBy(items, callback) {
|
|
280
|
+
return groupBy(items, callback, "Object")
|
|
281
|
+
}
|
|
282
|
+
// Object.groupBy's groups have a null prototype, so group names never collide with inherited methods.
|
|
283
|
+
function groupBy(items, callback, kind) {
|
|
284
|
+
if (items === null || items === undefined) $Throw("TypeError", kind + ".groupBy expects an iterable collection.")
|
|
285
|
+
$Callback(callback, kind + ".groupBy")
|
|
286
|
+
const groups = $Table("groups", kind)
|
|
287
|
+
let index = 0
|
|
288
|
+
let key
|
|
289
|
+
for (const item of items) {
|
|
290
|
+
key = callback(item, index++)
|
|
291
|
+
if (kind === "Object") key = String($Table("groupKey", key))
|
|
292
|
+
$Table("group", groups, key, item)
|
|
293
|
+
}
|
|
294
|
+
return groups
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// getOrInsertComputed when the key is missing: the callback's result wins over anything it inserted itself.
|
|
298
|
+
function insertComputed(target, key, callback) {
|
|
299
|
+
const value = callback(key)
|
|
300
|
+
$Table("add", target, key, value)
|
|
301
|
+
return value
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function Map$prototype$forEach(callback, thisArg = undefined) {
|
|
305
|
+
collectionForEach($Receiver(this, "Map", "Map.prototype.forEach"), callback, thisArg, "Map.forEach")
|
|
306
|
+
}
|
|
307
|
+
function Set$prototype$forEach(callback, thisArg = undefined) {
|
|
308
|
+
collectionForEach($Receiver(this, "Set", "Set.prototype.forEach"), callback, thisArg, "Set.forEach")
|
|
309
|
+
}
|
|
310
|
+
// Visits entries live, as in JS.
|
|
311
|
+
function collectionForEach(target, callback, thisArg, label) {
|
|
312
|
+
$Callback(callback, label)
|
|
313
|
+
for (const entry of $Table("entries", target)) $Call(callback, thisArg, entry[1], entry[0], target)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function Set$prototype$union(other) {
|
|
317
|
+
return setMethod($Receiver(this, "Set", "Set.prototype.union"), other, "union")
|
|
318
|
+
}
|
|
319
|
+
function Set$prototype$intersection(other) {
|
|
320
|
+
return setMethod($Receiver(this, "Set", "Set.prototype.intersection"), other, "intersection")
|
|
321
|
+
}
|
|
322
|
+
function Set$prototype$difference(other) {
|
|
323
|
+
return setMethod($Receiver(this, "Set", "Set.prototype.difference"), other, "difference")
|
|
324
|
+
}
|
|
325
|
+
function Set$prototype$symmetricDifference(other) {
|
|
326
|
+
return setMethod($Receiver(this, "Set", "Set.prototype.symmetricDifference"), other, "symmetricDifference")
|
|
327
|
+
}
|
|
328
|
+
function Set$prototype$isSubsetOf(other) {
|
|
329
|
+
return setMethod($Receiver(this, "Set", "Set.prototype.isSubsetOf"), other, "isSubsetOf")
|
|
330
|
+
}
|
|
331
|
+
function Set$prototype$isSupersetOf(other) {
|
|
332
|
+
return setMethod($Receiver(this, "Set", "Set.prototype.isSupersetOf"), other, "isSupersetOf")
|
|
333
|
+
}
|
|
334
|
+
function Set$prototype$isDisjointFrom(other) {
|
|
335
|
+
return setMethod($Receiver(this, "Set", "Set.prototype.isDisjointFrom"), other, "isDisjointFrom")
|
|
336
|
+
}
|
|
337
|
+
// A Set method over the other set's size, has, and keys (GetSetRecord): it either asks the other set whether it has
|
|
338
|
+
// each of this set's values, or takes all of the other set's values, whichever is smaller.
|
|
339
|
+
function setMethod(target, other, name) {
|
|
340
|
+
if (!$IsObject(other)) $Throw("TypeError", "Set." + name + " expects a Set-like object.")
|
|
341
|
+
const size = +other.size
|
|
342
|
+
if (size !== size) $Throw("TypeError", "Set." + name + " received a Set-like object with an invalid size.")
|
|
343
|
+
if (size < 0) $Throw("RangeError", "Set." + name + " received a Set-like object with a negative size.")
|
|
344
|
+
const has = other.has
|
|
345
|
+
const keys = other.keys
|
|
346
|
+
if (!$IsCallable(has) || !$IsCallable(keys)) $Throw("TypeError", "Set." + name + " expects callable 'has' and 'keys' methods.")
|
|
347
|
+
const own = $Table("size", target)
|
|
348
|
+
if (name === "isSubsetOf" && own > size) return false
|
|
349
|
+
if (name === "isSupersetOf" && own < size) return false
|
|
350
|
+
const result = name === "intersection" ? new Set() : name === "union" || name === "difference" || name === "symmetricDifference" ? $Table("copy", target) : undefined
|
|
351
|
+
const asks = name === "isSubsetOf" || ((name === "intersection" || name === "difference" || name === "isDisjointFrom") && own <= size)
|
|
352
|
+
let found
|
|
353
|
+
if (asks) {
|
|
354
|
+
for (const item of $Table("values", name === "difference" ? result : target)) {
|
|
355
|
+
found = !!$Call(has, other, item)
|
|
356
|
+
if (name === "intersection") {
|
|
357
|
+
if (found) $Table("add", result, item)
|
|
358
|
+
} else if (name === "difference") {
|
|
359
|
+
if (found) $Table("delete", result, item)
|
|
360
|
+
} else if (found === (name === "isDisjointFrom")) return false
|
|
361
|
+
}
|
|
362
|
+
return result === undefined ? true : result
|
|
363
|
+
}
|
|
364
|
+
const iterator = $Call(keys, other)
|
|
365
|
+
if (!$IsObject(iterator)) $Throw("TypeError", "Set." + name + " expects 'keys' to return an iterator object.")
|
|
366
|
+
for (const item of $Direct(iterator, true)) {
|
|
367
|
+
found = $Table("has", target, item)
|
|
368
|
+
if (name === "union") $Table("add", result, item)
|
|
369
|
+
else if (name === "intersection") {
|
|
370
|
+
if (found) $Table("add", result, item)
|
|
371
|
+
} else if (name === "difference") $Table("delete", result, item)
|
|
372
|
+
else if (name === "symmetricDifference") $Table(found ? "delete" : "add", result, item)
|
|
373
|
+
else if (found === (name === "isDisjointFrom")) return false
|
|
374
|
+
}
|
|
375
|
+
return result === undefined ? true : result
|
|
376
|
+
}
|
|
377
|
+
function Object$fromEntries(entries) {
|
|
378
|
+
const result = {}
|
|
379
|
+
for (const entry of entries) {
|
|
380
|
+
if (!$IsObject(entry, true)) $Throw("TypeError", "Object.fromEntries expects [key, value] entry objects.")
|
|
381
|
+
$Define(result, String(entry[0]), entry[1], true)
|
|
382
|
+
}
|
|
383
|
+
return result
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function Math$sumPrecise(items) {
|
|
387
|
+
const numbers = []
|
|
388
|
+
for (const item of items) {
|
|
389
|
+
if (typeof item !== "number") $Throw("TypeError", "Math.sumPrecise expects an iterable of numbers.")
|
|
390
|
+
numbers[numbers.length] = item
|
|
391
|
+
}
|
|
392
|
+
return $SumPrecise(numbers)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// new AggregateError: the error is made (message and cause) before its errors are read.
|
|
396
|
+
function aggregateError(error, errors) {
|
|
397
|
+
const list = []
|
|
398
|
+
for (const item of errors) list[list.length] = item
|
|
399
|
+
return $Define(error, "errors", list, false)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// A Uint8Array's bytes from an iterable: a new one with prototype into, or set into the Uint8Array into at
|
|
403
|
+
// offset. As in TypedArray.from, a mapper sees the original values, after the source is consumed in full.
|
|
404
|
+
function collectBytes(source, name, mapper, thisArg, into, offset) {
|
|
405
|
+
if (!$IsIterable(source)) $Throw("TypeError", name + " expects a Uint8Array, an array, or an iterable of numbers, received " + $Describe(source) + ".")
|
|
406
|
+
const values = []
|
|
407
|
+
for (const item of source) values[values.length] = item
|
|
408
|
+
let index = 0
|
|
409
|
+
if (mapper !== undefined) {
|
|
410
|
+
while (index < values.length) {
|
|
411
|
+
values[index] = $Call(mapper, thisArg, values[index], index)
|
|
412
|
+
index++
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return $Bytes(values, into, offset)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// Headers and URLSearchParams from iterable [name, value] pairs, read in full as strings before any is checked; from a
|
|
419
|
+
// record of names to values when init is not iterable.
|
|
420
|
+
function readPairs(init, label, kind, proto) {
|
|
421
|
+
if (!$IsIterable(init)) return $Pairs(kind, proto, init, label)
|
|
422
|
+
const pairs = []
|
|
423
|
+
let strings
|
|
424
|
+
for (const pair of init) {
|
|
425
|
+
if (!$IsIterable(pair)) $Throw("TypeError", label + " expects iterable [name, value] pairs.")
|
|
426
|
+
strings = []
|
|
427
|
+
for (const item of pair) strings[strings.length] = String(item)
|
|
428
|
+
pairs[pairs.length] = strings
|
|
429
|
+
}
|
|
430
|
+
return $Pairs(kind, proto, pairs, label)
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Headers and URLSearchParams forEach: the callback sees each pair as listed when it started.
|
|
434
|
+
function pairsForEach(target, pairs, callback, thisArg) {
|
|
435
|
+
let index = 0
|
|
436
|
+
while (index < pairs.length) {
|
|
437
|
+
$Call(callback, thisArg, pairs[index][1], pairs[index][0], target)
|
|
438
|
+
index++
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
// Iterator helpers are prelude generators, made with %IteratorHelper% as their prototype. Each is primed to its first
|
|
442
|
+
// yield, so a return() before the first next() still closes the source, from its finally.
|
|
443
|
+
function Iterator$from(value) {
|
|
444
|
+
if ($IsObject(value) && $Inherits(value, Iterator.prototype)) return value
|
|
445
|
+
return prime(wrapHelper(value, flattenable(value, true, "Iterator.from")))
|
|
446
|
+
}
|
|
447
|
+
function* wrapHelper(source, iterator) {
|
|
448
|
+
let started = false
|
|
449
|
+
try {
|
|
450
|
+
yield
|
|
451
|
+
started = true
|
|
452
|
+
for (const item of iterator) yield item
|
|
453
|
+
} finally {
|
|
454
|
+
if (!started) closeIterator(source)
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function Iterator$prototype$map(mapper) {
|
|
459
|
+
const iterator = $Direct(this, false)
|
|
460
|
+
$Callback(mapper, "Iterator.prototype.map")
|
|
461
|
+
return prime(mapHelper(this, iterator, mapper))
|
|
462
|
+
}
|
|
463
|
+
function* mapHelper(source, iterator, mapper) {
|
|
464
|
+
let started = false
|
|
465
|
+
try {
|
|
466
|
+
yield
|
|
467
|
+
started = true
|
|
468
|
+
let counter = 0
|
|
469
|
+
for (const item of iterator) yield mapper(item, counter++)
|
|
470
|
+
} finally {
|
|
471
|
+
if (!started) closeIterator(source)
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function Iterator$prototype$filter(predicate) {
|
|
476
|
+
const iterator = $Direct(this, false)
|
|
477
|
+
$Callback(predicate, "Iterator.prototype.filter")
|
|
478
|
+
return prime(filterHelper(this, iterator, predicate))
|
|
479
|
+
}
|
|
480
|
+
function* filterHelper(source, iterator, predicate) {
|
|
481
|
+
let started = false
|
|
482
|
+
try {
|
|
483
|
+
yield
|
|
484
|
+
started = true
|
|
485
|
+
let counter = 0
|
|
486
|
+
for (const item of iterator) if (predicate(item, counter++)) yield item
|
|
487
|
+
} finally {
|
|
488
|
+
if (!started) closeIterator(source)
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function Iterator$prototype$take(limit) {
|
|
493
|
+
const iterator = $Direct(this, false)
|
|
494
|
+
return prime(takeHelper(this, iterator, count(limit, "take")))
|
|
495
|
+
}
|
|
496
|
+
function* takeHelper(source, iterator, remaining) {
|
|
497
|
+
let started = false
|
|
498
|
+
try {
|
|
499
|
+
yield
|
|
500
|
+
started = true
|
|
501
|
+
if (remaining === 0) return closeIterator(source)
|
|
502
|
+
for (const item of iterator) {
|
|
503
|
+
remaining--
|
|
504
|
+
yield item
|
|
505
|
+
if (remaining === 0) break
|
|
506
|
+
}
|
|
507
|
+
} finally {
|
|
508
|
+
if (!started) closeIterator(source)
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function Iterator$prototype$drop(limit) {
|
|
513
|
+
const iterator = $Direct(this, false)
|
|
514
|
+
return prime(dropHelper(this, iterator, count(limit, "drop")))
|
|
515
|
+
}
|
|
516
|
+
function* dropHelper(source, iterator, remaining) {
|
|
517
|
+
let started = false
|
|
518
|
+
try {
|
|
519
|
+
yield
|
|
520
|
+
started = true
|
|
521
|
+
for (const item of iterator) {
|
|
522
|
+
if (remaining > 0) remaining--
|
|
523
|
+
else yield item
|
|
524
|
+
}
|
|
525
|
+
} finally {
|
|
526
|
+
if (!started) closeIterator(source)
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function Iterator$prototype$flatMap(mapper) {
|
|
531
|
+
const iterator = $Direct(this, false)
|
|
532
|
+
$Callback(mapper, "Iterator.prototype.flatMap")
|
|
533
|
+
return prime(flatMapHelper(this, iterator, mapper))
|
|
534
|
+
}
|
|
535
|
+
function* flatMapHelper(source, iterator, mapper) {
|
|
536
|
+
let started = false
|
|
537
|
+
try {
|
|
538
|
+
yield
|
|
539
|
+
started = true
|
|
540
|
+
let counter = 0
|
|
541
|
+
for (const item of iterator) {
|
|
542
|
+
for (const inner of flattenable(mapper(item, counter++), false, "Iterator.prototype.flatMap")) yield inner
|
|
543
|
+
}
|
|
544
|
+
} finally {
|
|
545
|
+
if (!started) closeIterator(source)
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function prime(helper) {
|
|
550
|
+
helper.next()
|
|
551
|
+
return helper
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// A take or drop count: a number that is not NaN or negative.
|
|
555
|
+
function count(limit, name) {
|
|
556
|
+
const number = +limit
|
|
557
|
+
const integer = number === Infinity || number === -Infinity ? number : number - (number % 1)
|
|
558
|
+
if (number !== number || integer < 0) $Throw("RangeError", "Iterator.prototype." + name + " expects a non-negative count, received " + number + ".")
|
|
559
|
+
return integer
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// GetIteratorFlattenable: an iterable by its [Symbol.iterator], otherwise the object itself as an iterator.
|
|
563
|
+
function flattenable(value, strings, name) {
|
|
564
|
+
if (typeof value === "string" ? !strings : !$IsObject(value)) $Throw("TypeError", name + " expects an iterable or iterator, received " + $Describe(value) + ".")
|
|
565
|
+
const method = value[Symbol.iterator]
|
|
566
|
+
return $Direct(method === undefined || method === null ? value : $Call(method, value), false)
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function closeIterator(iterator) {
|
|
570
|
+
const method = iterator.return
|
|
571
|
+
if (method !== undefined && method !== null && !$IsObject($Call(method, iterator))) $Throw("TypeError", "Iterator return() must return an object.")
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function Iterator$prototype$toArray() {
|
|
575
|
+
const result = []
|
|
576
|
+
for (const item of $Direct(this, false)) result[result.length] = item
|
|
577
|
+
return result
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function Iterator$prototype$reduce(callback, ...initial) {
|
|
581
|
+
const iterator = $Direct(this, false)
|
|
582
|
+
$Callback(callback, "Iterator.prototype.reduce")
|
|
583
|
+
let accumulator = initial[0]
|
|
584
|
+
let started = initial.length > 0
|
|
585
|
+
let index = 0
|
|
586
|
+
for (const item of iterator) {
|
|
587
|
+
if (started) accumulator = callback(accumulator, item, index)
|
|
588
|
+
else accumulator = item
|
|
589
|
+
started = true
|
|
590
|
+
index++
|
|
591
|
+
}
|
|
592
|
+
if (!started) $Throw("TypeError", "Iterator.prototype.reduce of an empty iterator with no initial value.")
|
|
593
|
+
return accumulator
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function Iterator$prototype$forEach(callback) {
|
|
597
|
+
iteratorTest(this, callback, "forEach")
|
|
598
|
+
}
|
|
599
|
+
function Iterator$prototype$some(callback) {
|
|
600
|
+
return iteratorTest(this, callback, "some")
|
|
601
|
+
}
|
|
602
|
+
function Iterator$prototype$every(callback) {
|
|
603
|
+
return iteratorTest(this, callback, "every")
|
|
604
|
+
}
|
|
605
|
+
function Iterator$prototype$find(callback) {
|
|
606
|
+
return iteratorTest(this, callback, "find")
|
|
607
|
+
}
|
|
608
|
+
// Calls the callback with each value and its index until its result decides; leaving early closes the iterator.
|
|
609
|
+
function iteratorTest(self, callback, name) {
|
|
610
|
+
const iterator = $Direct(self, false)
|
|
611
|
+
$Callback(callback, "Iterator.prototype." + name)
|
|
612
|
+
let index = 0
|
|
613
|
+
let result
|
|
614
|
+
for (const item of iterator) {
|
|
615
|
+
result = callback(item, index++)
|
|
616
|
+
if (name === "some" && result) return true
|
|
617
|
+
if (name === "every" && !result) return false
|
|
618
|
+
if (name === "find" && result) return item
|
|
619
|
+
}
|
|
620
|
+
return name === "every" ? true : name === "some" ? false : undefined
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// JSON.parse's reviver: children are revived first, each replacing (or, when the reviver gives undefined, removing)
|
|
624
|
+
// its property; then the reviver sees the value itself.
|
|
625
|
+
function revive(holder, key, reviver) {
|
|
626
|
+
const value = holder[key]
|
|
627
|
+
if ($IsObject(value)) {
|
|
628
|
+
const names = $Keys(value)
|
|
629
|
+
let index = 0
|
|
630
|
+
let revived
|
|
631
|
+
while (index < names.length) {
|
|
632
|
+
revived = revive(value, names[index], reviver)
|
|
633
|
+
if (revived === undefined) delete value[names[index]]
|
|
634
|
+
else value[names[index]] = revived
|
|
635
|
+
index++
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return $Call(reviver, holder, key, value)
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// String replace with a callback, once the matches are found: each is the text before it and the callback's
|
|
642
|
+
// arguments; the text after the last one follows.
|
|
643
|
+
function replaceWith(matches, rest, callback) {
|
|
644
|
+
let result = ""
|
|
645
|
+
let index = 0
|
|
646
|
+
while (index < matches.length) {
|
|
647
|
+
result += matches[index][0] + String(callback(...matches[index][1]))
|
|
648
|
+
index++
|
|
649
|
+
}
|
|
650
|
+
return result + rest
|
|
651
|
+
}
|
|
652
|
+
`;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Builtins } from "./intrinsics.js";
|
|
2
|
+
import { Callable, type NativeResult, type Value } from "./objects.js";
|
|
3
|
+
import { type Hints, Invoke } from "./signals.js";
|
|
4
|
+
/** Invoke(value, name), as the call to make: the method the value would find through its prototype. */
|
|
5
|
+
export declare const invokeMethod: (builtins: Builtins, value: Value, name: string, label: string) => Invoke;
|
|
6
|
+
/**
|
|
7
|
+
* The conversions a built-in received, while it runs again after them: by ordinal, the order its `withPrimitives`
|
|
8
|
+
* calls run in. The VM resets `count` before every built-in call.
|
|
9
|
+
*/
|
|
10
|
+
export declare const conversions: {
|
|
11
|
+
count: number;
|
|
12
|
+
replay: Array<Array<Value> | undefined> | undefined;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Runs a native body on values after ToPrimitive, in order, with one hint for all positions or one per position.
|
|
16
|
+
* Primitive values skip the conversion entirely. Otherwise the built-in asks the VM for the conversions (they
|
|
17
|
+
* call program code, so they run as prelude code) and runs again from the start once they are done; this call then
|
|
18
|
+
* receives them. So everything a built-in does before its last conversion must be repeatable: it reads, checks, and
|
|
19
|
+
* throws, but changes nothing. State the body needs as it was before the conversions (a Date's time) goes in `values`.
|
|
20
|
+
* With `coerce`, each value is also converted to a number or string (by its hint) before the next one is.
|
|
21
|
+
*/
|
|
22
|
+
export declare const withPrimitives: (hints: Hints, values: Array<Value>, body: (primitives: Array<Value>) => NativeResult, coerce?: boolean) => NativeResult;
|
|
23
|
+
/** Checks a callback argument: the callable it is, or the TypeError for one a callback site does not admit. */
|
|
24
|
+
export declare const requireCallback: (callback: Value, name: string) => Callable;
|
|
25
|
+
export declare const isSupportedCallback: (value: Value) => value is Callable;
|