@opencode/codemode 2.2.5 → 2.2.7
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 +164 -7
- package/dist/codemode.d.ts +60 -4
- package/dist/codemode.js +63 -8
- package/dist/extension.d.ts +27 -9
- package/dist/extension.js +7 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interpreter/callback.d.ts +12 -1
- package/dist/interpreter/callback.js +13 -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 +88 -4
- package/dist/interpreter/modules.d.ts +4 -0
- package/dist/interpreter/modules.js +33 -0
- package/dist/interpreter/promises.js +2 -2
- package/dist/interpreter/runner.d.ts +1 -11
- package/dist/interpreter/runner.js +1 -13
- package/dist/stdlib/iterator.js +1 -2
- package/dist/suspend-error.d.ts +24 -0
- package/dist/suspend-error.js +25 -0
- package/dist/tool-runtime.d.ts +31 -7
- package/dist/tool-runtime.js +116 -26
- package/dist/tool.d.ts +45 -2
- package/dist/tool.js +30 -7
- 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 +106 -0
- package/dist/vm/host.js +231 -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 +68 -0
- package/dist/vm/runtime/driver.js +254 -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 +442 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +337 -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 +53 -0
- package/dist/vm/runtime/interpreter.js +96 -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 +190 -0
- package/dist/vm/scheduler.js +74 -0
- package/dist/vm/snapshot/codec.d.ts +117 -0
- package/dist/vm/snapshot/codec.js +848 -0
- package/dist/vm/snapshot/snapshot.d.ts +53 -0
- package/dist/vm/snapshot/snapshot.js +74 -0
- package/dist/vm/snapshot.d.ts +32 -0
- package/dist/vm/snapshot.js +309 -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 +226 -0
- package/dist/vm/vm.js +2931 -0
- package/package.json +8 -5
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { locate } from "./runtime/errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Host work the program waits on. The VM queues it and hands it to the driver at its next stop; the driver runs it and
|
|
4
|
+
* reports how it ended with `Vm.complete`. A concurrent call's outcome waits in the inbox and settles its promise in a
|
|
5
|
+
* later job; a blocking call stops the VM, and no other job runs until it ends.
|
|
6
|
+
*/
|
|
7
|
+
export class HostCall {
|
|
8
|
+
id;
|
|
9
|
+
key;
|
|
10
|
+
request;
|
|
11
|
+
label;
|
|
12
|
+
blocking;
|
|
13
|
+
result;
|
|
14
|
+
resolving;
|
|
15
|
+
site;
|
|
16
|
+
/**
|
|
17
|
+
* Where the call is: `queued` (written ahead, not started), `running` (or in `start` for a durable tool),
|
|
18
|
+
* `started` (a durable tool whose `start` returned its `data`, now in `wait`), or `completed`.
|
|
19
|
+
*/
|
|
20
|
+
status = "queued";
|
|
21
|
+
/** The JSON data a durable tool's `start` returned, saved so a resumed run can call `wait(data)`. */
|
|
22
|
+
data = undefined;
|
|
23
|
+
/** Whether a tool call has already been counted against `maxToolCalls` and recorded in `toolCalls`. */
|
|
24
|
+
admitted = false;
|
|
25
|
+
constructor(id,
|
|
26
|
+
/** The call's stable idempotency key, `<runId>:<callId>`. */
|
|
27
|
+
key, request,
|
|
28
|
+
/** What runs: a tool path, or an extension function's label. */
|
|
29
|
+
label,
|
|
30
|
+
/** The running job waits for it: its outcome continues the frame that asked, before any other job. */
|
|
31
|
+
blocking,
|
|
32
|
+
/** How a concurrent call's result becomes a program value: a tool's crosses as JSON, an extension's is one. */
|
|
33
|
+
result,
|
|
34
|
+
/** Settles a concurrent call's promise. */
|
|
35
|
+
resolving,
|
|
36
|
+
/** The call that started the work, where a defect it raises is located. */
|
|
37
|
+
site) {
|
|
38
|
+
this.id = id;
|
|
39
|
+
this.key = key;
|
|
40
|
+
this.request = request;
|
|
41
|
+
this.label = label;
|
|
42
|
+
this.blocking = blocking;
|
|
43
|
+
this.result = result;
|
|
44
|
+
this.resolving = resolving;
|
|
45
|
+
this.site = site;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* CreateResolvingFunctions as data: settles one promise at most once, however many `Resolver`s share it. `reject` takes
|
|
50
|
+
* the failure as thrown; one without a location is located at `site`, the call that created the promise.
|
|
51
|
+
*/
|
|
52
|
+
export class Resolving {
|
|
53
|
+
scheduler;
|
|
54
|
+
promise;
|
|
55
|
+
site;
|
|
56
|
+
done = false;
|
|
57
|
+
constructor(scheduler, promise, site) {
|
|
58
|
+
this.scheduler = scheduler;
|
|
59
|
+
this.promise = promise;
|
|
60
|
+
this.site = site;
|
|
61
|
+
}
|
|
62
|
+
resolve(resolution) {
|
|
63
|
+
if (this.done)
|
|
64
|
+
return;
|
|
65
|
+
this.done = true;
|
|
66
|
+
this.scheduler.resolveWith(this.promise, resolution, this.site);
|
|
67
|
+
}
|
|
68
|
+
reject(reason) {
|
|
69
|
+
if (this.done)
|
|
70
|
+
return;
|
|
71
|
+
this.done = true;
|
|
72
|
+
this.scheduler.reject(this.promise, locate(reason, this.site));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { type AstNode } from "../runtime/model.js";
|
|
2
|
+
import { Native, Obj } from "../runtime/objects.js";
|
|
3
|
+
import { Interruption } from "../runtime/signals.js";
|
|
4
|
+
/** A plain JSON value, as a snapshot holds it. */
|
|
5
|
+
export type Json = null | boolean | number | string | ReadonlyArray<Json> | {
|
|
6
|
+
readonly [key: string]: Json;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* A value inside a record: a JSON primitive, or a tag (an array whose first item says what it is):
|
|
10
|
+
* - `["u"]` undefined, `["n", "NaN" | "Infinity" | "-Infinity" | "-0"]` a number JSON cannot spell, `["-"]` an absent
|
|
11
|
+
* field, `["h"]` an array hole;
|
|
12
|
+
* - `["s", name]` an interpreter symbol;
|
|
13
|
+
* - `["i", name]` a built-in or host global, unchanged since the runtime was made;
|
|
14
|
+
* - `["a", source, index]` an AST node (a call site, an error's location);
|
|
15
|
+
* - `["x", data]` something the engine names itself (the VM's compiled code, by source and function index);
|
|
16
|
+
* - `["r", id]` another record.
|
|
17
|
+
*/
|
|
18
|
+
export type Encoded = string | number | boolean | null | ReadonlyArray<Json>;
|
|
19
|
+
/** One object of the graph. `t` says how to read the rest. */
|
|
20
|
+
export type Record = {
|
|
21
|
+
readonly t: string;
|
|
22
|
+
readonly [field: string]: Json;
|
|
23
|
+
};
|
|
24
|
+
/** A saved state can't be read, or doesn't fit this runtime. */
|
|
25
|
+
import { SnapshotError } from "../../suspend-error.js";
|
|
26
|
+
export { SnapshotError };
|
|
27
|
+
/**
|
|
28
|
+
* Where a function inside a value an extension returned came from, so a saved state can name it instead of losing it:
|
|
29
|
+
* the extension, the value its function returned (`root`), which the extension's `snapshot` hooks save, and the path
|
|
30
|
+
* from there to the function.
|
|
31
|
+
*/
|
|
32
|
+
export type HostOrigin = {
|
|
33
|
+
readonly extension: string;
|
|
34
|
+
readonly root: object;
|
|
35
|
+
readonly path: ReadonlyArray<string | number>;
|
|
36
|
+
/** What the program calls it, for its errors (`fetch.text`). */
|
|
37
|
+
readonly label: string;
|
|
38
|
+
};
|
|
39
|
+
/** The extension bridge's side of saving and restoring those functions; see `extensionGlobals`. */
|
|
40
|
+
export type HostBridge = {
|
|
41
|
+
/** Each extension's name, and whether it has `snapshot` hooks: a state resumes only where both match. */
|
|
42
|
+
readonly extensions: ReadonlyArray<{
|
|
43
|
+
readonly name: string;
|
|
44
|
+
readonly hooked: boolean;
|
|
45
|
+
}>;
|
|
46
|
+
/** Where a program function wrapping a host function came from, when its extension can save it. */
|
|
47
|
+
readonly origin: (value: object) => HostOrigin | undefined;
|
|
48
|
+
/** A returned value as its extension saves it. */
|
|
49
|
+
readonly encode: (extension: string, root: object) => unknown;
|
|
50
|
+
/** A returned value back from what `encode` gave. */
|
|
51
|
+
readonly decode: (extension: string, data: unknown) => object;
|
|
52
|
+
/** Makes `target`, a restored program function, call the host function `origin` leads to. */
|
|
53
|
+
readonly rewrap: (target: Native, origin: HostOrigin) => void;
|
|
54
|
+
};
|
|
55
|
+
/** What the encoder asks of the runtime whose state it saves. */
|
|
56
|
+
export type EncodeEnv = {
|
|
57
|
+
/** The name of a built-in or host global object. */
|
|
58
|
+
readonly nameOf: (value: Obj) => string | undefined;
|
|
59
|
+
/** Whether a named object is still as the runtime made it, so its name alone restores it. */
|
|
60
|
+
readonly unchanged: (value: Obj) => boolean;
|
|
61
|
+
/** The anchors: objects the restored runtime already has, filled in place (the VM, its scheduler, ...). */
|
|
62
|
+
readonly anchorOf: (value: object) => string | undefined;
|
|
63
|
+
readonly nodeId: (node: AstNode) => readonly [source: string, index: number] | undefined;
|
|
64
|
+
/** Values with no data form are reported here; strict encoding throws on the first instead. */
|
|
65
|
+
readonly lost: Array<string>;
|
|
66
|
+
readonly strict: boolean;
|
|
67
|
+
readonly hosts?: HostBridge | undefined;
|
|
68
|
+
/** More classes to save, an engine's own (the VM's frames and closures), after the built-in ones. */
|
|
69
|
+
readonly specs?: ReadonlyArray<ClassSpec> | undefined;
|
|
70
|
+
/** The data an engine names an object or symbol by, saved as `["x", data]`, instead of copying it. */
|
|
71
|
+
readonly external?: ((value: object | symbol) => Json | undefined) | undefined;
|
|
72
|
+
};
|
|
73
|
+
/** What the decoder asks of the fresh runtime it restores into. */
|
|
74
|
+
export type DecodeEnv = {
|
|
75
|
+
readonly named: (name: string) => Obj | undefined;
|
|
76
|
+
readonly anchor: (name: string) => object | undefined;
|
|
77
|
+
readonly node: (source: string, index: number) => AstNode | undefined;
|
|
78
|
+
/** An `Interruption` (it holds an Effect cause, which this module does not make). */
|
|
79
|
+
readonly interruption: () => Interruption;
|
|
80
|
+
readonly hosts?: HostBridge | undefined;
|
|
81
|
+
readonly specs?: ReadonlyArray<ClassSpec> | undefined;
|
|
82
|
+
/** What `EncodeEnv.external` named. */
|
|
83
|
+
readonly external?: ((data: Json) => unknown) | undefined;
|
|
84
|
+
};
|
|
85
|
+
/** How one class saves: its own fields in order, and how a restored instance is made whole. */
|
|
86
|
+
export type ClassSpec = {
|
|
87
|
+
readonly name: string;
|
|
88
|
+
readonly ctor: Function;
|
|
89
|
+
/** Saved fields, in the order a constructor makes them. */
|
|
90
|
+
readonly fields: ReadonlyArray<string>;
|
|
91
|
+
/** Own fields that are not saved: caches and runtime configuration, set to `reset`'s values. */
|
|
92
|
+
readonly skip?: ReadonlyArray<string>;
|
|
93
|
+
/** Own fields with the same value on every instance. */
|
|
94
|
+
readonly constants?: Readonly<{
|
|
95
|
+
[field: string]: unknown;
|
|
96
|
+
}>;
|
|
97
|
+
/** An `Obj`: its prototype, extensibility, and properties are saved too. */
|
|
98
|
+
readonly obj?: boolean;
|
|
99
|
+
/** The restored runtime already has this object; it is filled in place. */
|
|
100
|
+
readonly anchor?: string;
|
|
101
|
+
/** Sets fields that are not saved, once the saved ones are in. */
|
|
102
|
+
readonly reset?: (value: never) => void;
|
|
103
|
+
/** The saved fields' values, when they are not the own fields as they are (a frame saves its live registers only). */
|
|
104
|
+
readonly view?: (value: never) => ReadonlyArray<unknown>;
|
|
105
|
+
};
|
|
106
|
+
/** The names of every class with a codec: a build that saves a different set cannot read this one's snapshots. */
|
|
107
|
+
export declare const codecShape: (extra?: ReadonlyArray<ClassSpec>) => string;
|
|
108
|
+
/** Encodes everything reachable from `roots`: the roots' values and the records they refer to. */
|
|
109
|
+
export declare const encode: (roots: ReadonlyArray<unknown>, env: EncodeEnv) => {
|
|
110
|
+
readonly roots: Array<Encoded>;
|
|
111
|
+
readonly records: Array<Record>;
|
|
112
|
+
};
|
|
113
|
+
/** Rebuilds the graph `encode` saved, filling anchors in place: the decoded roots. */
|
|
114
|
+
export declare const decode: (saved: {
|
|
115
|
+
readonly roots: ReadonlyArray<unknown>;
|
|
116
|
+
readonly records: ReadonlyArray<unknown>;
|
|
117
|
+
}, env: DecodeEnv) => Array<unknown>;
|