@opencode/codemode 2.2.6 → 2.3.0
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 +115 -29
- package/dist/{vm/async.d.ts → async.d.ts} +1 -1
- package/dist/codemode.d.ts +13 -22
- package/dist/codemode.js +18 -39
- package/dist/data.d.ts +4 -19
- package/dist/data.js +1 -82
- package/dist/extension.d.ts +20 -1
- package/dist/extension.js +14 -1
- package/dist/{vm/host.d.ts → host.d.ts} +5 -2
- package/dist/{vm/host.js → host.js} +9 -4
- package/dist/index.d.ts +1 -1
- package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
- package/dist/{vm/prelude → prelude}/helpers.js +1 -1
- package/dist/{vm/runtime → runtime}/driver.d.ts +18 -8
- package/dist/{vm/runtime → runtime}/driver.js +90 -20
- package/dist/{vm/runtime → runtime}/errors.js +2 -2
- package/dist/{vm/runtime → runtime}/execute.d.ts +18 -9
- package/dist/{vm/runtime → runtime}/execute.js +45 -15
- package/dist/{vm/runtime → runtime}/extensions.d.ts +2 -2
- package/dist/{vm/runtime → runtime}/extensions.js +121 -19
- package/dist/{vm/runtime → runtime}/globals.js +1 -1
- package/dist/{vm/runtime → runtime}/interpreter.d.ts +4 -1
- package/dist/{vm/runtime → runtime}/interpreter.js +2 -0
- package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.js +1 -1
- package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/operations.js +1 -1
- package/dist/scan.js +2 -2
- package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +16 -4
- package/dist/{vm/scheduler.js → scheduler.js} +13 -2
- package/dist/{vm/snapshot → snapshot}/codec.d.ts +37 -6
- package/dist/{vm/snapshot → snapshot}/codec.js +112 -8
- package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +13 -6
- package/dist/{vm/snapshot → snapshot}/snapshot.js +19 -4
- package/dist/{vm/snapshot.js → snapshot.js} +5 -2
- package/dist/stdlib/array.d.ts +14 -9
- package/dist/stdlib/array.js +89 -276
- package/dist/stdlib/bytes.d.ts +11 -7
- package/dist/stdlib/bytes.js +112 -79
- package/dist/stdlib/collections.d.ts +7 -7
- package/dist/stdlib/collections.js +30 -339
- package/dist/stdlib/console.d.ts +2 -2
- package/dist/stdlib/console.js +3 -3
- package/dist/stdlib/date.d.ts +2 -2
- package/dist/stdlib/date.js +21 -30
- package/dist/stdlib/headers.d.ts +2 -2
- package/dist/stdlib/headers.js +15 -33
- package/dist/stdlib/iterator.d.ts +2 -2
- package/dist/stdlib/iterator.js +10 -303
- package/dist/stdlib/json.d.ts +2 -2
- package/dist/stdlib/json.js +15 -31
- package/dist/stdlib/math.d.ts +2 -7
- package/dist/stdlib/math.js +6 -30
- package/dist/stdlib/number.d.ts +3 -3
- package/dist/stdlib/number.js +8 -8
- package/dist/stdlib/object.d.ts +4 -4
- package/dist/stdlib/object.js +12 -30
- package/dist/stdlib/regexp.d.ts +6 -5
- package/dist/stdlib/regexp.js +18 -13
- package/dist/stdlib/string.d.ts +2 -2
- package/dist/stdlib/string.js +53 -53
- package/dist/stdlib/url.d.ts +21 -9
- package/dist/stdlib/url.js +55 -55
- package/dist/stdlib/value.d.ts +3 -4
- package/dist/stdlib/value.js +5 -6
- package/dist/stdlib/web.d.ts +4 -4
- package/dist/stdlib/web.js +7 -7
- package/dist/tool-runtime.d.ts +34 -12
- package/dist/tool-runtime.js +350 -68
- package/dist/tool-schema.d.ts +5 -0
- package/dist/tool-schema.js +36 -16
- package/dist/tool.d.ts +60 -2
- package/dist/tool.js +17 -1
- package/dist/{vm/vm.d.ts → vm.d.ts} +4 -3
- package/dist/{vm/vm.js → vm.js} +1 -1
- package/package.json +5 -7
- package/dist/interpreter/callback.d.ts +0 -24
- package/dist/interpreter/callback.js +0 -86
- package/dist/interpreter/errors.js +0 -173
- package/dist/interpreter/execute.d.ts +0 -14
- package/dist/interpreter/execute.js +0 -207
- package/dist/interpreter/extensions.d.ts +0 -9
- package/dist/interpreter/extensions.js +0 -248
- package/dist/interpreter/generators.d.ts +0 -3
- package/dist/interpreter/generators.js +0 -168
- package/dist/interpreter/globals.js +0 -144
- package/dist/interpreter/interpreter.d.ts +0 -48
- package/dist/interpreter/interpreter.js +0 -2253
- package/dist/interpreter/intrinsics.d.ts +0 -15
- package/dist/interpreter/intrinsics.js +0 -105
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/native.d.ts +0 -17
- package/dist/interpreter/native.js +0 -35
- package/dist/interpreter/objects.d.ts +0 -301
- package/dist/interpreter/objects.js +0 -592
- package/dist/interpreter/promises.d.ts +0 -18
- package/dist/interpreter/promises.js +0 -292
- package/dist/interpreter/runner.d.ts +0 -188
- package/dist/interpreter/runner.js +0 -492
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/suspend-error.d.ts +0 -23
- package/dist/suspend-error.js +0 -25
- package/dist/vm/data.d.ts +0 -7
- package/dist/vm/data.js +0 -13
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
- package/dist/vm/runtime/errors.d.ts +0 -23
- package/dist/vm/runtime/globals.d.ts +0 -6
- package/dist/vm/runtime/limits.d.ts +0 -10
- package/dist/vm/runtime/limits.js +0 -19
- package/dist/vm/runtime/modules.d.ts +0 -4
- package/dist/vm/runtime/modules.js +0 -33
- package/dist/vm/runtime/references.d.ts +0 -8
- package/dist/vm/runtime/references.js +0 -51
- package/dist/vm/stdlib/array.d.ts +0 -16
- package/dist/vm/stdlib/array.js +0 -311
- package/dist/vm/stdlib/bytes.d.ts +0 -11
- package/dist/vm/stdlib/bytes.js +0 -222
- package/dist/vm/stdlib/collections.d.ts +0 -7
- package/dist/vm/stdlib/collections.js +0 -188
- package/dist/vm/stdlib/console.d.ts +0 -6
- package/dist/vm/stdlib/console.js +0 -93
- package/dist/vm/stdlib/date.d.ts +0 -2
- package/dist/vm/stdlib/date.js +0 -123
- package/dist/vm/stdlib/headers.d.ts +0 -2
- package/dist/vm/stdlib/headers.js +0 -92
- package/dist/vm/stdlib/iterator.d.ts +0 -2
- package/dist/vm/stdlib/iterator.js +0 -30
- package/dist/vm/stdlib/json.d.ts +0 -3
- package/dist/vm/stdlib/json.js +0 -42
- package/dist/vm/stdlib/math.d.ts +0 -3
- package/dist/vm/stdlib/math.js +0 -73
- package/dist/vm/stdlib/number.d.ts +0 -3
- package/dist/vm/stdlib/number.js +0 -72
- package/dist/vm/stdlib/object.d.ts +0 -6
- package/dist/vm/stdlib/object.js +0 -201
- package/dist/vm/stdlib/regexp.d.ts +0 -8
- package/dist/vm/stdlib/regexp.js +0 -119
- package/dist/vm/stdlib/string.d.ts +0 -2
- package/dist/vm/stdlib/string.js +0 -272
- package/dist/vm/stdlib/url.d.ts +0 -25
- package/dist/vm/stdlib/url.js +0 -255
- package/dist/vm/stdlib/value.d.ts +0 -6
- package/dist/vm/stdlib/value.js +0 -32
- package/dist/vm/stdlib/web.d.ts +0 -5
- package/dist/vm/stdlib/web.js +0 -97
- /package/dist/{vm/async.js → async.js} +0 -0
- /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
- /package/dist/{vm/code.js → code.js} +0 -0
- /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/compile.js +0 -0
- /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/scope.js +0 -0
- /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
- /package/dist/{vm/generators.js → generators.js} +0 -0
- /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
- /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
- /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
- /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
- /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/callback.js +0 -0
- /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.js +0 -0
- /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
- /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
- /package/dist/{interpreter → runtime}/limits.js +0 -0
- /package/dist/{vm/runtime → runtime}/model.js +0 -0
- /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
- /package/dist/{interpreter → runtime}/modules.js +0 -0
- /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/native.js +0 -0
- /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/promises.js +0 -0
- /package/dist/{interpreter → runtime}/references.d.ts +0 -0
- /package/dist/{interpreter → runtime}/references.js +0 -0
- /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/signals.js +0 -0
- /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/table.js +0 -0
- /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
package/dist/stdlib/array.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { Invoke } from "../runtime/signals.js";
|
|
2
|
+
import { builtinCursor } from "../runtime/operations.js";
|
|
3
|
+
import { constructor, methods, prototypeFrom, receiver, iteratorMethods } from "../runtime/native.js";
|
|
4
|
+
import { checkArrayLength, checkStringLength, MAX_ARRAY_LENGTH } from "../runtime/limits.js";
|
|
5
|
+
import { invalidData, IteratorSymbol, rangeError, typeError } from "../runtime/model.js";
|
|
6
|
+
import { changed, define, get, hidden, Arr, Bytes, GeneratorObj, Obj, PromiseObj, coerceToInteger, coerceToNumber, coerceToString, rejectAddition, } from "../runtime/objects.js";
|
|
7
|
+
import { describeValue, rejectCircularInsertion } from "../runtime/references.js";
|
|
8
|
+
import { requireCallback, withPrimitives } from "../runtime/callback.js";
|
|
7
9
|
import { compareText } from "../tool-runtime.js";
|
|
8
10
|
const arrayLikeSource = (source) => {
|
|
9
11
|
// JS would treat a promise as an empty array-like; that would hide a missing `await`.
|
|
@@ -14,64 +16,85 @@ const arrayLikeSource = (source) => {
|
|
|
14
16
|
}
|
|
15
17
|
throw invalidData(`Array.from expects an iterable or array-like value, received ${describeValue(source)}.`);
|
|
16
18
|
};
|
|
17
|
-
|
|
19
|
+
const iterable = (builtins, value) => {
|
|
20
|
+
if (builtinCursor(builtins, value) !== undefined)
|
|
21
|
+
return true;
|
|
22
|
+
if (!(value instanceof Obj))
|
|
23
|
+
return false;
|
|
24
|
+
const method = get(value, IteratorSymbol);
|
|
25
|
+
return method !== undefined && method !== null;
|
|
26
|
+
};
|
|
27
|
+
const arrayFrom = (ctx, args) => {
|
|
18
28
|
const source = args[0];
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
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)}.`);
|
|
25
|
-
}
|
|
26
|
-
const arrayLike = arrayLikeSource(source);
|
|
27
|
-
const values = [];
|
|
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]));
|
|
31
|
-
}
|
|
32
|
-
return new Arr(proto, values);
|
|
29
|
+
const mapper = args.length < 2 || args[1] === undefined ? undefined : requireCallback(args[1], "Array.from");
|
|
30
|
+
if (iterable(ctx.builtins, source)) {
|
|
31
|
+
return new Invoke(ctx.prelude.fn("arrayFromIterable"), undefined, [source, mapper, args[2]], undefined);
|
|
33
32
|
}
|
|
34
|
-
|
|
35
|
-
|
|
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;
|
|
33
|
+
if (source instanceof GeneratorObj) {
|
|
34
|
+
throw typeError(`Array.from expects a synchronous iterable or array-like value, received ${describeValue(source)}.`);
|
|
44
35
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return [...target].sort((a, b) => compareText(coerceToString(a), coerceToString(b)));
|
|
36
|
+
const arrayLike = arrayLikeSource(source);
|
|
37
|
+
if (mapper === undefined) {
|
|
38
|
+
return new Arr(ctx.builtins.Array, Array.from({ length: arrayLike.length }, (_, index) => get(arrayLike.source, index)));
|
|
49
39
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
40
|
+
return new Invoke(ctx.prelude.fn("arrayFromLike"), undefined, [arrayLike.source, arrayLike.length, mapper, args[2]], undefined);
|
|
41
|
+
};
|
|
42
|
+
/** The most values the default sort sorts in one native call; more sort in bounded steps (see `sortDefault`). */
|
|
43
|
+
export const SORT_RUN = 4096;
|
|
44
|
+
/**
|
|
45
|
+
* Sorts `target`'s values: at once without a comparator, unless there are many, otherwise in the prelude
|
|
46
|
+
* (`sortWith`), which calls it. The merges of a top-down merge sort are planned here, in post-order, so the prelude
|
|
47
|
+
* compares in the same order as the recursive sort. Holes and undefined values sort last without being compared.
|
|
48
|
+
*/
|
|
49
|
+
export const sortArray = (ctx, target, comparator, into) => {
|
|
50
|
+
const builtins = ctx.builtins;
|
|
51
|
+
const items = target instanceof Arr ? [...target.items] : [...target.bytes];
|
|
52
|
+
// Counted before any comparator runs; a comparator that changes the array does not change where holes go.
|
|
53
|
+
let holes = 0;
|
|
54
|
+
if (target instanceof Arr)
|
|
55
|
+
for (let index = 0; index < items.length; index++)
|
|
56
|
+
if (!(index in target.items))
|
|
57
|
+
holes++;
|
|
58
|
+
const callback = comparator === undefined ? undefined : requireCallback(comparator, into);
|
|
59
|
+
if (callback === undefined && items.length <= SORT_RUN) {
|
|
60
|
+
return sorted(builtins, target, into, holes, items.sort((a, b) => compareText(coerceToString(a), coerceToString(b))));
|
|
61
|
+
}
|
|
62
|
+
const defined = new Arr(builtins.Array, items.filter((item) => item !== undefined));
|
|
63
|
+
const undefs = items.length - defined.items.length;
|
|
64
|
+
if (callback === undefined) {
|
|
65
|
+
return new Invoke(ctx.prelude.fn("sortDefault"), undefined, [target, into, holes, defined, undefs], undefined);
|
|
66
|
+
}
|
|
67
|
+
const merges = [];
|
|
68
|
+
const plan = (low, high) => {
|
|
69
|
+
if (high - low <= 1)
|
|
70
|
+
return;
|
|
71
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
72
|
+
plan(low, middle);
|
|
73
|
+
plan(middle, high);
|
|
74
|
+
merges.push(low, middle, high);
|
|
69
75
|
};
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
plan(0, defined.items.length);
|
|
77
|
+
const args = [target, into, holes, callback, defined, undefs, new Arr(builtins.Array, merges)];
|
|
78
|
+
return new Invoke(ctx.prelude.fn("sortWith"), undefined, args, undefined);
|
|
79
|
+
};
|
|
80
|
+
export const sorted = (builtins, target, into, holes, result) => {
|
|
81
|
+
if (target instanceof Bytes) {
|
|
82
|
+
target.bytes.set(Uint8Array.from(result, coerceToNumber));
|
|
83
|
+
return target;
|
|
84
|
+
}
|
|
85
|
+
if (into === "Array.toSorted")
|
|
86
|
+
return new Arr(builtins.Array, result);
|
|
87
|
+
changed(target);
|
|
88
|
+
const items = target.items;
|
|
89
|
+
const count = result.length - holes;
|
|
90
|
+
result.slice(0, count).forEach((item, index) => {
|
|
91
|
+
items[index] = item;
|
|
92
|
+
});
|
|
93
|
+
Array.from({ length: holes }, (_, index) => count + index).forEach((index) => {
|
|
94
|
+
delete items[index];
|
|
95
|
+
});
|
|
96
|
+
return target;
|
|
97
|
+
};
|
|
75
98
|
// Array constructs identically with or without new, like JS.
|
|
76
99
|
export const arrayGlobal = (ctx) => {
|
|
77
100
|
const builtins = ctx.builtins;
|
|
@@ -113,6 +136,7 @@ export const arrayGlobal = (ctx) => {
|
|
|
113
136
|
if (!target.elements.writable && (length > 0 || growth !== undefined)) {
|
|
114
137
|
throw typeError(`Cannot assign to read only property '${length > 0 ? 0 : "length"}'.`);
|
|
115
138
|
}
|
|
139
|
+
changed(target);
|
|
116
140
|
return target;
|
|
117
141
|
};
|
|
118
142
|
const optNumber = (value) => (value === undefined ? undefined : coerceToInteger(value));
|
|
@@ -123,7 +147,7 @@ export const arrayGlobal = (ctx) => {
|
|
|
123
147
|
(thisValue, args) => {
|
|
124
148
|
// .map would keep holes, which the conversion would then hand to the body as undefined.
|
|
125
149
|
const parts = Array.from(self(thisValue, "join").items, (item) => item ?? "");
|
|
126
|
-
return withPrimitives(
|
|
150
|
+
return withPrimitives("string", [args[0] === undefined ? "," : args[0], ...parts], ([separator, ...items]) => {
|
|
127
151
|
const joined = items.map(coerceToString).join(coerceToString(separator));
|
|
128
152
|
checkStringLength(joined.length);
|
|
129
153
|
return joined;
|
|
@@ -133,7 +157,7 @@ export const arrayGlobal = (ctx) => {
|
|
|
133
157
|
[
|
|
134
158
|
"toString",
|
|
135
159
|
0,
|
|
136
|
-
(thisValue) => withPrimitives(
|
|
160
|
+
(thisValue) => withPrimitives("string", Array.from(self(thisValue, "toString").items, (item) => item ?? ""), (items) => items.map(coerceToString).join(",")),
|
|
137
161
|
],
|
|
138
162
|
[
|
|
139
163
|
"includes",
|
|
@@ -189,32 +213,8 @@ export const arrayGlobal = (ctx) => {
|
|
|
189
213
|
return target;
|
|
190
214
|
},
|
|
191
215
|
],
|
|
192
|
-
[
|
|
193
|
-
|
|
194
|
-
1,
|
|
195
|
-
function* (thisValue, args) {
|
|
196
|
-
const target = mutable(self(thisValue, "sort"));
|
|
197
|
-
const items = target.items;
|
|
198
|
-
const length = items.length;
|
|
199
|
-
const holeCount = Array.from({ length }, (_, index) => Object.hasOwn(items, index)).filter((o) => !o).length;
|
|
200
|
-
const itemCount = length - holeCount;
|
|
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];
|
|
207
|
-
});
|
|
208
|
-
return target;
|
|
209
|
-
},
|
|
210
|
-
],
|
|
211
|
-
[
|
|
212
|
-
"toSorted",
|
|
213
|
-
1,
|
|
214
|
-
function* (thisValue, args) {
|
|
215
|
-
return wrap(yield* sortArray(ctx, self(thisValue, "toSorted").items, args[0], "Array.toSorted"));
|
|
216
|
-
},
|
|
217
|
-
],
|
|
216
|
+
["sort", 1, (thisValue, args) => sortArray(ctx, mutable(self(thisValue, "sort")), args[0], "Array.sort")],
|
|
217
|
+
["toSorted", 1, (thisValue, args) => sortArray(ctx, self(thisValue, "toSorted"), args[0], "Array.toSorted")],
|
|
218
218
|
["toReversed", 0, (thisValue) => wrap([...self(thisValue, "toReversed").items].reverse())],
|
|
219
219
|
[
|
|
220
220
|
"with",
|
|
@@ -303,196 +303,9 @@ export const arrayGlobal = (ctx) => {
|
|
|
303
303
|
return target;
|
|
304
304
|
},
|
|
305
305
|
],
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
0,
|
|
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
|
-
},
|
|
321
|
-
],
|
|
322
|
-
["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").items.keys())],
|
|
323
|
-
["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").items.values())],
|
|
324
|
-
[
|
|
325
|
-
"entries",
|
|
326
|
-
0,
|
|
327
|
-
(thisValue) => hostIterator(builtins, self(thisValue, "entries")
|
|
328
|
-
.items.entries()
|
|
329
|
-
.map(([index, item]) => wrap([index, item]))),
|
|
330
|
-
],
|
|
331
|
-
[
|
|
332
|
-
"flatMap",
|
|
333
|
-
1,
|
|
334
|
-
function* (thisValue, args) {
|
|
335
|
-
const target = self(thisValue, "flatMap");
|
|
336
|
-
const apply = applyCollectionCallback(ctx, args[0], "Array.flatMap");
|
|
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;
|
|
347
|
-
}
|
|
348
|
-
values.push(mapped);
|
|
349
|
-
}
|
|
350
|
-
return wrap(values);
|
|
351
|
-
},
|
|
352
|
-
],
|
|
353
|
-
...callbackMethods(ctx, "Array", self, (target) => target.items, wrap),
|
|
306
|
+
...iteratorMethods(builtins, self),
|
|
307
|
+
// The callback methods are the prelude's.
|
|
354
308
|
]);
|
|
355
309
|
define(proto, IteratorSymbol, get(proto, "values"), hidden);
|
|
356
310
|
return array;
|
|
357
311
|
};
|
|
358
|
-
/**
|
|
359
|
-
* The callback methods Array and Uint8Array share. They fix the iteration length while reading existing elements
|
|
360
|
-
* live; `wrap` builds the collection `map` and `filter` return.
|
|
361
|
-
*/
|
|
362
|
-
export const callbackMethods = (ctx, label, self, elements, wrap) => {
|
|
363
|
-
const iterate = (name, length, body) => [
|
|
364
|
-
name,
|
|
365
|
-
length,
|
|
366
|
-
(thisValue, args) => {
|
|
367
|
-
const target = self(thisValue, name);
|
|
368
|
-
const call = applyCollectionCallback(ctx, args[0], `${label}.${name}`);
|
|
369
|
-
// reduce and reduceRight take an initial value where the others take a thisArg.
|
|
370
|
-
const thisArg = name.startsWith("reduce") ? undefined : args[1];
|
|
371
|
-
return body(elements(target), target, (callbackArgs) => call(callbackArgs, thisArg), args);
|
|
372
|
-
},
|
|
373
|
-
];
|
|
374
|
-
return [
|
|
375
|
-
iterate("map", 1, function* (target, receiver, apply) {
|
|
376
|
-
const length = target.length;
|
|
377
|
-
const values = [];
|
|
378
|
-
values.length = length;
|
|
379
|
-
for (let index = 0; index < length; index += 1) {
|
|
380
|
-
if (!(index in target))
|
|
381
|
-
continue;
|
|
382
|
-
values[index] = yield* apply([target[index], index, receiver]);
|
|
383
|
-
}
|
|
384
|
-
return wrap(values);
|
|
385
|
-
}),
|
|
386
|
-
iterate("filter", 1, function* (target, receiver, apply) {
|
|
387
|
-
const length = target.length;
|
|
388
|
-
const values = [];
|
|
389
|
-
for (let index = 0; index < length; index += 1) {
|
|
390
|
-
if (!(index in target))
|
|
391
|
-
continue;
|
|
392
|
-
const item = target[index];
|
|
393
|
-
if (yield* apply([item, index, receiver]))
|
|
394
|
-
values.push(item);
|
|
395
|
-
}
|
|
396
|
-
return wrap(values);
|
|
397
|
-
}),
|
|
398
|
-
iterate("find", 1, function* (target, receiver, apply) {
|
|
399
|
-
const length = target.length;
|
|
400
|
-
for (let index = 0; index < length; index += 1) {
|
|
401
|
-
const item = target[index];
|
|
402
|
-
if (yield* apply([item, index, receiver]))
|
|
403
|
-
return item;
|
|
404
|
-
}
|
|
405
|
-
return undefined;
|
|
406
|
-
}),
|
|
407
|
-
iterate("findIndex", 1, function* (target, receiver, apply) {
|
|
408
|
-
const length = target.length;
|
|
409
|
-
for (let index = 0; index < length; index += 1) {
|
|
410
|
-
if (yield* apply([target[index], index, receiver]))
|
|
411
|
-
return index;
|
|
412
|
-
}
|
|
413
|
-
return -1;
|
|
414
|
-
}),
|
|
415
|
-
iterate("findLast", 1, function* (target, receiver, apply) {
|
|
416
|
-
for (let index = target.length - 1; index >= 0; index -= 1) {
|
|
417
|
-
const item = target[index];
|
|
418
|
-
if (yield* apply([item, index, receiver]))
|
|
419
|
-
return item;
|
|
420
|
-
}
|
|
421
|
-
return undefined;
|
|
422
|
-
}),
|
|
423
|
-
iterate("findLastIndex", 1, function* (target, receiver, apply) {
|
|
424
|
-
for (let index = target.length - 1; index >= 0; index -= 1) {
|
|
425
|
-
if (yield* apply([target[index], index, receiver]))
|
|
426
|
-
return index;
|
|
427
|
-
}
|
|
428
|
-
return -1;
|
|
429
|
-
}),
|
|
430
|
-
iterate("some", 1, function* (target, receiver, apply) {
|
|
431
|
-
const length = target.length;
|
|
432
|
-
for (let index = 0; index < length; index += 1) {
|
|
433
|
-
if (!(index in target))
|
|
434
|
-
continue;
|
|
435
|
-
if (yield* apply([target[index], index, receiver]))
|
|
436
|
-
return true;
|
|
437
|
-
}
|
|
438
|
-
return false;
|
|
439
|
-
}),
|
|
440
|
-
iterate("every", 1, function* (target, receiver, apply) {
|
|
441
|
-
const length = target.length;
|
|
442
|
-
for (let index = 0; index < length; index += 1) {
|
|
443
|
-
if (!(index in target))
|
|
444
|
-
continue;
|
|
445
|
-
if (!(yield* apply([target[index], index, receiver])))
|
|
446
|
-
return false;
|
|
447
|
-
}
|
|
448
|
-
return true;
|
|
449
|
-
}),
|
|
450
|
-
iterate("forEach", 1, function* (target, receiver, apply) {
|
|
451
|
-
const length = target.length;
|
|
452
|
-
for (let index = 0; index < length; index += 1) {
|
|
453
|
-
if (index in target)
|
|
454
|
-
yield* apply([target[index], index, receiver]);
|
|
455
|
-
}
|
|
456
|
-
return undefined;
|
|
457
|
-
}),
|
|
458
|
-
iterate("reduce", 1, function* (target, receiver, apply, args) {
|
|
459
|
-
const length = target.length;
|
|
460
|
-
let start = 0;
|
|
461
|
-
let accumulator = args[1];
|
|
462
|
-
if (args.length < 2) {
|
|
463
|
-
while (start < length && !(start in target))
|
|
464
|
-
start += 1;
|
|
465
|
-
if (start === length) {
|
|
466
|
-
throw typeError(`${label}.reduce of an empty array with no initial value.`);
|
|
467
|
-
}
|
|
468
|
-
accumulator = target[start];
|
|
469
|
-
start += 1;
|
|
470
|
-
}
|
|
471
|
-
for (let index = start; index < length; index += 1) {
|
|
472
|
-
if (!(index in target))
|
|
473
|
-
continue;
|
|
474
|
-
accumulator = yield* apply([accumulator, target[index], index, receiver]);
|
|
475
|
-
}
|
|
476
|
-
return accumulator;
|
|
477
|
-
}),
|
|
478
|
-
iterate("reduceRight", 1, function* (target, receiver, apply, args) {
|
|
479
|
-
let start = target.length - 1;
|
|
480
|
-
let accumulator = args[1];
|
|
481
|
-
if (args.length < 2) {
|
|
482
|
-
while (start >= 0 && !(start in target))
|
|
483
|
-
start -= 1;
|
|
484
|
-
if (start < 0) {
|
|
485
|
-
throw typeError(`${label}.reduceRight of an empty array with no initial value.`);
|
|
486
|
-
}
|
|
487
|
-
accumulator = target[start];
|
|
488
|
-
start -= 1;
|
|
489
|
-
}
|
|
490
|
-
for (let index = start; index >= 0; index -= 1) {
|
|
491
|
-
if (!(index in target))
|
|
492
|
-
continue;
|
|
493
|
-
accumulator = yield* apply([accumulator, target[index], index, receiver]);
|
|
494
|
-
}
|
|
495
|
-
return accumulator;
|
|
496
|
-
}),
|
|
497
|
-
];
|
|
498
|
-
};
|
package/dist/stdlib/bytes.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { Obj } from "../
|
|
2
|
-
import type { Interpreter } from "../
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
5
|
-
|
|
1
|
+
import { Obj, type Value } from "../runtime/objects.js";
|
|
2
|
+
import type { Interpreter } from "../runtime/interpreter.js";
|
|
3
|
+
export declare const finishBytes: (bytes: Uint8Array, into: Obj, offset: number) => Value;
|
|
4
|
+
export declare const uint8ArrayGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
|
|
5
|
+
export declare const textEncoderGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
|
|
6
6
|
export declare class TextDecoderObj extends Obj {
|
|
7
7
|
readonly decoder: TextDecoder;
|
|
8
|
-
|
|
8
|
+
readonly ignoreBOM: boolean;
|
|
9
|
+
queued: Uint8Array;
|
|
10
|
+
seen: boolean;
|
|
11
|
+
constructor(proto: Obj, decoder: TextDecoder, ignoreBOM: boolean);
|
|
12
|
+
decode(bytes: Uint8Array, stream: boolean): string;
|
|
9
13
|
}
|
|
10
|
-
export declare const textDecoderGlobal: <R>(ctx: Interpreter<R>) => import("../
|
|
14
|
+
export declare const textDecoderGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
|