@opencode/codemode 0.0.0-dev-1179 → 0.0.0-dev-1189
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 +6 -12
- package/dist/codemode.d.ts +7 -21
- package/dist/codemode.js +17 -38
- package/dist/extension.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/scan.js +2 -2
- package/dist/tool-runtime.d.ts +8 -11
- package/dist/tool-runtime.js +13 -11
- package/dist/tool.d.ts +18 -9
- package/dist/tool.js +15 -23
- package/dist/vm/data.d.ts +1 -1
- package/dist/vm/runtime/execute.d.ts +16 -6
- package/dist/vm/runtime/execute.js +10 -7
- package/dist/vm/snapshot/codec.d.ts +3 -2
- package/dist/vm/snapshot/codec.js +3 -2
- package/dist/vm/snapshot/snapshot.d.ts +6 -3
- package/dist/vm/snapshot/snapshot.js +13 -2
- package/dist/vm/vm.d.ts +2 -2
- package/package.json +3 -5
- package/dist/data.d.ts +0 -21
- package/dist/data.js +0 -93
- package/dist/interpreter/callback.d.ts +0 -35
- package/dist/interpreter/callback.js +0 -98
- package/dist/interpreter/errors.d.ts +0 -23
- 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.d.ts +0 -6
- 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/limits.d.ts +0 -10
- package/dist/interpreter/limits.js +0 -19
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/modules.d.ts +0 -4
- package/dist/interpreter/modules.js +0 -33
- 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/references.d.ts +0 -8
- package/dist/interpreter/references.js +0 -51
- package/dist/interpreter/runner.d.ts +0 -178
- package/dist/interpreter/runner.js +0 -480
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/stdlib/array.d.ts +0 -11
- package/dist/stdlib/array.js +0 -498
- package/dist/stdlib/bytes.d.ts +0 -10
- package/dist/stdlib/bytes.js +0 -253
- package/dist/stdlib/collections.d.ts +0 -7
- package/dist/stdlib/collections.js +0 -497
- package/dist/stdlib/console.d.ts +0 -6
- package/dist/stdlib/console.js +0 -93
- package/dist/stdlib/date.d.ts +0 -2
- package/dist/stdlib/date.js +0 -132
- package/dist/stdlib/headers.d.ts +0 -2
- package/dist/stdlib/headers.js +0 -110
- package/dist/stdlib/iterator.d.ts +0 -2
- package/dist/stdlib/iterator.js +0 -322
- package/dist/stdlib/json.d.ts +0 -3
- package/dist/stdlib/json.js +0 -58
- package/dist/stdlib/math.d.ts +0 -8
- package/dist/stdlib/math.js +0 -97
- package/dist/stdlib/number.d.ts +0 -3
- package/dist/stdlib/number.js +0 -72
- package/dist/stdlib/object.d.ts +0 -6
- package/dist/stdlib/object.js +0 -219
- package/dist/stdlib/regexp.d.ts +0 -7
- package/dist/stdlib/regexp.js +0 -114
- package/dist/stdlib/string.d.ts +0 -2
- package/dist/stdlib/string.js +0 -272
- package/dist/stdlib/url.d.ts +0 -13
- package/dist/stdlib/url.js +0 -255
- package/dist/stdlib/value.d.ts +0 -7
- package/dist/stdlib/value.js +0 -33
- package/dist/stdlib/web.d.ts +0 -5
- package/dist/stdlib/web.js +0 -97
- package/dist/suspend-error.d.ts +0 -24
- package/dist/suspend-error.js +0 -25
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { SnapshotError } from "./codec.js";
|
|
2
|
+
export { SnapshotError };
|
|
3
|
+
/** A snapshot as bytes: UTF-8 JSON. */
|
|
4
|
+
export const toBytes = (snapshot) => new TextEncoder().encode(JSON.stringify(snapshot));
|
|
5
|
+
/** Reads bytes `toBytes` wrote; `restore` checks what they hold. */
|
|
6
|
+
export const fromBytes = (bytes) => {
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
throw new SnapshotError(`Invalid snapshot: the bytes are not UTF-8 JSON (${String(error)}).`);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
3
14
|
/** The snapshot format this build writes and reads: the bytecode VM's (see `src/vm/snapshot.ts`). */
|
|
4
15
|
export const FORMAT = 2;
|
|
5
16
|
export const hostsId = (ctx) => hash([
|
package/dist/vm/vm.d.ts
CHANGED
|
@@ -124,11 +124,11 @@ export declare class Vm {
|
|
|
124
124
|
} | undefined;
|
|
125
125
|
/** Saves the run's state as it is now. */
|
|
126
126
|
capture(options: CaptureOptions): {
|
|
127
|
-
readonly snapshot: import("./
|
|
127
|
+
readonly snapshot: import("./snapshot/snapshot.js").Snapshot;
|
|
128
128
|
readonly lost: ReadonlyArray<string>;
|
|
129
129
|
};
|
|
130
130
|
/** Restores a saved state into this fresh VM, after checking it belongs to this build and runtime. */
|
|
131
|
-
restore(saved: unknown, options: RestoreOptions): import("./
|
|
131
|
+
restore(saved: unknown, options: RestoreOptions): import("./snapshot/snapshot.js").Snapshot;
|
|
132
132
|
deliverAnswer(frame: Frame): void;
|
|
133
133
|
acquire(code: Code): Frame;
|
|
134
134
|
release(frame: Frame): void;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@opencode/codemode",
|
|
4
|
-
"version": "0.0.0-dev-
|
|
4
|
+
"version": "0.0.0-dev-1189",
|
|
5
5
|
"description": "Effect-native confined code execution over schema-described tools",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
@@ -27,10 +27,8 @@
|
|
|
27
27
|
"pack": "bun run script/pack.ts",
|
|
28
28
|
"typecheck": "bun check --no-script",
|
|
29
29
|
"typecheck:tsc": "tsc --noEmit",
|
|
30
|
-
"test": "bun test
|
|
31
|
-
"test:
|
|
32
|
-
"test:vm": "CODEMODE_ENGINE=vm bun test",
|
|
33
|
-
"test:roundtrip": "CODEMODE_ENGINE=vm CODEMODE_ROUNDTRIP=1 bun test"
|
|
30
|
+
"test": "bun test",
|
|
31
|
+
"test:roundtrip": "CODEMODE_ROUNDTRIP=1 bun test"
|
|
34
32
|
},
|
|
35
33
|
"dependencies": {
|
|
36
34
|
"acorn": "8.15.0",
|
package/dist/data.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export * as Data from "./data.js";
|
|
2
|
-
import type { Schema } from "effect";
|
|
3
|
-
import type { Interpreter } from "./interpreter/interpreter.js";
|
|
4
|
-
import { Obj, type Value } from "./interpreter/objects.js";
|
|
5
|
-
import { type Eval } from "./interpreter/runner.js";
|
|
6
|
-
export type Json = Schema.Json;
|
|
7
|
-
type Replacer = (args: Array<Value>, holder: Obj) => Eval<Value>;
|
|
8
|
-
/**
|
|
9
|
-
* What `JSON.stringify` would serialize for a program value, as host JSON: `toJSON` is honored, functions and
|
|
10
|
-
* `undefined` vanish, non-finite numbers become null, and everything else is copied. Two departures from JS
|
|
11
|
-
* so a mistake is not a silent `{}`: an Error serializes as `{ name, message, ...own }`, and a promise throws.
|
|
12
|
-
*/
|
|
13
|
-
export declare const toJson: <R>(ctx: Interpreter<R>, value: Value, replacer?: Replacer) => Eval<Schema.Json | undefined>;
|
|
14
|
-
/**
|
|
15
|
-
* The host boundary: `toJson`, plus what a program most likely meant when a value cannot be JSON. A promise is
|
|
16
|
-
* awaited, a Set crosses as an array, a URLSearchParams as its query string, a Uint8Array asks to be encoded as
|
|
17
|
-
* text first, and a `__proto__` key is dropped so host code can never receive one.
|
|
18
|
-
*/
|
|
19
|
-
export declare const toBoundary: <R>(ctx: Interpreter<R>, value: Value) => Eval<Schema.Json | undefined>;
|
|
20
|
-
/** Host JSON as program values: objects and arrays are copied, primitives pass through. */
|
|
21
|
-
export declare const fromJson: <R>(ctx: Interpreter<R>, value: Json | undefined) => Value;
|
package/dist/data.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
export * as Data from "./data.js";
|
|
2
|
-
import { MAX_VALUE_DEPTH } from "./interpreter/limits.js";
|
|
3
|
-
import { invalidData, rangeError, typeError } from "./interpreter/model.js";
|
|
4
|
-
import { Arr, Bytes, Callable, define, ErrorObj, get, keys, Obj, PromiseObj, record, SetObj, URLSearchParamsObj, HeadersObj, } from "./interpreter/objects.js";
|
|
5
|
-
import { typeofValue } from "./interpreter/references.js";
|
|
6
|
-
import { Slice } from "./interpreter/runner.js";
|
|
7
|
-
/**
|
|
8
|
-
* What `JSON.stringify` would serialize for a program value, as host JSON: `toJSON` is honored, functions and
|
|
9
|
-
* `undefined` vanish, non-finite numbers become null, and everything else is copied. Two departures from JS
|
|
10
|
-
* so a mistake is not a silent `{}`: an Error serializes as `{ name, message, ...own }`, and a promise throws.
|
|
11
|
-
*/
|
|
12
|
-
export const toJson = (ctx, value, replacer) => walk(ctx, value, replacer, false);
|
|
13
|
-
/**
|
|
14
|
-
* The host boundary: `toJson`, plus what a program most likely meant when a value cannot be JSON. A promise is
|
|
15
|
-
* awaited, a Set crosses as an array, a URLSearchParams as its query string, a Uint8Array asks to be encoded as
|
|
16
|
-
* text first, and a `__proto__` key is dropped so host code can never receive one.
|
|
17
|
-
*/
|
|
18
|
-
export const toBoundary = (ctx, value) => walk(ctx, value, undefined, true);
|
|
19
|
-
const walk = (ctx, value, replacer, boundary) => {
|
|
20
|
-
const stack = new Set();
|
|
21
|
-
const visit = function* (holder, key, depth) {
|
|
22
|
-
// Shared references expand into a tree, so the work is not bounded by the value's size or depth.
|
|
23
|
-
if (ctx.runner.tick())
|
|
24
|
-
yield Slice;
|
|
25
|
-
if (depth > MAX_VALUE_DEPTH)
|
|
26
|
-
throw rangeError(`Value exceeds the maximum depth of ${MAX_VALUE_DEPTH}.`);
|
|
27
|
-
const raw = get(holder, key);
|
|
28
|
-
if (raw instanceof PromiseObj && !boundary) {
|
|
29
|
-
throw invalidData("JSON.stringify received an un-awaited Promise; await it first - e.g. `const result = await tools.ns.tool(...)`.");
|
|
30
|
-
}
|
|
31
|
-
const settled = raw instanceof PromiseObj ? yield* ctx.awaitValue(raw) : raw;
|
|
32
|
-
const toJSON = settled instanceof Obj ? get(settled, "toJSON") : undefined;
|
|
33
|
-
const own = toJSON instanceof Callable ? yield* ctx.call(toJSON, settled, [key]) : settled;
|
|
34
|
-
const value = replacer === undefined ? own : yield* replacer([key, own], holder);
|
|
35
|
-
if (value === undefined || typeofValue(value) === "function")
|
|
36
|
-
return undefined;
|
|
37
|
-
if (typeof value === "number")
|
|
38
|
-
return Number.isFinite(value) ? value : null;
|
|
39
|
-
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
40
|
-
return value;
|
|
41
|
-
if (!(value instanceof Obj))
|
|
42
|
-
return {};
|
|
43
|
-
if (boundary && value instanceof Bytes) {
|
|
44
|
-
throw invalidData("A Uint8Array cannot cross to the host; pass text instead, e.g. `new TextDecoder().decode(bytes)` or `bytes.toBase64()`.");
|
|
45
|
-
}
|
|
46
|
-
if (boundary && value instanceof URLSearchParamsObj)
|
|
47
|
-
return value.params.toString();
|
|
48
|
-
if (value instanceof HeadersObj)
|
|
49
|
-
return Object.fromEntries(value.headers);
|
|
50
|
-
const target = boundary && value instanceof SetObj ? new Arr(ctx.builtins.Array, [...value.set]) : value;
|
|
51
|
-
if (stack.has(target))
|
|
52
|
-
throw typeError("Converting circular structure to JSON.");
|
|
53
|
-
stack.add(target);
|
|
54
|
-
if (target instanceof Arr) {
|
|
55
|
-
const items = [];
|
|
56
|
-
for (let index = 0; index < target.items.length; index += 1) {
|
|
57
|
-
items.push((yield* visit(target, String(index), depth + 1)) ?? null);
|
|
58
|
-
}
|
|
59
|
-
stack.delete(target);
|
|
60
|
-
return items;
|
|
61
|
-
}
|
|
62
|
-
const copied = {};
|
|
63
|
-
// Own data property regardless of the key, so "__proto__" never reaches the Object.prototype setter.
|
|
64
|
-
const put = (name, item) => {
|
|
65
|
-
if (item !== undefined)
|
|
66
|
-
Object.defineProperty(copied, name, { value: item, enumerable: true, writable: true, configurable: true });
|
|
67
|
-
};
|
|
68
|
-
// Errors serialize as { name, message, ...own }: both may be inherited, and neither is enumerable in JS.
|
|
69
|
-
if (target instanceof ErrorObj) {
|
|
70
|
-
put("name", yield* visit(target, "name", depth + 1));
|
|
71
|
-
put("message", yield* visit(target, "message", depth + 1));
|
|
72
|
-
}
|
|
73
|
-
for (const name of keys(target)) {
|
|
74
|
-
if (boundary && name === "__proto__")
|
|
75
|
-
continue;
|
|
76
|
-
put(name, yield* visit(target, name, depth + 1));
|
|
77
|
-
}
|
|
78
|
-
stack.delete(target);
|
|
79
|
-
return copied;
|
|
80
|
-
};
|
|
81
|
-
return visit(record(ctx.builtins.Object, { "": value }), "", 0);
|
|
82
|
-
};
|
|
83
|
-
/** Host JSON as program values: objects and arrays are copied, primitives pass through. */
|
|
84
|
-
export const fromJson = (ctx, value) => {
|
|
85
|
-
if (value === null || typeof value !== "object")
|
|
86
|
-
return value;
|
|
87
|
-
if (Array.isArray(value))
|
|
88
|
-
return new Arr(ctx.builtins.Array, value.map((item) => fromJson(ctx, item)));
|
|
89
|
-
const copied = new Obj(ctx.builtins.Object);
|
|
90
|
-
for (const [key, item] of Object.entries(value))
|
|
91
|
-
define(copied, key, fromJson(ctx, item));
|
|
92
|
-
return copied;
|
|
93
|
-
};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { Cause } from "effect";
|
|
2
|
-
import type { Interpreter } from "./interpreter.js";
|
|
3
|
-
import { Callable, type Cursor, type NativeResult, type Value } from "./objects.js";
|
|
4
|
-
import type { Eval } from "./runner.js";
|
|
5
|
-
/**
|
|
6
|
-
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
7
|
-
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
8
|
-
* program's body interrupts the execution.
|
|
9
|
-
*/
|
|
10
|
-
export declare class Interruption {
|
|
11
|
-
readonly cause: Cause.Cause<unknown>;
|
|
12
|
-
constructor(cause: Cause.Cause<unknown>);
|
|
13
|
-
}
|
|
14
|
-
export declare const isEval: <A>(value: A | Eval<A>) => value is Eval<A>;
|
|
15
|
-
/** IteratorClose: a consumer failure closes the iterator and wins over any close failure, except that a generator's
|
|
16
|
-
* return() is a return completion, so a failing close wins over it, as after `break`. */
|
|
17
|
-
export declare function preserveConsumerError<A>(close: Cursor["close"], body: Eval<A>): Eval<A>;
|
|
18
|
-
export type Hint = "number" | "string" | "default";
|
|
19
|
-
/**
|
|
20
|
-
* ToPrimitive: calls `valueOf`/`toString` in hint order and returns the first primitive result. Dates treat the
|
|
21
|
-
* default hint as "string", like their `Symbol.toPrimitive`. Opaque values (functions, promises, generators, tool
|
|
22
|
-
* references) pass through unchanged so callers reject or describe them in their built-in form.
|
|
23
|
-
*/
|
|
24
|
-
export declare function toPrimitive<R>(ctx: Interpreter<R>, value: Value, hint: Hint): Eval<Value>;
|
|
25
|
-
/** Invoke(value, name): calls the method the value would find through its prototype. */
|
|
26
|
-
export declare const invoke: <R>(ctx: Interpreter<R>, value: Value, name: string, label: string) => Eval<Value>;
|
|
27
|
-
export declare function toPrimitiveString<R>(ctx: Interpreter<R>, value: Value): Eval<string>;
|
|
28
|
-
export declare function toPrimitiveNumber<R>(ctx: Interpreter<R>, value: Value): Eval<number>;
|
|
29
|
-
/**
|
|
30
|
-
* Runs a native body on its arguments after ToPrimitive, in order, with one hint for all positions or one per
|
|
31
|
-
* position. Primitive arguments skip the conversion entirely.
|
|
32
|
-
*/
|
|
33
|
-
export declare const withPrimitives: <R>(ctx: Interpreter<R>, hints: Hint | ReadonlyArray<Hint>, values: Array<Value>, body: (primitives: Array<Value>) => NativeResult) => NativeResult;
|
|
34
|
-
export declare const isSupportedCallback: (value: Value) => value is Callable;
|
|
35
|
-
export declare const applyCollectionCallback: <R>(ctx: Interpreter<R>, callback: Value, name: string) => ((args: Array<Value>, thisValue?: Value) => Eval<Value>);
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { primitivePrototype } from "./intrinsics.js";
|
|
2
|
-
import { GeneratorReturn, typeError } from "./model.js";
|
|
3
|
-
import { Callable, get, Native, DateObj, Obj, coerceToNumber, coerceToString, } from "./objects.js";
|
|
4
|
-
import { isOpaque, typeofValue } from "./references.js";
|
|
5
|
-
/**
|
|
6
|
-
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
7
|
-
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
8
|
-
* program's body interrupts the execution.
|
|
9
|
-
*/
|
|
10
|
-
export class Interruption {
|
|
11
|
-
cause;
|
|
12
|
-
constructor(cause) {
|
|
13
|
-
this.cause = cause;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
// Program values never have a `next` method of their own: an object's properties live in its property map.
|
|
17
|
-
export const isEval = (value) => typeof value === "object" && value !== null && typeof value.next === "function";
|
|
18
|
-
/** IteratorClose: a consumer failure closes the iterator and wins over any close failure, except that a generator's
|
|
19
|
-
* return() is a return completion, so a failing close wins over it, as after `break`. */
|
|
20
|
-
export function* preserveConsumerError(close, body) {
|
|
21
|
-
try {
|
|
22
|
-
return yield* body;
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
if (error instanceof Interruption)
|
|
26
|
-
throw error;
|
|
27
|
-
try {
|
|
28
|
-
yield* close();
|
|
29
|
-
}
|
|
30
|
-
catch (closed) {
|
|
31
|
-
if (error instanceof GeneratorReturn)
|
|
32
|
-
throw closed;
|
|
33
|
-
}
|
|
34
|
-
throw error;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* ToPrimitive: calls `valueOf`/`toString` in hint order and returns the first primitive result. Dates treat the
|
|
39
|
-
* default hint as "string", like their `Symbol.toPrimitive`. Opaque values (functions, promises, generators, tool
|
|
40
|
-
* references) pass through unchanged so callers reject or describe them in their built-in form.
|
|
41
|
-
*/
|
|
42
|
-
export function* toPrimitive(ctx, value, hint) {
|
|
43
|
-
if (!(value instanceof Obj) || isOpaque(value))
|
|
44
|
-
return value;
|
|
45
|
-
const asString = hint === "string" || (hint === "default" && value instanceof DateObj);
|
|
46
|
-
for (const method of asString ? ["toString", "valueOf"] : ["valueOf", "toString"]) {
|
|
47
|
-
const callable = get(value, method);
|
|
48
|
-
if (!(callable instanceof Callable))
|
|
49
|
-
continue;
|
|
50
|
-
const result = yield* ctx.call(callable, value, []);
|
|
51
|
-
if (result === null || (typeof result !== "object" && typeof result !== "function"))
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
throw typeError("Cannot convert object to primitive value.");
|
|
55
|
-
}
|
|
56
|
-
/** Invoke(value, name): calls the method the value would find through its prototype. */
|
|
57
|
-
export const invoke = (ctx, value, name, label) => {
|
|
58
|
-
const target = value instanceof Obj ? value : primitivePrototype(ctx.builtins, value);
|
|
59
|
-
if (target === undefined)
|
|
60
|
-
throw typeError(`${label} called on null or undefined.`);
|
|
61
|
-
return ctx.call(get(target, name), value, []);
|
|
62
|
-
};
|
|
63
|
-
export function* toPrimitiveString(ctx, value) {
|
|
64
|
-
return coerceToString(yield* toPrimitive(ctx, value, "string"));
|
|
65
|
-
}
|
|
66
|
-
export function* toPrimitiveNumber(ctx, value) {
|
|
67
|
-
return coerceToNumber(yield* toPrimitive(ctx, value, "number"));
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Runs a native body on its arguments after ToPrimitive, in order, with one hint for all positions or one per
|
|
71
|
-
* position. Primitive arguments skip the conversion entirely.
|
|
72
|
-
*/
|
|
73
|
-
export const withPrimitives = (ctx, hints, values, body) => {
|
|
74
|
-
if (!values.some((value) => value instanceof Obj))
|
|
75
|
-
return body(values);
|
|
76
|
-
return (function* () {
|
|
77
|
-
const primitives = [];
|
|
78
|
-
for (const [index, value] of values.entries()) {
|
|
79
|
-
primitives.push(yield* toPrimitive(ctx, value, typeof hints === "string" ? hints : hints[index]));
|
|
80
|
-
}
|
|
81
|
-
const result = body(primitives);
|
|
82
|
-
return isEval(result) ? yield* result : result;
|
|
83
|
-
})();
|
|
84
|
-
};
|
|
85
|
-
// The single acceptance list for callbacks: collections, sort, string replacers,
|
|
86
|
-
// Array.from mappers, and promise reactions all admit exactly these callables.
|
|
87
|
-
// Admission means dispatchable, not necessarily invocable: new-requiring
|
|
88
|
-
// constructors pass the gate and throw a TypeError on call, like JS.
|
|
89
|
-
export const isSupportedCallback = (value) => value instanceof Callable && !(value instanceof Native && !value.callback);
|
|
90
|
-
export const applyCollectionCallback = (ctx, callback, name) => {
|
|
91
|
-
if (!isSupportedCallback(callback)) {
|
|
92
|
-
if (typeofValue(callback) === "function") {
|
|
93
|
-
throw typeError(`${name} cannot use this callable as a callback; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`);
|
|
94
|
-
}
|
|
95
|
-
throw typeError(`${name} expects a function callback.`);
|
|
96
|
-
}
|
|
97
|
-
return (callbackArgs, thisValue) => ctx.call(callback, thisValue, callbackArgs);
|
|
98
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Diagnostic } from "../codemode.js";
|
|
2
|
-
import { type AstNode } from "./model.js";
|
|
3
|
-
import { type ErrorType } from "./intrinsics.js";
|
|
4
|
-
import { type Native, ErrorObj, Obj, type Value } from "./objects.js";
|
|
5
|
-
import type { Interpreter } from "./interpreter.js";
|
|
6
|
-
/**
|
|
7
|
-
* A host error's message as text. A host can store anything there; an object must not cross as itself, and turning it
|
|
8
|
-
* into text would run its own methods.
|
|
9
|
-
*/
|
|
10
|
-
export declare const errorMessage: (error: Error) => string;
|
|
11
|
-
/** A host error's name as a program error type, read once so a getter cannot pass the check and then change. */
|
|
12
|
-
export declare const errorType: (error: Error) => ErrorType;
|
|
13
|
-
export declare const normalizeError: (error: unknown) => Diagnostic;
|
|
14
|
-
/**
|
|
15
|
-
* Gives a failure the source location of the expression that raised it, keeping the first one attached. Host errors
|
|
16
|
-
* that escape a built-in become the equivalent program error here.
|
|
17
|
-
*/
|
|
18
|
-
export declare const locate: (error: unknown, node?: AstNode) => unknown;
|
|
19
|
-
/** The program value a handler receives for a failure; one failure always yields the same value. */
|
|
20
|
-
export declare const materialize: <R>(ctx: Interpreter<R>, thrown: unknown) => Value;
|
|
21
|
-
export declare const createAggregateErrorValue: <R>(ctx: Interpreter<R>, errors: Array<Value>, message: string, proto?: Obj) => ErrorObj;
|
|
22
|
-
/** An error constructor such as `Error` or `TypeError`; callable with or without `new`, like JS. */
|
|
23
|
-
export declare const errorGlobal: <R>(type: ErrorType, ctx: Interpreter<R>) => Native;
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { ToolError } from "../tool-error.js";
|
|
2
|
-
import { ToolRuntimeError } from "../tool-runtime.js";
|
|
3
|
-
import { formatLocation, PendingThrow, Throw, sourceLocation, typeError } from "./model.js";
|
|
4
|
-
import { containsRuntimeReference } from "./references.js";
|
|
5
|
-
import { createErrorValue, isErrorType } from "./intrinsics.js";
|
|
6
|
-
import { constructor, methods, prototypeFrom, receiver } from "./native.js";
|
|
7
|
-
import { define, get, has, hidden, Arr, ErrorObj, Obj, coerceToString, } from "./objects.js";
|
|
8
|
-
import { toPrimitiveString } from "./callback.js";
|
|
9
|
-
import { formatValue } from "../stdlib/console.js";
|
|
10
|
-
/**
|
|
11
|
-
* A host error's message as text. A host can store anything there; an object must not cross as itself, and turning it
|
|
12
|
-
* into text would run its own methods.
|
|
13
|
-
*/
|
|
14
|
-
export const errorMessage = (error) => {
|
|
15
|
-
const message = error.message;
|
|
16
|
-
if (typeof message === "string")
|
|
17
|
-
return message;
|
|
18
|
-
if (message === undefined || typeof message === "function" || (typeof message === "object" && message !== null)) {
|
|
19
|
-
return "";
|
|
20
|
-
}
|
|
21
|
-
return String(message);
|
|
22
|
-
};
|
|
23
|
-
/** A host error's name as a program error type, read once so a getter cannot pass the check and then change. */
|
|
24
|
-
export const errorType = (error) => {
|
|
25
|
-
const name = error.name;
|
|
26
|
-
return typeof name === "string" && isErrorType(name) ? name : "Error";
|
|
27
|
-
};
|
|
28
|
-
export const normalizeError = (error) => {
|
|
29
|
-
if (error instanceof PendingThrow) {
|
|
30
|
-
const location = error.node?.loc ? sourceLocation(error.node) : undefined;
|
|
31
|
-
return {
|
|
32
|
-
kind: error.kind,
|
|
33
|
-
message: `${error.type}: ${error.message}${formatLocation(location)}`,
|
|
34
|
-
...(location ? { location } : {}),
|
|
35
|
-
...(error.suggestions ? { suggestions: error.suggestions } : {}),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
if (error instanceof ToolRuntimeError) {
|
|
39
|
-
return {
|
|
40
|
-
kind: error.kind,
|
|
41
|
-
message: error.message,
|
|
42
|
-
...(error.suggestions.length > 0 ? { suggestions: error.suggestions } : {}),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
if (error instanceof ToolError) {
|
|
46
|
-
return { kind: "ToolFailure", message: error.message };
|
|
47
|
-
}
|
|
48
|
-
if (error instanceof Throw) {
|
|
49
|
-
const value = error.value;
|
|
50
|
-
if (value instanceof ErrorObj) {
|
|
51
|
-
return value.host ? normalizeError(value.host) : { kind: "ExecutionFailure", message: errorToString(value) };
|
|
52
|
-
}
|
|
53
|
-
let message;
|
|
54
|
-
if (containsRuntimeReference(value)) {
|
|
55
|
-
// Never expose runtime reference internals through thrown values.
|
|
56
|
-
message = "a non-data value";
|
|
57
|
-
}
|
|
58
|
-
else if (typeof value === "string") {
|
|
59
|
-
message = value;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
message = formatValue(value);
|
|
63
|
-
}
|
|
64
|
-
return { kind: "ExecutionFailure", message: `Uncaught: ${message}` };
|
|
65
|
-
}
|
|
66
|
-
if (error instanceof RangeError && /call stack|recursion/i.test(errorMessage(error))) {
|
|
67
|
-
return {
|
|
68
|
-
kind: "ExecutionFailure",
|
|
69
|
-
message: "Execution exceeded the maximum nesting depth.",
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
if (error instanceof Error) {
|
|
73
|
-
return {
|
|
74
|
-
kind: error.name === "SyntaxError" ? "ParseError" : "ExecutionFailure",
|
|
75
|
-
message: errorMessage(error),
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
kind: "ExecutionFailure",
|
|
80
|
-
message: String(error),
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* Gives a failure the source location of the expression that raised it, keeping the first one attached. Host errors
|
|
85
|
-
* that escape a built-in become the equivalent program error here.
|
|
86
|
-
*/
|
|
87
|
-
export const locate = (error, node) => {
|
|
88
|
-
if (error instanceof PendingThrow) {
|
|
89
|
-
if (error.node === undefined && node)
|
|
90
|
-
error.node = node;
|
|
91
|
-
return error;
|
|
92
|
-
}
|
|
93
|
-
if (error instanceof Error && !(error instanceof ToolError) && !(error instanceof ToolRuntimeError)) {
|
|
94
|
-
return new PendingThrow(errorType(error), errorMessage(error), node);
|
|
95
|
-
}
|
|
96
|
-
return error;
|
|
97
|
-
};
|
|
98
|
-
/** The program value a handler receives for a failure; one failure always yields the same value. */
|
|
99
|
-
export const materialize = (ctx, thrown) => {
|
|
100
|
-
if (thrown instanceof Throw)
|
|
101
|
-
return thrown.value;
|
|
102
|
-
const builtins = ctx.builtins;
|
|
103
|
-
if (thrown instanceof PendingThrow) {
|
|
104
|
-
if (thrown.value === undefined) {
|
|
105
|
-
thrown.value = createErrorValue(builtins[thrown.type], thrown.message);
|
|
106
|
-
thrown.value.host = thrown;
|
|
107
|
-
}
|
|
108
|
-
return thrown.value;
|
|
109
|
-
}
|
|
110
|
-
return createErrorValue(builtins[thrown instanceof Error ? errorType(thrown) : "Error"], normalizeError(thrown).message);
|
|
111
|
-
};
|
|
112
|
-
/** Error.prototype.toString: `name: message`, omitting whichever side is empty. */
|
|
113
|
-
const errorToString = (self) => {
|
|
114
|
-
const name = get(self, "name");
|
|
115
|
-
const message = get(self, "message");
|
|
116
|
-
const shownName = name === undefined ? "Error" : coerceToString(name);
|
|
117
|
-
const shownMessage = message === undefined ? "" : coerceToString(message);
|
|
118
|
-
if (shownMessage === "")
|
|
119
|
-
return shownName;
|
|
120
|
-
if (shownName === "")
|
|
121
|
-
return shownMessage;
|
|
122
|
-
return `${shownName}: ${shownMessage}`;
|
|
123
|
-
};
|
|
124
|
-
export const createAggregateErrorValue = (ctx, errors, message, proto = ctx.builtins.AggregateError) => {
|
|
125
|
-
const value = createErrorValue(proto, message);
|
|
126
|
-
define(value, "errors", new Arr(ctx.builtins.Array, errors), { ...hidden });
|
|
127
|
-
return value;
|
|
128
|
-
};
|
|
129
|
-
function* constructAggregateErrorValue(ctx, args, proto) {
|
|
130
|
-
const message = args[1] === undefined ? "" : yield* toPrimitiveString(ctx, args[1]);
|
|
131
|
-
const cursor = yield* ctx.iterate(args[0]);
|
|
132
|
-
if (cursor === undefined)
|
|
133
|
-
throw typeError("new AggregateError(...) expects a synchronous iterable of errors.");
|
|
134
|
-
const errors = [];
|
|
135
|
-
while (true) {
|
|
136
|
-
const step = yield* cursor.next();
|
|
137
|
-
if (step.done)
|
|
138
|
-
return createAggregateErrorValue(ctx, errors, message, proto);
|
|
139
|
-
errors.push(step.value);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
/** An error constructor such as `Error` or `TypeError`; callable with or without `new`, like JS. */
|
|
143
|
-
export const errorGlobal = (type, ctx) => {
|
|
144
|
-
const builtins = ctx.builtins;
|
|
145
|
-
const prototype = builtins[type];
|
|
146
|
-
const construct = function* (args, newTarget) {
|
|
147
|
-
const proto = prototypeFrom(newTarget, prototype);
|
|
148
|
-
const created = type === "AggregateError"
|
|
149
|
-
? yield* constructAggregateErrorValue(ctx, args, proto)
|
|
150
|
-
: createErrorValue(proto, args[0] === undefined ? undefined : yield* toPrimitiveString(ctx, args[0]));
|
|
151
|
-
// ES2022 `new Error(message, { cause })`: installed only when the options object has the property at all.
|
|
152
|
-
const options = args[type === "AggregateError" ? 2 : 1];
|
|
153
|
-
if (options instanceof Obj && has(options, "cause"))
|
|
154
|
-
define(created, "cause", get(options, "cause"), hidden);
|
|
155
|
-
return created;
|
|
156
|
-
};
|
|
157
|
-
const ctor = constructor(builtins, prototype, {
|
|
158
|
-
name: type,
|
|
159
|
-
length: type === "AggregateError" ? 2 : 1,
|
|
160
|
-
call: (_, args) => construct(args, ctor),
|
|
161
|
-
construct,
|
|
162
|
-
});
|
|
163
|
-
if (type === "Error") {
|
|
164
|
-
methods(builtins, prototype, [
|
|
165
|
-
["toString", 0, (thisValue) => errorToString(receiver(Obj, thisValue, "Error.prototype.toString"))],
|
|
166
|
-
]);
|
|
167
|
-
methods(builtins, ctor, [["isError", 1, (_, args) => args[0] instanceof ErrorObj]]);
|
|
168
|
-
return ctor;
|
|
169
|
-
}
|
|
170
|
-
// Derived constructors extend Error itself, so its statics are inherited. The globals table creates Error first.
|
|
171
|
-
ctor.proto = get(builtins.Error, "constructor");
|
|
172
|
-
return ctor;
|
|
173
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type Program } from "acorn";
|
|
2
|
-
import { Effect } from "effect";
|
|
3
|
-
import type { Diagnostic, ResolvedExecutionLimits, Result } from "../codemode.js";
|
|
4
|
-
import { ToolRuntime } from "../tool-runtime.js";
|
|
5
|
-
import { Interpreter } from "./interpreter.js";
|
|
6
|
-
import type { Value } from "./objects.js";
|
|
7
|
-
export declare const executeProgram: <R>(code: string, prepared: ToolRuntime.Prepared<R>, limits: ResolvedExecutionLimits, hooks: ToolRuntime.Hooks<R>, globals?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>) => Effect.Effect<Result, never, R>;
|
|
8
|
-
export declare const parseProgram: (code: string) => {
|
|
9
|
-
ok: true;
|
|
10
|
-
program: Program;
|
|
11
|
-
} | {
|
|
12
|
-
ok: false;
|
|
13
|
-
error: Diagnostic;
|
|
14
|
-
};
|