Repository navigation
Expand file tree
/
Copy patharray.ts
More file actions
534 lines (527 loc) · 19.4 KB
/
Copy patharray.ts
File metadata and controls
534 lines (527 loc) · 19.4 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import type { Eval } from "../interpreter/runner.js"
import { constructor, type Method, methods, prototypeFrom, receiver } from "../interpreter/native.js"
import { checkArrayLength, checkStringLength, MAX_ARRAY_LENGTH } from "../interpreter/limits.js"
import { invalidData, IteratorSymbol, rangeError, typeError } from "../interpreter/model.js"
import {
define,
get,
hidden,
Arr,
GeneratorObj,
hostIterator,
Obj,
PromiseObj,
coerceToInteger,
coerceToNumber,
coerceToString,
rejectAddition,
type Value,
} from "../interpreter/objects.js"
import { describeValue, rejectCircularInsertion } from "../interpreter/references.js"
import { applyCollectionCallback, invoke, preserveConsumerError, withPrimitives } from "../interpreter/callback.js"
import type { Interpreter } from "../interpreter/interpreter.js"
import { compareText } from "../tool-runtime.js"
const arrayLikeSource = (source: Value): { readonly length: number; readonly source: Obj } => {
// JS would treat a promise as an empty array-like; that would hide a missing `await`.
if (source instanceof Obj && !(source instanceof PromiseObj)) {
const length = Math.max(0, coerceToInteger(get(source, "length")))
checkArrayLength(length)
return { length, source }
}
throw invalidData(`Array.from expects an iterable or array-like value, received ${describeValue(source)}.`)
}
function* arrayFrom<R>(ctx: Interpreter<R>, args: Array<Value>): Eval<Value> {
const source = args[0]
const proto = ctx.builtins.Array
const apply =
args.length < 2 || args[1] === undefined ? undefined : applyCollectionCallback(ctx, args[1], "Array.from")
const cursor = yield* ctx.iterate(source)
if (cursor === undefined) {
if (source instanceof GeneratorObj) {
throw typeError(
`Array.from expects a synchronous iterable or array-like value, received ${describeValue(source)}.`,
)
}
const arrayLike = arrayLikeSource(source)
const values: Array<Value> = []
for (let index = 0; index < arrayLike.length; index += 1) {
const item = get(arrayLike.source, index)
values.push(apply === undefined ? item : yield* apply([item, index], args[2]))
}
return new Arr(proto, values)
}
const values: Array<Value> = []
let index = 0
while (true) {
const step = yield* cursor.next()
if (step.done) return new Arr(proto, values)
values.push(
apply === undefined
? step.value
: yield* preserveConsumerError(cursor.close, apply([step.value, index], args[2])),
)
index += 1
}
}
export function* sortArray<R>(
ctx: Interpreter<R>,
target: Array<Value>,
comparator: Value,
name: string,
): Eval<Array<Value>> {
if (comparator === undefined) {
return [...target].sort((a, b) => compareText(coerceToString(a), coerceToString(b)))
}
const apply = applyCollectionCallback(ctx, comparator, name)
const mergeSort = function* (items: Array<Value>): Eval<Array<Value>> {
if (items.length <= 1) return items
const midpoint = Math.floor(items.length / 2)
const left = yield* mergeSort(items.slice(0, midpoint))
const right = yield* mergeSort(items.slice(midpoint))
const merged: Array<Value> = []
let leftIndex = 0
let rightIndex = 0
while (leftIndex < left.length && rightIndex < right.length) {
// Treat a NaN comparator result as equal to preserve stable ordering.
const order = coerceToNumber(yield* apply([left[leftIndex], right[rightIndex]]))
if (Number.isNaN(order) || order <= 0) merged.push(left[leftIndex++])
else merged.push(right[rightIndex++])
}
return [...merged, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
const defined = target.filter((item) => item !== undefined)
const undefinedCount = target.length - defined.length
const sorted = yield* mergeSort(defined)
return [...sorted, ...Array(undefinedCount).fill(undefined)]
}
// Array constructs identically with or without new, like JS.
export const arrayGlobal = <R>(ctx: Interpreter<R>) => {
const builtins = ctx.builtins
const proto = builtins.Array
const wrap = (items: Array<Value>) => new Arr(proto, items)
const construct = (args: Array<Value>, into: Obj): Arr => {
if (args.length !== 1) return new Arr(into, [...args])
const first = args[0]
if (typeof first !== "number") return new Arr(into, [first])
if (!Number.isInteger(first) || first < 0 || first > MAX_ARRAY_LENGTH) throw rangeError("Invalid array length.")
// Sparse like JS: Array(3) has holes, and combinator loops already skip them.
return new Arr(into, new Array(first))
}
const array = constructor(builtins, proto, {
name: "Array",
length: 1,
call: (_, args) => construct(args, proto),
construct: (args, newTarget) => construct(args, prototypeFrom(newTarget, proto)),
})
methods(builtins, array, [
["isArray", 1, (_, args) => args[0] instanceof Arr],
["of", 0, (_, args) => wrap([...args])],
["from", 1, (_, args) => arrayFrom(ctx, args)],
])
const self = (thisValue: Value, name: string) => receiver(Arr, thisValue, `Array.prototype.${name}`)
// A mutating method fails where its first element write, delete, or `length` write would on a frozen, sealed, or
// non-extensible array. `growth` is given by the methods that always write `length`, even when it is 0; holes a
// method would fill on a non-extensible array are not checked.
const mutable = (target: Arr, growth?: number): Arr => {
const length = target.items.length
if ((growth ?? 0) > 0) rejectAddition(target, length)
if ((growth ?? 0) < 0 && length > 0 && !target.elements.configurable) {
throw typeError(`Cannot delete property '${length - 1}'.`)
}
if (!target.elements.writable && (length > 0 || growth !== undefined)) {
throw typeError(`Cannot assign to read only property '${length > 0 ? 0 : "length"}'.`)
}
return target
}
const optNumber = (value: Value): number | undefined => (value === undefined ? undefined : coerceToInteger(value))
methods(builtins, proto, [
[
"join",
1,
(thisValue, args) => {
// .map would keep holes, which the conversion would then hand to the body as undefined.
const parts = Array.from(self(thisValue, "join").items, (item) => item ?? "")
return withPrimitives(
ctx,
"string",
[args[0] === undefined ? "," : args[0], ...parts],
([separator, ...items]) => {
const joined = items.map(coerceToString).join(coerceToString(separator))
checkStringLength(joined.length)
return joined
},
)
},
],
[
"toString",
0,
(thisValue) =>
withPrimitives(
ctx,
"string",
Array.from(self(thisValue, "toString").items, (item) => item ?? ""),
(items) => items.map(coerceToString).join(","),
),
],
[
"includes",
1,
(thisValue, args) => {
return self(thisValue, "includes").items.includes(args[0], optNumber(args[1]))
},
],
["indexOf", 1, (thisValue, args) => self(thisValue, "indexOf").items.indexOf(args[0], optNumber(args[1]))],
[
"lastIndexOf",
1,
(thisValue, args) => {
const target = self(thisValue, "lastIndexOf").items
// An explicit undefined is a fromIndex of 0, unlike omitting it.
return args.length < 2 ? target.lastIndexOf(args[0]) : target.lastIndexOf(args[0], optNumber(args[1]))
},
],
["at", 1, (thisValue, args) => self(thisValue, "at").items.at(optNumber(args[0]) ?? 0)],
[
"slice",
2,
(thisValue, args) => wrap(self(thisValue, "slice").items.slice(optNumber(args[0]), optNumber(args[1]))),
],
[
"concat",
1,
(thisValue, args) => {
const joined = self(thisValue, "concat").items.concat(
...args.map((item) => (item instanceof Arr ? item.items : item)),
)
checkArrayLength(joined.length)
return wrap(joined)
},
],
[
"flat",
0,
(thisValue, args) => {
const flatten = (items: Array<Value>, depth: number): Array<Value> =>
items.flatMap((item) => (item instanceof Arr && depth > 0 ? flatten(item.items, depth - 1) : [item]))
const flattened = flatten(self(thisValue, "flat").items, optNumber(args[0]) ?? 1)
checkArrayLength(flattened.length)
return wrap(flattened)
},
],
[
"reverse",
0,
(thisValue) => {
const target = self(thisValue, "reverse")
// Fewer than two elements means no writes at all, so a frozen one-element array reverses fine.
if (target.items.length > 1) mutable(target)
target.items.reverse()
return target
},
],
[
"sort",
1,
function* (thisValue, args) {
const target = mutable(self(thisValue, "sort"))
const items = target.items
const length = items.length
const holeCount = Array.from({ length }, (_, index) => Object.hasOwn(items, index)).filter((o) => !o).length
const itemCount = length - holeCount
const sorted = yield* sortArray(ctx, items, args[0], "Array.sort")
sorted.slice(0, itemCount).forEach((item, index) => {
items[index] = item
})
Array.from({ length: holeCount }, (_, index) => itemCount + index).forEach((index) => {
delete items[index]
})
return target
},
],
[
"toSorted",
1,
function* (thisValue, args) {
return wrap(yield* sortArray(ctx, self(thisValue, "toSorted").items, args[0], "Array.toSorted"))
},
],
["toReversed", 0, (thisValue) => wrap([...self(thisValue, "toReversed").items].reverse())],
[
"with",
2,
(thisValue, args) => {
const target = self(thisValue, "with").items
const index = optNumber(args[0]) ?? 0
const resolved = index < 0 ? target.length + index : index
if (resolved < 0 || resolved >= target.length) throw rangeError("Array.with index is out of range.")
const copied = [...target]
copied[resolved] = args[1]
return wrap(copied)
},
],
[
"push",
1,
(thisValue, args) => {
const target = mutable(self(thisValue, "push"), args.length)
// Validate all insertions before mutating to avoid partial cyclic updates.
for (const item of args) rejectCircularInsertion(target, item, "Array.push result")
return target.items.push(...args)
},
],
[
"unshift",
1,
(thisValue, args) => {
const target = mutable(self(thisValue, "unshift"), args.length)
for (const item of args) rejectCircularInsertion(target, item, "Array.unshift result")
return target.items.unshift(...args)
},
],
["pop", 0, (thisValue) => mutable(self(thisValue, "pop"), -1).items.pop()],
["shift", 0, (thisValue) => mutable(self(thisValue, "shift"), -1).items.shift()],
[
"splice",
2,
(thisValue, args) => {
const target = self(thisValue, "splice")
const length = target.items.length
const start = optNumber(args[0]) ?? 0
const from = start < 0 ? Math.max(length + start, 0) : Math.min(start, length)
const deleteCount =
args.length === 1 ? length - from : Math.min(Math.max(optNumber(args[1]) ?? 0, 0), length - from)
const inserted = args.slice(2)
for (const item of inserted) rejectCircularInsertion(target, item, "Array.splice result")
mutable(target, inserted.length - deleteCount)
return wrap(target.items.splice(from, deleteCount, ...inserted))
},
],
[
"toSpliced",
2,
(thisValue, args) => {
const copied = [...self(thisValue, "toSpliced").items]
if (args.length === 0) return wrap(copied)
const start = optNumber(args[0]) ?? 0
if (args.length === 1) copied.splice(start)
else copied.splice(start, optNumber(args[1]) ?? 0, ...args.slice(2))
return wrap(copied)
},
],
[
"fill",
1,
(thisValue, args) => {
const target = mutable(self(thisValue, "fill"))
rejectCircularInsertion(target, args[0], "Array.fill result")
target.items.fill(args[0], optNumber(args[1]), optNumber(args[2]))
return target
},
],
[
"copyWithin",
2,
(thisValue, args) => {
const target = mutable(self(thisValue, "copyWithin"))
target.items.copyWithin(optNumber(args[0]) ?? 0, optNumber(args[1]) ?? 0, optNumber(args[2]))
return target
},
],
[
"toLocaleString",
0,
function* (thisValue) {
const items = self(thisValue, "toLocaleString").items
const length = items.length
const parts: Array<string> = []
for (let index = 0; index < length; index += 1) {
const item = items[index]
parts.push(
item === null || item === undefined
? ""
: coerceToString(yield* invoke(ctx, item, "toLocaleString", "Array.prototype.toLocaleString")),
)
}
return parts.join(",")
},
],
["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").items.keys())],
["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").items.values())],
[
"entries",
0,
(thisValue) =>
hostIterator(
builtins,
self(thisValue, "entries")
.items.entries()
.map(([index, item]) => wrap([index, item])),
),
],
[
"flatMap",
1,
function* (thisValue, args) {
const target = self(thisValue, "flatMap")
const apply = applyCollectionCallback(ctx, args[0], "Array.flatMap")
const length = target.items.length
const values: Array<Value> = []
for (let index = 0; index < length; index += 1) {
if (!(index in target.items)) continue
const mapped = yield* apply([target.items[index], index, target], args[1])
if (mapped instanceof Arr) {
// forEach skips holes, as flattening does; spreading would turn them into undefined.
mapped.items.forEach((item) => values.push(item))
continue
}
values.push(mapped)
}
return wrap(values)
},
],
...callbackMethods(ctx, "Array", self, (target) => target.items, wrap),
])
define(proto, IteratorSymbol, get(proto, "values"), hidden)
return array
}
/**
* The callback methods Array and Uint8Array share. They fix the iteration length while reading existing elements
* live; `wrap` builds the collection `map` and `filter` return.
*/
export const callbackMethods = <R, T extends Obj>(
ctx: Interpreter<R>,
label: string,
self: (thisValue: Value, name: string) => T,
elements: (target: T) => ArrayLike<Value>,
wrap: (values: Array<Value>) => Value,
): Array<Method> => {
const iterate = (
name: string,
length: number,
body: (
target: ArrayLike<Value>,
receiver: T,
apply: (args: Array<Value>) => Eval<Value>,
args: Array<Value>,
) => Eval<Value>,
): Method => [
name,
length,
(thisValue, args) => {
const target = self(thisValue, name)
const call = applyCollectionCallback(ctx, args[0], `${label}.${name}`)
// reduce and reduceRight take an initial value where the others take a thisArg.
const thisArg = name.startsWith("reduce") ? undefined : args[1]
return body(elements(target), target, (callbackArgs) => call(callbackArgs, thisArg), args)
},
]
return [
iterate("map", 1, function* (target, receiver, apply) {
const length = target.length
const values: Array<Value> = []
values.length = length
for (let index = 0; index < length; index += 1) {
if (!(index in target)) continue
values[index] = yield* apply([target[index], index, receiver])
}
return wrap(values)
}),
iterate("filter", 1, function* (target, receiver, apply) {
const length = target.length
const values: Array<Value> = []
for (let index = 0; index < length; index += 1) {
if (!(index in target)) continue
const item = target[index]
if (yield* apply([item, index, receiver])) values.push(item)
}
return wrap(values)
}),
iterate("find", 1, function* (target, receiver, apply) {
const length = target.length
for (let index = 0; index < length; index += 1) {
const item = target[index]
if (yield* apply([item, index, receiver])) return item
}
return undefined
}),
iterate("findIndex", 1, function* (target, receiver, apply) {
const length = target.length
for (let index = 0; index < length; index += 1) {
if (yield* apply([target[index], index, receiver])) return index
}
return -1
}),
iterate("findLast", 1, function* (target, receiver, apply) {
for (let index = target.length - 1; index >= 0; index -= 1) {
const item = target[index]
if (yield* apply([item, index, receiver])) return item
}
return undefined
}),
iterate("findLastIndex", 1, function* (target, receiver, apply) {
for (let index = target.length - 1; index >= 0; index -= 1) {
if (yield* apply([target[index], index, receiver])) return index
}
return -1
}),
iterate("some", 1, function* (target, receiver, apply) {
const length = target.length
for (let index = 0; index < length; index += 1) {
if (!(index in target)) continue
if (yield* apply([target[index], index, receiver])) return true
}
return false
}),
iterate("every", 1, function* (target, receiver, apply) {
const length = target.length
for (let index = 0; index < length; index += 1) {
if (!(index in target)) continue
if (!(yield* apply([target[index], index, receiver]))) return false
}
return true
}),
iterate("forEach", 1, function* (target, receiver, apply) {
const length = target.length
for (let index = 0; index < length; index += 1) {
if (index in target) yield* apply([target[index], index, receiver])
}
return undefined
}),
iterate("reduce", 1, function* (target, receiver, apply, args) {
const length = target.length
let start = 0
let accumulator = args[1]
if (args.length < 2) {
while (start < length && !(start in target)) start += 1
if (start === length) {
throw typeError(`${label}.reduce of an empty array with no initial value.`)
}
accumulator = target[start]
start += 1
}
for (let index = start; index < length; index += 1) {
if (!(index in target)) continue
accumulator = yield* apply([accumulator, target[index], index, receiver])
}
return accumulator
}),
iterate("reduceRight", 1, function* (target, receiver, apply, args) {
let start = target.length - 1
let accumulator = args[1]
if (args.length < 2) {
while (start >= 0 && !(start in target)) start -= 1
if (start < 0) {
throw typeError(`${label}.reduceRight of an empty array with no initial value.`)
}
accumulator = target[start]
start -= 1
}
for (let index = start; index >= 0; index -= 1) {
if (!(index in target)) continue
accumulator = yield* apply([accumulator, target[index], index, receiver])
}
return accumulator
}),
]
}