@opencode/codemode 0.0.0-dev-20138 → 0.0.0-dev-20141
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/dist/data.d.ts +2 -2
- package/dist/data.js +1 -1
- package/dist/interpreter/callback.d.ts +1 -1
- package/dist/interpreter/callback.js +1 -1
- package/dist/interpreter/globals.d.ts +1 -1
- package/dist/interpreter/globals.js +33 -1
- package/dist/interpreter/interpreter.js +39 -6
- package/dist/interpreter/model.d.ts +1 -1
- package/dist/interpreter/model.js +1 -1
- package/dist/interpreter/objects.d.ts +12 -1
- package/dist/interpreter/objects.js +21 -1
- package/dist/stdlib/json.js +1 -1
- package/package.json +1 -1
package/dist/data.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export * as Data from "./data.js";
|
|
2
2
|
import { Effect, type Schema } from "effect";
|
|
3
3
|
import type { Interpreter } from "./interpreter/interpreter.js";
|
|
4
|
-
import { type Value } from "./interpreter/objects.js";
|
|
4
|
+
import { Obj, type Value } from "./interpreter/objects.js";
|
|
5
5
|
export type Json = Schema.Json;
|
|
6
|
-
type Replacer<R> = (args: Array<Value
|
|
6
|
+
type Replacer<R> = (args: Array<Value>, holder: Obj) => Effect.Effect<Value, unknown, R>;
|
|
7
7
|
/**
|
|
8
8
|
* What `JSON.stringify` would serialize for a program value, as host JSON: `toJSON` is honored, functions and
|
|
9
9
|
* `undefined` vanish, non-finite numbers become null, and everything else is copied. Two departures from JS
|
package/dist/data.js
CHANGED
|
@@ -28,7 +28,7 @@ const walk = (ctx, value, replacer, boundary) => {
|
|
|
28
28
|
const settled = raw instanceof PromiseObj ? yield* ctx.await(raw) : raw;
|
|
29
29
|
const toJSON = settled instanceof Obj ? get(settled, "toJSON") : undefined;
|
|
30
30
|
const own = toJSON instanceof Callable ? yield* ctx.call(toJSON, settled, [key]) : settled;
|
|
31
|
-
const value = replacer === undefined ? own : yield* replacer([key, own]);
|
|
31
|
+
const value = replacer === undefined ? own : yield* replacer([key, own], holder);
|
|
32
32
|
if (value === undefined || typeofValue(value) === "function")
|
|
33
33
|
return undefined;
|
|
34
34
|
if (typeof value === "number")
|
|
@@ -14,4 +14,4 @@ export declare const invoke: <R>(ctx: Interpreter<R>, value: Value, name: string
|
|
|
14
14
|
export declare const toPrimitiveString: <R>(ctx: Interpreter<R>, value: Value) => Effect.Effect<string, unknown, R>;
|
|
15
15
|
export declare const toPrimitiveNumber: <R>(ctx: Interpreter<R>, value: Value) => Effect.Effect<number, unknown, R>;
|
|
16
16
|
export declare const isSupportedCallback: (value: Value) => value is Callable;
|
|
17
|
-
export declare const applyCollectionCallback: <R>(ctx: Interpreter<R>, callback: Value, name: string) => ((args: Array<Value
|
|
17
|
+
export declare const applyCollectionCallback: <R>(ctx: Interpreter<R>, callback: Value, name: string) => ((args: Array<Value>, thisValue?: Value) => Effect.Effect<Value, unknown, R>);
|
|
@@ -57,5 +57,5 @@ export const applyCollectionCallback = (ctx, callback, name) => {
|
|
|
57
57
|
}
|
|
58
58
|
throw typeError(`${name} expects a function callback.`);
|
|
59
59
|
}
|
|
60
|
-
return (callbackArgs) => ctx.call(callback,
|
|
60
|
+
return (callbackArgs, thisValue) => ctx.call(callback, thisValue, callbackArgs);
|
|
61
61
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
+
import { Arr, Callable, coerceToInteger, coerceToString, get, Obj } from "./objects.js";
|
|
2
3
|
import { arrayGlobal } from "../stdlib/array.js";
|
|
3
4
|
import { textDecoderGlobal, textEncoderGlobal, uint8ArrayGlobal } from "../stdlib/bytes.js";
|
|
4
5
|
import { mapGlobal, setGlobal } from "../stdlib/collections.js";
|
|
@@ -18,8 +19,9 @@ import { base64Global, cryptoGlobal, structuredCloneGlobal } from "../stdlib/web
|
|
|
18
19
|
import { ToolReference } from "../tool-runtime.js";
|
|
19
20
|
import { errorGlobal } from "./errors.js";
|
|
20
21
|
import { errorTypes } from "./intrinsics.js";
|
|
21
|
-
import { constants, constructor, native } from "./native.js";
|
|
22
|
+
import { constants, constructor, methods, native, receiver } from "./native.js";
|
|
22
23
|
import { AsyncIteratorSymbol, IteratorSymbol, typeError } from "./model.js";
|
|
24
|
+
import { checkArrayLength } from "./limits.js";
|
|
23
25
|
import { generatorGlobals } from "./generators.js";
|
|
24
26
|
import { promiseGlobal } from "./promises.js";
|
|
25
27
|
// Function.prototype.constructor exists so `fn.constructor === Function` holds; dynamic code is unsupported.
|
|
@@ -27,6 +29,24 @@ const functionGlobal = (ctx) => {
|
|
|
27
29
|
const reject = () => Effect.sync(() => {
|
|
28
30
|
throw typeError("The Function constructor is not supported; write the function inline.");
|
|
29
31
|
});
|
|
32
|
+
const target = (thisValue, method) => receiver(Callable, thisValue, `Function.prototype.${method}`);
|
|
33
|
+
methods(ctx.builtins, ctx.builtins.Function, [
|
|
34
|
+
["call", 1, (thisValue, args) => ctx.call(target(thisValue, "call"), args[0], args.slice(1))],
|
|
35
|
+
["apply", 2, (thisValue, args) => ctx.call(target(thisValue, "apply"), args[0], listFromArrayLike(args[1]))],
|
|
36
|
+
[
|
|
37
|
+
"bind",
|
|
38
|
+
1,
|
|
39
|
+
(thisValue, args) => {
|
|
40
|
+
const fn = target(thisValue, "bind");
|
|
41
|
+
const bound = args.slice(1);
|
|
42
|
+
return native(ctx.builtins, {
|
|
43
|
+
name: `bound ${coerceToString(get(fn, "name"))}`,
|
|
44
|
+
length: Math.max(0, fn.length - bound.length),
|
|
45
|
+
call: (_, rest) => ctx.call(fn, args[0], [...bound, ...rest]),
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
]);
|
|
30
50
|
return constructor(ctx.builtins, ctx.builtins.Function, {
|
|
31
51
|
name: "Function",
|
|
32
52
|
length: 1,
|
|
@@ -34,6 +54,18 @@ const functionGlobal = (ctx) => {
|
|
|
34
54
|
construct: reject,
|
|
35
55
|
});
|
|
36
56
|
};
|
|
57
|
+
// CreateListFromArrayLike: `apply` reads `length` and the indexed properties of any object.
|
|
58
|
+
const listFromArrayLike = (value) => {
|
|
59
|
+
if (value === undefined || value === null)
|
|
60
|
+
return [];
|
|
61
|
+
if (value instanceof Arr)
|
|
62
|
+
return [...value.items];
|
|
63
|
+
if (!(value instanceof Obj))
|
|
64
|
+
throw typeError("Function.prototype.apply expects an array-like argument list.");
|
|
65
|
+
const length = Math.max(0, coerceToInteger(get(value, "length")));
|
|
66
|
+
checkArrayLength(length);
|
|
67
|
+
return Array.from({ length }, (_, index) => get(value, String(index)));
|
|
68
|
+
};
|
|
37
69
|
const symbolGlobal = (ctx) => {
|
|
38
70
|
const symbol = native(ctx.builtins, {
|
|
39
71
|
name: "Symbol",
|
|
@@ -6,7 +6,7 @@ import { checkStringLength } from "./limits.js";
|
|
|
6
6
|
import { locate, materialize } from "./errors.js";
|
|
7
7
|
import { primitivePrototype } from "./intrinsics.js";
|
|
8
8
|
import { globals } from "./globals.js";
|
|
9
|
-
import { assign, Callable, define, frozen, get, hostCursor, IteratorObj, has, hidden, hasPrototype, keys, Native, parseArrayIndex, Arr, Fn, GeneratorObj, Obj, PromiseObj, record, remove, set, coerceToNumber, coerceToString, } from "./objects.js";
|
|
9
|
+
import { assign, Callable, define, frozen, get, hostCursor, IteratorObj, has, hidden, hasPrototype, keys, Native, parseArrayIndex, Arguments, Arr, Fn, GeneratorObj, Obj, PromiseObj, record, remove, set, coerceToNumber, coerceToString, } from "./objects.js";
|
|
10
10
|
import { preserveConsumerError } from "./callback.js";
|
|
11
11
|
import { Pending, resolvePromise, resolvePromiseValue } from "./promises.js";
|
|
12
12
|
import { describeValue, isOpaque, rejectCircularInsertion, typeofValue } from "./references.js";
|
|
@@ -94,6 +94,25 @@ const collectPatternNames = (pattern, out = []) => {
|
|
|
94
94
|
}
|
|
95
95
|
return out;
|
|
96
96
|
};
|
|
97
|
+
// Whether a function body (or a parameter default) reads `arguments`, looking through arrows but not nested
|
|
98
|
+
// functions, which own theirs. Memoized so the object is only built for calls that can observe it.
|
|
99
|
+
const argumentsUse = new WeakMap();
|
|
100
|
+
const usesArguments = (fn) => {
|
|
101
|
+
const cached = argumentsUse.get(fn.body);
|
|
102
|
+
if (cached !== undefined)
|
|
103
|
+
return cached;
|
|
104
|
+
const found = [...fn.parameters, fn.body].some(function visit(node) {
|
|
105
|
+
if (node === null || typeof node !== "object")
|
|
106
|
+
return false;
|
|
107
|
+
if (node.type === "Identifier")
|
|
108
|
+
return node.name === "arguments";
|
|
109
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression")
|
|
110
|
+
return false;
|
|
111
|
+
return Object.values(node).some((child) => Array.isArray(child) ? child.some((item) => visit(item)) : visit(child));
|
|
112
|
+
});
|
|
113
|
+
argumentsUse.set(fn.body, found);
|
|
114
|
+
return found;
|
|
115
|
+
};
|
|
97
116
|
// `var` names declared anywhere in a function body except inside nested functions, which own theirs.
|
|
98
117
|
// Memoized per body: a function's var names never change, and hoisting runs on every call.
|
|
99
118
|
const varNames = new WeakMap();
|
|
@@ -171,7 +190,8 @@ export class Interpreter {
|
|
|
171
190
|
this.pending = options.pending;
|
|
172
191
|
this.builtins = options.builtins;
|
|
173
192
|
this.logs = options.logs ?? [];
|
|
174
|
-
|
|
193
|
+
// Program code has no receiver: top-level `this` is undefined, as in a module.
|
|
194
|
+
const globalScope = new Map([["this", { mutable: false, value: undefined }]]);
|
|
175
195
|
// Calling back into the program never reads frame state, so any frame serves; the root is always alive.
|
|
176
196
|
this.root = new Frame(this, new ScopeStack([globalScope]));
|
|
177
197
|
for (const [name, value] of [...globals(this), ...(options.globals?.(this) ?? [])]) {
|
|
@@ -321,7 +341,7 @@ class Frame {
|
|
|
321
341
|
}
|
|
322
342
|
createFunction(node, name = node.type === "ArrowFunctionExpression" ? "" : (node.id?.name ?? "")) {
|
|
323
343
|
const builtins = this.ctx.builtins;
|
|
324
|
-
const fn = new Fn(builtins.Function, name, node.params, node.body, this.scopes.capture(), node.async, node.generator);
|
|
344
|
+
const fn = new Fn(builtins.Function, name, node.params, node.body, this.scopes.capture(), node.async, node.generator, node.type === "ArrowFunctionExpression");
|
|
325
345
|
// Each generator function gets its own prototype, so `g() instanceof g` holds as in JS.
|
|
326
346
|
if (node.generator)
|
|
327
347
|
define(fn, "prototype", new Obj(node.async ? builtins.AsyncGenerator : builtins.Generator), hidden);
|
|
@@ -978,6 +998,8 @@ class Frame {
|
|
|
978
998
|
}
|
|
979
999
|
case "Identifier":
|
|
980
1000
|
return Effect.sync(() => this.scopes.get(node.name, node));
|
|
1001
|
+
case "ThisExpression":
|
|
1002
|
+
return Effect.sync(() => this.scopes.get("this", node));
|
|
981
1003
|
case "BinaryExpression":
|
|
982
1004
|
return this.evaluateBinaryExpression(node);
|
|
983
1005
|
case "LogicalExpression":
|
|
@@ -1328,7 +1350,7 @@ class Frame {
|
|
|
1328
1350
|
return yield* self.createToolCallPromise(callable.path, args);
|
|
1329
1351
|
}
|
|
1330
1352
|
if (callable instanceof Fn)
|
|
1331
|
-
return yield* self.invokeFunction(callable, args, node);
|
|
1353
|
+
return yield* self.invokeFunction(callable, thisValue, args, node);
|
|
1332
1354
|
if (callable instanceof Native) {
|
|
1333
1355
|
return yield* self.native(() => callable.call(thisValue, args), node);
|
|
1334
1356
|
}
|
|
@@ -1364,15 +1386,26 @@ class Frame {
|
|
|
1364
1386
|
});
|
|
1365
1387
|
}
|
|
1366
1388
|
// A callback invoked by a built-in runs below the call that invoked the built-in, so the deeper of the two counts.
|
|
1367
|
-
invokeFunction(fn, args, node) {
|
|
1389
|
+
invokeFunction(fn, thisValue, args, node) {
|
|
1368
1390
|
const self = this;
|
|
1369
1391
|
return Effect.flatMap(CallSite, (site) => {
|
|
1370
1392
|
const depth = Math.max(self.depth, site.depth) + 1;
|
|
1371
1393
|
if (depth > MAX_CALL_DEPTH)
|
|
1372
1394
|
throw rangeError("Maximum call stack size exceeded", node);
|
|
1373
1395
|
const invocation = new Frame(this.ctx, new ScopeStack([...fn.capturedScopes, new Map()]), depth);
|
|
1374
|
-
// Seed all parameters first so defaults cannot fall through to same-named outer bindings.
|
|
1375
1396
|
const paramScope = invocation.scopes.current();
|
|
1397
|
+
// `this` and `arguments` are scope bindings so arrows resolve them lexically; a parameter named
|
|
1398
|
+
// `arguments` shadows the object, as in JS.
|
|
1399
|
+
if (!fn.arrow)
|
|
1400
|
+
paramScope.set("this", { mutable: false, value: thisValue, initialized: true });
|
|
1401
|
+
if (!fn.arrow && usesArguments(fn)) {
|
|
1402
|
+
paramScope.set("arguments", {
|
|
1403
|
+
mutable: true,
|
|
1404
|
+
value: new Arguments(self.ctx.builtins.Object, args),
|
|
1405
|
+
initialized: true,
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
// Seed all parameters first so defaults cannot fall through to same-named outer bindings.
|
|
1376
1409
|
for (const parameter of fn.parameters) {
|
|
1377
1410
|
for (const name of collectPatternNames(parameter)) {
|
|
1378
1411
|
paramScope.set(name, { mutable: true, value: undefined, initialized: false });
|
|
@@ -62,7 +62,7 @@ export declare const rangeError: (message: string, node?: AstNode) => PendingThr
|
|
|
62
62
|
export declare const referenceError: (message: string, node?: AstNode) => PendingThrow;
|
|
63
63
|
export declare const syntaxError: (message: string, node?: AstNode) => PendingThrow;
|
|
64
64
|
export declare const uriError: (message: string, node?: AstNode) => PendingThrow;
|
|
65
|
-
export declare const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes,
|
|
65
|
+
export declare const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes, getters/setters, BigInt, and custom Symbols. Use plain functions and data objects instead.";
|
|
66
66
|
export declare const unsupportedSyntax: (kind: string, node: AstNode) => PendingThrow;
|
|
67
67
|
export declare const sourceLocation: (node: AstNode) => {
|
|
68
68
|
readonly line: number;
|
|
@@ -49,7 +49,7 @@ export const referenceError = failure("ReferenceError");
|
|
|
49
49
|
export const syntaxError = failure("SyntaxError");
|
|
50
50
|
export const uriError = failure("URIError");
|
|
51
51
|
// Orient the agent rather than enumerate JavaScript; interpreter-support.md is the full matrix.
|
|
52
|
-
export const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes,
|
|
52
|
+
export const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes, getters/setters, BigInt, and custom Symbols. Use plain functions and data objects instead.";
|
|
53
53
|
export const unsupportedSyntax = (kind, node) => new PendingThrow("SyntaxError", `Syntax '${kind}' is not supported. ${supportedSyntaxMessage}`, node, "UnsupportedSyntax", [supportedSyntaxMessage]);
|
|
54
54
|
// Acorn lines are 1-based and its columns are 0-based. Diagnostics use 1-based columns of the submitted source.
|
|
55
55
|
export const sourceLocation = (node) => ({
|
|
@@ -84,6 +84,7 @@ export declare abstract class Opaque extends Obj {
|
|
|
84
84
|
inspect(): string;
|
|
85
85
|
}
|
|
86
86
|
export declare abstract class Callable extends Opaque {
|
|
87
|
+
readonly length: number;
|
|
87
88
|
readonly tag = "Function";
|
|
88
89
|
constructor(proto: Obj, name: string, length: number);
|
|
89
90
|
get describe(): string;
|
|
@@ -94,7 +95,17 @@ export declare class Fn extends Callable {
|
|
|
94
95
|
readonly capturedScopes: Array<Map<string, Binding>>;
|
|
95
96
|
readonly async: boolean;
|
|
96
97
|
readonly generator: boolean;
|
|
97
|
-
|
|
98
|
+
/** Arrows have no `this` or `arguments` of their own; they read the enclosing function's. */
|
|
99
|
+
readonly arrow: boolean;
|
|
100
|
+
constructor(proto: Obj, name: string, parameters: ReadonlyArray<Pattern>, body: BlockStatement | Expression, capturedScopes: Array<Map<string, Binding>>, async: boolean, generator: boolean,
|
|
101
|
+
/** Arrows have no `this` or `arguments` of their own; they read the enclosing function's. */
|
|
102
|
+
arrow: boolean);
|
|
103
|
+
}
|
|
104
|
+
/** The strict `arguments` object: an ordinary object with indexed own properties and a hidden `length`. */
|
|
105
|
+
export declare class Arguments extends Obj {
|
|
106
|
+
readonly tag = "Arguments";
|
|
107
|
+
constructor(proto: Obj, args: Array<Value>);
|
|
108
|
+
iterator(): ArrayIterator<Value>;
|
|
98
109
|
}
|
|
99
110
|
export type NativeCall<R> = (thisValue: Value, args: Array<Value>) => Effect.Effect<Value, unknown, R>;
|
|
100
111
|
export type NativeConstruct<R> = (args: Array<Value>, newTarget: Callable) => Effect.Effect<Value, unknown, R>;
|
|
@@ -112,9 +112,11 @@ export class Opaque extends Obj {
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
export class Callable extends Opaque {
|
|
115
|
+
length;
|
|
115
116
|
tag = "Function";
|
|
116
117
|
constructor(proto, name, length) {
|
|
117
118
|
super(proto);
|
|
119
|
+
this.length = length;
|
|
118
120
|
define(this, "length", length, readonly);
|
|
119
121
|
define(this, "name", name, readonly);
|
|
120
122
|
}
|
|
@@ -128,7 +130,10 @@ export class Fn extends Callable {
|
|
|
128
130
|
capturedScopes;
|
|
129
131
|
async;
|
|
130
132
|
generator;
|
|
131
|
-
|
|
133
|
+
arrow;
|
|
134
|
+
constructor(proto, name, parameters, body, capturedScopes, async, generator,
|
|
135
|
+
/** Arrows have no `this` or `arguments` of their own; they read the enclosing function's. */
|
|
136
|
+
arrow) {
|
|
132
137
|
const optional = parameters.findIndex((p) => p.type === "AssignmentPattern" || p.type === "RestElement");
|
|
133
138
|
super(proto, name, optional === -1 ? parameters.length : optional);
|
|
134
139
|
this.parameters = parameters;
|
|
@@ -136,6 +141,21 @@ export class Fn extends Callable {
|
|
|
136
141
|
this.capturedScopes = capturedScopes;
|
|
137
142
|
this.async = async;
|
|
138
143
|
this.generator = generator;
|
|
144
|
+
this.arrow = arrow;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** The strict `arguments` object: an ordinary object with indexed own properties and a hidden `length`. */
|
|
148
|
+
export class Arguments extends Obj {
|
|
149
|
+
tag = "Arguments";
|
|
150
|
+
constructor(proto, args) {
|
|
151
|
+
super(proto);
|
|
152
|
+
args.forEach((arg, index) => define(this, String(index), arg));
|
|
153
|
+
define(this, "length", args.length, hidden);
|
|
154
|
+
}
|
|
155
|
+
iterator() {
|
|
156
|
+
return keys(this)
|
|
157
|
+
.map((key) => get(this, key))
|
|
158
|
+
.values();
|
|
139
159
|
}
|
|
140
160
|
}
|
|
141
161
|
export class Native extends Callable {
|
package/dist/stdlib/json.js
CHANGED
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-20141",
|
|
5
5
|
"description": "Effect-native confined code execution over schema-described tools",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|