@opencode/codemode 2.1.4 → 2.2.1
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 +33 -4
- package/dist/codemode.d.ts +9 -7
- package/dist/codemode.js +1 -1
- package/dist/data.d.ts +5 -4
- package/dist/data.js +7 -4
- package/dist/extension.d.ts +17 -4
- package/dist/extension.js +8 -0
- package/dist/interpreter/callback.d.ts +10 -10
- package/dist/interpreter/callback.js +43 -31
- package/dist/interpreter/errors.d.ts +1 -1
- package/dist/interpreter/errors.js +12 -15
- package/dist/interpreter/execute.js +59 -29
- package/dist/interpreter/extensions.d.ts +2 -2
- package/dist/interpreter/extensions.js +46 -18
- package/dist/interpreter/generators.js +162 -19
- package/dist/interpreter/globals.js +12 -9
- package/dist/interpreter/interpreter.d.ts +20 -13
- package/dist/interpreter/interpreter.js +1329 -1147
- package/dist/interpreter/intrinsics.js +1 -2
- package/dist/interpreter/model.d.ts +4 -7
- package/dist/interpreter/model.js +1 -11
- package/dist/interpreter/native.d.ts +7 -8
- package/dist/interpreter/native.js +4 -10
- package/dist/interpreter/objects.d.ts +47 -21
- package/dist/interpreter/objects.js +33 -12
- package/dist/interpreter/promises.d.ts +15 -53
- package/dist/interpreter/promises.js +226 -401
- package/dist/interpreter/runner.d.ts +188 -0
- package/dist/interpreter/runner.js +492 -0
- package/dist/stdlib/array.d.ts +3 -3
- package/dist/stdlib/array.js +106 -102
- package/dist/stdlib/bytes.d.ts +3 -3
- package/dist/stdlib/bytes.js +46 -41
- package/dist/stdlib/collections.d.ts +5 -5
- package/dist/stdlib/collections.js +160 -165
- package/dist/stdlib/date.d.ts +1 -1
- package/dist/stdlib/date.js +28 -20
- package/dist/stdlib/headers.d.ts +1 -1
- package/dist/stdlib/headers.js +14 -19
- package/dist/stdlib/iterator.d.ts +1 -1
- package/dist/stdlib/iterator.js +140 -122
- package/dist/stdlib/json.js +9 -12
- package/dist/stdlib/math.js +10 -10
- package/dist/stdlib/number.d.ts +2 -2
- package/dist/stdlib/object.d.ts +1 -1
- package/dist/stdlib/object.js +21 -22
- package/dist/stdlib/regexp.d.ts +1 -1
- package/dist/stdlib/regexp.js +2 -3
- package/dist/stdlib/string.d.ts +1 -1
- package/dist/stdlib/string.js +18 -20
- package/dist/stdlib/url.d.ts +5 -5
- package/dist/stdlib/url.js +32 -33
- package/dist/stdlib/value.d.ts +2 -2
- package/dist/stdlib/web.d.ts +2 -2
- package/package.json +2 -1
package/dist/stdlib/array.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
1
|
import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
|
|
3
2
|
import { checkArrayLength, checkStringLength, MAX_ARRAY_LENGTH } from "../interpreter/limits.js";
|
|
4
3
|
import { invalidData, IteratorSymbol, rangeError, typeError } from "../interpreter/model.js";
|
|
@@ -15,67 +14,64 @@ const arrayLikeSource = (source) => {
|
|
|
15
14
|
}
|
|
16
15
|
throw invalidData(`Array.from expects an iterable or array-like value, received ${describeValue(source)}.`);
|
|
17
16
|
};
|
|
18
|
-
|
|
17
|
+
function* arrayFrom(ctx, args) {
|
|
19
18
|
const source = args[0];
|
|
20
19
|
const proto = ctx.builtins.Array;
|
|
21
20
|
const apply = args.length < 2 || args[1] === undefined ? undefined : applyCollectionCallback(ctx, args[1], "Array.from");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
throw typeError(`Array.from expects a synchronous iterable or array-like value, received ${describeValue(source)}.`);
|
|
27
|
-
}
|
|
28
|
-
const arrayLike = arrayLikeSource(source);
|
|
29
|
-
const values = [];
|
|
30
|
-
for (let index = 0; index < arrayLike.length; index += 1) {
|
|
31
|
-
const item = get(arrayLike.source, index);
|
|
32
|
-
values.push(apply === undefined ? item : yield* apply([item, index], args[2]));
|
|
33
|
-
}
|
|
34
|
-
return new Arr(proto, values);
|
|
21
|
+
const cursor = yield* ctx.iterate(source);
|
|
22
|
+
if (cursor === undefined) {
|
|
23
|
+
if (source instanceof GeneratorObj) {
|
|
24
|
+
throw typeError(`Array.from expects a synchronous iterable or array-like value, received ${describeValue(source)}.`);
|
|
35
25
|
}
|
|
26
|
+
const arrayLike = arrayLikeSource(source);
|
|
36
27
|
const values = [];
|
|
37
|
-
let index = 0;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (step.done)
|
|
41
|
-
return new Arr(proto, values);
|
|
42
|
-
values.push(apply === undefined
|
|
43
|
-
? step.value
|
|
44
|
-
: yield* preserveConsumerError(cursor.close, apply([step.value, index], args[2])));
|
|
45
|
-
index += 1;
|
|
28
|
+
for (let index = 0; index < arrayLike.length; index += 1) {
|
|
29
|
+
const item = get(arrayLike.source, index);
|
|
30
|
+
values.push(apply === undefined ? item : yield* apply([item, index], args[2]));
|
|
46
31
|
}
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
32
|
+
return new Arr(proto, values);
|
|
33
|
+
}
|
|
34
|
+
const values = [];
|
|
35
|
+
let index = 0;
|
|
36
|
+
while (true) {
|
|
37
|
+
const step = yield* cursor.next();
|
|
38
|
+
if (step.done)
|
|
39
|
+
return new Arr(proto, values);
|
|
40
|
+
values.push(apply === undefined
|
|
41
|
+
? step.value
|
|
42
|
+
: yield* preserveConsumerError(cursor.close, apply([step.value, index], args[2])));
|
|
43
|
+
index += 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function* sortArray(ctx, target, comparator, name) {
|
|
50
47
|
if (comparator === undefined) {
|
|
51
|
-
return
|
|
48
|
+
return [...target].sort((a, b) => compareText(coerceToString(a), coerceToString(b)));
|
|
52
49
|
}
|
|
53
50
|
const apply = applyCollectionCallback(ctx, comparator, name);
|
|
54
|
-
const mergeSort = (items)
|
|
51
|
+
const mergeSort = function* (items) {
|
|
55
52
|
if (items.length <= 1)
|
|
56
|
-
return
|
|
53
|
+
return items;
|
|
57
54
|
const midpoint = Math.floor(items.length / 2);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return [...merged, ...left.slice(leftIndex), ...right.slice(rightIndex)];
|
|
73
|
-
});
|
|
55
|
+
const left = yield* mergeSort(items.slice(0, midpoint));
|
|
56
|
+
const right = yield* mergeSort(items.slice(midpoint));
|
|
57
|
+
const merged = [];
|
|
58
|
+
let leftIndex = 0;
|
|
59
|
+
let rightIndex = 0;
|
|
60
|
+
while (leftIndex < left.length && rightIndex < right.length) {
|
|
61
|
+
// Treat a NaN comparator result as equal to preserve stable ordering.
|
|
62
|
+
const order = coerceToNumber(yield* apply([left[leftIndex], right[rightIndex]]));
|
|
63
|
+
if (Number.isNaN(order) || order <= 0)
|
|
64
|
+
merged.push(left[leftIndex++]);
|
|
65
|
+
else
|
|
66
|
+
merged.push(right[rightIndex++]);
|
|
67
|
+
}
|
|
68
|
+
return [...merged, ...left.slice(leftIndex), ...right.slice(rightIndex)];
|
|
74
69
|
};
|
|
75
70
|
const defined = target.filter((item) => item !== undefined);
|
|
76
71
|
const undefinedCount = target.length - defined.length;
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
const sorted = yield* mergeSort(defined);
|
|
73
|
+
return [...sorted, ...Array(undefinedCount).fill(undefined)];
|
|
74
|
+
}
|
|
79
75
|
// Array constructs identically with or without new, like JS.
|
|
80
76
|
export const arrayGlobal = (ctx) => {
|
|
81
77
|
const builtins = ctx.builtins;
|
|
@@ -95,8 +91,8 @@ export const arrayGlobal = (ctx) => {
|
|
|
95
91
|
const array = constructor(builtins, proto, {
|
|
96
92
|
name: "Array",
|
|
97
93
|
length: 1,
|
|
98
|
-
call: (_, args) =>
|
|
99
|
-
construct: (args, newTarget) =>
|
|
94
|
+
call: (_, args) => construct(args, proto),
|
|
95
|
+
construct: (args, newTarget) => construct(args, prototypeFrom(newTarget, proto)),
|
|
100
96
|
});
|
|
101
97
|
methods(builtins, array, [
|
|
102
98
|
["isArray", 1, (_, args) => args[0] instanceof Arr],
|
|
@@ -125,7 +121,7 @@ export const arrayGlobal = (ctx) => {
|
|
|
125
121
|
"join",
|
|
126
122
|
1,
|
|
127
123
|
(thisValue, args) => {
|
|
128
|
-
// .map would keep holes, which
|
|
124
|
+
// .map would keep holes, which the conversion would then hand to the body as undefined.
|
|
129
125
|
const parts = Array.from(self(thisValue, "join").items, (item) => item ?? "");
|
|
130
126
|
return withPrimitives(ctx, "string", [args[0] === undefined ? "," : args[0], ...parts], ([separator, ...items]) => {
|
|
131
127
|
const joined = items.map(coerceToString).join(coerceToString(separator));
|
|
@@ -196,27 +192,28 @@ export const arrayGlobal = (ctx) => {
|
|
|
196
192
|
[
|
|
197
193
|
"sort",
|
|
198
194
|
1,
|
|
199
|
-
(thisValue, args)
|
|
195
|
+
function* (thisValue, args) {
|
|
200
196
|
const target = mutable(self(thisValue, "sort"));
|
|
201
197
|
const items = target.items;
|
|
202
198
|
const length = items.length;
|
|
203
199
|
const holeCount = Array.from({ length }, (_, index) => Object.hasOwn(items, index)).filter((o) => !o).length;
|
|
204
200
|
const itemCount = length - holeCount;
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
});
|
|
212
|
-
return target;
|
|
201
|
+
const sorted = yield* sortArray(ctx, items, args[0], "Array.sort");
|
|
202
|
+
sorted.slice(0, itemCount).forEach((item, index) => {
|
|
203
|
+
items[index] = item;
|
|
204
|
+
});
|
|
205
|
+
Array.from({ length: holeCount }, (_, index) => itemCount + index).forEach((index) => {
|
|
206
|
+
delete items[index];
|
|
213
207
|
});
|
|
208
|
+
return target;
|
|
214
209
|
},
|
|
215
210
|
],
|
|
216
211
|
[
|
|
217
212
|
"toSorted",
|
|
218
213
|
1,
|
|
219
|
-
|
|
214
|
+
function* (thisValue, args) {
|
|
215
|
+
return wrap(yield* sortArray(ctx, self(thisValue, "toSorted").items, args[0], "Array.toSorted"));
|
|
216
|
+
},
|
|
220
217
|
],
|
|
221
218
|
["toReversed", 0, (thisValue) => wrap([...self(thisValue, "toReversed").items].reverse())],
|
|
222
219
|
[
|
|
@@ -309,9 +306,18 @@ export const arrayGlobal = (ctx) => {
|
|
|
309
306
|
[
|
|
310
307
|
"toLocaleString",
|
|
311
308
|
0,
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
309
|
+
function* (thisValue) {
|
|
310
|
+
const items = self(thisValue, "toLocaleString").items;
|
|
311
|
+
const length = items.length;
|
|
312
|
+
const parts = [];
|
|
313
|
+
for (let index = 0; index < length; index += 1) {
|
|
314
|
+
const item = items[index];
|
|
315
|
+
parts.push(item === null || item === undefined
|
|
316
|
+
? ""
|
|
317
|
+
: coerceToString(yield* invoke(ctx, item, "toLocaleString", "Array.prototype.toLocaleString")));
|
|
318
|
+
}
|
|
319
|
+
return parts.join(",");
|
|
320
|
+
},
|
|
315
321
|
],
|
|
316
322
|
["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").items.keys())],
|
|
317
323
|
["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").items.values())],
|
|
@@ -325,25 +331,23 @@ export const arrayGlobal = (ctx) => {
|
|
|
325
331
|
[
|
|
326
332
|
"flatMap",
|
|
327
333
|
1,
|
|
328
|
-
(thisValue, args)
|
|
334
|
+
function* (thisValue, args) {
|
|
329
335
|
const target = self(thisValue, "flatMap");
|
|
330
336
|
const apply = applyCollectionCallback(ctx, args[0], "Array.flatMap");
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
values.push(mapped);
|
|
337
|
+
const length = target.items.length;
|
|
338
|
+
const values = [];
|
|
339
|
+
for (let index = 0; index < length; index += 1) {
|
|
340
|
+
if (!(index in target.items))
|
|
341
|
+
continue;
|
|
342
|
+
const mapped = yield* apply([target.items[index], index, target], args[1]);
|
|
343
|
+
if (mapped instanceof Arr) {
|
|
344
|
+
// forEach skips holes, as flattening does; spreading would turn them into undefined.
|
|
345
|
+
mapped.items.forEach((item) => values.push(item));
|
|
346
|
+
continue;
|
|
344
347
|
}
|
|
345
|
-
|
|
346
|
-
}
|
|
348
|
+
values.push(mapped);
|
|
349
|
+
}
|
|
350
|
+
return wrap(values);
|
|
347
351
|
},
|
|
348
352
|
],
|
|
349
353
|
...callbackMethods(ctx, "Array", self, (target) => target.items, wrap),
|
|
@@ -368,7 +372,7 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
368
372
|
},
|
|
369
373
|
];
|
|
370
374
|
return [
|
|
371
|
-
iterate("map", 1, (target, receiver, apply)
|
|
375
|
+
iterate("map", 1, function* (target, receiver, apply) {
|
|
372
376
|
const length = target.length;
|
|
373
377
|
const values = [];
|
|
374
378
|
values.length = length;
|
|
@@ -378,8 +382,8 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
378
382
|
values[index] = yield* apply([target[index], index, receiver]);
|
|
379
383
|
}
|
|
380
384
|
return wrap(values);
|
|
381
|
-
})
|
|
382
|
-
iterate("filter", 1, (target, receiver, apply)
|
|
385
|
+
}),
|
|
386
|
+
iterate("filter", 1, function* (target, receiver, apply) {
|
|
383
387
|
const length = target.length;
|
|
384
388
|
const values = [];
|
|
385
389
|
for (let index = 0; index < length; index += 1) {
|
|
@@ -390,8 +394,8 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
390
394
|
values.push(item);
|
|
391
395
|
}
|
|
392
396
|
return wrap(values);
|
|
393
|
-
})
|
|
394
|
-
iterate("find", 1, (target, receiver, apply)
|
|
397
|
+
}),
|
|
398
|
+
iterate("find", 1, function* (target, receiver, apply) {
|
|
395
399
|
const length = target.length;
|
|
396
400
|
for (let index = 0; index < length; index += 1) {
|
|
397
401
|
const item = target[index];
|
|
@@ -399,31 +403,31 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
399
403
|
return item;
|
|
400
404
|
}
|
|
401
405
|
return undefined;
|
|
402
|
-
})
|
|
403
|
-
iterate("findIndex", 1, (target, receiver, apply)
|
|
406
|
+
}),
|
|
407
|
+
iterate("findIndex", 1, function* (target, receiver, apply) {
|
|
404
408
|
const length = target.length;
|
|
405
409
|
for (let index = 0; index < length; index += 1) {
|
|
406
410
|
if (yield* apply([target[index], index, receiver]))
|
|
407
411
|
return index;
|
|
408
412
|
}
|
|
409
413
|
return -1;
|
|
410
|
-
})
|
|
411
|
-
iterate("findLast", 1, (target, receiver, apply)
|
|
414
|
+
}),
|
|
415
|
+
iterate("findLast", 1, function* (target, receiver, apply) {
|
|
412
416
|
for (let index = target.length - 1; index >= 0; index -= 1) {
|
|
413
417
|
const item = target[index];
|
|
414
418
|
if (yield* apply([item, index, receiver]))
|
|
415
419
|
return item;
|
|
416
420
|
}
|
|
417
421
|
return undefined;
|
|
418
|
-
})
|
|
419
|
-
iterate("findLastIndex", 1, (target, receiver, apply)
|
|
422
|
+
}),
|
|
423
|
+
iterate("findLastIndex", 1, function* (target, receiver, apply) {
|
|
420
424
|
for (let index = target.length - 1; index >= 0; index -= 1) {
|
|
421
425
|
if (yield* apply([target[index], index, receiver]))
|
|
422
426
|
return index;
|
|
423
427
|
}
|
|
424
428
|
return -1;
|
|
425
|
-
})
|
|
426
|
-
iterate("some", 1, (target, receiver, apply)
|
|
429
|
+
}),
|
|
430
|
+
iterate("some", 1, function* (target, receiver, apply) {
|
|
427
431
|
const length = target.length;
|
|
428
432
|
for (let index = 0; index < length; index += 1) {
|
|
429
433
|
if (!(index in target))
|
|
@@ -432,8 +436,8 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
432
436
|
return true;
|
|
433
437
|
}
|
|
434
438
|
return false;
|
|
435
|
-
})
|
|
436
|
-
iterate("every", 1, (target, receiver, apply)
|
|
439
|
+
}),
|
|
440
|
+
iterate("every", 1, function* (target, receiver, apply) {
|
|
437
441
|
const length = target.length;
|
|
438
442
|
for (let index = 0; index < length; index += 1) {
|
|
439
443
|
if (!(index in target))
|
|
@@ -442,16 +446,16 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
442
446
|
return false;
|
|
443
447
|
}
|
|
444
448
|
return true;
|
|
445
|
-
})
|
|
446
|
-
iterate("forEach", 1, (target, receiver, apply)
|
|
449
|
+
}),
|
|
450
|
+
iterate("forEach", 1, function* (target, receiver, apply) {
|
|
447
451
|
const length = target.length;
|
|
448
452
|
for (let index = 0; index < length; index += 1) {
|
|
449
453
|
if (index in target)
|
|
450
454
|
yield* apply([target[index], index, receiver]);
|
|
451
455
|
}
|
|
452
456
|
return undefined;
|
|
453
|
-
})
|
|
454
|
-
iterate("reduce", 1, (target, receiver, apply, args)
|
|
457
|
+
}),
|
|
458
|
+
iterate("reduce", 1, function* (target, receiver, apply, args) {
|
|
455
459
|
const length = target.length;
|
|
456
460
|
let start = 0;
|
|
457
461
|
let accumulator = args[1];
|
|
@@ -470,8 +474,8 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
470
474
|
accumulator = yield* apply([accumulator, target[index], index, receiver]);
|
|
471
475
|
}
|
|
472
476
|
return accumulator;
|
|
473
|
-
})
|
|
474
|
-
iterate("reduceRight", 1, (target, receiver, apply, args)
|
|
477
|
+
}),
|
|
478
|
+
iterate("reduceRight", 1, function* (target, receiver, apply, args) {
|
|
475
479
|
let start = target.length - 1;
|
|
476
480
|
let accumulator = args[1];
|
|
477
481
|
if (args.length < 2) {
|
|
@@ -489,6 +493,6 @@ export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
|
489
493
|
accumulator = yield* apply([accumulator, target[index], index, receiver]);
|
|
490
494
|
}
|
|
491
495
|
return accumulator;
|
|
492
|
-
})
|
|
496
|
+
}),
|
|
493
497
|
];
|
|
494
498
|
};
|
package/dist/stdlib/bytes.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Obj } from "../interpreter/objects.js";
|
|
2
2
|
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
3
|
-
export declare const uint8ArrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
4
|
-
export declare const textEncoderGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
3
|
+
export declare const uint8ArrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
|
4
|
+
export declare const textEncoderGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
|
5
5
|
/** A `TextDecoder` holding the host decoder its label and options configured. */
|
|
6
6
|
export declare class TextDecoderObj extends Obj {
|
|
7
7
|
readonly decoder: TextDecoder;
|
|
8
8
|
constructor(proto: Obj, decoder: TextDecoder);
|
|
9
9
|
}
|
|
10
|
-
export declare const textDecoderGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
10
|
+
export declare const textDecoderGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
package/dist/stdlib/bytes.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
1
|
import { checkArrayLength, checkStringLength } from "../interpreter/limits.js";
|
|
3
2
|
import { constructor, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
|
|
4
3
|
import { IteratorSymbol, rangeError, syntaxError, typeError } from "../interpreter/model.js";
|
|
@@ -10,38 +9,40 @@ import { callbackMethods, sortArray } from "./array.js";
|
|
|
10
9
|
* The bytes a Uint8Array, array, or other iterable of numbers describes; the host array clamps each value. As in
|
|
11
10
|
* TypedArray.from, a mapper sees the original values, after the source is consumed in full.
|
|
12
11
|
*/
|
|
13
|
-
|
|
12
|
+
function* collectBytes(ctx, source, name, mapper, thisArg) {
|
|
14
13
|
const apply = mapper === undefined ? undefined : applyCollectionCallback(ctx, mapper, name);
|
|
15
14
|
if (source instanceof Bytes && apply === undefined)
|
|
16
|
-
return
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
15
|
+
return new Uint8Array(source.bytes);
|
|
16
|
+
const cursor = yield* ctx.iterate(source);
|
|
17
|
+
if (cursor === undefined) {
|
|
18
|
+
throw typeError(`${name} expects a Uint8Array, an array, or an iterable of numbers, received ${describeValue(source)}.`);
|
|
19
|
+
}
|
|
20
|
+
const values = [];
|
|
21
|
+
while (true) {
|
|
22
|
+
const step = yield* cursor.next();
|
|
23
|
+
if (step.done)
|
|
24
|
+
break;
|
|
25
|
+
values.push(step.value);
|
|
26
|
+
checkArrayLength(values.length);
|
|
27
|
+
}
|
|
28
|
+
if (apply === undefined)
|
|
29
|
+
return Uint8Array.from(values, coerceToNumber);
|
|
30
|
+
const mapped = [];
|
|
31
|
+
for (const [index, value] of values.entries())
|
|
32
|
+
mapped.push(yield* apply([value, index], thisArg));
|
|
33
|
+
return Uint8Array.from(mapped, coerceToNumber);
|
|
34
|
+
}
|
|
35
|
+
function* constructBytes(ctx, args, proto) {
|
|
35
36
|
const source = args[0];
|
|
36
37
|
if (source !== null && typeof source === "object") {
|
|
37
|
-
return
|
|
38
|
+
return new Bytes(proto, yield* collectBytes(ctx, source, "new Uint8Array(...)"));
|
|
38
39
|
}
|
|
39
40
|
const length = source === undefined ? 0 : coerceToNumber(source);
|
|
40
41
|
if (!Number.isInteger(length) || length < 0)
|
|
41
42
|
throw rangeError(`Invalid typed array length: ${coerceToString(source)}`);
|
|
42
43
|
checkArrayLength(length);
|
|
43
|
-
return
|
|
44
|
-
}
|
|
44
|
+
return new Bytes(proto, new Uint8Array(length));
|
|
45
|
+
}
|
|
45
46
|
export const uint8ArrayGlobal = (ctx) => {
|
|
46
47
|
const builtins = ctx.builtins;
|
|
47
48
|
const proto = builtins.Uint8Array;
|
|
@@ -63,7 +64,13 @@ export const uint8ArrayGlobal = (ctx) => {
|
|
|
63
64
|
}
|
|
64
65
|
};
|
|
65
66
|
methods(builtins, uint8Array, [
|
|
66
|
-
[
|
|
67
|
+
[
|
|
68
|
+
"from",
|
|
69
|
+
1,
|
|
70
|
+
function* (_, args) {
|
|
71
|
+
return wrap(yield* collectBytes(ctx, args[0], "Uint8Array.from", args[1], args[2]));
|
|
72
|
+
},
|
|
73
|
+
],
|
|
67
74
|
["of", 0, (_, args) => wrap(Uint8Array.from(args, coerceToNumber))],
|
|
68
75
|
["fromBase64", 1, (_, args) => decode("fromBase64", args, (text) => Uint8Array.fromBase64(text))],
|
|
69
76
|
["fromHex", 1, (_, args) => decode("fromHex", args, (text) => Uint8Array.fromHex(text))],
|
|
@@ -90,16 +97,15 @@ export const uint8ArrayGlobal = (ctx) => {
|
|
|
90
97
|
[
|
|
91
98
|
"set",
|
|
92
99
|
1,
|
|
93
|
-
(thisValue, args)
|
|
100
|
+
function* (thisValue, args) {
|
|
94
101
|
const target = self(thisValue, "set");
|
|
95
102
|
const offset = optNumber(args[1]) ?? 0;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
});
|
|
103
|
+
const source = yield* collectBytes(ctx, args[0], "Uint8Array.set");
|
|
104
|
+
if (!Number.isInteger(offset) || offset < 0 || source.length + offset > target.bytes.length) {
|
|
105
|
+
throw rangeError("Uint8Array.set: the source does not fit at that offset.");
|
|
106
|
+
}
|
|
107
|
+
target.bytes.set(source, offset);
|
|
108
|
+
return undefined;
|
|
103
109
|
},
|
|
104
110
|
],
|
|
105
111
|
[
|
|
@@ -123,17 +129,16 @@ export const uint8ArrayGlobal = (ctx) => {
|
|
|
123
129
|
[
|
|
124
130
|
"sort",
|
|
125
131
|
1,
|
|
126
|
-
(thisValue, args)
|
|
132
|
+
function* (thisValue, args) {
|
|
127
133
|
const target = self(thisValue, "sort");
|
|
128
134
|
// Without a comparator, typed arrays sort numerically rather than by string.
|
|
129
135
|
if (args[0] === undefined) {
|
|
130
136
|
target.bytes.sort();
|
|
131
137
|
return target;
|
|
132
138
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
});
|
|
139
|
+
const sorted = yield* sortArray(ctx, [...target.bytes], args[0], "Uint8Array.sort");
|
|
140
|
+
target.bytes.set(Uint8Array.from(sorted, coerceToNumber));
|
|
141
|
+
return target;
|
|
137
142
|
},
|
|
138
143
|
],
|
|
139
144
|
["indexOf", 1, (thisValue, args) => self(thisValue, "indexOf").bytes.indexOf(search(args[0]), optNumber(args[1]))],
|
|
@@ -189,7 +194,7 @@ export const textEncoderGlobal = (ctx) => {
|
|
|
189
194
|
return constructor(builtins, proto, {
|
|
190
195
|
name: "TextEncoder",
|
|
191
196
|
call: requiresNew("TextEncoder"),
|
|
192
|
-
construct: (_, newTarget) =>
|
|
197
|
+
construct: (_, newTarget) => new Obj(prototypeFrom(newTarget, proto)),
|
|
193
198
|
});
|
|
194
199
|
};
|
|
195
200
|
/** A `TextDecoder` holding the host decoder its label and options configured. */
|
|
@@ -232,13 +237,13 @@ export const textDecoderGlobal = (ctx) => {
|
|
|
232
237
|
return constructor(builtins, proto, {
|
|
233
238
|
name: "TextDecoder",
|
|
234
239
|
call: requiresNew("TextDecoder"),
|
|
235
|
-
construct: (args, newTarget) =>
|
|
240
|
+
construct: (args, newTarget) => {
|
|
236
241
|
const label = args[0] === undefined ? "utf-8" : coerceToString(args[0]).trim().toLowerCase();
|
|
237
242
|
if (!utf8Labels.has(label))
|
|
238
243
|
throw rangeError(`The "${label}" encoding is not supported; only UTF-8 is.`);
|
|
239
244
|
const options = args[1] instanceof Obj ? args[1] : undefined;
|
|
240
245
|
const flag = (name) => options !== undefined && Boolean(get(options, name));
|
|
241
246
|
return new TextDecoderObj(prototypeFrom(newTarget, proto), new TextDecoder("utf-8", { fatal: flag("fatal"), ignoreBOM: flag("ignoreBOM") }));
|
|
242
|
-
}
|
|
247
|
+
},
|
|
243
248
|
});
|
|
244
249
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
2
2
|
/** `Map.groupBy` and `Object.groupBy`: the same iteration, keyed into a Map or a data object. */
|
|
3
|
-
export declare const groupBy: <R>(ctx: Interpreter<R>, namespace: "Map" | "Object") => import("../interpreter/objects.js").Native
|
|
4
|
-
export declare const mapGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
5
|
-
export declare const setGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
6
|
-
export declare const weakMapGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
7
|
-
export declare const weakSetGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
3
|
+
export declare const groupBy: <R>(ctx: Interpreter<R>, namespace: "Map" | "Object") => import("../interpreter/objects.js").Native;
|
|
4
|
+
export declare const mapGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
|
5
|
+
export declare const setGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
|
6
|
+
export declare const weakMapGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
|
7
|
+
export declare const weakSetGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|