@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
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { fromJson, toBoundary } from "../data.js";
|
|
2
2
|
import { ToolReference } from "../tool-runtime.js";
|
|
3
|
-
import { AsyncIteratorSymbol, GeneratorReturn, IteratorSymbol, OptionalShortCircuit, invalidData, Throw, rangeError, typeError, unsupportedSyntax, } from "./model.js";
|
|
3
|
+
import { AsyncIteratorSymbol, GeneratorReturn, IteratorSymbol, OptionalShortCircuit, invalidData, PendingThrow, syntaxError, Throw, rangeError, typeError, unsupportedSyntax, } from "./model.js";
|
|
4
4
|
import { checkStringLength } from "./limits.js";
|
|
5
5
|
import { locate, materialize } from "./errors.js";
|
|
6
6
|
import { primitivePrototype } from "./intrinsics.js";
|
|
7
7
|
import { globals } from "./globals.js";
|
|
8
|
-
import { assign, Callable, define, frozen, get, hostCursor, IteratorObj, has, hasOwn, hidden, hasPrototype, keys, Native, parseArrayIndex, Arguments, Arr, Fn, GeneratorObj, Obj, PromiseObj, iteratorResult, remove, set, coerceToNumber, coerceToString, } from "./objects.js";
|
|
8
|
+
import { assign, Callable, define, frozen, get, hostCursor, IteratorObj, has, hasOwn, hidden, hasPrototype, keys, Native, parseArrayIndex, Arguments, Arr, Fn, GeneratorObj, Obj, PromiseObj, iteratorResult, remove, set, coerceToNumber, coerceToString, entries, getOwn, } from "./objects.js";
|
|
9
9
|
import { preserveConsumerError, toPrimitive } from "./callback.js";
|
|
10
10
|
import { AsyncBoundary, Await, Call, Interruption, isEval, MAX_CALL_DEPTH, Runner, Slice, Task, Yield, normal, } from "./runner.js";
|
|
11
|
+
import { moduleName, resolveModule } from "./modules.js";
|
|
11
12
|
import { describeValue, isOpaque, rejectCircularInsertion, typeofValue } from "./references.js";
|
|
12
13
|
import { fn } from "./native.js";
|
|
13
14
|
import { capability, promiseLike } from "./promises.js";
|
|
@@ -252,6 +253,9 @@ export class Interpreter {
|
|
|
252
253
|
/** Template objects by site: a tag sees the same `strings` array every time its literal is evaluated, as in JS. */
|
|
253
254
|
templates = new WeakMap();
|
|
254
255
|
root;
|
|
256
|
+
globalScope;
|
|
257
|
+
modules = new Map();
|
|
258
|
+
namespaces = new Map();
|
|
255
259
|
constructor(options) {
|
|
256
260
|
this.tools = options.tools;
|
|
257
261
|
this.builtins = options.builtins;
|
|
@@ -261,10 +265,44 @@ export class Interpreter {
|
|
|
261
265
|
const globalScope = new Map([["this", { mutable: false, value: undefined }]]);
|
|
262
266
|
// Calling back into the program never reads frame state, so any frame serves; the root is always alive.
|
|
263
267
|
this.root = new Frame(this, new ScopeStack([globalScope]));
|
|
268
|
+
this.globalScope = globalScope;
|
|
264
269
|
for (const [name, value] of [...globals(this), ...(options.globals?.(this) ?? [])]) {
|
|
265
270
|
globalScope.set(name, { mutable: false, value });
|
|
266
271
|
}
|
|
267
272
|
}
|
|
273
|
+
/** A module, as `require` returns it and a default import binds it: a global, by its Node module name. */
|
|
274
|
+
module(specifier, site) {
|
|
275
|
+
const name = moduleName(specifier);
|
|
276
|
+
const cached = this.modules.get(name);
|
|
277
|
+
if (cached !== undefined)
|
|
278
|
+
return cached;
|
|
279
|
+
const module = resolveModule(name, (global) => {
|
|
280
|
+
const binding = this.globalScope.get(global);
|
|
281
|
+
return binding && "value" in binding ? binding.value : undefined;
|
|
282
|
+
});
|
|
283
|
+
if (module === undefined)
|
|
284
|
+
throw new PendingThrow("Error", `Cannot import "${specifier}": it isn't available in Code Mode, which can't load packages or files. Use the provided globals, or call tools through \`tools\`.`, site);
|
|
285
|
+
this.modules.set(name, module);
|
|
286
|
+
return module;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* What `import * as` and `import()` see: the module's own properties, and the module itself as `default`, the way
|
|
290
|
+
* Node presents a CommonJS module.
|
|
291
|
+
*/
|
|
292
|
+
namespace(specifier, site) {
|
|
293
|
+
const module = this.module(specifier, site);
|
|
294
|
+
const name = moduleName(specifier);
|
|
295
|
+
const cached = this.namespaces.get(name);
|
|
296
|
+
if (cached)
|
|
297
|
+
return cached;
|
|
298
|
+
const namespace = new Obj(null);
|
|
299
|
+
if (module instanceof Obj)
|
|
300
|
+
for (const [key, value] of entries(module))
|
|
301
|
+
define(namespace, key, value);
|
|
302
|
+
define(namespace, "default", module);
|
|
303
|
+
this.namespaces.set(name, namespace);
|
|
304
|
+
return namespace;
|
|
305
|
+
}
|
|
268
306
|
/** The program's body, as the first job: its completion value, copied out for the host. */
|
|
269
307
|
*main(program) {
|
|
270
308
|
return yield* toBoundary(this, yield* this.root.run(program));
|
|
@@ -310,11 +348,17 @@ class Frame {
|
|
|
310
348
|
// Keep top-level declarations separate so they can shadow builtins.
|
|
311
349
|
this.scopes.push();
|
|
312
350
|
try {
|
|
351
|
+
// Imports load before any code runs, as in a module.
|
|
352
|
+
for (const statement of program.body)
|
|
353
|
+
if (statement.type === "ImportDeclaration")
|
|
354
|
+
this.bindImport(statement);
|
|
313
355
|
this.predeclareLexical(program.body);
|
|
314
356
|
this.hoistFunctions(program.body);
|
|
315
357
|
this.hoistVars(program.body);
|
|
316
358
|
let value = undefined;
|
|
317
359
|
for (const [index, statement] of program.body.entries()) {
|
|
360
|
+
if (statement.type === "ImportDeclaration")
|
|
361
|
+
continue;
|
|
318
362
|
if (index === program.body.length - 1 && statement.type === "ExpressionStatement") {
|
|
319
363
|
value = yield* this.evaluateExpression(statement.expression);
|
|
320
364
|
break;
|
|
@@ -335,6 +379,26 @@ class Frame {
|
|
|
335
379
|
this.scopes.pop();
|
|
336
380
|
}
|
|
337
381
|
}
|
|
382
|
+
bindImport(node) {
|
|
383
|
+
const specifier = String(node.source.value);
|
|
384
|
+
if (node.specifiers.length === 0)
|
|
385
|
+
this.ctx.module(specifier, node);
|
|
386
|
+
for (const binding of node.specifiers) {
|
|
387
|
+
this.scopes.declare(binding.local.name, this.imported(binding, specifier), false, binding);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// A default import binds the module itself and a named import one of its properties, as Node imports CommonJS.
|
|
391
|
+
imported(binding, specifier) {
|
|
392
|
+
if (binding.type === "ImportDefaultSpecifier")
|
|
393
|
+
return this.ctx.module(specifier, binding);
|
|
394
|
+
const namespace = this.ctx.namespace(specifier, binding);
|
|
395
|
+
if (binding.type === "ImportNamespaceSpecifier")
|
|
396
|
+
return namespace;
|
|
397
|
+
const name = binding.imported.type === "Identifier" ? binding.imported.name : String(binding.imported.value);
|
|
398
|
+
if (!hasOwn(namespace, name))
|
|
399
|
+
throw syntaxError(`"${specifier}" has no export named "${name}".`, binding);
|
|
400
|
+
return getOwn(namespace, name);
|
|
401
|
+
}
|
|
338
402
|
// The call returns its promise at once. Arguments cross on the caller's stack, since `toJSON` is program code, and a
|
|
339
403
|
// promise argument is awaited first; the host call starts once they have crossed, so admission and hooks happen at
|
|
340
404
|
// the call site, and runs concurrently with the program.
|
|
@@ -404,6 +468,8 @@ class Frame {
|
|
|
404
468
|
return none;
|
|
405
469
|
case "FunctionDeclaration":
|
|
406
470
|
return none;
|
|
471
|
+
case "ImportDeclaration":
|
|
472
|
+
throw syntaxError("An import declaration may only appear at the top level of the program.", node);
|
|
407
473
|
default:
|
|
408
474
|
throw unsupportedSyntax(node.type, node);
|
|
409
475
|
}
|
|
@@ -1311,6 +1377,8 @@ class Frame {
|
|
|
1311
1377
|
return this.evaluateYieldExpression(node);
|
|
1312
1378
|
case "NewExpression":
|
|
1313
1379
|
return this.evaluateNewExpression(node);
|
|
1380
|
+
case "ImportExpression":
|
|
1381
|
+
return this.evaluateImportExpression(node);
|
|
1314
1382
|
default:
|
|
1315
1383
|
throw unsupportedSyntax(node.type, node);
|
|
1316
1384
|
}
|
|
@@ -1625,6 +1693,22 @@ class Frame {
|
|
|
1625
1693
|
}
|
|
1626
1694
|
throw typeError("Update target must be an Identifier or MemberExpression.", argument);
|
|
1627
1695
|
}
|
|
1696
|
+
// A module that fails to load rejects the promise, as a failed dynamic import does.
|
|
1697
|
+
*evaluateImportExpression(node) {
|
|
1698
|
+
const specifier = coerceToString(yield* this.evaluateExpression(node.source));
|
|
1699
|
+
if (node.options)
|
|
1700
|
+
yield* this.evaluateExpression(node.options);
|
|
1701
|
+
const promise = capability(this.ctx, node);
|
|
1702
|
+
try {
|
|
1703
|
+
promise.resolve(this.ctx.namespace(specifier, node));
|
|
1704
|
+
}
|
|
1705
|
+
catch (error) {
|
|
1706
|
+
if (!(error instanceof PendingThrow))
|
|
1707
|
+
throw error;
|
|
1708
|
+
promise.reject(error);
|
|
1709
|
+
}
|
|
1710
|
+
return promise.promise;
|
|
1711
|
+
}
|
|
1628
1712
|
// EvaluateCall: a member callee supplies its base object as `this`; anything else calls with undefined.
|
|
1629
1713
|
*evaluateCallExpression(node) {
|
|
1630
1714
|
const callee = node.callee;
|
|
@@ -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,23 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { Snapshot } from "./vm/snapshot/snapshot.js";
|
|
3
|
+
declare const SuspendError_base: Schema.Class<SuspendError, Schema.TaggedStruct<"SuspendError", {
|
|
4
|
+
readonly message: Schema.String;
|
|
5
|
+
readonly running: Schema.$Array<Schema.Struct<{
|
|
6
|
+
readonly path: Schema.String;
|
|
7
|
+
}>>;
|
|
8
|
+
}>, import("effect/Cause").YieldableError>;
|
|
9
|
+
/**
|
|
10
|
+
* A suspend that failed, leaving the run running: `running` names each host call that blocked it by its path
|
|
11
|
+
* (`tools.x.y`, `search`, or an extension function's path such as `fetch`). Empty when the state couldn't be
|
|
12
|
+
* saved or was too large.
|
|
13
|
+
*/
|
|
14
|
+
export declare class SuspendError extends SuspendError_base {
|
|
15
|
+
}
|
|
16
|
+
export declare class SnapshotError extends Error {
|
|
17
|
+
readonly name = "SnapshotError";
|
|
18
|
+
}
|
|
19
|
+
/** A snapshot as bytes: UTF-8 JSON. */
|
|
20
|
+
export declare const toBytes: (snapshot: Snapshot) => Uint8Array;
|
|
21
|
+
/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
|
|
22
|
+
export declare const fromBytes: (bytes: Uint8Array) => unknown;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* A suspend that failed, leaving the run running: `running` names each host call that blocked it by its path
|
|
4
|
+
* (`tools.x.y`, `search`, or an extension function's path such as `fetch`). Empty when the state couldn't be
|
|
5
|
+
* saved or was too large.
|
|
6
|
+
*/
|
|
7
|
+
export class SuspendError extends Schema.TaggedError()("SuspendError", {
|
|
8
|
+
message: Schema.String,
|
|
9
|
+
running: Schema.Array(Schema.Struct({ path: Schema.String })),
|
|
10
|
+
}) {
|
|
11
|
+
}
|
|
12
|
+
export class SnapshotError extends Error {
|
|
13
|
+
name = "SnapshotError";
|
|
14
|
+
}
|
|
15
|
+
/** A snapshot as bytes: UTF-8 JSON. */
|
|
16
|
+
export const toBytes = (snapshot) => new TextEncoder().encode(JSON.stringify(snapshot));
|
|
17
|
+
/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
|
|
18
|
+
export const fromBytes = (bytes) => {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
throw new SnapshotError(`Invalid snapshot: the bytes are not UTF-8 JSON (${String(error)}).`);
|
|
24
|
+
}
|
|
25
|
+
};
|
package/dist/tool-runtime.d.ts
CHANGED
|
@@ -18,7 +18,10 @@ export type ToolInvocation = {
|
|
|
18
18
|
readonly name: string;
|
|
19
19
|
readonly input: unknown;
|
|
20
20
|
};
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* A call the program is making to an extension global or module function, with its arguments. A module function is
|
|
23
|
+
* named by its path from the module, such as `fs.readFile`.
|
|
24
|
+
*/
|
|
22
25
|
export type ExtensionInvocation = {
|
|
23
26
|
readonly extension: string;
|
|
24
27
|
readonly name: string;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Diagnostic } from "../codemode.js";
|
|
2
|
+
import { type AstNode } from "./runtime/model.js";
|
|
3
|
+
import { type Callable, PromiseObj, type Value } from "./runtime/objects.js";
|
|
4
|
+
import { type Completion } from "./runtime/signals.js";
|
|
5
|
+
import { type Job, type Reaction, Resolving } from "./scheduler.js";
|
|
6
|
+
import { type Frame, type FrameMode, type Vm } from "./vm.js";
|
|
7
|
+
export type SettlingTarget = {
|
|
8
|
+
resolve(value: Value): void;
|
|
9
|
+
reject(reason: unknown): void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Target for a `Promise.prototype.finally` cleanup frame running in `Mode.SETTLE`.
|
|
13
|
+
* When the cleanup completes with `result`, chains `finallyCleaned` and `finallyOutcome` reactions.
|
|
14
|
+
*/
|
|
15
|
+
export declare class FinallyTarget implements SettlingTarget {
|
|
16
|
+
readonly vm: Vm;
|
|
17
|
+
readonly made: Resolving;
|
|
18
|
+
readonly site: AstNode | undefined;
|
|
19
|
+
readonly outcome: Completion;
|
|
20
|
+
constructor(vm: Vm, made: Resolving, site: AstNode | undefined, outcome: Completion);
|
|
21
|
+
resolve(result: Value): void;
|
|
22
|
+
reject(reason: unknown): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Target for `$vmAsyncFromSyncClose`: regardless of whether `iterator.return()` succeeds or throws,
|
|
26
|
+
* the original rejection reason wins.
|
|
27
|
+
*/
|
|
28
|
+
export declare class AsyncFromSyncCloseTarget implements SettlingTarget {
|
|
29
|
+
readonly made: Resolving;
|
|
30
|
+
readonly reason: unknown;
|
|
31
|
+
constructor(made: Resolving, reason: unknown);
|
|
32
|
+
resolve(_value: Value): void;
|
|
33
|
+
reject(_error: unknown): void;
|
|
34
|
+
}
|
|
35
|
+
type Parked = {
|
|
36
|
+
readonly promise: PromiseObj;
|
|
37
|
+
readonly node: AstNode | undefined;
|
|
38
|
+
readonly result: Value;
|
|
39
|
+
};
|
|
40
|
+
export declare class VmScheduler {
|
|
41
|
+
readonly vm: Vm;
|
|
42
|
+
intrinsicThen: Callable | undefined;
|
|
43
|
+
readonly parked: Map<Frame, Parked>;
|
|
44
|
+
readonly queue: Array<Job>;
|
|
45
|
+
head: number;
|
|
46
|
+
pending: number;
|
|
47
|
+
nextID: number;
|
|
48
|
+
readonly unhandled: Map<PromiseObj, {
|
|
49
|
+
readonly kind: "ExecutionFailure" | "InvalidDataValue" | "InvalidToolInput" | "InvalidToolOutput" | "ParseError" | "TimeoutExceeded" | "ToolCallLimitExceeded" | "ToolFailure" | "Truncated" | "UnknownTool" | "UnsupportedSyntax";
|
|
50
|
+
readonly message: string;
|
|
51
|
+
readonly location?: {
|
|
52
|
+
readonly line: number;
|
|
53
|
+
readonly column: number;
|
|
54
|
+
} | undefined;
|
|
55
|
+
readonly suggestions?: readonly string[] | undefined;
|
|
56
|
+
}>;
|
|
57
|
+
constructor(vm: Vm);
|
|
58
|
+
get hasJobs(): boolean;
|
|
59
|
+
newPromise(): PromiseObj;
|
|
60
|
+
resolving(promise: PromiseObj, site?: AstNode): Resolving;
|
|
61
|
+
then(promise: PromiseObj, reaction: Reaction): void;
|
|
62
|
+
detach(promise: PromiseObj, reaction: Reaction): void;
|
|
63
|
+
promiseResolve(value: Value): PromiseObj;
|
|
64
|
+
reject(promise: PromiseObj, reason: unknown): void;
|
|
65
|
+
resolveWith(promise: PromiseObj, resolution: Value, site?: AstNode): void;
|
|
66
|
+
settle(promise: PromiseObj, completion: Completion): void;
|
|
67
|
+
enqueue(job: Job): void;
|
|
68
|
+
dequeue(): Job | undefined;
|
|
69
|
+
compact(): void;
|
|
70
|
+
park(frames: Array<Frame>, promise: PromiseObj, node: AstNode | undefined, result: Value): void;
|
|
71
|
+
stuckAwait(): AstNode | undefined;
|
|
72
|
+
diagnostics(): Array<Diagnostic>;
|
|
73
|
+
beginJob(job: Job): void;
|
|
74
|
+
enterSettlingCall(callerFrame: Frame | undefined, callable: Value, thisVal: Value, args: Array<Value>, mode: FrameMode, target: SettlingTarget, answer: Value, site: AstNode | undefined): number;
|
|
75
|
+
handlePromisePush(callerFrame: Frame, kindName: string, slots: Array<unknown>, site: AstNode | undefined): number | undefined;
|
|
76
|
+
}
|
|
77
|
+
export declare const registerAsyncIntrinsics: (map: Map<string, unknown>, vm: Vm) => void;
|
|
78
|
+
export {};
|
package/dist/vm/async.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { normalizeError, locate } from "./runtime/errors.js";
|
|
2
|
+
import { MAX_PENDING_PROMISES } from "./runtime/limits.js";
|
|
3
|
+
import { IteratorSymbol, rangeError, typeError } from "./runtime/model.js";
|
|
4
|
+
import { Arr, BoundFn, Fn, get, iteratorResult, Obj, Position, PromiseObj, Resolver, } from "./runtime/objects.js";
|
|
5
|
+
import { builtinCursor, isThenable } from "./runtime/operations.js";
|
|
6
|
+
import { adoptThenable, capability, finishCombination, react } from "./runtime/promises.js";
|
|
7
|
+
import { describeValue, typeofValue } from "./runtime/references.js";
|
|
8
|
+
import { abrupt, Interruption, Invoke, MAX_CALL_DEPTH, normal } from "./runtime/signals.js";
|
|
9
|
+
import { Resolving } from "./scheduler.js";
|
|
10
|
+
import { internalCode, VmIntrinsic } from "./internal-prelude.js";
|
|
11
|
+
import { CodeFlags } from "./opcodes.js";
|
|
12
|
+
import { Mode } from "./vm.js";
|
|
13
|
+
/**
|
|
14
|
+
* Target for a `Promise.prototype.finally` cleanup frame running in `Mode.SETTLE`.
|
|
15
|
+
* When the cleanup completes with `result`, chains `finallyCleaned` and `finallyOutcome` reactions.
|
|
16
|
+
*/
|
|
17
|
+
export class FinallyTarget {
|
|
18
|
+
vm;
|
|
19
|
+
made;
|
|
20
|
+
site;
|
|
21
|
+
outcome;
|
|
22
|
+
constructor(vm, made, site, outcome) {
|
|
23
|
+
this.vm = vm;
|
|
24
|
+
this.made = made;
|
|
25
|
+
this.site = site;
|
|
26
|
+
this.outcome = outcome;
|
|
27
|
+
}
|
|
28
|
+
resolve(result) {
|
|
29
|
+
const after = this.vm.scheduler.promiseResolve(result);
|
|
30
|
+
const derived = capability(this.vm.ctx, this.site);
|
|
31
|
+
this.vm.scheduler.then(after, { kind: "finallyCleaned", target: derived });
|
|
32
|
+
this.vm.scheduler.then(derived.promise, {
|
|
33
|
+
kind: "finallyOutcome",
|
|
34
|
+
target: this.made,
|
|
35
|
+
outcome: this.outcome,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
reject(reason) {
|
|
39
|
+
this.made.reject(reason);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Target for `$vmAsyncFromSyncClose`: regardless of whether `iterator.return()` succeeds or throws,
|
|
44
|
+
* the original rejection reason wins.
|
|
45
|
+
*/
|
|
46
|
+
export class AsyncFromSyncCloseTarget {
|
|
47
|
+
made;
|
|
48
|
+
reason;
|
|
49
|
+
constructor(made, reason) {
|
|
50
|
+
this.made = made;
|
|
51
|
+
this.reason = reason;
|
|
52
|
+
}
|
|
53
|
+
resolve(_value) {
|
|
54
|
+
this.made.reject(this.reason);
|
|
55
|
+
}
|
|
56
|
+
reject(_error) {
|
|
57
|
+
this.made.reject(this.reason);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export class VmScheduler {
|
|
61
|
+
vm;
|
|
62
|
+
intrinsicThen;
|
|
63
|
+
parked = new Map();
|
|
64
|
+
queue = [];
|
|
65
|
+
head = 0;
|
|
66
|
+
pending = 0;
|
|
67
|
+
nextID = 0;
|
|
68
|
+
unhandled = new Map();
|
|
69
|
+
constructor(vm) {
|
|
70
|
+
this.vm = vm;
|
|
71
|
+
}
|
|
72
|
+
get hasJobs() {
|
|
73
|
+
return this.head < this.queue.length;
|
|
74
|
+
}
|
|
75
|
+
newPromise() {
|
|
76
|
+
if (this.pending >= MAX_PENDING_PROMISES) {
|
|
77
|
+
throw rangeError(`Too many pending promises (limit ${MAX_PENDING_PROMISES}); await promises before creating more.`);
|
|
78
|
+
}
|
|
79
|
+
this.pending++;
|
|
80
|
+
return new PromiseObj(this.vm.ctx.builtins.Promise, this.nextID++);
|
|
81
|
+
}
|
|
82
|
+
resolving(promise, site) {
|
|
83
|
+
return new Resolving(this, promise, site);
|
|
84
|
+
}
|
|
85
|
+
then(promise, reaction) {
|
|
86
|
+
if (promise.state === "pending") {
|
|
87
|
+
promise.reactions.push(reaction);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
if (promise.state === "rejected")
|
|
91
|
+
this.unhandled.delete(promise);
|
|
92
|
+
const settled = promise.state === "fulfilled" ? normal(promise.result) : abrupt(promise.result);
|
|
93
|
+
this.enqueue({ kind: "reaction", reaction, settled });
|
|
94
|
+
}
|
|
95
|
+
promise.handled = true;
|
|
96
|
+
}
|
|
97
|
+
detach(promise, reaction) {
|
|
98
|
+
const index = promise.reactions.indexOf(reaction);
|
|
99
|
+
if (index >= 0)
|
|
100
|
+
promise.reactions.splice(index, 1);
|
|
101
|
+
}
|
|
102
|
+
promiseResolve(value) {
|
|
103
|
+
if (value instanceof PromiseObj)
|
|
104
|
+
return value;
|
|
105
|
+
const promise = this.newPromise();
|
|
106
|
+
this.resolveWith(promise, value);
|
|
107
|
+
return promise;
|
|
108
|
+
}
|
|
109
|
+
reject(promise, reason) {
|
|
110
|
+
this.settle(promise, abrupt(reason));
|
|
111
|
+
}
|
|
112
|
+
resolveWith(promise, resolution, site) {
|
|
113
|
+
if (resolution === promise) {
|
|
114
|
+
const cycle = typeError("Chaining cycle detected: a promise cannot resolve with itself.", site);
|
|
115
|
+
this.settle(promise, abrupt(cycle));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (!(resolution instanceof Obj)) {
|
|
119
|
+
this.settle(promise, normal(resolution));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const then = get(resolution, "then");
|
|
123
|
+
if (typeofValue(then) !== "function") {
|
|
124
|
+
this.settle(promise, normal(resolution));
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
this.enqueue({ kind: "thenable", promise, thenable: resolution, then });
|
|
128
|
+
}
|
|
129
|
+
settle(promise, completion) {
|
|
130
|
+
if (promise.state !== "pending")
|
|
131
|
+
return;
|
|
132
|
+
this.pending--;
|
|
133
|
+
promise.state = completion.ok ? "fulfilled" : "rejected";
|
|
134
|
+
promise.result = completion.ok ? completion.value : completion.error;
|
|
135
|
+
const reactions = promise.reactions;
|
|
136
|
+
promise.reactions = [];
|
|
137
|
+
if (!completion.ok && !promise.handled && !(completion.error instanceof Interruption)) {
|
|
138
|
+
const failure = normalizeError(completion.error);
|
|
139
|
+
this.unhandled.set(promise, {
|
|
140
|
+
...failure,
|
|
141
|
+
message: `Unhandled rejection from an un-awaited promise: ${failure.message}`,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
for (const reaction of reactions) {
|
|
145
|
+
this.enqueue({ kind: "reaction", reaction, settled: completion });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
enqueue(job) {
|
|
149
|
+
this.queue.push(job);
|
|
150
|
+
}
|
|
151
|
+
dequeue() {
|
|
152
|
+
if (this.head === this.queue.length) {
|
|
153
|
+
if (this.head > 0) {
|
|
154
|
+
this.queue.length = 0;
|
|
155
|
+
this.head = 0;
|
|
156
|
+
}
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
const job = this.queue[this.head++];
|
|
160
|
+
if (this.head > 4096 && this.head * 2 > this.queue.length) {
|
|
161
|
+
this.queue.splice(0, this.head);
|
|
162
|
+
this.head = 0;
|
|
163
|
+
}
|
|
164
|
+
return job;
|
|
165
|
+
}
|
|
166
|
+
compact() {
|
|
167
|
+
this.queue.splice(0, this.head);
|
|
168
|
+
this.head = 0;
|
|
169
|
+
}
|
|
170
|
+
park(frames, promise, node, result) {
|
|
171
|
+
this.parked.set(frames[0], { promise, node, result });
|
|
172
|
+
this.then(promise, { kind: "await", frames });
|
|
173
|
+
}
|
|
174
|
+
stuckAwait() {
|
|
175
|
+
const results = new Set();
|
|
176
|
+
for (const parked of this.parked.values()) {
|
|
177
|
+
if (parked.result instanceof PromiseObj)
|
|
178
|
+
results.add(parked.result);
|
|
179
|
+
}
|
|
180
|
+
const located = [...this.parked.values()].filter((parked) => parked.node !== undefined);
|
|
181
|
+
return (located.find((parked) => !results.has(parked.promise)) ?? located[0])?.node;
|
|
182
|
+
}
|
|
183
|
+
diagnostics() {
|
|
184
|
+
return [...this.unhandled].sort(([left], [right]) => left.id - right.id).map(([, diagnostic]) => diagnostic);
|
|
185
|
+
}
|
|
186
|
+
beginJob(job) {
|
|
187
|
+
const vm = this.vm;
|
|
188
|
+
if (job.kind === "thenable") {
|
|
189
|
+
const call = adoptThenable(vm.ctx, job.promise, job.thenable, job.then);
|
|
190
|
+
if (call === undefined || call.kind !== "thenable")
|
|
191
|
+
return;
|
|
192
|
+
this.enterSettlingCall(undefined, call.then, call.thenable, call.functions, Mode.REJECT_ONLY, call.resolving, undefined, undefined);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const reaction = job.reaction;
|
|
196
|
+
if (reaction.kind === "await") {
|
|
197
|
+
const frames = reaction.frames;
|
|
198
|
+
this.parked.delete(frames[0]);
|
|
199
|
+
for (const f of frames) {
|
|
200
|
+
if (f.counted)
|
|
201
|
+
vm.depth++;
|
|
202
|
+
vm.frames.push(f);
|
|
203
|
+
}
|
|
204
|
+
const top = frames[frames.length - 1];
|
|
205
|
+
if (job.settled.ok) {
|
|
206
|
+
top.regs[top.sp++] = job.settled.value;
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
top.pc--;
|
|
210
|
+
vm.unwind(job.settled.error);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (reaction.kind === "asyncFromSync") {
|
|
214
|
+
if (job.settled.ok) {
|
|
215
|
+
reaction.made.resolve(iteratorResult(vm.ctx.builtins.Object, job.settled.value, reaction.done));
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (reaction.done ||
|
|
219
|
+
reaction.close === undefined ||
|
|
220
|
+
job.settled.error instanceof Interruption ||
|
|
221
|
+
reaction.close instanceof Position) {
|
|
222
|
+
reaction.made.reject(job.settled.error);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
vm.pushInternal(internalCode("$vmAsyncFromSyncClose"), [reaction.close.iterator], reaction.site, Mode.SETTLE, new AsyncFromSyncCloseTarget(reaction.made, job.settled.error));
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const call = react(vm.ctx, reaction, job.settled);
|
|
229
|
+
if (call === undefined)
|
|
230
|
+
return;
|
|
231
|
+
if (call.kind === "handler") {
|
|
232
|
+
this.enterSettlingCall(undefined, call.handler, undefined, [call.argument], Mode.SETTLE, call.derived, undefined, undefined);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (call.kind === "finally") {
|
|
236
|
+
this.enterSettlingCall(undefined, call.cleanup, undefined, [], Mode.SETTLE, new FinallyTarget(vm, call.made, call.site, call.outcome), undefined, call.site);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
enterSettlingCall(callerFrame, callable, thisVal, args, mode, target, answer, site) {
|
|
240
|
+
const vm = this.vm;
|
|
241
|
+
while (callable instanceof BoundFn) {
|
|
242
|
+
thisVal = callable.boundThis;
|
|
243
|
+
args = [...callable.boundArgs, ...args];
|
|
244
|
+
callable = callable.target;
|
|
245
|
+
}
|
|
246
|
+
if (callable instanceof Resolver) {
|
|
247
|
+
try {
|
|
248
|
+
const res = callable.invoke(thisVal, args);
|
|
249
|
+
if (mode === Mode.SETTLE)
|
|
250
|
+
target.resolve(res);
|
|
251
|
+
}
|
|
252
|
+
catch (err) {
|
|
253
|
+
target.reject(locate(err, site ?? vm.currentSite()));
|
|
254
|
+
}
|
|
255
|
+
if (answer !== undefined && callerFrame !== undefined) {
|
|
256
|
+
callerFrame.regs[callerFrame.sp++] = answer;
|
|
257
|
+
}
|
|
258
|
+
return 0;
|
|
259
|
+
}
|
|
260
|
+
if (callable instanceof Fn) {
|
|
261
|
+
const code = callable.code;
|
|
262
|
+
if ((code.flags & (CodeFlags.ASYNC | CodeFlags.GENERATOR)) === 0) {
|
|
263
|
+
const counted = (code.flags & CodeFlags.INTERNAL) === 0;
|
|
264
|
+
if (counted && vm.depth > MAX_CALL_DEPTH) {
|
|
265
|
+
target.reject(rangeError("Maximum call stack size exceeded", site ?? vm.currentSite()));
|
|
266
|
+
if (answer !== undefined && callerFrame !== undefined) {
|
|
267
|
+
callerFrame.regs[callerFrame.sp++] = answer;
|
|
268
|
+
}
|
|
269
|
+
return 0;
|
|
270
|
+
}
|
|
271
|
+
vm.pushFn(callable, thisVal, args, site ?? callerFrame?.origin, mode, target, answer);
|
|
272
|
+
return 1;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
vm.pushInternal(internalCode("$vmSettleCall"), [callable, thisVal, new Arr(vm.ctx.builtins.Array, args)], site ?? callerFrame?.origin, mode, target, answer);
|
|
276
|
+
return 1;
|
|
277
|
+
}
|
|
278
|
+
handlePromisePush(callerFrame, kindName, slots, site) {
|
|
279
|
+
// An executor's result is ignored and only its failure rejects; `Promise.try` settles with its callback's outcome.
|
|
280
|
+
if (kindName === "Promise executor" || kindName === "Promise.try") {
|
|
281
|
+
const made = slots[0];
|
|
282
|
+
const mode = kindName === "Promise.try" ? Mode.SETTLE : Mode.REJECT_ONLY;
|
|
283
|
+
return this.enterSettlingCall(callerFrame, slots[1], undefined, slots[2], mode, made, made.promise, site);
|
|
284
|
+
}
|
|
285
|
+
if (kindName === "Promise combinator") {
|
|
286
|
+
const made = slots[0];
|
|
287
|
+
this.vm.pushInternal(internalCode("$vmCombine"), [slots[1], slots[2]], site ?? callerFrame.origin, Mode.REJECT_ONLY, made, made.promise);
|
|
288
|
+
return 1;
|
|
289
|
+
}
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
export const registerAsyncIntrinsics = (map, vm) => {
|
|
294
|
+
const ctx = vm.ctx;
|
|
295
|
+
const builtins = ctx.builtins;
|
|
296
|
+
const add = (name, fn) => {
|
|
297
|
+
map.set(name, new VmIntrinsic(name, fn));
|
|
298
|
+
};
|
|
299
|
+
add("$Apply", ([callable, thisVal, args]) => {
|
|
300
|
+
const argItems = args instanceof Arr ? [...args.items] : [];
|
|
301
|
+
return new Invoke(callable, thisVal, argItems, vm.currentSite());
|
|
302
|
+
});
|
|
303
|
+
add("$CombineStart", ([nameVal, items]) => {
|
|
304
|
+
const name = nameVal;
|
|
305
|
+
const isIter = builtinCursor(builtins, items) !== undefined ||
|
|
306
|
+
(items instanceof Obj && get(items, IteratorSymbol) !== undefined && get(items, IteratorSymbol) !== null);
|
|
307
|
+
if (!isIter) {
|
|
308
|
+
throw typeError(`Promise.${name} expects a synchronous iterable, received ${describeValue(items)}.`);
|
|
309
|
+
}
|
|
310
|
+
const top = vm.frames[vm.frames.length - 1];
|
|
311
|
+
const made = top.target;
|
|
312
|
+
const comb = { name, made, values: [], reasons: [], remaining: 1, attached: [] };
|
|
313
|
+
return comb;
|
|
314
|
+
});
|
|
315
|
+
add("$CombineAdd", ([combVal, item]) => {
|
|
316
|
+
const c = combVal;
|
|
317
|
+
const p = vm.scheduler.promiseResolve(item);
|
|
318
|
+
const reaction = { kind: "combinator", combination: c, index: c.values.length, called: false };
|
|
319
|
+
c.values.push(undefined);
|
|
320
|
+
c.reasons.push(undefined);
|
|
321
|
+
c.remaining++;
|
|
322
|
+
c.attached.push([p, reaction]);
|
|
323
|
+
vm.scheduler.then(p, reaction);
|
|
324
|
+
return undefined;
|
|
325
|
+
});
|
|
326
|
+
add("$CombineEnd", ([combVal]) => {
|
|
327
|
+
const c = combVal;
|
|
328
|
+
if (c.name === "race" && c.attached.length === 0) {
|
|
329
|
+
throw typeError("Promise.race([]) would never settle; provide at least one promise or value.");
|
|
330
|
+
}
|
|
331
|
+
finishCombination(ctx, c);
|
|
332
|
+
return undefined;
|
|
333
|
+
});
|
|
334
|
+
add("$IsThenable", ([val]) => isThenable(val));
|
|
335
|
+
};
|