@opencode/codemode 2.2.6 → 2.3.0
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 +115 -29
- package/dist/{vm/async.d.ts → async.d.ts} +1 -1
- package/dist/codemode.d.ts +13 -22
- package/dist/codemode.js +18 -39
- package/dist/data.d.ts +4 -19
- package/dist/data.js +1 -82
- package/dist/extension.d.ts +20 -1
- package/dist/extension.js +14 -1
- package/dist/{vm/host.d.ts → host.d.ts} +5 -2
- package/dist/{vm/host.js → host.js} +9 -4
- package/dist/index.d.ts +1 -1
- package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
- package/dist/{vm/prelude → prelude}/helpers.js +1 -1
- package/dist/{vm/runtime → runtime}/driver.d.ts +18 -8
- package/dist/{vm/runtime → runtime}/driver.js +90 -20
- package/dist/{vm/runtime → runtime}/errors.js +2 -2
- package/dist/{vm/runtime → runtime}/execute.d.ts +18 -9
- package/dist/{vm/runtime → runtime}/execute.js +45 -15
- package/dist/{vm/runtime → runtime}/extensions.d.ts +2 -2
- package/dist/{vm/runtime → runtime}/extensions.js +121 -19
- package/dist/{vm/runtime → runtime}/globals.js +1 -1
- package/dist/{vm/runtime → runtime}/interpreter.d.ts +4 -1
- package/dist/{vm/runtime → runtime}/interpreter.js +2 -0
- package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.js +1 -1
- package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/operations.js +1 -1
- package/dist/scan.js +2 -2
- package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +16 -4
- package/dist/{vm/scheduler.js → scheduler.js} +13 -2
- package/dist/{vm/snapshot → snapshot}/codec.d.ts +37 -6
- package/dist/{vm/snapshot → snapshot}/codec.js +112 -8
- package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +13 -6
- package/dist/{vm/snapshot → snapshot}/snapshot.js +19 -4
- package/dist/{vm/snapshot.js → snapshot.js} +5 -2
- package/dist/stdlib/array.d.ts +14 -9
- package/dist/stdlib/array.js +89 -276
- package/dist/stdlib/bytes.d.ts +11 -7
- package/dist/stdlib/bytes.js +112 -79
- package/dist/stdlib/collections.d.ts +7 -7
- package/dist/stdlib/collections.js +30 -339
- package/dist/stdlib/console.d.ts +2 -2
- package/dist/stdlib/console.js +3 -3
- package/dist/stdlib/date.d.ts +2 -2
- package/dist/stdlib/date.js +21 -30
- package/dist/stdlib/headers.d.ts +2 -2
- package/dist/stdlib/headers.js +15 -33
- package/dist/stdlib/iterator.d.ts +2 -2
- package/dist/stdlib/iterator.js +10 -303
- package/dist/stdlib/json.d.ts +2 -2
- package/dist/stdlib/json.js +15 -31
- package/dist/stdlib/math.d.ts +2 -7
- package/dist/stdlib/math.js +6 -30
- package/dist/stdlib/number.d.ts +3 -3
- package/dist/stdlib/number.js +8 -8
- package/dist/stdlib/object.d.ts +4 -4
- package/dist/stdlib/object.js +12 -30
- package/dist/stdlib/regexp.d.ts +6 -5
- package/dist/stdlib/regexp.js +18 -13
- package/dist/stdlib/string.d.ts +2 -2
- package/dist/stdlib/string.js +53 -53
- package/dist/stdlib/url.d.ts +21 -9
- package/dist/stdlib/url.js +55 -55
- package/dist/stdlib/value.d.ts +3 -4
- package/dist/stdlib/value.js +5 -6
- package/dist/stdlib/web.d.ts +4 -4
- package/dist/stdlib/web.js +7 -7
- package/dist/tool-runtime.d.ts +34 -12
- package/dist/tool-runtime.js +350 -68
- package/dist/tool-schema.d.ts +5 -0
- package/dist/tool-schema.js +36 -16
- package/dist/tool.d.ts +60 -2
- package/dist/tool.js +17 -1
- package/dist/{vm/vm.d.ts → vm.d.ts} +4 -3
- package/dist/{vm/vm.js → vm.js} +1 -1
- package/package.json +5 -7
- package/dist/interpreter/callback.d.ts +0 -24
- package/dist/interpreter/callback.js +0 -86
- package/dist/interpreter/errors.js +0 -173
- package/dist/interpreter/execute.d.ts +0 -14
- package/dist/interpreter/execute.js +0 -207
- package/dist/interpreter/extensions.d.ts +0 -9
- package/dist/interpreter/extensions.js +0 -248
- package/dist/interpreter/generators.d.ts +0 -3
- package/dist/interpreter/generators.js +0 -168
- package/dist/interpreter/globals.js +0 -144
- package/dist/interpreter/interpreter.d.ts +0 -48
- package/dist/interpreter/interpreter.js +0 -2253
- package/dist/interpreter/intrinsics.d.ts +0 -15
- package/dist/interpreter/intrinsics.js +0 -105
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/native.d.ts +0 -17
- package/dist/interpreter/native.js +0 -35
- package/dist/interpreter/objects.d.ts +0 -301
- package/dist/interpreter/objects.js +0 -592
- package/dist/interpreter/promises.d.ts +0 -18
- package/dist/interpreter/promises.js +0 -292
- package/dist/interpreter/runner.d.ts +0 -188
- package/dist/interpreter/runner.js +0 -492
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/suspend-error.d.ts +0 -23
- package/dist/suspend-error.js +0 -25
- package/dist/vm/data.d.ts +0 -7
- package/dist/vm/data.js +0 -13
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
- package/dist/vm/runtime/errors.d.ts +0 -23
- package/dist/vm/runtime/globals.d.ts +0 -6
- package/dist/vm/runtime/limits.d.ts +0 -10
- package/dist/vm/runtime/limits.js +0 -19
- package/dist/vm/runtime/modules.d.ts +0 -4
- package/dist/vm/runtime/modules.js +0 -33
- package/dist/vm/runtime/references.d.ts +0 -8
- package/dist/vm/runtime/references.js +0 -51
- package/dist/vm/stdlib/array.d.ts +0 -16
- package/dist/vm/stdlib/array.js +0 -311
- package/dist/vm/stdlib/bytes.d.ts +0 -11
- package/dist/vm/stdlib/bytes.js +0 -222
- package/dist/vm/stdlib/collections.d.ts +0 -7
- package/dist/vm/stdlib/collections.js +0 -188
- package/dist/vm/stdlib/console.d.ts +0 -6
- package/dist/vm/stdlib/console.js +0 -93
- package/dist/vm/stdlib/date.d.ts +0 -2
- package/dist/vm/stdlib/date.js +0 -123
- package/dist/vm/stdlib/headers.d.ts +0 -2
- package/dist/vm/stdlib/headers.js +0 -92
- package/dist/vm/stdlib/iterator.d.ts +0 -2
- package/dist/vm/stdlib/iterator.js +0 -30
- package/dist/vm/stdlib/json.d.ts +0 -3
- package/dist/vm/stdlib/json.js +0 -42
- package/dist/vm/stdlib/math.d.ts +0 -3
- package/dist/vm/stdlib/math.js +0 -73
- package/dist/vm/stdlib/number.d.ts +0 -3
- package/dist/vm/stdlib/number.js +0 -72
- package/dist/vm/stdlib/object.d.ts +0 -6
- package/dist/vm/stdlib/object.js +0 -201
- package/dist/vm/stdlib/regexp.d.ts +0 -8
- package/dist/vm/stdlib/regexp.js +0 -119
- package/dist/vm/stdlib/string.d.ts +0 -2
- package/dist/vm/stdlib/string.js +0 -272
- package/dist/vm/stdlib/url.d.ts +0 -25
- package/dist/vm/stdlib/url.js +0 -255
- package/dist/vm/stdlib/value.d.ts +0 -6
- package/dist/vm/stdlib/value.js +0 -32
- package/dist/vm/stdlib/web.d.ts +0 -5
- package/dist/vm/stdlib/web.js +0 -97
- /package/dist/{vm/async.js → async.js} +0 -0
- /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
- /package/dist/{vm/code.js → code.js} +0 -0
- /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/compile.js +0 -0
- /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/scope.js +0 -0
- /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
- /package/dist/{vm/generators.js → generators.js} +0 -0
- /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
- /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
- /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
- /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
- /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/callback.js +0 -0
- /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.js +0 -0
- /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
- /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
- /package/dist/{interpreter → runtime}/limits.js +0 -0
- /package/dist/{vm/runtime → runtime}/model.js +0 -0
- /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
- /package/dist/{interpreter → runtime}/modules.js +0 -0
- /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/native.js +0 -0
- /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/promises.js +0 -0
- /package/dist/{interpreter → runtime}/references.d.ts +0 -0
- /package/dist/{interpreter → runtime}/references.js +0 -0
- /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/signals.js +0 -0
- /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/table.js +0 -0
- /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import { isAsync } from "
|
|
3
|
-
import { hooked } from "
|
|
2
|
+
import { isAsync, StreamingResponse } from "../extension.js";
|
|
3
|
+
import { hooked } from "../tool-runtime.js";
|
|
4
4
|
import { errorMessage, errorType } from "./errors.js";
|
|
5
5
|
import { createErrorValue } from "./intrinsics.js";
|
|
6
6
|
import { MAX_ARRAY_LENGTH, MAX_VALUE_DEPTH } from "./limits.js";
|
|
7
|
-
import { PendingThrow, rangeError, Throw, typeError } from "./model.js";
|
|
7
|
+
import { AsyncIteratorSymbol, PendingThrow, rangeError, Throw, typeError } from "./model.js";
|
|
8
8
|
import { native } from "./native.js";
|
|
9
|
-
import { define, get, has, hidden, keys, Arr, Bytes, DateObj, ErrorObj, HeadersObj, PromiseObj, MapObj, Obj, RegExpObj, SetObj, URLObj, URLSearchParamsObj, coerceToString, } from "./objects.js";
|
|
9
|
+
import { define, defineAccessor, get, has, hidden, keys, Arr, Bytes, DateObj, ErrorObj, HeadersObj, PromiseObj, MapObj, Obj, RegExpObj, SetObj, URLObj, URLSearchParamsObj, coerceToString, } from "./objects.js";
|
|
10
10
|
import { describeValue, isOpaque } from "./references.js";
|
|
11
11
|
import { Push } from "./signals.js";
|
|
12
|
+
import { SnapshotError } from "../snapshot/codec.js";
|
|
12
13
|
/**
|
|
13
14
|
* The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in
|
|
14
15
|
* wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise
|
|
@@ -16,6 +17,9 @@ import { Push } from "./signals.js";
|
|
|
16
17
|
*/
|
|
17
18
|
export const extensionGlobals = (ctx, extensions) => {
|
|
18
19
|
const builtins = ctx.builtins;
|
|
20
|
+
const byName = new Map(extensions.map((extension) => [extension.name, extension]));
|
|
21
|
+
// The program functions wrapping functions inside values a hooked extension returned, by where they came from.
|
|
22
|
+
const origins = new WeakMap();
|
|
19
23
|
const toHost = (value, label, depth = 0, seen = new Set()) => {
|
|
20
24
|
if (depth > MAX_VALUE_DEPTH)
|
|
21
25
|
throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`);
|
|
@@ -53,20 +57,58 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
53
57
|
seen.delete(value);
|
|
54
58
|
return copied;
|
|
55
59
|
};
|
|
56
|
-
// `
|
|
57
|
-
const fromHost = (value, label, depth = 0, seen = new Set(),
|
|
60
|
+
// `within` says where the value is, so a function inside it fires the hooks, or can be saved; see `Within`.
|
|
61
|
+
const fromHost = (value, label, depth = 0, seen = new Set(), within) => {
|
|
58
62
|
if (depth > MAX_VALUE_DEPTH)
|
|
59
63
|
throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`);
|
|
60
64
|
if (isPrimitive(value))
|
|
61
65
|
return value;
|
|
62
66
|
if (typeof value === "function")
|
|
63
|
-
return wrap(value, label,
|
|
67
|
+
return wrap(value, label, within);
|
|
64
68
|
if (value !== null && typeof value === "object") {
|
|
65
|
-
const next = (item, path) => fromHost(item, path, depth + 1, seen,
|
|
69
|
+
const next = (item, path, key) => fromHost(item, path, depth + 1, seen, within && {
|
|
70
|
+
...within,
|
|
71
|
+
path: key === undefined || within.path === undefined ? undefined : [...within.path, key],
|
|
72
|
+
});
|
|
66
73
|
// Nothing a host value hands back is kept: results are read as primitives or copied. Where the value's own
|
|
67
74
|
// method could hand back host storage or an object (`getTime`, `slice`, `map`), its internal data is read.
|
|
68
75
|
if (value instanceof Date)
|
|
69
76
|
return new DateObj(builtins.Date, Date.prototype.getTime.call(value));
|
|
77
|
+
if (value instanceof StreamingResponse) {
|
|
78
|
+
const response = value.response;
|
|
79
|
+
const body = response.body;
|
|
80
|
+
const stream = new Obj(builtins.Object);
|
|
81
|
+
if (body) {
|
|
82
|
+
defineAccessor(stream, "locked", () => body.locked);
|
|
83
|
+
define(stream, "cancel", next(async (reason) => body.cancel(reason), `${label}.body.cancel`));
|
|
84
|
+
define(stream, "getReader", next((options) => {
|
|
85
|
+
if (options?.mode !== undefined)
|
|
86
|
+
throw new TypeError("Only the default stream reader is supported.");
|
|
87
|
+
const reader = body.getReader();
|
|
88
|
+
return {
|
|
89
|
+
read: async () => reader.read(),
|
|
90
|
+
cancel: async (reason) => reader.cancel(reason),
|
|
91
|
+
releaseLock: () => reader.releaseLock(),
|
|
92
|
+
};
|
|
93
|
+
}, `${label}.body.getReader`));
|
|
94
|
+
define(stream, AsyncIteratorSymbol, next(() => {
|
|
95
|
+
const iterator = body.values();
|
|
96
|
+
return {
|
|
97
|
+
next: async () => iterator.next(),
|
|
98
|
+
return: async () => iterator.return(),
|
|
99
|
+
};
|
|
100
|
+
}, `${label}.body[Symbol.asyncIterator]`));
|
|
101
|
+
}
|
|
102
|
+
const result = new Obj(builtins.Object);
|
|
103
|
+
for (const key of ["url", "status", "statusText", "ok", "headers"])
|
|
104
|
+
define(result, key, next(response[key], `${label}.${key}`));
|
|
105
|
+
define(result, "redirected", value.redirected);
|
|
106
|
+
define(result, "body", body ? stream : null);
|
|
107
|
+
defineAccessor(result, "bodyUsed", () => response.bodyUsed);
|
|
108
|
+
for (const key of ["text", "json", "bytes", "arrayBuffer"])
|
|
109
|
+
define(result, key, next(async () => response[key](), `${label}.${key}`));
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
70
112
|
if (value instanceof RegExp)
|
|
71
113
|
return new RegExpObj(builtins.RegExp, value.source, value.flags);
|
|
72
114
|
if (value instanceof Uint8Array)
|
|
@@ -91,7 +133,7 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
91
133
|
const field = fields[key];
|
|
92
134
|
if (typeof field === "function")
|
|
93
135
|
continue;
|
|
94
|
-
const item = crossing(() => next(field, `${label}.${key}
|
|
136
|
+
const item = crossing(() => next(field, `${label}.${key}`, key));
|
|
95
137
|
if (item === left)
|
|
96
138
|
continue;
|
|
97
139
|
define(copied, key, item, key === "cause" ? hidden : undefined);
|
|
@@ -130,7 +172,7 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
130
172
|
const items = [];
|
|
131
173
|
for (let index = 0; index < length; index += 1) {
|
|
132
174
|
if (index in value)
|
|
133
|
-
items[index] = next(value[index], `${label}[${index}]
|
|
175
|
+
items[index] = next(value[index], `${label}[${index}]`, index);
|
|
134
176
|
}
|
|
135
177
|
items.length = length;
|
|
136
178
|
seen.delete(value);
|
|
@@ -140,7 +182,7 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
140
182
|
if (prototype === Object.prototype || prototype === null) {
|
|
141
183
|
const copied = new Obj(builtins.Object);
|
|
142
184
|
for (const [key, item] of Object.entries(value))
|
|
143
|
-
define(copied, key, next(item, `${label}.${key}
|
|
185
|
+
define(copied, key, next(item, `${label}.${key}`, key));
|
|
144
186
|
seen.delete(value);
|
|
145
187
|
return copied;
|
|
146
188
|
}
|
|
@@ -150,26 +192,67 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
150
192
|
// A host function as a program function. Arguments cross in; for a global or a global object's function, the
|
|
151
193
|
// `extension.before` hook runs before the host function is called, so a call it denies never reaches the host. The
|
|
152
194
|
// host's result, or what it threw, crosses out so the program catches a copy, and `extension.after` sees how the
|
|
153
|
-
// call ended. Functions inside results are part of a value's API and skip the hooks
|
|
154
|
-
|
|
195
|
+
// call ended. Functions inside results are part of a value's API and skip the hooks; one inside a value an extension
|
|
196
|
+
// with `snapshot` hooks returned is known by where it came from, so a suspended run can save it.
|
|
197
|
+
const wrap = (value, label, within) => {
|
|
155
198
|
// The host function's own `name` and `length` can be anything, even getters: each is read once, and only text
|
|
156
199
|
// and a count cross.
|
|
157
200
|
const name = value.name;
|
|
158
201
|
const length = value.length;
|
|
159
|
-
|
|
202
|
+
const extension = within?.extension.name;
|
|
203
|
+
const root = within?.root;
|
|
204
|
+
const wrapped = native(builtins, {
|
|
160
205
|
runtime: "extension",
|
|
161
206
|
name: typeof name === "string" ? name : "",
|
|
162
207
|
length: typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : 0,
|
|
163
208
|
call: (_, values, site) => {
|
|
164
209
|
const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`));
|
|
165
|
-
const invocation =
|
|
210
|
+
const invocation = within !== undefined && root === undefined ? { extension: extension, name: label, args } : undefined;
|
|
166
211
|
// A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
|
|
167
212
|
if (isAsync(value)) {
|
|
168
|
-
return pending(ctx, { stage: "run", label, invocation, host: value, args, threw: false }, site);
|
|
213
|
+
return pending(ctx, { stage: "run", label, extension, invocation, host: value, args, threw: false }, site);
|
|
169
214
|
}
|
|
170
|
-
return new Push("extension call", [value, args, label, invocation]);
|
|
215
|
+
return new Push("extension call", [value, args, label, invocation, extension]);
|
|
171
216
|
},
|
|
172
217
|
});
|
|
218
|
+
if (root !== undefined && within.path !== undefined) {
|
|
219
|
+
origins.set(wrapped, { extension: extension, root, path: within.path, label });
|
|
220
|
+
}
|
|
221
|
+
return wrapped;
|
|
222
|
+
};
|
|
223
|
+
ctx.hosts = {
|
|
224
|
+
extensions: extensions.map((extension) => ({ name: extension.name, hooked: extension.snapshot !== undefined })),
|
|
225
|
+
origin: (value) => origins.get(value),
|
|
226
|
+
encode: (name, root) => hostStep(name, "save", () => hooks(name).encode(root)),
|
|
227
|
+
decode: (name, data) => {
|
|
228
|
+
const root = hostStep(name, "restore", () => hooks(name).decode(data));
|
|
229
|
+
if ((typeof root !== "object" && typeof root !== "function") || root === null) {
|
|
230
|
+
throw new SnapshotError(`The extension "${name}" restored a value it returned as ${typeof root}, not an object.`);
|
|
231
|
+
}
|
|
232
|
+
return root;
|
|
233
|
+
},
|
|
234
|
+
rewrap: (target, origin) => {
|
|
235
|
+
const host = origin.path.reduce((at, key) => (typeof at === "object" && at !== null) || typeof at === "function"
|
|
236
|
+
? at[key]
|
|
237
|
+
: undefined, origin.root);
|
|
238
|
+
if (typeof host !== "function") {
|
|
239
|
+
throw new SnapshotError(`The extension "${origin.extension}" restored a value with no function at ${origin.label}, so this state can't resume.`);
|
|
240
|
+
}
|
|
241
|
+
const made = wrap(host, origin.label, {
|
|
242
|
+
extension: byName.get(origin.extension),
|
|
243
|
+
root: origin.root,
|
|
244
|
+
path: origin.path,
|
|
245
|
+
});
|
|
246
|
+
// The restored function keeps its own properties; only what calling it does comes from the new one.
|
|
247
|
+
Object.assign(target, { tag: made.tag, call: made.call, construct: made.construct, callback: made.callback });
|
|
248
|
+
origins.set(target, origin);
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
const hooks = (name) => {
|
|
252
|
+
const snapshot = byName.get(name)?.snapshot;
|
|
253
|
+
if (snapshot === undefined)
|
|
254
|
+
throw new SnapshotError(`The extension "${name}" has no snapshot hooks.`);
|
|
255
|
+
return snapshot;
|
|
173
256
|
};
|
|
174
257
|
// The Effect each step of an extension call runs as, with the program's services in its requirements.
|
|
175
258
|
ctx.extensionWork = (request) => {
|
|
@@ -181,12 +264,20 @@ export const extensionGlobals = (ctx, extensions) => {
|
|
|
181
264
|
if (request.stage === "before")
|
|
182
265
|
return before ?? Effect.void;
|
|
183
266
|
const after = call && hooks["extension.after"];
|
|
184
|
-
const
|
|
267
|
+
const extension = request.extension === undefined ? undefined : byName.get(request.extension);
|
|
268
|
+
// What a function returns is a value of its extension's, which its `snapshot` hooks save, when it has them.
|
|
269
|
+
const settle = (effect) => (after ? hooked(call, undefined, after, effect) : effect).pipe(Effect.mapBoth({
|
|
270
|
+
onFailure: cross,
|
|
271
|
+
onSuccess: (result) => fromHost(result, label, 0, new Set(), extension?.snapshot !== undefined &&
|
|
272
|
+
((typeof result === "object" && result !== null) || typeof result === "function")
|
|
273
|
+
? { extension, root: result, path: [] }
|
|
274
|
+
: undefined),
|
|
275
|
+
}));
|
|
185
276
|
if (request.stage === "settle")
|
|
186
277
|
return settle(outcome(request.host, request.threw));
|
|
187
278
|
return Effect.andThen(before ?? Effect.void, settle(Effect.suspend(() => outcome(...callHost(request.host, request.args)))));
|
|
188
279
|
};
|
|
189
|
-
return extensions.flatMap((extension) => Object.entries(extension.globals).map(([name, value]) => [name, fromHost(value, name, 0, new Set(), extension
|
|
280
|
+
return extensions.flatMap((extension) => Object.entries(extension.globals).map(([name, value]) => [name, fromHost(value, name, 0, new Set(), { extension, root: undefined, path: undefined })]));
|
|
190
281
|
};
|
|
191
282
|
// An asynchronous host call: the program holds a promise that settles when the host's work does.
|
|
192
283
|
const pending = (ctx, call, site) => {
|
|
@@ -205,6 +296,17 @@ export const startExtensionCall = (ctx, host, args, call, site) => {
|
|
|
205
296
|
return pending(ctx, request, site);
|
|
206
297
|
return request;
|
|
207
298
|
};
|
|
299
|
+
// Runs an extension's `snapshot` hook; what goes wrong is the state's problem, said as such.
|
|
300
|
+
const hostStep = (extension, what, step) => {
|
|
301
|
+
try {
|
|
302
|
+
return step();
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
if (error instanceof SnapshotError)
|
|
306
|
+
throw error;
|
|
307
|
+
throw new SnapshotError(`The extension "${extension}" couldn't ${what} a value it returned: ${error instanceof Error ? error.message : String(error)}`);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
208
310
|
/** Calls a host function: what it returned, or what it threw. */
|
|
209
311
|
const callHost = (value, args) => {
|
|
210
312
|
try {
|
|
@@ -15,7 +15,7 @@ import { headersGlobal } from "../stdlib/headers.js";
|
|
|
15
15
|
import { iteratorGlobals } from "../stdlib/iterator.js";
|
|
16
16
|
import { coercion } from "../stdlib/value.js";
|
|
17
17
|
import { base64Global, cryptoGlobal, structuredCloneGlobal } from "../stdlib/web.js";
|
|
18
|
-
import { ToolReference } from "
|
|
18
|
+
import { ToolReference } from "../tool-runtime.js";
|
|
19
19
|
import { errorGlobal } from "./errors.js";
|
|
20
20
|
import { errorTypes } from "./intrinsics.js";
|
|
21
21
|
import { constants, constructor, methods, native, receiver } from "./native.js";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { ToolRuntime } from "
|
|
1
|
+
import type { ToolRuntime } from "../tool-runtime.js";
|
|
2
2
|
import type { ExtensionRequest } from "../scheduler.js";
|
|
3
|
+
import type { HostBridge } from "../snapshot/codec.js";
|
|
3
4
|
import { Vm } from "../vm.js";
|
|
4
5
|
import type { Builtins } from "./intrinsics.js";
|
|
5
6
|
import { IntrinsicNames } from "./intrinsic-names.js";
|
|
@@ -21,6 +22,8 @@ export declare class Interpreter<R> {
|
|
|
21
22
|
readonly prelude: Prelude;
|
|
22
23
|
/** Makes the Effect an extension function's host work runs as; set by the extension bridge. */
|
|
23
24
|
extensionWork: ((request: ExtensionRequest) => unknown) | undefined;
|
|
25
|
+
/** Saves and restores values returned by extensions with `snapshot` hooks; set by the extension bridge. */
|
|
26
|
+
hosts: HostBridge | undefined;
|
|
24
27
|
/** The parsed sources this execution runs, by id, so a `NodeId` resolves to its node. */
|
|
25
28
|
readonly sources: Map<string, Source>;
|
|
26
29
|
/** The global bindings: the intrinsics, the host's globals, and the program's bindings. */
|
|
@@ -19,6 +19,8 @@ export class Interpreter {
|
|
|
19
19
|
prelude;
|
|
20
20
|
/** Makes the Effect an extension function's host work runs as; set by the extension bridge. */
|
|
21
21
|
extensionWork;
|
|
22
|
+
/** Saves and restores values returned by extensions with `snapshot` hooks; set by the extension bridge. */
|
|
23
|
+
hosts;
|
|
22
24
|
/** The parsed sources this execution runs, by id, so a `NodeId` resolves to its node. */
|
|
23
25
|
sources = new Map();
|
|
24
26
|
/** The global bindings: the intrinsics, the host's globals, and the program's bindings. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Node } from "acorn";
|
|
2
2
|
import type { ErrorType } from "./intrinsics.js";
|
|
3
|
-
import type { DiagnosticKind } from "
|
|
3
|
+
import type { DiagnosticKind } from "../codemode.js";
|
|
4
4
|
import type { ErrorObj, Value } from "./objects.js";
|
|
5
5
|
/** Any parsed node; the compiler narrows on `type`, and diagnostics read `loc`. */
|
|
6
6
|
export type AstNode = Node;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ToolReference } from "
|
|
1
|
+
import { ToolReference } from "../tool-runtime.js";
|
|
2
2
|
import type { Builtins } from "./intrinsics.js";
|
|
3
3
|
import { type AstNode, type PendingThrow } from "./model.js";
|
|
4
4
|
import { type Convert, type GeneratorRequest, Invoke, type Push, type Resume } from "./signals.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ToolReference } from "
|
|
1
|
+
import { ToolReference } from "../tool-runtime.js";
|
|
2
2
|
import { checkArrayLength } from "./limits.js";
|
|
3
3
|
import { AsyncIteratorSymbol, IteratorSymbol, rangeError, Throw, typeError, } from "./model.js";
|
|
4
4
|
import { Invoke } from "./signals.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Expression, MemberExpression, NewExpression, Super } from "acorn";
|
|
2
|
-
import { ToolReference } from "
|
|
2
|
+
import { ToolReference } from "../tool-runtime.js";
|
|
3
3
|
import { type AstNode } from "./model.js";
|
|
4
4
|
import { type Builtins } from "./intrinsics.js";
|
|
5
5
|
import { type Cursor, Obj, type Value } from "./objects.js";
|
package/dist/scan.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Result } from "effect";
|
|
2
|
-
import { parseProgram } from "./
|
|
3
|
-
import { formatLocation } from "./
|
|
2
|
+
import { parseProgram } from "./runtime/execute.js";
|
|
3
|
+
import { formatLocation } from "./runtime/model.js";
|
|
4
4
|
/**
|
|
5
5
|
* Extracts every `tools.<path>` referenced by a Code Mode program, in first-seen order. Fails on syntax errors,
|
|
6
6
|
* dynamic bracket access (`tools[name]`), or aliasing/passing `tools` rather than calling `tools.<path>(...)` directly.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Json } from "./data.js";
|
|
2
|
-
import type { ExtensionInvocation } from "
|
|
2
|
+
import type { ExtensionInvocation } from "./tool-runtime.js";
|
|
3
3
|
import type { AstNode } from "./runtime/model.js";
|
|
4
4
|
import type { Callable, Cursor, GeneratorObj, Obj, PromiseObj, Value } from "./runtime/objects.js";
|
|
5
5
|
import type { Completion } from "./runtime/signals.js";
|
|
@@ -101,6 +101,7 @@ export type HostRequest = {
|
|
|
101
101
|
export type ExtensionRequest = {
|
|
102
102
|
readonly stage: "before" | "settle" | "run";
|
|
103
103
|
readonly label: string;
|
|
104
|
+
readonly extension?: string | undefined;
|
|
104
105
|
invocation: ExtensionInvocation | undefined;
|
|
105
106
|
host: unknown;
|
|
106
107
|
args: ReadonlyArray<unknown>;
|
|
@@ -113,6 +114,8 @@ export type ExtensionRequest = {
|
|
|
113
114
|
*/
|
|
114
115
|
export declare class HostCall {
|
|
115
116
|
readonly id: number;
|
|
117
|
+
/** The call's stable idempotency key, `<runId>:<callId>`. */
|
|
118
|
+
readonly key: string;
|
|
116
119
|
readonly request: HostRequest;
|
|
117
120
|
/** What runs: a tool path, or an extension function's label. */
|
|
118
121
|
readonly label: string;
|
|
@@ -124,9 +127,18 @@ export declare class HostCall {
|
|
|
124
127
|
readonly resolving: Resolving | undefined;
|
|
125
128
|
/** The call that started the work, where a defect it raises is located. */
|
|
126
129
|
readonly site: AstNode | undefined;
|
|
127
|
-
/**
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Where the call is: `queued` (written ahead, not started), `running` (or in `start` for a durable tool),
|
|
132
|
+
* `started` (a durable tool whose `start` returned its `data`, now in `wait`), or `completed`.
|
|
133
|
+
*/
|
|
134
|
+
status: "queued" | "running" | "started" | "completed";
|
|
135
|
+
/** The JSON data a durable tool's `start` returned, saved so a resumed run can call `wait(data)`. */
|
|
136
|
+
data: Json | undefined;
|
|
137
|
+
/** Whether a tool call has already been counted against `maxToolCalls` and recorded in `toolCalls`. */
|
|
138
|
+
admitted: boolean;
|
|
139
|
+
constructor(id: number,
|
|
140
|
+
/** The call's stable idempotency key, `<runId>:<callId>`. */
|
|
141
|
+
key: string, request: HostRequest,
|
|
130
142
|
/** What runs: a tool path, or an extension function's label. */
|
|
131
143
|
label: string,
|
|
132
144
|
/** The running job waits for it: its outcome continues the frame that asked, before any other job. */
|
|
@@ -6,15 +6,25 @@ import { locate } from "./runtime/errors.js";
|
|
|
6
6
|
*/
|
|
7
7
|
export class HostCall {
|
|
8
8
|
id;
|
|
9
|
+
key;
|
|
9
10
|
request;
|
|
10
11
|
label;
|
|
11
12
|
blocking;
|
|
12
13
|
result;
|
|
13
14
|
resolving;
|
|
14
15
|
site;
|
|
15
|
-
/**
|
|
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
|
+
*/
|
|
16
20
|
status = "queued";
|
|
17
|
-
|
|
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,
|
|
18
28
|
/** What runs: a tool path, or an extension function's label. */
|
|
19
29
|
label,
|
|
20
30
|
/** The running job waits for it: its outcome continues the frame that asked, before any other job. */
|
|
@@ -26,6 +36,7 @@ export class HostCall {
|
|
|
26
36
|
/** The call that started the work, where a defect it raises is located. */
|
|
27
37
|
site) {
|
|
28
38
|
this.id = id;
|
|
39
|
+
this.key = key;
|
|
29
40
|
this.request = request;
|
|
30
41
|
this.label = label;
|
|
31
42
|
this.blocking = blocking;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AstNode } from "../runtime/model.js";
|
|
2
|
-
import { Obj } from "../runtime/objects.js";
|
|
2
|
+
import { Native, Obj } from "../runtime/objects.js";
|
|
3
3
|
import { Interruption } from "../runtime/signals.js";
|
|
4
4
|
/** A plain JSON value, as a snapshot holds it. */
|
|
5
5
|
export type Json = null | boolean | number | string | ReadonlyArray<Json> | {
|
|
@@ -12,7 +12,7 @@ export type Json = null | boolean | number | string | ReadonlyArray<Json> | {
|
|
|
12
12
|
* - `["s", name]` an interpreter symbol;
|
|
13
13
|
* - `["i", name]` a built-in or host global, unchanged since the runtime was made;
|
|
14
14
|
* - `["a", source, index]` an AST node (a call site, an error's location);
|
|
15
|
-
* - `["x", data]` something the
|
|
15
|
+
* - `["x", data]` something the VM names itself (compiled code, by source and function index);
|
|
16
16
|
* - `["r", id]` another record.
|
|
17
17
|
*/
|
|
18
18
|
export type Encoded = string | number | boolean | null | ReadonlyArray<Json>;
|
|
@@ -22,8 +22,37 @@ export type Record = {
|
|
|
22
22
|
readonly [field: string]: Json;
|
|
23
23
|
};
|
|
24
24
|
/** A saved state can't be read, or doesn't fit this runtime. */
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
export declare class SnapshotError extends Error {
|
|
26
|
+
readonly name = "SnapshotError";
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Where a function inside a value an extension returned came from, so a saved state can name it instead of losing it:
|
|
30
|
+
* the extension, the value its function returned (`root`), which the extension's `snapshot` hooks save, and the path
|
|
31
|
+
* from there to the function.
|
|
32
|
+
*/
|
|
33
|
+
export type HostOrigin = {
|
|
34
|
+
readonly extension: string;
|
|
35
|
+
readonly root: object;
|
|
36
|
+
readonly path: ReadonlyArray<string | number>;
|
|
37
|
+
/** What the program calls it, for its errors (`fetch.text`). */
|
|
38
|
+
readonly label: string;
|
|
39
|
+
};
|
|
40
|
+
/** The extension bridge's side of saving and restoring those functions; see `extensionGlobals`. */
|
|
41
|
+
export type HostBridge = {
|
|
42
|
+
/** Each extension's name, and whether it has `snapshot` hooks: a state resumes only where both match. */
|
|
43
|
+
readonly extensions: ReadonlyArray<{
|
|
44
|
+
readonly name: string;
|
|
45
|
+
readonly hooked: boolean;
|
|
46
|
+
}>;
|
|
47
|
+
/** Where a program function wrapping a host function came from, when its extension can save it. */
|
|
48
|
+
readonly origin: (value: object) => HostOrigin | undefined;
|
|
49
|
+
/** A returned value as its extension saves it. */
|
|
50
|
+
readonly encode: (extension: string, root: object) => unknown;
|
|
51
|
+
/** A returned value back from what `encode` gave. */
|
|
52
|
+
readonly decode: (extension: string, data: unknown) => object;
|
|
53
|
+
/** Makes `target`, a restored program function, call the host function `origin` leads to. */
|
|
54
|
+
readonly rewrap: (target: Native, origin: HostOrigin) => void;
|
|
55
|
+
};
|
|
27
56
|
/** What the encoder asks of the runtime whose state it saves. */
|
|
28
57
|
export type EncodeEnv = {
|
|
29
58
|
/** The name of a built-in or host global object. */
|
|
@@ -36,9 +65,10 @@ export type EncodeEnv = {
|
|
|
36
65
|
/** Values with no data form are reported here; strict encoding throws on the first instead. */
|
|
37
66
|
readonly lost: Array<string>;
|
|
38
67
|
readonly strict: boolean;
|
|
39
|
-
|
|
68
|
+
readonly hosts?: HostBridge | undefined;
|
|
69
|
+
/** More classes to save (the VM's frames and closures), after the built-in ones. */
|
|
40
70
|
readonly specs?: ReadonlyArray<ClassSpec> | undefined;
|
|
41
|
-
/** The data
|
|
71
|
+
/** The data the VM names an object or symbol by, saved as `["x", data]`, instead of copying it. */
|
|
42
72
|
readonly external?: ((value: object | symbol) => Json | undefined) | undefined;
|
|
43
73
|
};
|
|
44
74
|
/** What the decoder asks of the fresh runtime it restores into. */
|
|
@@ -48,6 +78,7 @@ export type DecodeEnv = {
|
|
|
48
78
|
readonly node: (source: string, index: number) => AstNode | undefined;
|
|
49
79
|
/** An `Interruption` (it holds an Effect cause, which this module does not make). */
|
|
50
80
|
readonly interruption: () => Interruption;
|
|
81
|
+
readonly hosts?: HostBridge | undefined;
|
|
51
82
|
readonly specs?: ReadonlyArray<ClassSpec> | undefined;
|
|
52
83
|
/** What `EncodeEnv.external` named. */
|
|
53
84
|
readonly external?: ((data: Json) => unknown) | undefined;
|