@opencode/codemode 2.2.5 → 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,751 @@
|
|
|
1
|
+
import { Interpreter } from "../runtime/interpreter.js";
|
|
2
|
+
import { AsyncIteratorSymbol, IteratorSymbol, Lost, lostError, PendingThrow, Throw, } from "../runtime/model.js";
|
|
3
|
+
import { Arguments, Arr, BoundFn, Bytes, changed, DateObj, ErrorObj, GeneratorObj, HeadersObj, IteratorObj, MapObj, Native, Obj, Position, PromiseObj, RegExpObj, Resolver, SetObj, URLObj, URLSearchParamsObj, WeakMapObj, WeakSetObj, } from "../runtime/objects.js";
|
|
4
|
+
import { Convert, Interruption, Invoke } from "../runtime/signals.js";
|
|
5
|
+
import { deleted, OrderedTable } from "../runtime/table.js";
|
|
6
|
+
import { AsyncFromSyncIterator } from "../generators.js";
|
|
7
|
+
import { HostCall, Resolving } from "../scheduler.js";
|
|
8
|
+
import { TextDecoderObj } from "../stdlib/bytes.js";
|
|
9
|
+
import { ToolError, toolError } from "../../tool-error.js";
|
|
10
|
+
import { ToolReference, ToolRuntimeError } from "../../tool-runtime.js";
|
|
11
|
+
/** A saved state can't be read, or doesn't fit this runtime. */
|
|
12
|
+
import { SnapshotError } from "../../suspend-error.js";
|
|
13
|
+
export { SnapshotError };
|
|
14
|
+
// --- Registries ---
|
|
15
|
+
const symbols = new Map([
|
|
16
|
+
["iterator", IteratorSymbol],
|
|
17
|
+
["asyncIterator", AsyncIteratorSymbol],
|
|
18
|
+
["deleted", deleted],
|
|
19
|
+
]);
|
|
20
|
+
const symbolNames = new Map([...symbols].map(([name, symbol]) => [symbol, name]));
|
|
21
|
+
// `intrinsic` is not saved: a restored object is the program's, or a named intrinsic that keeps its own.
|
|
22
|
+
const objFields = ["props", "extensible", "proto", "intrinsic"];
|
|
23
|
+
const callable = { tag: "Function" };
|
|
24
|
+
// Built on first use: the VM's modules and this one import each other, so the classes aren't there at load time.
|
|
25
|
+
const builtinSpecs = () => [
|
|
26
|
+
{ name: "Obj", ctor: Obj, fields: [], obj: true, constants: { tag: "Object" } },
|
|
27
|
+
{ name: "Arr", ctor: Arr, fields: ["items", "elements"], obj: true, constants: { tag: "Array" } },
|
|
28
|
+
{ name: "ErrorObj", ctor: ErrorObj, fields: ["failure"], obj: true, constants: { tag: "Error" } },
|
|
29
|
+
{ name: "Arguments", ctor: Arguments, fields: [], obj: true, constants: { tag: "Arguments" } },
|
|
30
|
+
// Only named natives are saved (by name); an unnamed one is a host function, saved as `LostNative`.
|
|
31
|
+
{ name: "Native", ctor: Native, fields: [], skip: ["length", "call", "construct", "callback"], obj: true },
|
|
32
|
+
{
|
|
33
|
+
name: "BoundFn",
|
|
34
|
+
ctor: BoundFn,
|
|
35
|
+
fields: ["length", "target", "boundThis", "boundArgs"],
|
|
36
|
+
obj: true,
|
|
37
|
+
constants: { ...callable, construct: undefined, callback: true },
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: "Resolver",
|
|
41
|
+
ctor: Resolver,
|
|
42
|
+
fields: ["length", "resolving", "kind"],
|
|
43
|
+
obj: true,
|
|
44
|
+
constants: { ...callable, construct: undefined, callback: true },
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "PromiseObj",
|
|
48
|
+
ctor: PromiseObj,
|
|
49
|
+
fields: ["state", "result", "reactions", "handled", "id"],
|
|
50
|
+
obj: true,
|
|
51
|
+
constants: { tag: "Promise" },
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "GeneratorObj",
|
|
55
|
+
ctor: GeneratorObj,
|
|
56
|
+
fields: ["state", "frame", "queue", "asynchronous"],
|
|
57
|
+
obj: true,
|
|
58
|
+
constants: { tag: "Generator" },
|
|
59
|
+
},
|
|
60
|
+
{ name: "IteratorObj", ctor: IteratorObj, fields: ["cursor"], obj: true, constants: { tag: "Iterator" } },
|
|
61
|
+
{ name: "DateObj", ctor: DateObj, fields: ["time"], obj: true, constants: { tag: "Date" } },
|
|
62
|
+
{
|
|
63
|
+
name: "RegExpObj",
|
|
64
|
+
ctor: RegExpObj,
|
|
65
|
+
fields: ["source", "flags", "lastIndex"],
|
|
66
|
+
skip: ["compiled"],
|
|
67
|
+
obj: true,
|
|
68
|
+
constants: { tag: "RegExp" },
|
|
69
|
+
reset: (value) => {
|
|
70
|
+
value.compiled = undefined;
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{ name: "MapObj", ctor: MapObj, fields: ["map"], obj: true, constants: { tag: "Map" } },
|
|
74
|
+
{ name: "SetObj", ctor: SetObj, fields: ["set"], obj: true, constants: { tag: "Set" } },
|
|
75
|
+
{ name: "WeakMapObj", ctor: WeakMapObj, fields: ["map"], obj: true, constants: { tag: "WeakMap" } },
|
|
76
|
+
{ name: "WeakSetObj", ctor: WeakSetObj, fields: ["set"], obj: true, constants: { tag: "WeakSet" } },
|
|
77
|
+
{
|
|
78
|
+
name: "URLSearchParamsObj",
|
|
79
|
+
ctor: URLSearchParamsObj,
|
|
80
|
+
fields: ["params"],
|
|
81
|
+
obj: true,
|
|
82
|
+
constants: { tag: "URLSearchParams" },
|
|
83
|
+
},
|
|
84
|
+
{ name: "HeadersObj", ctor: HeadersObj, fields: ["headers"], obj: true, constants: { tag: "Headers" } },
|
|
85
|
+
{ name: "URLObj", ctor: URLObj, fields: ["searchParams", "url"], obj: true, constants: { tag: "URL" } },
|
|
86
|
+
{ name: "Bytes", ctor: Bytes, fields: ["bytes"], obj: true, constants: { tag: "Uint8Array" } },
|
|
87
|
+
{ name: "TextDecoderObj", ctor: TextDecoderObj, fields: ["decoder"], obj: true, constants: { tag: "Object" } },
|
|
88
|
+
{
|
|
89
|
+
name: "AsyncFromSyncIterator",
|
|
90
|
+
ctor: AsyncFromSyncIterator,
|
|
91
|
+
fields: ["sync", "site"],
|
|
92
|
+
obj: true,
|
|
93
|
+
constants: { tag: "AsyncFromSyncIterator" },
|
|
94
|
+
},
|
|
95
|
+
{ name: "Position", ctor: Position, fields: ["index", "done", "source", "kind", "arrays"] },
|
|
96
|
+
{
|
|
97
|
+
name: "OrderedTable",
|
|
98
|
+
ctor: OrderedTable,
|
|
99
|
+
fields: ["keys", "values", "cursors", "tombstones", "pruneAt"],
|
|
100
|
+
skip: ["index"],
|
|
101
|
+
// The index is derived from the keys.
|
|
102
|
+
reset: (table) => {
|
|
103
|
+
table.index = new Map();
|
|
104
|
+
table.keys.forEach((key, at) => {
|
|
105
|
+
if (key !== deleted)
|
|
106
|
+
table.index.set(key, at);
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "HostCall",
|
|
112
|
+
ctor: HostCall,
|
|
113
|
+
fields: ["status", "id", "request", "label", "blocking", "result", "resolving", "site"],
|
|
114
|
+
},
|
|
115
|
+
{ name: "Resolving", ctor: Resolving, fields: ["done", "scheduler", "promise", "site"] },
|
|
116
|
+
{ name: "Throw", ctor: Throw, fields: ["value"] },
|
|
117
|
+
{ name: "PendingThrow", ctor: PendingThrow, fields: ["type", "message", "kind", "suggestions", "node", "value"] },
|
|
118
|
+
{ name: "Convert", ctor: Convert, fields: ["hints", "values", "ordinal", "coerce"] },
|
|
119
|
+
{ name: "Invoke", ctor: Invoke, fields: ["callable", "thisValue", "args", "site"] },
|
|
120
|
+
{ name: "ToolReference", ctor: ToolReference, fields: ["path"] },
|
|
121
|
+
{ name: "Lost", ctor: Lost, fields: ["description"] },
|
|
122
|
+
{
|
|
123
|
+
name: "Interpreter",
|
|
124
|
+
ctor: Interpreter,
|
|
125
|
+
anchor: "ctx",
|
|
126
|
+
fields: ["logs", "globalScope", "modules", "namespaces"],
|
|
127
|
+
skip: ["tools", "builtins", "vm", "intrinsics", "extensionWork", "sources", "prelude"],
|
|
128
|
+
},
|
|
129
|
+
];
|
|
130
|
+
let builtins;
|
|
131
|
+
const builtinRegistry = () => {
|
|
132
|
+
if (builtins !== undefined)
|
|
133
|
+
return builtins;
|
|
134
|
+
const specs = builtinSpecs();
|
|
135
|
+
builtins = {
|
|
136
|
+
byCtor: new Map(specs.map((spec) => [spec.ctor, spec])),
|
|
137
|
+
byName: new Map(specs.map((spec) => [spec.name, spec])),
|
|
138
|
+
};
|
|
139
|
+
return builtins;
|
|
140
|
+
};
|
|
141
|
+
// A function an extension made while the program ran, which has no data form: saved as `LostNative`.
|
|
142
|
+
const hostFunction = {
|
|
143
|
+
name: "LostNative",
|
|
144
|
+
ctor: Native,
|
|
145
|
+
fields: ["length"],
|
|
146
|
+
obj: true,
|
|
147
|
+
constants: callable,
|
|
148
|
+
};
|
|
149
|
+
/** Host error classes that can travel as a failure, by name. */
|
|
150
|
+
const errorClasses = new Map([
|
|
151
|
+
["Error", Error],
|
|
152
|
+
["TypeError", TypeError],
|
|
153
|
+
["RangeError", RangeError],
|
|
154
|
+
["SyntaxError", SyntaxError],
|
|
155
|
+
["ReferenceError", ReferenceError],
|
|
156
|
+
["EvalError", EvalError],
|
|
157
|
+
["URIError", URIError],
|
|
158
|
+
["ToolRuntimeError", ToolRuntimeError],
|
|
159
|
+
["ToolError", ToolError],
|
|
160
|
+
]);
|
|
161
|
+
const errorNames = new Map([...errorClasses].map(([name, ctor]) => [ctor, name]));
|
|
162
|
+
/** The names of every class with a codec: a build that saves a different set cannot read this one's snapshots. */
|
|
163
|
+
export const codecShape = (extra = []) => JSON.stringify([[...builtinSpecs(), ...extra].map((spec) => [spec.name, spec.fields]), [...symbols.keys()]]);
|
|
164
|
+
const registry = (extra) => {
|
|
165
|
+
const { byCtor, byName } = builtinRegistry();
|
|
166
|
+
return extra === undefined || extra.length === 0
|
|
167
|
+
? { byCtor, byName }
|
|
168
|
+
: {
|
|
169
|
+
byCtor: new Map([...byCtor, ...extra.map((spec) => [spec.ctor, spec])]),
|
|
170
|
+
byName: new Map([...byName, ...extra.map((spec) => [spec.name, spec])]),
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
// --- Encoding ---
|
|
174
|
+
/** Encodes everything reachable from `roots`: the roots' values and the records they refer to. */
|
|
175
|
+
export const encode = (roots, env) => {
|
|
176
|
+
const encoder = new Encoder(env);
|
|
177
|
+
const encoded = roots.map((root) => encoder.value(root));
|
|
178
|
+
encoder.drain();
|
|
179
|
+
return { roots: encoded, records: encoder.records };
|
|
180
|
+
};
|
|
181
|
+
class Encoder {
|
|
182
|
+
env;
|
|
183
|
+
records = [];
|
|
184
|
+
ids = new Map();
|
|
185
|
+
work = [];
|
|
186
|
+
// A URL's `searchParams` is the same host object its URLSearchParams wraps; it is encoded once every URL is known.
|
|
187
|
+
owners = new Map();
|
|
188
|
+
params = [];
|
|
189
|
+
specs;
|
|
190
|
+
constructor(env) {
|
|
191
|
+
this.env = env;
|
|
192
|
+
this.specs = registry(env.specs);
|
|
193
|
+
}
|
|
194
|
+
value(value) {
|
|
195
|
+
switch (typeof value) {
|
|
196
|
+
case "string":
|
|
197
|
+
case "boolean":
|
|
198
|
+
return value;
|
|
199
|
+
case "undefined":
|
|
200
|
+
return ["u"];
|
|
201
|
+
case "number":
|
|
202
|
+
if (Number.isFinite(value) && !Object.is(value, -0))
|
|
203
|
+
return value;
|
|
204
|
+
return ["n", Object.is(value, -0) ? "-0" : String(value)];
|
|
205
|
+
case "symbol": {
|
|
206
|
+
const name = symbolNames.get(value);
|
|
207
|
+
const external = name === undefined ? this.env.external?.(value) : undefined;
|
|
208
|
+
if (external !== undefined)
|
|
209
|
+
return ["x", external];
|
|
210
|
+
if (name === undefined)
|
|
211
|
+
throw new SnapshotError(`Codec gap: the symbol ${String(value)} has no name.`);
|
|
212
|
+
return ["s", name];
|
|
213
|
+
}
|
|
214
|
+
case "bigint":
|
|
215
|
+
throw new SnapshotError("Codec gap: a bigint has no data form.");
|
|
216
|
+
}
|
|
217
|
+
if (value === null)
|
|
218
|
+
return null;
|
|
219
|
+
const object = value;
|
|
220
|
+
const external = this.env.external?.(object);
|
|
221
|
+
if (external !== undefined)
|
|
222
|
+
return ["x", external];
|
|
223
|
+
if (object instanceof Obj) {
|
|
224
|
+
const name = this.env.nameOf(object);
|
|
225
|
+
if (name !== undefined && this.env.unchanged(object))
|
|
226
|
+
return ["i", name];
|
|
227
|
+
}
|
|
228
|
+
else if (!Array.isArray(object)) {
|
|
229
|
+
const node = this.env.nodeId(object);
|
|
230
|
+
if (node !== undefined)
|
|
231
|
+
return ["a", node[0], node[1]];
|
|
232
|
+
}
|
|
233
|
+
return this.ref(object);
|
|
234
|
+
}
|
|
235
|
+
drain() {
|
|
236
|
+
for (let object = this.work.shift(); object !== undefined; object = this.work.shift()) {
|
|
237
|
+
const id = this.ids.get(object);
|
|
238
|
+
this.records[id] = this.record(object, id);
|
|
239
|
+
}
|
|
240
|
+
for (const [id, params] of this.params) {
|
|
241
|
+
const owner = this.owners.get(params);
|
|
242
|
+
this.records[id] =
|
|
243
|
+
owner === undefined
|
|
244
|
+
? { t: "URLSearchParams", s: params.toString() }
|
|
245
|
+
: { t: "URLSearchParams", o: this.ref(owner) };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
ref(object) {
|
|
249
|
+
const known = this.ids.get(object);
|
|
250
|
+
if (known !== undefined)
|
|
251
|
+
return ["r", known];
|
|
252
|
+
const id = this.records.length;
|
|
253
|
+
this.ids.set(object, id);
|
|
254
|
+
this.records.push(undefined);
|
|
255
|
+
this.work.push(object);
|
|
256
|
+
return ["r", id];
|
|
257
|
+
}
|
|
258
|
+
record(object, id) {
|
|
259
|
+
if (object instanceof Obj) {
|
|
260
|
+
const name = this.env.nameOf(object);
|
|
261
|
+
if (name !== undefined) {
|
|
262
|
+
const spec = this.spec(object);
|
|
263
|
+
return { t: "@", n: name, c: spec.name, ...this.fields(object, spec) };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (Array.isArray(object)) {
|
|
267
|
+
// A sparse array (`length` can be ten million with nothing in it) saves only the indexes it has.
|
|
268
|
+
if (object.length > 1024) {
|
|
269
|
+
const present = Object.keys(object);
|
|
270
|
+
if (present.length * 2 < object.length) {
|
|
271
|
+
const entries = [];
|
|
272
|
+
for (const key of present)
|
|
273
|
+
entries.push(Number(key), this.value(object[Number(key)]));
|
|
274
|
+
return { t: "Array", l: object.length, s: entries };
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const items = [];
|
|
278
|
+
for (let at = 0; at < object.length; at++)
|
|
279
|
+
items.push(at in object ? this.value(object[at]) : ["h"]);
|
|
280
|
+
return { t: "Array", v: items };
|
|
281
|
+
}
|
|
282
|
+
if (object instanceof Map) {
|
|
283
|
+
const entries = [];
|
|
284
|
+
for (const [key, item] of object)
|
|
285
|
+
entries.push(this.value(key), this.value(item));
|
|
286
|
+
const anchor = this.env.anchorOf(object);
|
|
287
|
+
return anchor === undefined ? { t: "Map", e: entries } : { t: "Map", e: entries, a: anchor };
|
|
288
|
+
}
|
|
289
|
+
if (object instanceof Set)
|
|
290
|
+
return { t: "Set", e: [...object].map((item) => this.value(item)) };
|
|
291
|
+
const spec = this.specs.byCtor.get(object.constructor);
|
|
292
|
+
if (spec !== undefined && spec.name !== "Native") {
|
|
293
|
+
if (spec.anchor !== undefined && this.env.anchorOf(object) !== spec.anchor) {
|
|
294
|
+
throw new SnapshotError(`Codec gap: a second ${spec.name} is reachable from the state.`);
|
|
295
|
+
}
|
|
296
|
+
return { t: spec.name, ...this.fields(object, spec) };
|
|
297
|
+
}
|
|
298
|
+
if (object instanceof Native) {
|
|
299
|
+
// A host function made while the program ran, inside a value an extension returned: its name says what was lost.
|
|
300
|
+
this.lose(`the function ${functionName(object)}`);
|
|
301
|
+
return { t: "LostNative", ...this.fields(object, hostFunction, hostFunction.fields) };
|
|
302
|
+
}
|
|
303
|
+
const proto = Object.getPrototypeOf(object);
|
|
304
|
+
if (proto === Object.prototype || proto === null) {
|
|
305
|
+
const entries = [];
|
|
306
|
+
for (const key of Object.keys(object))
|
|
307
|
+
entries.push(key, this.value(object[key]));
|
|
308
|
+
return proto === null ? { t: "Object", e: entries, n: 1 } : { t: "Object", e: entries };
|
|
309
|
+
}
|
|
310
|
+
if (object instanceof Error)
|
|
311
|
+
return this.error(object);
|
|
312
|
+
if (object instanceof Interruption)
|
|
313
|
+
return { t: "Interruption" };
|
|
314
|
+
if (object instanceof URL) {
|
|
315
|
+
this.owners.set(object.searchParams, object);
|
|
316
|
+
return { t: "URL", h: object.href };
|
|
317
|
+
}
|
|
318
|
+
if (object instanceof URLSearchParams) {
|
|
319
|
+
this.params.push([id, object]);
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
if (object instanceof Headers)
|
|
323
|
+
return { t: "Headers", e: [...object].map(([key, item]) => [key, item]) };
|
|
324
|
+
if (object instanceof Uint8Array) {
|
|
325
|
+
return { t: "Uint8Array", b: this.ref(object.buffer), o: object.byteOffset, l: object.length };
|
|
326
|
+
}
|
|
327
|
+
if (object instanceof ArrayBuffer)
|
|
328
|
+
return { t: "ArrayBuffer", d: base64(new Uint8Array(object)) };
|
|
329
|
+
if (object instanceof TextDecoder)
|
|
330
|
+
return { t: "TextDecoder", f: object.fatal, i: object.ignoreBOM };
|
|
331
|
+
const what = typeof object === "function" ? "a host function" : `a host ${object.constructor?.name ?? "object"}`;
|
|
332
|
+
this.lose(what);
|
|
333
|
+
return { t: "Lost", f: [what] };
|
|
334
|
+
}
|
|
335
|
+
lose(what) {
|
|
336
|
+
if (this.env.strict)
|
|
337
|
+
throw new SnapshotError(`Codec gap: ${what} has no data form.`);
|
|
338
|
+
this.env.lost.push(what);
|
|
339
|
+
}
|
|
340
|
+
spec(object) {
|
|
341
|
+
const spec = this.specs.byCtor.get(object.constructor);
|
|
342
|
+
if (spec === undefined)
|
|
343
|
+
throw new SnapshotError(`Codec gap: ${object.constructor.name} has no codec.`);
|
|
344
|
+
return spec;
|
|
345
|
+
}
|
|
346
|
+
// A class instance's saved fields, positional, with its `Obj` header when it is one. Fields the spec does not know
|
|
347
|
+
// are a codec gap: they would be silently dropped.
|
|
348
|
+
fields(object, spec, only) {
|
|
349
|
+
const record = object;
|
|
350
|
+
const fields = only ?? spec.fields;
|
|
351
|
+
if (only === undefined && spec.view === undefined) {
|
|
352
|
+
for (const key of Object.keys(object)) {
|
|
353
|
+
if (fields.includes(key) || spec.skip?.includes(key) || (spec.constants !== undefined && key in spec.constants))
|
|
354
|
+
continue;
|
|
355
|
+
if (spec.obj && (objFields.includes(key) || key === "tag"))
|
|
356
|
+
continue;
|
|
357
|
+
throw new SnapshotError(`Codec gap: ${spec.name}.${key} is not saved.`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const values = only === undefined && spec.view !== undefined
|
|
361
|
+
? spec.view(object).map((item) => this.value(item))
|
|
362
|
+
: fields.map((field) => (Object.hasOwn(object, field) ? this.value(record[field]) : ["-"]));
|
|
363
|
+
if (!(object instanceof Obj))
|
|
364
|
+
return { f: values };
|
|
365
|
+
const props = [];
|
|
366
|
+
for (const [key, slot] of object.props) {
|
|
367
|
+
props.push(typeof key === "symbol" ? this.value(key) : key);
|
|
368
|
+
if ("value" in slot) {
|
|
369
|
+
props.push(this.value(slot.value), (slot.writable ? 1 : 0) | (slot.enumerable ? 2 : 0) | (slot.configurable ? 4 : 0));
|
|
370
|
+
}
|
|
371
|
+
else
|
|
372
|
+
props.push(null, 8 | (slot.enumerable ? 2 : 0) | (slot.configurable ? 4 : 0));
|
|
373
|
+
}
|
|
374
|
+
return { f: values, p: object.proto === null ? null : this.value(object.proto), x: object.extensible, o: props };
|
|
375
|
+
}
|
|
376
|
+
error(error) {
|
|
377
|
+
const name = errorNames.get(error.constructor);
|
|
378
|
+
if (name === undefined) {
|
|
379
|
+
// A failure only ever reaches the program when it is thrown, so it is saved as what throwing a lost value does.
|
|
380
|
+
this.lose(`a host ${error.constructor.name}`);
|
|
381
|
+
return {
|
|
382
|
+
t: "PendingThrow",
|
|
383
|
+
...this.fields(lostError(`An error of class ${error.constructor.name}`), this.specs.byName.get("PendingThrow")),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
const fields = [];
|
|
387
|
+
for (const key of Object.keys(error)) {
|
|
388
|
+
if (key === "message" || key === "stack")
|
|
389
|
+
continue;
|
|
390
|
+
fields.push(key, this.value(error[key]));
|
|
391
|
+
}
|
|
392
|
+
return { t: "Error", c: name, m: error.message, e: fields };
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
// --- Decoding ---
|
|
396
|
+
/** Rebuilds the graph `encode` saved, filling anchors in place: the decoded roots. */
|
|
397
|
+
export const decode = (saved, env) => {
|
|
398
|
+
const decoder = new Decoder(saved.records, env);
|
|
399
|
+
for (let id = 0; id < saved.records.length; id++)
|
|
400
|
+
decoder.shell(id);
|
|
401
|
+
for (let id = 0; id < saved.records.length; id++)
|
|
402
|
+
decoder.fill(id);
|
|
403
|
+
decoder.finish();
|
|
404
|
+
return saved.roots.map((root) => decoder.value(root));
|
|
405
|
+
};
|
|
406
|
+
const invalid = (message) => {
|
|
407
|
+
throw new SnapshotError(`Invalid snapshot: ${message}`);
|
|
408
|
+
};
|
|
409
|
+
const isRecord = (value) => typeof value === "object" &&
|
|
410
|
+
value !== null &&
|
|
411
|
+
!Array.isArray(value) &&
|
|
412
|
+
typeof value.t === "string";
|
|
413
|
+
const list = (value, what) => Array.isArray(value) ? value : invalid(`${what} is not a list.`);
|
|
414
|
+
const text = (value, what) => typeof value === "string" ? value : invalid(`${what} is not text.`);
|
|
415
|
+
const count = (value, what) => Number.isSafeInteger(value) && value >= 0 ? value : invalid(`${what} is not a count.`);
|
|
416
|
+
const forbidden = new Set(["__proto__", "constructor", "prototype"]);
|
|
417
|
+
class Decoder {
|
|
418
|
+
records;
|
|
419
|
+
env;
|
|
420
|
+
shells;
|
|
421
|
+
filled;
|
|
422
|
+
// Derived state is rebuilt once every record is filled in, since it reads other records.
|
|
423
|
+
resets = [];
|
|
424
|
+
specs;
|
|
425
|
+
constructor(records, env) {
|
|
426
|
+
this.records = records;
|
|
427
|
+
this.env = env;
|
|
428
|
+
this.specs = registry(env.specs);
|
|
429
|
+
this.shells = new Array(records.length);
|
|
430
|
+
this.filled = new Array(records.length).fill(false);
|
|
431
|
+
}
|
|
432
|
+
at(id) {
|
|
433
|
+
const record = this.records[id];
|
|
434
|
+
return isRecord(record) ? record : invalid(`record ${id} is malformed.`);
|
|
435
|
+
}
|
|
436
|
+
// Allocates the object a record restores, before anything is filled in, so references and cycles resolve.
|
|
437
|
+
shell(id) {
|
|
438
|
+
const known = this.shells[id];
|
|
439
|
+
if (known !== undefined)
|
|
440
|
+
return known;
|
|
441
|
+
const record = this.at(id);
|
|
442
|
+
const made = this.make(record, id);
|
|
443
|
+
this.shells[id] = made;
|
|
444
|
+
return made;
|
|
445
|
+
}
|
|
446
|
+
make(record, id) {
|
|
447
|
+
switch (record.t) {
|
|
448
|
+
case "Array":
|
|
449
|
+
return [];
|
|
450
|
+
case "Object":
|
|
451
|
+
return record.n === 1 ? Object.create(null) : {};
|
|
452
|
+
case "Map":
|
|
453
|
+
return record.a === undefined ? new Map() : this.anchor(text(record.a, "an anchor"), Map);
|
|
454
|
+
case "Set":
|
|
455
|
+
return new Set();
|
|
456
|
+
case "@": {
|
|
457
|
+
const name = text(record.n, "a built-in name");
|
|
458
|
+
const named = this.env.named(name);
|
|
459
|
+
if (named === undefined)
|
|
460
|
+
return invalid(`this runtime has no built-in or host global named ${name}.`);
|
|
461
|
+
const spec = this.classSpec(record.c);
|
|
462
|
+
if (named.constructor !== spec.ctor)
|
|
463
|
+
return invalid(`${name} is not a ${spec.name} in this runtime.`);
|
|
464
|
+
return named;
|
|
465
|
+
}
|
|
466
|
+
case "LostNative":
|
|
467
|
+
return Object.create(Native.prototype);
|
|
468
|
+
case "Interruption":
|
|
469
|
+
return this.env.interruption();
|
|
470
|
+
case "Error": {
|
|
471
|
+
const name = text(record.c, "an error class");
|
|
472
|
+
const ctor = errorClasses.get(name) ?? invalid(`unknown error class ${name}.`);
|
|
473
|
+
const message = text(record.m, "an error message");
|
|
474
|
+
if (ctor === ToolError)
|
|
475
|
+
return toolError(message);
|
|
476
|
+
const made = Object.create(ctor.prototype);
|
|
477
|
+
Object.defineProperty(made, "message", {
|
|
478
|
+
value: message,
|
|
479
|
+
writable: true,
|
|
480
|
+
configurable: true,
|
|
481
|
+
enumerable: false,
|
|
482
|
+
});
|
|
483
|
+
return made;
|
|
484
|
+
}
|
|
485
|
+
case "URL":
|
|
486
|
+
return new URL(text(record.h, "a URL"));
|
|
487
|
+
case "URLSearchParams": {
|
|
488
|
+
if (record.o === undefined)
|
|
489
|
+
return new URLSearchParams(text(record.s, "search params"));
|
|
490
|
+
const owner = this.value(record.o);
|
|
491
|
+
return owner instanceof URL ? owner.searchParams : invalid(`record ${id} names no URL.`);
|
|
492
|
+
}
|
|
493
|
+
case "Headers": {
|
|
494
|
+
const entries = list(record.e, "headers").map((entry) => {
|
|
495
|
+
const pair = list(entry, "a header");
|
|
496
|
+
return [text(pair[0], "a header name"), text(pair[1], "a header value")];
|
|
497
|
+
});
|
|
498
|
+
return new Headers(entries);
|
|
499
|
+
}
|
|
500
|
+
case "ArrayBuffer":
|
|
501
|
+
return fromBase64(text(record.d, "bytes")).buffer;
|
|
502
|
+
case "Uint8Array": {
|
|
503
|
+
const buffer = this.value(record.b);
|
|
504
|
+
if (!(buffer instanceof ArrayBuffer))
|
|
505
|
+
return invalid(`record ${id} names no buffer.`);
|
|
506
|
+
const offset = count(record.o, "a byte offset");
|
|
507
|
+
const length = count(record.l, "a byte length");
|
|
508
|
+
if (offset + length > buffer.byteLength)
|
|
509
|
+
return invalid(`record ${id} reaches past its buffer.`);
|
|
510
|
+
return new Uint8Array(buffer, offset, length);
|
|
511
|
+
}
|
|
512
|
+
case "TextDecoder":
|
|
513
|
+
return new TextDecoder("utf-8", { fatal: record.f === true, ignoreBOM: record.i === true });
|
|
514
|
+
}
|
|
515
|
+
const spec = this.classSpec(record.t);
|
|
516
|
+
if (spec.name === "Native")
|
|
517
|
+
return invalid("a native is saved only by name.");
|
|
518
|
+
if (spec.anchor !== undefined)
|
|
519
|
+
return this.anchor(spec.anchor, spec.ctor);
|
|
520
|
+
return Object.create(spec.ctor.prototype);
|
|
521
|
+
}
|
|
522
|
+
anchor(name, ctor) {
|
|
523
|
+
const anchor = this.env.anchor(name);
|
|
524
|
+
if (anchor === undefined || anchor.constructor !== ctor)
|
|
525
|
+
return invalid(`unknown anchor ${name}.`);
|
|
526
|
+
return anchor;
|
|
527
|
+
}
|
|
528
|
+
classSpec(name) {
|
|
529
|
+
return this.specs.byName.get(text(name, "a class")) ?? invalid(`unknown record kind ${String(name)}.`);
|
|
530
|
+
}
|
|
531
|
+
fill(id) {
|
|
532
|
+
if (this.filled[id])
|
|
533
|
+
return;
|
|
534
|
+
this.filled[id] = true;
|
|
535
|
+
const record = this.at(id);
|
|
536
|
+
const target = this.shell(id);
|
|
537
|
+
switch (record.t) {
|
|
538
|
+
case "Array": {
|
|
539
|
+
const array = target;
|
|
540
|
+
if (record.v === undefined) {
|
|
541
|
+
const length = count(record.l, "an array length");
|
|
542
|
+
if (length > 4_294_967_295)
|
|
543
|
+
invalid("an array is too long.");
|
|
544
|
+
array.length = length;
|
|
545
|
+
const entries = list(record.s, "a sparse array");
|
|
546
|
+
for (let at = 0; at < entries.length; at += 2) {
|
|
547
|
+
const index = count(entries[at], "an array index");
|
|
548
|
+
if (index >= length)
|
|
549
|
+
invalid("an array index is past its length.");
|
|
550
|
+
array[index] = this.value(entries[at + 1]);
|
|
551
|
+
}
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
const items = list(record.v, "an array");
|
|
555
|
+
items.forEach((item, at) => {
|
|
556
|
+
if (Array.isArray(item) && item[0] === "h")
|
|
557
|
+
return;
|
|
558
|
+
array[at] = this.value(item);
|
|
559
|
+
});
|
|
560
|
+
array.length = items.length;
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
case "Object":
|
|
564
|
+
case "Error": {
|
|
565
|
+
const entries = list(record.e, record.t === "Error" ? "an error" : "an object");
|
|
566
|
+
for (let at = 0; at < entries.length; at += 2) {
|
|
567
|
+
const key = text(entries[at], "a key");
|
|
568
|
+
if (record.t === "Error" && forbidden.has(key))
|
|
569
|
+
invalid(`an error field is named ${key}.`);
|
|
570
|
+
Object.defineProperty(target, key, {
|
|
571
|
+
value: this.value(entries[at + 1]),
|
|
572
|
+
writable: true,
|
|
573
|
+
enumerable: true,
|
|
574
|
+
configurable: true,
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
case "Map": {
|
|
580
|
+
const map = target;
|
|
581
|
+
if (record.a !== undefined)
|
|
582
|
+
map.clear();
|
|
583
|
+
const entries = list(record.e, "a map");
|
|
584
|
+
for (let at = 0; at < entries.length; at += 2)
|
|
585
|
+
map.set(this.value(entries[at]), this.value(entries[at + 1]));
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
case "Set": {
|
|
589
|
+
const set = target;
|
|
590
|
+
for (const item of list(record.e, "a set"))
|
|
591
|
+
set.add(this.value(item));
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
case "@":
|
|
595
|
+
this.fillClass(target, record, this.classSpec(record.c), true);
|
|
596
|
+
// Changed before it was saved, so it stays changed: a later save records it again.
|
|
597
|
+
return changed(target);
|
|
598
|
+
case "LostNative": {
|
|
599
|
+
this.fillClass(target, record, hostFunction, false);
|
|
600
|
+
const native = target;
|
|
601
|
+
const name = functionName(target);
|
|
602
|
+
native.tag = "Function";
|
|
603
|
+
native.call = (_, __, site) => {
|
|
604
|
+
throw lostError(`The function ${name}`, site);
|
|
605
|
+
};
|
|
606
|
+
native.construct = undefined;
|
|
607
|
+
native.callback = true;
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
case "Interruption":
|
|
611
|
+
case "URL":
|
|
612
|
+
case "URLSearchParams":
|
|
613
|
+
case "Headers":
|
|
614
|
+
case "ArrayBuffer":
|
|
615
|
+
case "Uint8Array":
|
|
616
|
+
case "TextDecoder":
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const spec = this.classSpec(record.t);
|
|
620
|
+
this.fillClass(target, record, spec, false);
|
|
621
|
+
}
|
|
622
|
+
fillClass(target, record, spec, named, only) {
|
|
623
|
+
const fields = only ?? spec.fields;
|
|
624
|
+
const values = list(record.f, `the fields of a ${spec.name}`);
|
|
625
|
+
if (values.length !== fields.length)
|
|
626
|
+
invalid(`a ${spec.name} has ${values.length} fields, not ${fields.length}.`);
|
|
627
|
+
if (spec.obj || named)
|
|
628
|
+
this.fillObj(target, record);
|
|
629
|
+
if (spec.constants !== undefined)
|
|
630
|
+
Object.assign(target, spec.constants);
|
|
631
|
+
fields.forEach((field, at) => {
|
|
632
|
+
const value = values[at];
|
|
633
|
+
if (Array.isArray(value) && value[0] === "-") {
|
|
634
|
+
delete target[field];
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
target[field] = this.value(value);
|
|
638
|
+
});
|
|
639
|
+
const reset = spec.reset;
|
|
640
|
+
if (reset !== undefined)
|
|
641
|
+
this.resets.push(() => reset(target));
|
|
642
|
+
}
|
|
643
|
+
finish() {
|
|
644
|
+
for (const reset of this.resets)
|
|
645
|
+
reset();
|
|
646
|
+
}
|
|
647
|
+
// An object's prototype, extensibility, and properties. A named object's accessors stay the ones it was made with.
|
|
648
|
+
fillObj(target, record) {
|
|
649
|
+
const proto = record.p === null ? null : this.value(record.p);
|
|
650
|
+
if (proto !== null && !(proto instanceof Obj))
|
|
651
|
+
invalid("a prototype is not an object.");
|
|
652
|
+
if (typeof record.x !== "boolean")
|
|
653
|
+
invalid("an object's extensibility is not a boolean.");
|
|
654
|
+
const entries = list(record.o, "an object's properties");
|
|
655
|
+
const props = new Map();
|
|
656
|
+
for (let at = 0; at < entries.length; at += 3) {
|
|
657
|
+
const raw = entries[at];
|
|
658
|
+
const key = typeof raw === "string" ? raw : this.value(raw);
|
|
659
|
+
if (typeof key !== "string" && typeof key !== "symbol")
|
|
660
|
+
invalid("a property key is not a string or symbol.");
|
|
661
|
+
const flags = count(entries[at + 2], "property flags");
|
|
662
|
+
const enumerable = (flags & 2) !== 0;
|
|
663
|
+
const configurable = (flags & 4) !== 0;
|
|
664
|
+
if ((flags & 8) !== 0) {
|
|
665
|
+
const original = target.props?.get(key);
|
|
666
|
+
if (original === undefined || "value" in original)
|
|
667
|
+
invalid(`the accessor ${String(key)} has no original.`);
|
|
668
|
+
const accessor = original;
|
|
669
|
+
props.set(key, { get: accessor.get, set: accessor.set, enumerable, configurable });
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
props.set(key, {
|
|
673
|
+
value: this.value(entries[at + 1]),
|
|
674
|
+
writable: (flags & 1) !== 0,
|
|
675
|
+
enumerable,
|
|
676
|
+
configurable,
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
const writable = target;
|
|
680
|
+
writable.intrinsic ??= 0;
|
|
681
|
+
writable.props = props;
|
|
682
|
+
writable.extensible = record.x;
|
|
683
|
+
writable.proto = proto;
|
|
684
|
+
}
|
|
685
|
+
value(value) {
|
|
686
|
+
switch (typeof value) {
|
|
687
|
+
case "string":
|
|
688
|
+
case "boolean":
|
|
689
|
+
return value;
|
|
690
|
+
case "number":
|
|
691
|
+
return Number.isFinite(value) ? value : invalid("a number is not finite.");
|
|
692
|
+
}
|
|
693
|
+
if (value === null)
|
|
694
|
+
return null;
|
|
695
|
+
if (!Array.isArray(value))
|
|
696
|
+
return invalid("a value is neither JSON data nor a tag.");
|
|
697
|
+
switch (value[0]) {
|
|
698
|
+
case "u":
|
|
699
|
+
return undefined;
|
|
700
|
+
case "n":
|
|
701
|
+
switch (value[1]) {
|
|
702
|
+
case "NaN":
|
|
703
|
+
return Number.NaN;
|
|
704
|
+
case "Infinity":
|
|
705
|
+
return Number.POSITIVE_INFINITY;
|
|
706
|
+
case "-Infinity":
|
|
707
|
+
return Number.NEGATIVE_INFINITY;
|
|
708
|
+
case "-0":
|
|
709
|
+
return -0;
|
|
710
|
+
}
|
|
711
|
+
return invalid(`unknown number ${String(value[1])}.`);
|
|
712
|
+
case "s":
|
|
713
|
+
return symbols.get(text(value[1], "a symbol")) ?? invalid(`unknown symbol ${String(value[1])}.`);
|
|
714
|
+
case "x":
|
|
715
|
+
return this.env.external?.(value[1]) ?? invalid(`unknown external value ${JSON.stringify(value[1])}.`);
|
|
716
|
+
case "i": {
|
|
717
|
+
const name = text(value[1], "a built-in name");
|
|
718
|
+
return this.env.named(name) ?? invalid(`this runtime has no built-in or host global named ${name}.`);
|
|
719
|
+
}
|
|
720
|
+
case "a":
|
|
721
|
+
return (this.env.node(text(value[1], "a source"), count(value[2], "a node index")) ??
|
|
722
|
+
invalid(`no node ${String(value[2])} in source ${String(value[1])}.`));
|
|
723
|
+
case "r": {
|
|
724
|
+
const id = count(value[1], "a record id");
|
|
725
|
+
if (id >= this.records.length)
|
|
726
|
+
invalid(`record ${id} does not exist.`);
|
|
727
|
+
return this.shell(id);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return invalid(`unknown tag ${String(value[0])}.`);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
const functionName = (fn) => {
|
|
734
|
+
const slot = fn.props.get("name");
|
|
735
|
+
return slot !== undefined && "value" in slot && typeof slot.value === "string" && slot.value !== ""
|
|
736
|
+
? slot.value
|
|
737
|
+
: "(anonymous)";
|
|
738
|
+
};
|
|
739
|
+
const base64 = (bytes) => {
|
|
740
|
+
let binary = "";
|
|
741
|
+
for (let at = 0; at < bytes.length; at += 0x8000)
|
|
742
|
+
binary += String.fromCharCode(...bytes.subarray(at, at + 0x8000));
|
|
743
|
+
return btoa(binary);
|
|
744
|
+
};
|
|
745
|
+
const fromBase64 = (encoded) => {
|
|
746
|
+
const binary = atob(encoded);
|
|
747
|
+
const bytes = new Uint8Array(binary.length);
|
|
748
|
+
for (let at = 0; at < binary.length; at++)
|
|
749
|
+
bytes[at] = binary.charCodeAt(at);
|
|
750
|
+
return bytes;
|
|
751
|
+
};
|