@opencode/codemode 2.2.4 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -1
- package/dist/codemode.d.ts +55 -4
- package/dist/codemode.js +63 -8
- package/dist/extension.d.ts +14 -9
- package/dist/extension.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interpreter/execute.d.ts +1 -1
- package/dist/interpreter/execute.js +16 -4
- package/dist/interpreter/extensions.d.ts +3 -3
- package/dist/interpreter/extensions.js +17 -11
- package/dist/interpreter/globals.js +10 -0
- package/dist/interpreter/interpreter.d.ts +11 -1
- package/dist/interpreter/interpreter.js +86 -2
- package/dist/interpreter/modules.d.ts +4 -0
- package/dist/interpreter/modules.js +33 -0
- package/dist/suspend-error.d.ts +23 -0
- package/dist/suspend-error.js +25 -0
- package/dist/tool-runtime.d.ts +4 -1
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +6 -0
- package/dist/vm/index.js +6 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +58 -0
- package/dist/vm/runtime/driver.js +184 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +80 -0
- package/dist/vm/runtime/execute.js +414 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +50 -0
- package/dist/vm/runtime/interpreter.js +94 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +87 -0
- package/dist/vm/snapshot/codec.js +751 -0
- package/dist/vm/snapshot/snapshot.d.ts +49 -0
- package/dist/vm/snapshot/snapshot.js +70 -0
- package/dist/vm/snapshot.d.ts +32 -0
- package/dist/vm/snapshot.js +306 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +225 -0
- package/dist/vm/vm.js +2931 -0
- package/package.json +6 -3
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Builtins } from "./intrinsics.js";
|
|
2
|
+
import { Obj, type Value } from "./objects.js";
|
|
3
|
+
/**
|
|
4
|
+
* Deterministic names for one runtime's intrinsic objects, so saved state can refer to a built-in by name instead of
|
|
5
|
+
* copying it, and whether each one changed since the runtime was made.
|
|
6
|
+
*
|
|
7
|
+
* - **Static ids.** Every object made with the intrinsics (`createBuiltins`, the globals, the prelude) has an id: its
|
|
8
|
+
* creation index, which is the same in every runtime because making them is deterministic (see `makingIntrinsics`).
|
|
9
|
+
* The ids are named once per process, by a breadth-first walk of the first runtime made, before any program runs:
|
|
10
|
+
* from the global names (`Array.prototype.map`), then `%Name%` for built-in prototypes the walk cannot reach
|
|
11
|
+
* (`%Generator%`) and the prelude's internals, and what is reachable from them. The first path found names a value.
|
|
12
|
+
* No later runtime walks anything.
|
|
13
|
+
* - **`@host` names.** The host's globals (extensions, a test harness) differ between runtimes, so each runtime names
|
|
14
|
+
* them itself, `@name`, by a walk over only the objects they reach that have no static id. Host lists are small.
|
|
15
|
+
* - **Dirty bits.** `Obj.intrinsic` is the id plus one while the object is as made, negated once it changes. Every
|
|
16
|
+
* write to an object's properties, prototype, extensibility or elements marks it (`changed` in objects.ts), and so
|
|
17
|
+
* does restoring it from a saved record, so a run suspended twice keeps what changed before the first suspend.
|
|
18
|
+
*/
|
|
19
|
+
export declare class IntrinsicNames {
|
|
20
|
+
/** The objects made with the intrinsics, by id. */
|
|
21
|
+
private readonly made;
|
|
22
|
+
private readonly hostValues;
|
|
23
|
+
private readonly hostNames;
|
|
24
|
+
private readonly hostIndex;
|
|
25
|
+
private named;
|
|
26
|
+
constructor(
|
|
27
|
+
/** The objects made with the intrinsics, by id. */
|
|
28
|
+
made: ReadonlyArray<Obj>, roots: Roots, hosts?: ReadonlyArray<readonly [string, Value]>);
|
|
29
|
+
/** The named intrinsics, in name order (see `all`). */
|
|
30
|
+
get values(): ReadonlyArray<Obj>;
|
|
31
|
+
/** The name of an intrinsic, or undefined for any other object. */
|
|
32
|
+
nameOf(value: Obj): string | undefined;
|
|
33
|
+
/** The intrinsic a name refers to. */
|
|
34
|
+
valueOf(name: string): Obj | undefined;
|
|
35
|
+
/** Every name: the static ones in walk order, then the host globals'. */
|
|
36
|
+
all(): ReadonlyArray<string>;
|
|
37
|
+
/** The host globals' names, `@name`, in walk order. */
|
|
38
|
+
hosts(): ReadonlyArray<string>;
|
|
39
|
+
/**
|
|
40
|
+
* The named intrinsics a saved state has to record: those changed since the runtime was made, and host values of
|
|
41
|
+
* classes whose state is not their properties (an extension's Map), which count as changed.
|
|
42
|
+
*/
|
|
43
|
+
changed(): Array<Obj>;
|
|
44
|
+
private isStatic;
|
|
45
|
+
}
|
|
46
|
+
/** Whether a named object is still as the runtime made it, so its name alone restores it. */
|
|
47
|
+
export declare const unchanged: (value: Obj) => boolean;
|
|
48
|
+
/** The static names, in walk order, once a runtime has been made in this process. */
|
|
49
|
+
export declare const staticNames: () => ReadonlyArray<string>;
|
|
50
|
+
/** Where the walk that names the static intrinsics starts. */
|
|
51
|
+
export type Roots = {
|
|
52
|
+
readonly globals: ReadonlyArray<readonly [string, Value]>;
|
|
53
|
+
readonly builtins: Builtins;
|
|
54
|
+
/** Internal objects no global reaches (the prelude's functions and helpers), named like the builtins. */
|
|
55
|
+
readonly internals: ReadonlyArray<readonly [string, Obj]>;
|
|
56
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { AsyncIteratorSymbol, IteratorSymbol } from "./model.js";
|
|
2
|
+
import { Arr, Fn, Native, Obj } from "./objects.js";
|
|
3
|
+
/**
|
|
4
|
+
* Deterministic names for one runtime's intrinsic objects, so saved state can refer to a built-in by name instead of
|
|
5
|
+
* copying it, and whether each one changed since the runtime was made.
|
|
6
|
+
*
|
|
7
|
+
* - **Static ids.** Every object made with the intrinsics (`createBuiltins`, the globals, the prelude) has an id: its
|
|
8
|
+
* creation index, which is the same in every runtime because making them is deterministic (see `makingIntrinsics`).
|
|
9
|
+
* The ids are named once per process, by a breadth-first walk of the first runtime made, before any program runs:
|
|
10
|
+
* from the global names (`Array.prototype.map`), then `%Name%` for built-in prototypes the walk cannot reach
|
|
11
|
+
* (`%Generator%`) and the prelude's internals, and what is reachable from them. The first path found names a value.
|
|
12
|
+
* No later runtime walks anything.
|
|
13
|
+
* - **`@host` names.** The host's globals (extensions, a test harness) differ between runtimes, so each runtime names
|
|
14
|
+
* them itself, `@name`, by a walk over only the objects they reach that have no static id. Host lists are small.
|
|
15
|
+
* - **Dirty bits.** `Obj.intrinsic` is the id plus one while the object is as made, negated once it changes. Every
|
|
16
|
+
* write to an object's properties, prototype, extensibility or elements marks it (`changed` in objects.ts), and so
|
|
17
|
+
* does restoring it from a saved record, so a run suspended twice keeps what changed before the first suspend.
|
|
18
|
+
*/
|
|
19
|
+
export class IntrinsicNames {
|
|
20
|
+
made;
|
|
21
|
+
hostValues = [];
|
|
22
|
+
hostNames = [];
|
|
23
|
+
hostIndex = new Map();
|
|
24
|
+
named;
|
|
25
|
+
constructor(
|
|
26
|
+
/** The objects made with the intrinsics, by id. */
|
|
27
|
+
made, roots, hosts = []) {
|
|
28
|
+
this.made = made;
|
|
29
|
+
made.forEach((value, id) => {
|
|
30
|
+
value.intrinsic = id + 1;
|
|
31
|
+
});
|
|
32
|
+
table ??= walk(made, roots);
|
|
33
|
+
if (made.length !== table.count) {
|
|
34
|
+
throw new Error(`This runtime made ${made.length} intrinsics, and the first one made ${table.count}.`);
|
|
35
|
+
}
|
|
36
|
+
const visit = (value, name) => {
|
|
37
|
+
if (!(value instanceof Obj) || this.hostIndex.has(value) || this.isStatic(value))
|
|
38
|
+
return;
|
|
39
|
+
this.hostIndex.set(value, this.hostValues.length);
|
|
40
|
+
this.hostValues.push(value);
|
|
41
|
+
this.hostNames.push(name());
|
|
42
|
+
value.intrinsic = table.count + this.hostValues.length;
|
|
43
|
+
};
|
|
44
|
+
let next = 0;
|
|
45
|
+
for (const [name, value] of hosts) {
|
|
46
|
+
visit(value, () => `@${name}`);
|
|
47
|
+
for (; next < this.hostValues.length; next++) {
|
|
48
|
+
const item = this.hostValues[next];
|
|
49
|
+
const base = this.hostNames[next];
|
|
50
|
+
for (const [key, slot] of item.props)
|
|
51
|
+
if ("value" in slot)
|
|
52
|
+
visit(slot.value, () => member(base, key));
|
|
53
|
+
if (item.proto !== null)
|
|
54
|
+
visit(item.proto, () => member(base, prototypeKey));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** The named intrinsics, in name order (see `all`). */
|
|
59
|
+
get values() {
|
|
60
|
+
return (this.named ??= [...table.order.map((id) => this.made[id]), ...this.hostValues]);
|
|
61
|
+
}
|
|
62
|
+
/** The name of an intrinsic, or undefined for any other object. */
|
|
63
|
+
nameOf(value) {
|
|
64
|
+
if (this.isStatic(value)) {
|
|
65
|
+
const at = table.position[Math.abs(value.intrinsic) - 1];
|
|
66
|
+
return at < 0 ? undefined : table.names[at];
|
|
67
|
+
}
|
|
68
|
+
const host = this.hostIndex.get(value);
|
|
69
|
+
return host === undefined ? undefined : this.hostNames[host];
|
|
70
|
+
}
|
|
71
|
+
/** The intrinsic a name refers to. */
|
|
72
|
+
valueOf(name) {
|
|
73
|
+
const id = staticIds().get(name);
|
|
74
|
+
if (id !== undefined)
|
|
75
|
+
return this.made[id];
|
|
76
|
+
const host = this.hostNames.indexOf(name);
|
|
77
|
+
return host < 0 ? undefined : this.hostValues[host];
|
|
78
|
+
}
|
|
79
|
+
/** Every name: the static ones in walk order, then the host globals'. */
|
|
80
|
+
all() {
|
|
81
|
+
return [...table.names, ...this.hostNames];
|
|
82
|
+
}
|
|
83
|
+
/** The host globals' names, `@name`, in walk order. */
|
|
84
|
+
hosts() {
|
|
85
|
+
return this.hostNames;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The named intrinsics a saved state has to record: those changed since the runtime was made, and host values of
|
|
89
|
+
* classes whose state is not their properties (an extension's Map), which count as changed.
|
|
90
|
+
*/
|
|
91
|
+
changed() {
|
|
92
|
+
return this.values.filter((value) => !unchanged(value));
|
|
93
|
+
}
|
|
94
|
+
isStatic(value) {
|
|
95
|
+
const id = Math.abs(value.intrinsic) - 1;
|
|
96
|
+
return id >= 0 && id < this.made.length && this.made[id] === value;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** Whether a named object is still as the runtime made it, so its name alone restores it. */
|
|
100
|
+
export const unchanged = (value) => {
|
|
101
|
+
if (value.intrinsic <= 0)
|
|
102
|
+
return false;
|
|
103
|
+
const ctor = value.constructor;
|
|
104
|
+
return ctor === Obj || ctor === Arr || ctor === Native || ctor === Fn;
|
|
105
|
+
};
|
|
106
|
+
/** The static names, in walk order, once a runtime has been made in this process. */
|
|
107
|
+
export const staticNames = () => {
|
|
108
|
+
if (table === undefined)
|
|
109
|
+
throw new Error("No runtime has been made yet, so the intrinsics have no names.");
|
|
110
|
+
return table.names;
|
|
111
|
+
};
|
|
112
|
+
let table;
|
|
113
|
+
let ids;
|
|
114
|
+
const staticIds = () => (ids ??= new Map(table.names.map((name, at) => [name, table.order[at]])));
|
|
115
|
+
const walk = (made, roots) => {
|
|
116
|
+
const order = [];
|
|
117
|
+
const paths = [];
|
|
118
|
+
const position = made.map(() => -1);
|
|
119
|
+
const visit = (value, path) => {
|
|
120
|
+
if (!(value instanceof Obj))
|
|
121
|
+
return;
|
|
122
|
+
const id = value.intrinsic - 1;
|
|
123
|
+
if (id < 0 || made[id] !== value) {
|
|
124
|
+
throw new Error(`The intrinsic at ${describe(path)} was not made with this runtime's intrinsics.`);
|
|
125
|
+
}
|
|
126
|
+
if (position[id] !== -1)
|
|
127
|
+
return;
|
|
128
|
+
position[id] = order.length;
|
|
129
|
+
order.push(id);
|
|
130
|
+
paths.push(path);
|
|
131
|
+
};
|
|
132
|
+
const describe = (path) => typeof path === "string" ? path : member(describe(paths[path[0]]), path[1]);
|
|
133
|
+
let next = 0;
|
|
134
|
+
const drain = () => {
|
|
135
|
+
for (; next < order.length; next++) {
|
|
136
|
+
const item = made[order[next]];
|
|
137
|
+
for (const [key, slot] of item.props)
|
|
138
|
+
if ("value" in slot)
|
|
139
|
+
visit(slot.value, [next, key]);
|
|
140
|
+
if (item.proto !== null)
|
|
141
|
+
visit(item.proto, [next, prototypeKey]);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
for (const [name, value] of roots.globals)
|
|
145
|
+
visit(value, name);
|
|
146
|
+
drain();
|
|
147
|
+
for (const [name, value] of [...Object.entries(roots.builtins), ...roots.internals]) {
|
|
148
|
+
visit(value, `%${name}%`);
|
|
149
|
+
drain();
|
|
150
|
+
}
|
|
151
|
+
const names = [];
|
|
152
|
+
for (const path of paths)
|
|
153
|
+
names.push(typeof path === "string" ? path : member(names[path[0]], path[1]));
|
|
154
|
+
return { count: made.length, order, names, position };
|
|
155
|
+
};
|
|
156
|
+
const prototypeKey = Symbol("[[Prototype]]");
|
|
157
|
+
const symbolNames = new Map([
|
|
158
|
+
[IteratorSymbol, "[Symbol.iterator]"],
|
|
159
|
+
[AsyncIteratorSymbol, "[Symbol.asyncIterator]"],
|
|
160
|
+
[prototypeKey, ".[[Prototype]]"],
|
|
161
|
+
]);
|
|
162
|
+
const member = (base, key) => {
|
|
163
|
+
if (typeof key === "symbol")
|
|
164
|
+
return `${base}${symbolNames.get(key) ?? `[${String(key)}]`}`;
|
|
165
|
+
return /^[A-Za-z_$][\w$]*$/.test(key) ? `${base}.${key}` : `${base}[${JSON.stringify(key)}]`;
|
|
166
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ErrorObj, Obj, type Value } from "./objects.js";
|
|
2
|
+
export declare const errorTypes: readonly ["Error", "TypeError", "RangeError", "SyntaxError", "ReferenceError", "EvalError", "URIError", "AggregateError"];
|
|
3
|
+
export type ErrorType = (typeof errorTypes)[number];
|
|
4
|
+
export declare const isErrorType: (name: string) => name is ErrorType;
|
|
5
|
+
declare const builtins: readonly ["Object", "Function", "Array", "String", "Number", "Boolean", "Date", "RegExp", "Map", "Set", "WeakMap", "WeakSet", "URL", "URLSearchParams", "Headers", "Uint8Array", "TextEncoder", "TextDecoder", "Promise", "PromiseLike", "Iterator", "IteratorHelper", "AsyncIterator", "Generator", "AsyncGenerator", "AsyncFromSyncIterator"];
|
|
6
|
+
/**
|
|
7
|
+
* The built-in prototype objects of one runtime, allocated empty in dependency order. The globals populate them
|
|
8
|
+
* and attach their constructors when the runtime is built.
|
|
9
|
+
*/
|
|
10
|
+
export type Builtins = Readonly<Record<(typeof builtins)[number] | ErrorType, Obj>>;
|
|
11
|
+
export declare const createErrorValue: (prototype: Obj, message: string | undefined) => ErrorObj;
|
|
12
|
+
/** The prototype a primitive reads its methods from without being boxed; none for null, undefined, and symbols. */
|
|
13
|
+
export declare const primitivePrototype: (builtins: Builtins, value: Value) => Obj | undefined;
|
|
14
|
+
export declare const createBuiltins: () => Builtins;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { define, hidden, Native, Arr, ErrorObj, Obj } from "./objects.js";
|
|
2
|
+
export const errorTypes = [
|
|
3
|
+
"Error",
|
|
4
|
+
"TypeError",
|
|
5
|
+
"RangeError",
|
|
6
|
+
"SyntaxError",
|
|
7
|
+
"ReferenceError",
|
|
8
|
+
"EvalError",
|
|
9
|
+
"URIError",
|
|
10
|
+
"AggregateError",
|
|
11
|
+
];
|
|
12
|
+
export const isErrorType = (name) => errorTypes.includes(name);
|
|
13
|
+
const builtins = [
|
|
14
|
+
"Object",
|
|
15
|
+
"Function",
|
|
16
|
+
"Array",
|
|
17
|
+
"String",
|
|
18
|
+
"Number",
|
|
19
|
+
"Boolean",
|
|
20
|
+
"Date",
|
|
21
|
+
"RegExp",
|
|
22
|
+
"Map",
|
|
23
|
+
"Set",
|
|
24
|
+
"WeakMap",
|
|
25
|
+
"WeakSet",
|
|
26
|
+
"URL",
|
|
27
|
+
"URLSearchParams",
|
|
28
|
+
"Headers",
|
|
29
|
+
"Uint8Array",
|
|
30
|
+
"TextEncoder",
|
|
31
|
+
"TextDecoder",
|
|
32
|
+
"Promise",
|
|
33
|
+
"PromiseLike",
|
|
34
|
+
"Iterator",
|
|
35
|
+
"IteratorHelper",
|
|
36
|
+
"AsyncIterator",
|
|
37
|
+
"Generator",
|
|
38
|
+
"AsyncGenerator",
|
|
39
|
+
"AsyncFromSyncIterator",
|
|
40
|
+
];
|
|
41
|
+
export const createErrorValue = (prototype, message) => {
|
|
42
|
+
const value = new ErrorObj(prototype);
|
|
43
|
+
if (message !== undefined)
|
|
44
|
+
define(value, "message", message, hidden);
|
|
45
|
+
return value;
|
|
46
|
+
};
|
|
47
|
+
/** The prototype a primitive reads its methods from without being boxed; none for null, undefined, and symbols. */
|
|
48
|
+
export const primitivePrototype = (builtins, value) => {
|
|
49
|
+
if (typeof value === "string")
|
|
50
|
+
return builtins.String;
|
|
51
|
+
if (typeof value === "number")
|
|
52
|
+
return builtins.Number;
|
|
53
|
+
if (typeof value === "boolean")
|
|
54
|
+
return builtins.Boolean;
|
|
55
|
+
return undefined;
|
|
56
|
+
};
|
|
57
|
+
export const createBuiltins = () => {
|
|
58
|
+
const object = new Obj(null);
|
|
59
|
+
// Function.prototype is itself callable and returns undefined.
|
|
60
|
+
const fn = new Native(object, { name: "", call: () => undefined });
|
|
61
|
+
const plain = () => new Obj(object);
|
|
62
|
+
const error = plain();
|
|
63
|
+
define(error, "name", "Error", hidden);
|
|
64
|
+
define(error, "message", "", hidden);
|
|
65
|
+
const derived = (type) => {
|
|
66
|
+
const proto = new Obj(error);
|
|
67
|
+
define(proto, "name", type, hidden);
|
|
68
|
+
define(proto, "message", "", hidden);
|
|
69
|
+
return proto;
|
|
70
|
+
};
|
|
71
|
+
const iterator = plain();
|
|
72
|
+
const asyncIterator = plain();
|
|
73
|
+
return {
|
|
74
|
+
Object: object,
|
|
75
|
+
Function: fn,
|
|
76
|
+
Array: new Arr(object),
|
|
77
|
+
String: plain(),
|
|
78
|
+
Number: plain(),
|
|
79
|
+
Boolean: plain(),
|
|
80
|
+
Date: plain(),
|
|
81
|
+
RegExp: plain(),
|
|
82
|
+
Map: plain(),
|
|
83
|
+
Set: plain(),
|
|
84
|
+
WeakMap: plain(),
|
|
85
|
+
WeakSet: plain(),
|
|
86
|
+
URL: plain(),
|
|
87
|
+
URLSearchParams: plain(),
|
|
88
|
+
Headers: plain(),
|
|
89
|
+
Uint8Array: plain(),
|
|
90
|
+
TextEncoder: plain(),
|
|
91
|
+
TextDecoder: plain(),
|
|
92
|
+
Promise: plain(),
|
|
93
|
+
/** What a synchronous result that can also be awaited inherits: then/catch/finally over a plain copy. */
|
|
94
|
+
PromiseLike: plain(),
|
|
95
|
+
Iterator: iterator,
|
|
96
|
+
IteratorHelper: new Obj(iterator),
|
|
97
|
+
AsyncIterator: asyncIterator,
|
|
98
|
+
Generator: new Obj(iterator),
|
|
99
|
+
AsyncGenerator: new Obj(asyncIterator),
|
|
100
|
+
/** CreateAsyncFromSyncIterator's objects; programs never see them. */
|
|
101
|
+
AsyncFromSyncIterator: new Obj(asyncIterator),
|
|
102
|
+
Error: error,
|
|
103
|
+
TypeError: derived("TypeError"),
|
|
104
|
+
RangeError: derived("RangeError"),
|
|
105
|
+
SyntaxError: derived("SyntaxError"),
|
|
106
|
+
ReferenceError: derived("ReferenceError"),
|
|
107
|
+
EvalError: derived("EvalError"),
|
|
108
|
+
URIError: derived("URIError"),
|
|
109
|
+
AggregateError: derived("AggregateError"),
|
|
110
|
+
};
|
|
111
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Longest string a built-in or operator may produce. */
|
|
2
|
+
export declare const MAX_STRING_LENGTH: number;
|
|
3
|
+
/** Longest array a built-in may create or grow to. */
|
|
4
|
+
export declare const MAX_ARRAY_LENGTH = 10000000;
|
|
5
|
+
/** Most promises that may be pending at once. */
|
|
6
|
+
export declare const MAX_PENDING_PROMISES = 10000;
|
|
7
|
+
/** Deepest nesting a value may have when it crosses to or from the host. */
|
|
8
|
+
export declare const MAX_VALUE_DEPTH = 32;
|
|
9
|
+
export declare const checkStringLength: (length: number) => void;
|
|
10
|
+
export declare const checkArrayLength: (length: number) => void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { rangeError } from "./model.js";
|
|
2
|
+
// The timeout only fires between interpreter steps, so a single built-in must not be able to materialize an
|
|
3
|
+
// unbounded value. These bound what one call may build; programs cannot reach such sizes any other way.
|
|
4
|
+
/** Longest string a built-in or operator may produce. */
|
|
5
|
+
export const MAX_STRING_LENGTH = 1 << 24;
|
|
6
|
+
/** Longest array a built-in may create or grow to. */
|
|
7
|
+
export const MAX_ARRAY_LENGTH = 10_000_000;
|
|
8
|
+
/** Most promises that may be pending at once. */
|
|
9
|
+
export const MAX_PENDING_PROMISES = 10_000;
|
|
10
|
+
/** Deepest nesting a value may have when it crosses to or from the host. */
|
|
11
|
+
export const MAX_VALUE_DEPTH = 32;
|
|
12
|
+
export const checkStringLength = (length) => {
|
|
13
|
+
if (length > MAX_STRING_LENGTH)
|
|
14
|
+
throw rangeError("Invalid string length");
|
|
15
|
+
};
|
|
16
|
+
export const checkArrayLength = (length) => {
|
|
17
|
+
if (length > MAX_ARRAY_LENGTH)
|
|
18
|
+
throw rangeError("Invalid array length");
|
|
19
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Node } from "acorn";
|
|
2
|
+
import type { ErrorType } from "./intrinsics.js";
|
|
3
|
+
import type { DiagnosticKind } from "../../codemode.js";
|
|
4
|
+
import type { ErrorObj, Value } from "./objects.js";
|
|
5
|
+
/** Any parsed node; the compiler narrows on `type`, and diagnostics read `loc`. */
|
|
6
|
+
export type AstNode = Node;
|
|
7
|
+
/** A global binding: globals are immutable per run, and top-level declarations are the program's own locals. */
|
|
8
|
+
export type Binding = {
|
|
9
|
+
readonly value: Value;
|
|
10
|
+
};
|
|
11
|
+
export type GeneratorRequestKind = "next" | "return" | "throw";
|
|
12
|
+
export declare const AsyncIteratorSymbol: unique symbol;
|
|
13
|
+
export declare const IteratorSymbol: unique symbol;
|
|
14
|
+
export declare class Throw {
|
|
15
|
+
readonly value: Value;
|
|
16
|
+
constructor(value: Value);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A failure raised by the interpreter or a built-in. It travels as a defect and becomes one program Error object
|
|
20
|
+
* the first time a handler observes it, so every observer of the same failure sees the same value.
|
|
21
|
+
*/
|
|
22
|
+
export declare class PendingThrow {
|
|
23
|
+
/** The JS error class a program sees when it catches this failure. */
|
|
24
|
+
readonly type: ErrorType;
|
|
25
|
+
readonly message: string;
|
|
26
|
+
readonly kind: DiagnosticKind;
|
|
27
|
+
readonly suggestions?: ReadonlyArray<string> | undefined;
|
|
28
|
+
node?: AstNode;
|
|
29
|
+
value?: ErrorObj;
|
|
30
|
+
constructor(
|
|
31
|
+
/** The JS error class a program sees when it catches this failure. */
|
|
32
|
+
type: ErrorType, message: string, node?: AstNode, kind?: DiagnosticKind, suggestions?: ReadonlyArray<string> | undefined);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A host value a restored run cannot have back: a function an extension returned, or what a host function returned,
|
|
36
|
+
* which have no data form. Using it fails with `lostError`.
|
|
37
|
+
*/
|
|
38
|
+
export declare class Lost {
|
|
39
|
+
readonly description: string;
|
|
40
|
+
constructor(description: string);
|
|
41
|
+
}
|
|
42
|
+
/** What using a value lost to a suspend throws. */
|
|
43
|
+
export declare const lostError: (description: string, node?: AstNode) => PendingThrow;
|
|
44
|
+
export declare const typeError: (message: string, node?: AstNode) => PendingThrow;
|
|
45
|
+
export declare const invalidData: (message: string, node?: AstNode) => PendingThrow;
|
|
46
|
+
export declare const rangeError: (message: string, node?: AstNode) => PendingThrow;
|
|
47
|
+
export declare const referenceError: (message: string, node?: AstNode) => PendingThrow;
|
|
48
|
+
export declare const syntaxError: (message: string, node?: AstNode) => PendingThrow;
|
|
49
|
+
export declare const uriError: (message: string, node?: AstNode) => PendingThrow;
|
|
50
|
+
export declare const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes, getters/setters, BigInt, and custom Symbols. Use plain functions and data objects instead.";
|
|
51
|
+
export declare const sourceLocation: (node: AstNode) => {
|
|
52
|
+
readonly line: number;
|
|
53
|
+
readonly column: number;
|
|
54
|
+
};
|
|
55
|
+
export declare const formatLocation: (location?: {
|
|
56
|
+
readonly line: number;
|
|
57
|
+
readonly column: number;
|
|
58
|
+
}) => string;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export const AsyncIteratorSymbol = Symbol("codemode.async-iterator");
|
|
2
|
+
export const IteratorSymbol = Symbol("codemode.iterator");
|
|
3
|
+
export class Throw {
|
|
4
|
+
value;
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.value = value;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A failure raised by the interpreter or a built-in. It travels as a defect and becomes one program Error object
|
|
11
|
+
* the first time a handler observes it, so every observer of the same failure sees the same value.
|
|
12
|
+
*/
|
|
13
|
+
export class PendingThrow {
|
|
14
|
+
type;
|
|
15
|
+
message;
|
|
16
|
+
kind;
|
|
17
|
+
suggestions;
|
|
18
|
+
node;
|
|
19
|
+
value;
|
|
20
|
+
constructor(
|
|
21
|
+
/** The JS error class a program sees when it catches this failure. */
|
|
22
|
+
type, message, node, kind = "ExecutionFailure", suggestions) {
|
|
23
|
+
this.type = type;
|
|
24
|
+
this.message = message;
|
|
25
|
+
this.kind = kind;
|
|
26
|
+
this.suggestions = suggestions;
|
|
27
|
+
if (node)
|
|
28
|
+
this.node = node;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A host value a restored run cannot have back: a function an extension returned, or what a host function returned,
|
|
33
|
+
* which have no data form. Using it fails with `lostError`.
|
|
34
|
+
*/
|
|
35
|
+
export class Lost {
|
|
36
|
+
description;
|
|
37
|
+
constructor(description) {
|
|
38
|
+
this.description = description;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** What using a value lost to a suspend throws. */
|
|
42
|
+
export const lostError = (description, node) => new PendingThrow("TypeError", `${description} came from the host and was lost when the run was suspended; get it again after resuming.`, node);
|
|
43
|
+
const failure = (type, kind) => (message, node) => new PendingThrow(type, message, node, kind);
|
|
44
|
+
export const typeError = failure("TypeError");
|
|
45
|
+
export const invalidData = failure("TypeError", "InvalidDataValue");
|
|
46
|
+
export const rangeError = failure("RangeError");
|
|
47
|
+
export const referenceError = failure("ReferenceError");
|
|
48
|
+
export const syntaxError = failure("SyntaxError");
|
|
49
|
+
export const uriError = failure("URIError");
|
|
50
|
+
// Orient the agent rather than enumerate JavaScript; interpreter-support.md is the full matrix.
|
|
51
|
+
export const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes, getters/setters, BigInt, and custom Symbols. Use plain functions and data objects instead.";
|
|
52
|
+
// Acorn lines are 1-based and its columns are 0-based. Diagnostics use 1-based columns of the submitted source.
|
|
53
|
+
export const sourceLocation = (node) => ({
|
|
54
|
+
line: node.loc?.start.line ?? 1,
|
|
55
|
+
column: (node.loc?.start.column ?? 0) + 1,
|
|
56
|
+
});
|
|
57
|
+
export const formatLocation = (location) => location ? ` (line ${location.line}, col ${location.column})` : "";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { define, get, Obj } from "./objects.js";
|
|
2
|
+
/**
|
|
3
|
+
* What `import` and `require` load: a global, by the Node module name that provides it. `fs` loads the `fs` global
|
|
4
|
+
* and `crypto` the `crypto` global; the names below come from globals with different names. A module that would be
|
|
5
|
+
* empty isn't available.
|
|
6
|
+
*/
|
|
7
|
+
const aliases = {
|
|
8
|
+
"fs/promises": (global) => {
|
|
9
|
+
const fs = global("fs");
|
|
10
|
+
return fs instanceof Obj ? get(fs, "promises") : undefined;
|
|
11
|
+
},
|
|
12
|
+
url: (global) => pick(global, ["URL", "URLSearchParams"]),
|
|
13
|
+
util: (global) => pick(global, ["TextEncoder", "TextDecoder"]),
|
|
14
|
+
buffer: (global) => pick(global, ["Buffer", "atob", "btoa"]),
|
|
15
|
+
};
|
|
16
|
+
/** `node:fs` names the same module as `fs`. */
|
|
17
|
+
export const moduleName = (specifier) => (specifier.startsWith("node:") ? specifier.slice(5) : specifier);
|
|
18
|
+
export const resolveModule = (specifier, global) => {
|
|
19
|
+
const name = moduleName(specifier);
|
|
20
|
+
return global(name) ?? aliases[name]?.(global);
|
|
21
|
+
};
|
|
22
|
+
function pick(global, names) {
|
|
23
|
+
const found = names.flatMap((name) => {
|
|
24
|
+
const value = global(name);
|
|
25
|
+
return value === undefined ? [] : [[name, value]];
|
|
26
|
+
});
|
|
27
|
+
if (found.length === 0)
|
|
28
|
+
return undefined;
|
|
29
|
+
const module = new Obj(null);
|
|
30
|
+
for (const [name, value] of found)
|
|
31
|
+
define(module, name, value);
|
|
32
|
+
return module;
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Builtins } from "./intrinsics.js";
|
|
2
|
+
import { type Callable, Native, type NativeCall, type NativeOptions, Obj, type PositionSource, type Value } from "./objects.js";
|
|
3
|
+
/** A native function body: a value, a thrown `PendingThrow`, or what the VM carries out as the call. */
|
|
4
|
+
export type Impl = NativeCall;
|
|
5
|
+
export declare const native: (builtins: Builtins, options: NativeOptions) => Native;
|
|
6
|
+
export declare const fn: (builtins: Builtins, name: string, length: number, impl: Impl) => Native;
|
|
7
|
+
export type Method = readonly [name: string, length: number, impl: Impl];
|
|
8
|
+
export declare const methods: (builtins: Builtins, target: Obj, table: ReadonlyArray<Method>) => void;
|
|
9
|
+
/** `keys`, `values`, and `entries`: built-in iterators over the receiver `self` checks. */
|
|
10
|
+
export declare const iteratorMethods: (builtins: Builtins, self: (thisValue: Value, name: string) => PositionSource) => Array<Method>;
|
|
11
|
+
export declare const constants: (target: Obj, table: Record<string, Value>) => void;
|
|
12
|
+
/** A constructor wired to its prototype: `C.prototype === proto` and `proto.constructor === C`. */
|
|
13
|
+
export declare const constructor: (builtins: Builtins, proto: Obj, options: NativeOptions) => Native;
|
|
14
|
+
/** The `call` of a constructor that JS requires to be invoked with `new`. */
|
|
15
|
+
export declare const requiresNew: (name: string) => () => never;
|
|
16
|
+
/** The instance prototype for `new` via `newTarget.prototype`, falling back to the built-in's own. */
|
|
17
|
+
export declare const prototypeFrom: (newTarget: Callable, fallback: Obj) => Obj;
|
|
18
|
+
/** Narrows a method receiver to the built-in it belongs to, or throws the TypeError JS would. */
|
|
19
|
+
export declare const receiver: <T extends Obj>(cls: abstract new (...args: never) => T, thisValue: Value, method: string) => T;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { typeError } from "./model.js";
|
|
2
|
+
import { define, frozen, hidden, Native, Obj, positionIterator, } from "./objects.js";
|
|
3
|
+
import { describeValue } from "./references.js";
|
|
4
|
+
export const native = (builtins, options) => new Native(builtins.Function, options);
|
|
5
|
+
export const fn = (builtins, name, length, impl) => native(builtins, { name, length, call: impl });
|
|
6
|
+
export const methods = (builtins, target, table) => {
|
|
7
|
+
for (const [name, length, impl] of table)
|
|
8
|
+
define(target, name, fn(builtins, name, length, impl), hidden);
|
|
9
|
+
};
|
|
10
|
+
/** `keys`, `values`, and `entries`: built-in iterators over the receiver `self` checks. */
|
|
11
|
+
export const iteratorMethods = (builtins, self) => ["keys", "values", "entries"].map((kind) => [
|
|
12
|
+
kind,
|
|
13
|
+
0,
|
|
14
|
+
(thisValue) => positionIterator(builtins, self(thisValue, kind), kind),
|
|
15
|
+
]);
|
|
16
|
+
export const constants = (target, table) => {
|
|
17
|
+
for (const [name, value] of Object.entries(table))
|
|
18
|
+
define(target, name, value, frozen);
|
|
19
|
+
};
|
|
20
|
+
/** A constructor wired to its prototype: `C.prototype === proto` and `proto.constructor === C`. */
|
|
21
|
+
export const constructor = (builtins, proto, options) => {
|
|
22
|
+
const ctor = native(builtins, options);
|
|
23
|
+
define(ctor, "prototype", proto, frozen);
|
|
24
|
+
define(proto, "constructor", ctor, hidden);
|
|
25
|
+
return ctor;
|
|
26
|
+
};
|
|
27
|
+
/** The `call` of a constructor that JS requires to be invoked with `new`. */
|
|
28
|
+
export const requiresNew = (name) => () => {
|
|
29
|
+
throw typeError(`Constructor ${name} requires 'new'.`);
|
|
30
|
+
};
|
|
31
|
+
/** The instance prototype for `new` via `newTarget.prototype`, falling back to the built-in's own. */
|
|
32
|
+
export const prototypeFrom = (newTarget, fallback) => {
|
|
33
|
+
const proto = newTarget.props.get("prototype");
|
|
34
|
+
return proto !== undefined && "value" in proto && proto.value instanceof Obj ? proto.value : fallback;
|
|
35
|
+
};
|
|
36
|
+
/** Narrows a method receiver to the built-in it belongs to, or throws the TypeError JS would. */
|
|
37
|
+
export const receiver = (cls, thisValue, method) => {
|
|
38
|
+
if (thisValue instanceof cls)
|
|
39
|
+
return thisValue;
|
|
40
|
+
throw typeError(`${method} called on incompatible receiver ${describeValue(thisValue)}.`);
|
|
41
|
+
};
|