@opencode/codemode 0.0.0-dev-1180 → 0.0.0-dev-1189
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 +6 -12
- package/dist/codemode.d.ts +7 -21
- package/dist/codemode.js +17 -38
- package/dist/extension.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/scan.js +2 -2
- package/dist/tool-runtime.d.ts +8 -11
- package/dist/tool-runtime.js +13 -11
- package/dist/tool.d.ts +18 -9
- package/dist/tool.js +15 -23
- package/dist/vm/data.d.ts +1 -1
- package/dist/vm/runtime/execute.d.ts +16 -6
- package/dist/vm/runtime/execute.js +10 -7
- package/dist/vm/snapshot/codec.d.ts +3 -2
- package/dist/vm/snapshot/codec.js +3 -2
- package/dist/vm/snapshot/snapshot.d.ts +6 -3
- package/dist/vm/snapshot/snapshot.js +13 -2
- package/dist/vm/vm.d.ts +2 -2
- package/package.json +3 -5
- package/dist/data.d.ts +0 -21
- package/dist/data.js +0 -93
- package/dist/interpreter/callback.d.ts +0 -35
- package/dist/interpreter/callback.js +0 -98
- package/dist/interpreter/errors.d.ts +0 -23
- 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.d.ts +0 -6
- 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/limits.d.ts +0 -10
- package/dist/interpreter/limits.js +0 -19
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/modules.d.ts +0 -4
- package/dist/interpreter/modules.js +0 -33
- 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/references.d.ts +0 -8
- package/dist/interpreter/references.js +0 -51
- package/dist/interpreter/runner.d.ts +0 -178
- package/dist/interpreter/runner.js +0 -480
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/stdlib/array.d.ts +0 -11
- package/dist/stdlib/array.js +0 -498
- package/dist/stdlib/bytes.d.ts +0 -10
- package/dist/stdlib/bytes.js +0 -253
- package/dist/stdlib/collections.d.ts +0 -7
- package/dist/stdlib/collections.js +0 -497
- package/dist/stdlib/console.d.ts +0 -6
- package/dist/stdlib/console.js +0 -93
- package/dist/stdlib/date.d.ts +0 -2
- package/dist/stdlib/date.js +0 -132
- package/dist/stdlib/headers.d.ts +0 -2
- package/dist/stdlib/headers.js +0 -110
- package/dist/stdlib/iterator.d.ts +0 -2
- package/dist/stdlib/iterator.js +0 -322
- package/dist/stdlib/json.d.ts +0 -3
- package/dist/stdlib/json.js +0 -58
- package/dist/stdlib/math.d.ts +0 -8
- package/dist/stdlib/math.js +0 -97
- package/dist/stdlib/number.d.ts +0 -3
- package/dist/stdlib/number.js +0 -72
- package/dist/stdlib/object.d.ts +0 -6
- package/dist/stdlib/object.js +0 -219
- package/dist/stdlib/regexp.d.ts +0 -7
- package/dist/stdlib/regexp.js +0 -114
- package/dist/stdlib/string.d.ts +0 -2
- package/dist/stdlib/string.js +0 -272
- package/dist/stdlib/url.d.ts +0 -13
- package/dist/stdlib/url.js +0 -255
- package/dist/stdlib/value.d.ts +0 -7
- package/dist/stdlib/value.js +0 -33
- package/dist/stdlib/web.d.ts +0 -5
- package/dist/stdlib/web.js +0 -97
- package/dist/suspend-error.d.ts +0 -24
- package/dist/suspend-error.js +0 -25
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
package/dist/stdlib/date.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
|
|
2
|
-
import { rangeError } from "../interpreter/model.js";
|
|
3
|
-
import { DateObj, Obj, coerceToNumber, coerceToString } from "../interpreter/objects.js";
|
|
4
|
-
import { toPrimitive, toPrimitiveNumber } from "../interpreter/callback.js";
|
|
5
|
-
function* constructDate(ctx, args, proto) {
|
|
6
|
-
if (args.length === 0)
|
|
7
|
-
return new DateObj(proto, Date.now());
|
|
8
|
-
if (args.length === 1) {
|
|
9
|
-
const arg = args[0];
|
|
10
|
-
if (arg instanceof DateObj)
|
|
11
|
-
return new DateObj(proto, arg.time);
|
|
12
|
-
const value = yield* toPrimitive(ctx, arg, "default");
|
|
13
|
-
return typeof value === "string"
|
|
14
|
-
? new DateObj(proto, Date.parse(value))
|
|
15
|
-
: new DateObj(proto, new Date(coerceToNumber(value)).getTime());
|
|
16
|
-
}
|
|
17
|
-
// The spec converts at most seven components, in order, so extra arguments never run program code.
|
|
18
|
-
const parts = yield* toNumbers(ctx, args.slice(0, 7));
|
|
19
|
-
return new DateObj(proto, new Date(...parts).getTime());
|
|
20
|
-
}
|
|
21
|
-
// ToNumber on each value in turn, so a symbol throws before any later value's conversion runs program code.
|
|
22
|
-
function* toNumbers(ctx, values) {
|
|
23
|
-
const numbers = [];
|
|
24
|
-
for (const value of values)
|
|
25
|
-
numbers.push(yield* toPrimitiveNumber(ctx, value));
|
|
26
|
-
return numbers;
|
|
27
|
-
}
|
|
28
|
-
const getters = [
|
|
29
|
-
"getFullYear",
|
|
30
|
-
"getMonth",
|
|
31
|
-
"getDate",
|
|
32
|
-
"getDay",
|
|
33
|
-
"getHours",
|
|
34
|
-
"getMinutes",
|
|
35
|
-
"getSeconds",
|
|
36
|
-
"getMilliseconds",
|
|
37
|
-
"getUTCFullYear",
|
|
38
|
-
"getUTCMonth",
|
|
39
|
-
"getUTCDate",
|
|
40
|
-
"getUTCDay",
|
|
41
|
-
"getUTCHours",
|
|
42
|
-
"getUTCMinutes",
|
|
43
|
-
"getUTCSeconds",
|
|
44
|
-
"getUTCMilliseconds",
|
|
45
|
-
"getTimezoneOffset",
|
|
46
|
-
];
|
|
47
|
-
const setters = [
|
|
48
|
-
["setTime", 1],
|
|
49
|
-
["setMilliseconds", 1],
|
|
50
|
-
["setUTCMilliseconds", 1],
|
|
51
|
-
["setSeconds", 2],
|
|
52
|
-
["setUTCSeconds", 2],
|
|
53
|
-
["setMinutes", 3],
|
|
54
|
-
["setUTCMinutes", 3],
|
|
55
|
-
["setHours", 4],
|
|
56
|
-
["setUTCHours", 4],
|
|
57
|
-
["setDate", 1],
|
|
58
|
-
["setUTCDate", 1],
|
|
59
|
-
["setMonth", 2],
|
|
60
|
-
["setUTCMonth", 2],
|
|
61
|
-
["setFullYear", 3],
|
|
62
|
-
["setUTCFullYear", 3],
|
|
63
|
-
];
|
|
64
|
-
export const dateGlobal = (ctx) => {
|
|
65
|
-
const builtins = ctx.builtins;
|
|
66
|
-
const proto = builtins.Date;
|
|
67
|
-
const date = constructor(builtins, proto, {
|
|
68
|
-
name: "Date",
|
|
69
|
-
length: 7,
|
|
70
|
-
// ISO instead of the host's locale string: date strings are deterministic and must not leak the host timezone.
|
|
71
|
-
call: () => new Date().toISOString(),
|
|
72
|
-
construct: (args, newTarget) => constructDate(ctx, args, prototypeFrom(newTarget, proto)),
|
|
73
|
-
});
|
|
74
|
-
methods(builtins, date, [
|
|
75
|
-
["now", 0, () => Date.now()],
|
|
76
|
-
["parse", 1, (_, args) => Date.parse(coerceToString(args[0]))],
|
|
77
|
-
[
|
|
78
|
-
"UTC",
|
|
79
|
-
7,
|
|
80
|
-
function* (_, args) {
|
|
81
|
-
const parts = yield* toNumbers(ctx, args.slice(0, 7));
|
|
82
|
-
return Date.UTC(...parts);
|
|
83
|
-
},
|
|
84
|
-
],
|
|
85
|
-
]);
|
|
86
|
-
const self = (thisValue, name) => receiver(DateObj, thisValue, `Date.prototype.${name}`);
|
|
87
|
-
const iso = (value) => {
|
|
88
|
-
if (!Number.isFinite(value.time))
|
|
89
|
-
throw rangeError("Invalid time value.");
|
|
90
|
-
return new Date(value.time).toISOString();
|
|
91
|
-
};
|
|
92
|
-
methods(builtins, proto, [
|
|
93
|
-
["getTime", 0, (thisValue) => self(thisValue, "getTime").time],
|
|
94
|
-
["valueOf", 0, (thisValue) => self(thisValue, "valueOf").time],
|
|
95
|
-
["toISOString", 0, (thisValue) => iso(self(thisValue, "toISOString"))],
|
|
96
|
-
[
|
|
97
|
-
"toJSON",
|
|
98
|
-
1,
|
|
99
|
-
(thisValue) => {
|
|
100
|
-
const value = self(thisValue, "toJSON");
|
|
101
|
-
return Number.isFinite(value.time) ? iso(value) : null;
|
|
102
|
-
},
|
|
103
|
-
],
|
|
104
|
-
["toString", 0, (thisValue) => coerceToString(self(thisValue, "toString"))],
|
|
105
|
-
["toDateString", 0, (thisValue) => new Date(self(thisValue, "toDateString").time).toDateString()],
|
|
106
|
-
["toTimeString", 0, (thisValue) => new Date(self(thisValue, "toTimeString").time).toTimeString()],
|
|
107
|
-
["toUTCString", 0, (thisValue) => new Date(self(thisValue, "toUTCString").time).toUTCString()],
|
|
108
|
-
["toGMTString", 0, (thisValue) => new Date(self(thisValue, "toGMTString").time).toUTCString()],
|
|
109
|
-
...["toLocaleString", "toLocaleDateString", "toLocaleTimeString"].map((name) => [
|
|
110
|
-
name,
|
|
111
|
-
0,
|
|
112
|
-
(thisValue) => new Date(self(thisValue, name).time)[name]("en-US", { timeZone: "UTC" }),
|
|
113
|
-
]),
|
|
114
|
-
...getters.map((name) => [name, 0, (thisValue) => new Date(self(thisValue, name).time)[name]()]),
|
|
115
|
-
...setters.map(([name, length]) => [
|
|
116
|
-
name,
|
|
117
|
-
length,
|
|
118
|
-
function* (thisValue, args) {
|
|
119
|
-
const target = self(thisValue, name);
|
|
120
|
-
// Native setters read the current time before argument coercion, whose callbacks may mutate the Date.
|
|
121
|
-
const hosted = new Date(target.time);
|
|
122
|
-
const values = yield* toNumbers(ctx, args.slice(0, length));
|
|
123
|
-
// Every setter but setTime and setFullYear leaves an invalid Date untouched and answers NaN.
|
|
124
|
-
if (Number.isNaN(hosted.getTime()) && name !== "setTime" && !name.endsWith("FullYear"))
|
|
125
|
-
return NaN;
|
|
126
|
-
target.time = hosted[name](...values);
|
|
127
|
-
return target.time;
|
|
128
|
-
},
|
|
129
|
-
]),
|
|
130
|
-
]);
|
|
131
|
-
return date;
|
|
132
|
-
};
|
package/dist/stdlib/headers.d.ts
DELETED
package/dist/stdlib/headers.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { constructor, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
|
|
2
|
-
import { IteratorSymbol, typeError } from "../interpreter/model.js";
|
|
3
|
-
import { define, entries, get, hidden, Arr, HeadersObj, hostIterator, Obj, coerceToString, isRuntimeReference, } from "../interpreter/objects.js";
|
|
4
|
-
import { applyCollectionCallback } from "../interpreter/callback.js";
|
|
5
|
-
import { readPairs } from "./url.js";
|
|
6
|
-
// The host validates header names and values and throws its own TypeError; the program gets one of its own.
|
|
7
|
-
const attempt = (run) => {
|
|
8
|
-
try {
|
|
9
|
-
return run();
|
|
10
|
-
}
|
|
11
|
-
catch (error) {
|
|
12
|
-
throw typeError(error instanceof Error ? error.message : String(error));
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
function* constructHeaders(ctx, init, proto) {
|
|
16
|
-
const wrap = (headers) => new HeadersObj(proto, headers);
|
|
17
|
-
if (init === undefined)
|
|
18
|
-
return wrap(new Headers());
|
|
19
|
-
const pairs = init instanceof Obj ? yield* readPairs(ctx, init, "new Headers(...)") : undefined;
|
|
20
|
-
if (pairs !== undefined)
|
|
21
|
-
return wrap(attempt(() => new Headers(pairs)));
|
|
22
|
-
if (!(init instanceof Obj) || isRuntimeReference(init)) {
|
|
23
|
-
throw typeError("new Headers(...) expects a record of names to values, iterable [name, value] pairs, or Headers.");
|
|
24
|
-
}
|
|
25
|
-
return wrap(attempt(() => new Headers(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)])))));
|
|
26
|
-
}
|
|
27
|
-
export const headersGlobal = (ctx) => {
|
|
28
|
-
const builtins = ctx.builtins;
|
|
29
|
-
const proto = builtins.Headers;
|
|
30
|
-
const headers = constructor(builtins, proto, {
|
|
31
|
-
name: "Headers",
|
|
32
|
-
call: requiresNew("Headers"),
|
|
33
|
-
construct: (args, newTarget) => constructHeaders(ctx, args[0], prototypeFrom(newTarget, proto)),
|
|
34
|
-
});
|
|
35
|
-
const self = (thisValue, name) => receiver(HeadersObj, thisValue, `Headers.prototype.${name}`);
|
|
36
|
-
const wrap = (items) => new Arr(builtins.Array, items);
|
|
37
|
-
const arg = (args, index) => coerceToString(args[index]);
|
|
38
|
-
const requireArgs = (name, args, count) => {
|
|
39
|
-
if (args.length < count)
|
|
40
|
-
throw typeError(`Headers.${name} requires ${count} argument${count === 1 ? "" : "s"}.`);
|
|
41
|
-
};
|
|
42
|
-
methods(builtins, proto, [
|
|
43
|
-
[
|
|
44
|
-
"append",
|
|
45
|
-
2,
|
|
46
|
-
(thisValue, args) => {
|
|
47
|
-
requireArgs("append", args, 2);
|
|
48
|
-
const target = self(thisValue, "append").headers;
|
|
49
|
-
attempt(() => target.append(arg(args, 0), arg(args, 1)));
|
|
50
|
-
return undefined;
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
[
|
|
54
|
-
"delete",
|
|
55
|
-
1,
|
|
56
|
-
(thisValue, args) => {
|
|
57
|
-
requireArgs("delete", args, 1);
|
|
58
|
-
const target = self(thisValue, "delete").headers;
|
|
59
|
-
attempt(() => target.delete(arg(args, 0)));
|
|
60
|
-
return undefined;
|
|
61
|
-
},
|
|
62
|
-
],
|
|
63
|
-
[
|
|
64
|
-
"get",
|
|
65
|
-
1,
|
|
66
|
-
(thisValue, args) => {
|
|
67
|
-
requireArgs("get", args, 1);
|
|
68
|
-
const target = self(thisValue, "get").headers;
|
|
69
|
-
return attempt(() => target.get(arg(args, 0)));
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
["getSetCookie", 0, (thisValue) => wrap(self(thisValue, "getSetCookie").headers.getSetCookie())],
|
|
73
|
-
[
|
|
74
|
-
"has",
|
|
75
|
-
1,
|
|
76
|
-
(thisValue, args) => {
|
|
77
|
-
requireArgs("has", args, 1);
|
|
78
|
-
const target = self(thisValue, "has").headers;
|
|
79
|
-
return attempt(() => target.has(arg(args, 0)));
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
[
|
|
83
|
-
"set",
|
|
84
|
-
2,
|
|
85
|
-
(thisValue, args) => {
|
|
86
|
-
requireArgs("set", args, 2);
|
|
87
|
-
const target = self(thisValue, "set").headers;
|
|
88
|
-
attempt(() => target.set(arg(args, 0), arg(args, 1)));
|
|
89
|
-
return undefined;
|
|
90
|
-
},
|
|
91
|
-
],
|
|
92
|
-
["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").headers.keys())],
|
|
93
|
-
["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").headers.values())],
|
|
94
|
-
["entries", 0, (thisValue) => hostIterator(builtins, self(thisValue, "entries").iterator(builtins))],
|
|
95
|
-
[
|
|
96
|
-
"forEach",
|
|
97
|
-
1,
|
|
98
|
-
function* (thisValue, args) {
|
|
99
|
-
requireArgs("forEach", args, 1);
|
|
100
|
-
const target = self(thisValue, "forEach");
|
|
101
|
-
const apply = applyCollectionCallback(ctx, args[0], "Headers.forEach");
|
|
102
|
-
for (const [key, value] of Array.from(target.headers.entries()))
|
|
103
|
-
yield* apply([value, key, target], args[1]);
|
|
104
|
-
return undefined;
|
|
105
|
-
},
|
|
106
|
-
],
|
|
107
|
-
]);
|
|
108
|
-
define(proto, IteratorSymbol, get(proto, "entries"), hidden);
|
|
109
|
-
return headers;
|
|
110
|
-
};
|
package/dist/stdlib/iterator.js
DELETED
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
import { applyCollectionCallback, Interruption } from "../interpreter/callback.js";
|
|
2
|
-
import { rangeError, typeError } from "../interpreter/model.js";
|
|
3
|
-
import { constructor, fn, methods, receiver, requiresNew } from "../interpreter/native.js";
|
|
4
|
-
import { Arr, coerceToNumber, define, hasPrototype, hidden, iteratorResult, IteratorObj, Obj, } from "../interpreter/objects.js";
|
|
5
|
-
import { describeValue } from "../interpreter/references.js";
|
|
6
|
-
const finished = { done: true, value: undefined };
|
|
7
|
-
// Every built-in iterator and helper shares the Iterator prototype; JS gives each collection its own, which is only
|
|
8
|
-
// observable through getPrototypeOf.
|
|
9
|
-
export const iteratorGlobals = (ctx) => {
|
|
10
|
-
const builtins = ctx.builtins;
|
|
11
|
-
const proto = builtins.Iterator;
|
|
12
|
-
const method = (name) => `Iterator.prototype.${name}`;
|
|
13
|
-
// A step that runs program code: when it throws, close the source first, as IteratorClose does.
|
|
14
|
-
const guarded = function* (source, body) {
|
|
15
|
-
try {
|
|
16
|
-
return yield* body;
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
if (error instanceof Interruption)
|
|
20
|
-
throw error;
|
|
21
|
-
try {
|
|
22
|
-
yield* source.close();
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
// The step's failure wins over a failing close.
|
|
26
|
-
}
|
|
27
|
-
throw error;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
// A lazy helper: `pull` advances it once. Once it reports done, throws, or is closed, it stays done. A callback
|
|
31
|
-
// that re-enters its own helper is a TypeError, as for a running generator.
|
|
32
|
-
const helper = (source, pull, close = source.close) => {
|
|
33
|
-
let done = false;
|
|
34
|
-
let running = false;
|
|
35
|
-
const enter = () => {
|
|
36
|
-
if (running)
|
|
37
|
-
throw typeError("Iterator helper is already running.");
|
|
38
|
-
running = true;
|
|
39
|
-
};
|
|
40
|
-
return new IteratorObj(builtins.IteratorHelper, {
|
|
41
|
-
next: function* () {
|
|
42
|
-
if (done)
|
|
43
|
-
return finished;
|
|
44
|
-
enter();
|
|
45
|
-
try {
|
|
46
|
-
const step = yield* pull();
|
|
47
|
-
done = step.done;
|
|
48
|
-
return step;
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
done = true;
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
54
|
-
finally {
|
|
55
|
-
running = false;
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
close: function* () {
|
|
59
|
-
if (done)
|
|
60
|
-
return;
|
|
61
|
-
enter();
|
|
62
|
-
done = true;
|
|
63
|
-
try {
|
|
64
|
-
yield* close();
|
|
65
|
-
}
|
|
66
|
-
finally {
|
|
67
|
-
running = false;
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
// GetIteratorFlattenable: an iterable by `[Symbol.iterator]`, otherwise the object itself as an iterator.
|
|
73
|
-
const flattenable = function* (value, name, strings) {
|
|
74
|
-
if (typeof value === "string" ? !strings : !(value instanceof Obj)) {
|
|
75
|
-
throw typeError(`${name} expects an iterable or iterator, received ${describeValue(value)}.`);
|
|
76
|
-
}
|
|
77
|
-
return (yield* ctx.iterate(value)) ?? ctx.iterateDirect(value);
|
|
78
|
-
};
|
|
79
|
-
const limit = (value, name) => {
|
|
80
|
-
const count = Math.trunc(coerceToNumber(value));
|
|
81
|
-
if (Number.isNaN(count) || count < 0) {
|
|
82
|
-
throw rangeError(`${method(name)} expects a non-negative count, received ${count}.`);
|
|
83
|
-
}
|
|
84
|
-
return count;
|
|
85
|
-
};
|
|
86
|
-
methods(builtins, proto, [
|
|
87
|
-
[
|
|
88
|
-
"next",
|
|
89
|
-
0,
|
|
90
|
-
function* (thisValue) {
|
|
91
|
-
const step = yield* receiver(IteratorObj, thisValue, method("next")).cursor.next();
|
|
92
|
-
return iteratorResult(builtins.Object, step.value, step.done);
|
|
93
|
-
},
|
|
94
|
-
],
|
|
95
|
-
[
|
|
96
|
-
"map",
|
|
97
|
-
1,
|
|
98
|
-
(thisValue, args) => {
|
|
99
|
-
const source = ctx.iterateDirect(thisValue);
|
|
100
|
-
const apply = applyCollectionCallback(ctx, args[0], method("map"));
|
|
101
|
-
let counter = 0;
|
|
102
|
-
return helper(source, function* () {
|
|
103
|
-
const step = yield* source.next();
|
|
104
|
-
if (step.done)
|
|
105
|
-
return finished;
|
|
106
|
-
return { done: false, value: yield* guarded(source, apply([step.value, counter++])) };
|
|
107
|
-
});
|
|
108
|
-
},
|
|
109
|
-
],
|
|
110
|
-
[
|
|
111
|
-
"filter",
|
|
112
|
-
1,
|
|
113
|
-
(thisValue, args) => {
|
|
114
|
-
const source = ctx.iterateDirect(thisValue);
|
|
115
|
-
const apply = applyCollectionCallback(ctx, args[0], method("filter"));
|
|
116
|
-
let counter = 0;
|
|
117
|
-
return helper(source, function* () {
|
|
118
|
-
while (true) {
|
|
119
|
-
const step = yield* source.next();
|
|
120
|
-
if (step.done)
|
|
121
|
-
return finished;
|
|
122
|
-
if (yield* guarded(source, apply([step.value, counter++])))
|
|
123
|
-
return step;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
},
|
|
127
|
-
],
|
|
128
|
-
[
|
|
129
|
-
"take",
|
|
130
|
-
1,
|
|
131
|
-
(thisValue, args) => {
|
|
132
|
-
const source = ctx.iterateDirect(thisValue);
|
|
133
|
-
let remaining = limit(args[0], "take");
|
|
134
|
-
return helper(source, function* () {
|
|
135
|
-
if (remaining === 0) {
|
|
136
|
-
yield* source.close();
|
|
137
|
-
return finished;
|
|
138
|
-
}
|
|
139
|
-
remaining -= 1;
|
|
140
|
-
return yield* source.next();
|
|
141
|
-
});
|
|
142
|
-
},
|
|
143
|
-
],
|
|
144
|
-
[
|
|
145
|
-
"drop",
|
|
146
|
-
1,
|
|
147
|
-
(thisValue, args) => {
|
|
148
|
-
const source = ctx.iterateDirect(thisValue);
|
|
149
|
-
let remaining = limit(args[0], "drop");
|
|
150
|
-
return helper(source, function* () {
|
|
151
|
-
while (remaining > 0) {
|
|
152
|
-
remaining -= 1;
|
|
153
|
-
const step = yield* source.next();
|
|
154
|
-
if (step.done)
|
|
155
|
-
return finished;
|
|
156
|
-
}
|
|
157
|
-
return yield* source.next();
|
|
158
|
-
});
|
|
159
|
-
},
|
|
160
|
-
],
|
|
161
|
-
[
|
|
162
|
-
"flatMap",
|
|
163
|
-
1,
|
|
164
|
-
(thisValue, args) => {
|
|
165
|
-
const source = ctx.iterateDirect(thisValue);
|
|
166
|
-
const apply = applyCollectionCallback(ctx, args[0], method("flatMap"));
|
|
167
|
-
let counter = 0;
|
|
168
|
-
let inner;
|
|
169
|
-
return helper(source, function* () {
|
|
170
|
-
while (true) {
|
|
171
|
-
if (inner !== undefined) {
|
|
172
|
-
const step = yield* guarded(source, inner.next());
|
|
173
|
-
if (!step.done)
|
|
174
|
-
return step;
|
|
175
|
-
inner = undefined;
|
|
176
|
-
}
|
|
177
|
-
const step = yield* source.next();
|
|
178
|
-
if (step.done)
|
|
179
|
-
return finished;
|
|
180
|
-
const mapped = yield* guarded(source, apply([step.value, counter++]));
|
|
181
|
-
inner = yield* guarded(source, flattenable(mapped, method("flatMap"), false));
|
|
182
|
-
}
|
|
183
|
-
}, function* () {
|
|
184
|
-
if (inner !== undefined)
|
|
185
|
-
yield* inner.close();
|
|
186
|
-
yield* source.close();
|
|
187
|
-
});
|
|
188
|
-
},
|
|
189
|
-
],
|
|
190
|
-
[
|
|
191
|
-
"reduce",
|
|
192
|
-
1,
|
|
193
|
-
function* (thisValue, args) {
|
|
194
|
-
const source = ctx.iterateDirect(thisValue);
|
|
195
|
-
const apply = applyCollectionCallback(ctx, args[0], method("reduce"));
|
|
196
|
-
let counter = 0;
|
|
197
|
-
let accumulator = args[1];
|
|
198
|
-
if (args.length < 2) {
|
|
199
|
-
const first = yield* source.next();
|
|
200
|
-
if (first.done)
|
|
201
|
-
throw typeError("Iterator.prototype.reduce of an empty iterator with no initial value.");
|
|
202
|
-
accumulator = first.value;
|
|
203
|
-
counter = 1;
|
|
204
|
-
}
|
|
205
|
-
while (true) {
|
|
206
|
-
const step = yield* source.next();
|
|
207
|
-
if (step.done)
|
|
208
|
-
return accumulator;
|
|
209
|
-
accumulator = yield* guarded(source, apply([accumulator, step.value, counter++]));
|
|
210
|
-
}
|
|
211
|
-
},
|
|
212
|
-
],
|
|
213
|
-
[
|
|
214
|
-
"toArray",
|
|
215
|
-
0,
|
|
216
|
-
function* (thisValue) {
|
|
217
|
-
const source = ctx.iterateDirect(thisValue);
|
|
218
|
-
const items = [];
|
|
219
|
-
while (true) {
|
|
220
|
-
const step = yield* source.next();
|
|
221
|
-
if (step.done)
|
|
222
|
-
return new Arr(builtins.Array, items);
|
|
223
|
-
items.push(step.value);
|
|
224
|
-
}
|
|
225
|
-
},
|
|
226
|
-
],
|
|
227
|
-
[
|
|
228
|
-
"forEach",
|
|
229
|
-
1,
|
|
230
|
-
function* (thisValue, args) {
|
|
231
|
-
const source = ctx.iterateDirect(thisValue);
|
|
232
|
-
const apply = applyCollectionCallback(ctx, args[0], method("forEach"));
|
|
233
|
-
let counter = 0;
|
|
234
|
-
while (true) {
|
|
235
|
-
const step = yield* source.next();
|
|
236
|
-
if (step.done)
|
|
237
|
-
return undefined;
|
|
238
|
-
yield* guarded(source, apply([step.value, counter++]));
|
|
239
|
-
}
|
|
240
|
-
},
|
|
241
|
-
],
|
|
242
|
-
[
|
|
243
|
-
"some",
|
|
244
|
-
1,
|
|
245
|
-
function* (thisValue, args) {
|
|
246
|
-
const source = ctx.iterateDirect(thisValue);
|
|
247
|
-
const apply = applyCollectionCallback(ctx, args[0], method("some"));
|
|
248
|
-
let counter = 0;
|
|
249
|
-
while (true) {
|
|
250
|
-
const step = yield* source.next();
|
|
251
|
-
if (step.done)
|
|
252
|
-
return false;
|
|
253
|
-
if (yield* guarded(source, apply([step.value, counter++]))) {
|
|
254
|
-
yield* source.close();
|
|
255
|
-
return true;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
},
|
|
259
|
-
],
|
|
260
|
-
[
|
|
261
|
-
"every",
|
|
262
|
-
1,
|
|
263
|
-
function* (thisValue, args) {
|
|
264
|
-
const source = ctx.iterateDirect(thisValue);
|
|
265
|
-
const apply = applyCollectionCallback(ctx, args[0], method("every"));
|
|
266
|
-
let counter = 0;
|
|
267
|
-
while (true) {
|
|
268
|
-
const step = yield* source.next();
|
|
269
|
-
if (step.done)
|
|
270
|
-
return true;
|
|
271
|
-
if (!(yield* guarded(source, apply([step.value, counter++])))) {
|
|
272
|
-
yield* source.close();
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
],
|
|
278
|
-
[
|
|
279
|
-
"find",
|
|
280
|
-
1,
|
|
281
|
-
function* (thisValue, args) {
|
|
282
|
-
const source = ctx.iterateDirect(thisValue);
|
|
283
|
-
const apply = applyCollectionCallback(ctx, args[0], method("find"));
|
|
284
|
-
let counter = 0;
|
|
285
|
-
while (true) {
|
|
286
|
-
const step = yield* source.next();
|
|
287
|
-
if (step.done)
|
|
288
|
-
return undefined;
|
|
289
|
-
if (yield* guarded(source, apply([step.value, counter++]))) {
|
|
290
|
-
yield* source.close();
|
|
291
|
-
return step.value;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
},
|
|
295
|
-
],
|
|
296
|
-
]);
|
|
297
|
-
// Helpers and `Iterator.from` wrappers can be closed; collection iterators, as in JS, cannot.
|
|
298
|
-
methods(builtins, builtins.IteratorHelper, [
|
|
299
|
-
[
|
|
300
|
-
"return",
|
|
301
|
-
0,
|
|
302
|
-
function* (thisValue) {
|
|
303
|
-
yield* receiver(IteratorObj, thisValue, "Iterator.prototype.return").cursor.close();
|
|
304
|
-
return iteratorResult(builtins.Object, undefined, true);
|
|
305
|
-
},
|
|
306
|
-
],
|
|
307
|
-
]);
|
|
308
|
-
const iterator = constructor(builtins, proto, {
|
|
309
|
-
name: "Iterator",
|
|
310
|
-
length: 0,
|
|
311
|
-
call: requiresNew("Iterator"),
|
|
312
|
-
construct: () => {
|
|
313
|
-
throw typeError("Iterator is abstract; use Iterator.from(...) or a built-in iterator.");
|
|
314
|
-
},
|
|
315
|
-
});
|
|
316
|
-
define(iterator, "from", fn(builtins, "from", 1, function* (_, args) {
|
|
317
|
-
if (args[0] instanceof Obj && hasPrototype(args[0], proto))
|
|
318
|
-
return args[0];
|
|
319
|
-
return new IteratorObj(builtins.IteratorHelper, yield* flattenable(args[0], "Iterator.from", true));
|
|
320
|
-
}), hidden);
|
|
321
|
-
return iterator;
|
|
322
|
-
};
|
package/dist/stdlib/json.d.ts
DELETED
package/dist/stdlib/json.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { methods } from "../interpreter/native.js";
|
|
2
|
-
import { applyCollectionCallback } from "../interpreter/callback.js";
|
|
3
|
-
import { checkStringLength } from "../interpreter/limits.js";
|
|
4
|
-
import { syntaxError } from "../interpreter/model.js";
|
|
5
|
-
import { typeofValue } from "../interpreter/references.js";
|
|
6
|
-
import { fromJson, toJson } from "../data.js";
|
|
7
|
-
import { get, keys, Arr, Obj, coerceToString, record, remove, set } from "../interpreter/objects.js";
|
|
8
|
-
export const jsonGlobal = (ctx) => {
|
|
9
|
-
const json = new Obj(ctx.builtins.Object);
|
|
10
|
-
methods(ctx.builtins, json, [
|
|
11
|
-
["parse", 2, (_, args) => parse(ctx, args)],
|
|
12
|
-
["stringify", 3, (_, args) => stringify(ctx, args)],
|
|
13
|
-
]);
|
|
14
|
-
return json;
|
|
15
|
-
};
|
|
16
|
-
const parse = (ctx, args) => {
|
|
17
|
-
const text = coerceToString(args[0]);
|
|
18
|
-
const parsed = (() => {
|
|
19
|
-
try {
|
|
20
|
-
return fromJson(ctx, JSON.parse(text));
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
throw syntaxError(`JSON.parse received invalid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
24
|
-
}
|
|
25
|
-
})();
|
|
26
|
-
if (typeofValue(args[1]) !== "function")
|
|
27
|
-
return parsed;
|
|
28
|
-
const apply = applyCollectionCallback(ctx, args[1], "JSON.parse");
|
|
29
|
-
const visit = function* (holder, key) {
|
|
30
|
-
const value = get(holder, key);
|
|
31
|
-
if (value instanceof Obj) {
|
|
32
|
-
for (const name of keys(value)) {
|
|
33
|
-
const revived = yield* visit(value, name);
|
|
34
|
-
if (revived === undefined)
|
|
35
|
-
remove(value, name);
|
|
36
|
-
else
|
|
37
|
-
set(value, name, revived);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return yield* apply([key, value], holder);
|
|
41
|
-
};
|
|
42
|
-
return visit(record(ctx.builtins.Object, { "": parsed }), "");
|
|
43
|
-
};
|
|
44
|
-
function* stringify(ctx, args) {
|
|
45
|
-
const space = args[2];
|
|
46
|
-
const indent = typeof space === "number" || typeof space === "string" ? space : undefined;
|
|
47
|
-
const replacer = args[1];
|
|
48
|
-
const properties = replacer instanceof Arr
|
|
49
|
-
? replacer.items
|
|
50
|
-
.filter((item) => typeof item === "string" || typeof item === "number")
|
|
51
|
-
.map(String)
|
|
52
|
-
: null;
|
|
53
|
-
const callback = typeofValue(replacer) === "function" ? applyCollectionCallback(ctx, replacer, "JSON.stringify") : undefined;
|
|
54
|
-
const text = JSON.stringify(yield* toJson(ctx, args[0], callback), properties, indent);
|
|
55
|
-
if (text !== undefined)
|
|
56
|
-
checkStringLength(text.length);
|
|
57
|
-
return text;
|
|
58
|
-
}
|
package/dist/stdlib/math.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Obj } from "../interpreter/objects.js";
|
|
2
|
-
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
3
|
-
declare global {
|
|
4
|
-
interface Math {
|
|
5
|
-
sumPrecise(values: Iterable<number>): number;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export declare const mathGlobal: <R>(ctx: Interpreter<R>) => Obj;
|