@opencode/codemode 0.0.0-beta-19500 → 0.0.0-dev-19274

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.
Files changed (65) hide show
  1. package/README.md +0 -6
  2. package/dist/codemode.d.ts +12 -9
  3. package/dist/codemode.js +9 -5
  4. package/dist/index.d.ts +0 -1
  5. package/dist/index.js +0 -1
  6. package/dist/interpreter/errors.d.ts +6 -4
  7. package/dist/interpreter/errors.js +10 -25
  8. package/dist/interpreter/execute.d.ts +3 -4
  9. package/dist/interpreter/execute.js +18 -11
  10. package/dist/interpreter/iterator.d.ts +13 -0
  11. package/dist/interpreter/iterator.js +4 -0
  12. package/dist/interpreter/methods.d.ts +16 -3
  13. package/dist/interpreter/methods.js +290 -101
  14. package/dist/interpreter/model.d.ts +79 -10
  15. package/dist/interpreter/model.js +102 -2
  16. package/dist/interpreter/promises.d.ts +13 -15
  17. package/dist/interpreter/promises.js +52 -70
  18. package/dist/interpreter/references.d.ts +0 -1
  19. package/dist/interpreter/references.js +77 -57
  20. package/dist/interpreter/runtime.d.ts +95 -16
  21. package/dist/interpreter/runtime.js +960 -549
  22. package/dist/openapi/spec.js +6 -3
  23. package/dist/stdlib/collections.d.ts +1 -6
  24. package/dist/stdlib/collections.js +1 -117
  25. package/dist/stdlib/console.d.ts +2 -3
  26. package/dist/stdlib/console.js +28 -39
  27. package/dist/stdlib/date.d.ts +4 -5
  28. package/dist/stdlib/date.js +12 -34
  29. package/dist/stdlib/json.d.ts +6 -3
  30. package/dist/stdlib/json.js +63 -40
  31. package/dist/stdlib/math.d.ts +7 -3
  32. package/dist/stdlib/math.js +153 -85
  33. package/dist/stdlib/number.d.ts +4 -2
  34. package/dist/stdlib/number.js +37 -30
  35. package/dist/stdlib/object.d.ts +6 -6
  36. package/dist/stdlib/object.js +87 -84
  37. package/dist/stdlib/promise.d.ts +2 -0
  38. package/dist/stdlib/promise.js +1 -0
  39. package/dist/stdlib/regexp.d.ts +7 -6
  40. package/dist/stdlib/regexp.js +34 -48
  41. package/dist/stdlib/string.d.ts +3 -1
  42. package/dist/stdlib/string.js +17 -20
  43. package/dist/stdlib/url.d.ts +6 -10
  44. package/dist/stdlib/url.js +25 -102
  45. package/dist/stdlib/value.d.ts +7 -8
  46. package/dist/stdlib/value.js +56 -56
  47. package/dist/tool-runtime.d.ts +15 -16
  48. package/dist/tool-runtime.js +150 -13
  49. package/dist/values.d.ts +16 -22
  50. package/dist/values.js +17 -23
  51. package/package.json +1 -1
  52. package/dist/data.d.ts +0 -25
  53. package/dist/data.js +0 -153
  54. package/dist/interpreter/globals.d.ts +0 -13
  55. package/dist/interpreter/globals.js +0 -63
  56. package/dist/interpreter/host.d.ts +0 -41
  57. package/dist/interpreter/host.js +0 -44
  58. package/dist/interpreter/objects.d.ts +0 -37
  59. package/dist/interpreter/objects.js +0 -151
  60. package/dist/interpreter/runner.d.ts +0 -24
  61. package/dist/interpreter/runner.js +0 -45
  62. package/dist/stdlib/array.d.ts +0 -3
  63. package/dist/stdlib/array.js +0 -68
  64. package/dist/stdlib/web.d.ts +0 -4
  65. package/dist/stdlib/web.js +0 -20
@@ -1,44 +0,0 @@
1
- import { Effect } from "effect";
2
- import { InterpreterRuntimeError } from "./model.js";
3
- /** A host-implemented function value. `typeof` is "function". */
4
- export class HostFunction {
5
- name;
6
- call;
7
- construct;
8
- member;
9
- instanceOf;
10
- callback;
11
- constructor(options) {
12
- this.name = options.name;
13
- this.call = options.call;
14
- this.construct = options.construct;
15
- this.member = memberLookup(options.members);
16
- this.instanceOf = options.instanceOf;
17
- this.callback = options.callback ?? true;
18
- }
19
- }
20
- /** A host-implemented object of static members. `typeof` is "object" and it is not callable. */
21
- export class HostNamespace {
22
- name;
23
- member;
24
- constructor(name, members) {
25
- this.name = name;
26
- this.member = memberLookup(members);
27
- }
28
- }
29
- const memberLookup = (members) => {
30
- if (members === undefined)
31
- return () => undefined;
32
- if (typeof members === "function")
33
- return members;
34
- const table = new Map(Object.entries(members));
35
- return (key) => (typeof key === "string" ? table.get(key) : undefined);
36
- };
37
- /** Lifts a synchronous implementation, which may throw `InterpreterRuntimeError`, into a host call. */
38
- export const syncCall = (impl) => (args, node) => Effect.sync(() => impl(args, node));
39
- /** A synchronous host function. */
40
- export const sync = (name, impl, options = {}) => new HostFunction({ name, call: syncCall(impl), ...options });
41
- /** The `call` of a constructor that JS requires to be invoked with `new`. */
42
- export const requiresNew = (name) => (_, node) => Effect.sync(() => {
43
- throw new InterpreterRuntimeError(`Constructor ${name} requires 'new'.`, node).as("TypeError");
44
- });
@@ -1,37 +0,0 @@
1
- import type { BlockStatement, Expression, Pattern } from "acorn";
2
- import { type Binding } from "./model.js";
3
- /** An object owned by the program: own properties plus a prototype link. */
4
- export declare class ProgramObject {
5
- proto: ProgramObject | null;
6
- readonly props: Map<PropertyKey, unknown>;
7
- constructor(proto?: ProgramObject | null);
8
- }
9
- export declare class ProgramArray extends ProgramObject {
10
- readonly items: Array<unknown>;
11
- constructor(items?: Array<unknown>);
12
- }
13
- export declare class ProgramError extends ProgramObject {
14
- readonly errorName: string;
15
- constructor(errorName: string);
16
- }
17
- export declare class ProgramFunction extends ProgramObject {
18
- readonly name: string;
19
- readonly parameters: ReadonlyArray<Pattern>;
20
- readonly body: BlockStatement | Expression;
21
- readonly capturedScopes: ReadonlyArray<Map<string, Binding>>;
22
- readonly async: boolean;
23
- readonly generator: boolean;
24
- readonly length: number;
25
- constructor(name: string, parameters: ReadonlyArray<Pattern>, body: BlockStatement | Expression, capturedScopes: ReadonlyArray<Map<string, Binding>>, async: boolean, generator: boolean);
26
- }
27
- export declare const parseArrayIndex: (key: string | number) => number | undefined;
28
- export declare const hasOwn: (target: ProgramObject, key: PropertyKey) => boolean;
29
- export declare const getOwn: (target: ProgramObject, key: PropertyKey) => unknown;
30
- export declare const get: (target: ProgramObject, key: PropertyKey) => unknown;
31
- export declare const has: (target: ProgramObject, key: PropertyKey) => boolean;
32
- export declare const set: (target: ProgramObject, key: PropertyKey, value: unknown) => boolean;
33
- export declare const remove: (target: ProgramObject, key: PropertyKey) => boolean;
34
- export declare const ownKeys: (target: ProgramObject) => Array<string | symbol>;
35
- export declare const ownEntries: (target: ProgramObject) => Array<[string, unknown]>;
36
- export declare const record: (entries: Record<string, unknown>) => ProgramObject;
37
- export declare const assign: (target: ProgramObject, source: ProgramObject, skip?: ReadonlySet<PropertyKey>) => void;
@@ -1,151 +0,0 @@
1
- import { AsyncIteratorSymbol, IteratorSymbol } from "./model.js";
2
- /** An object owned by the program: own properties plus a prototype link. */
3
- export class ProgramObject {
4
- proto;
5
- props = new Map();
6
- constructor(proto = null) {
7
- this.proto = proto;
8
- }
9
- }
10
- export class ProgramArray extends ProgramObject {
11
- items;
12
- constructor(items = []) {
13
- super();
14
- this.items = items;
15
- }
16
- }
17
- export class ProgramError extends ProgramObject {
18
- errorName;
19
- constructor(errorName) {
20
- super();
21
- this.errorName = errorName;
22
- }
23
- }
24
- export class ProgramFunction extends ProgramObject {
25
- name;
26
- parameters;
27
- body;
28
- capturedScopes;
29
- async;
30
- generator;
31
- length;
32
- constructor(name, parameters, body, capturedScopes, async, generator) {
33
- super();
34
- this.name = name;
35
- this.parameters = parameters;
36
- this.body = body;
37
- this.capturedScopes = capturedScopes;
38
- this.async = async;
39
- this.generator = generator;
40
- const optional = parameters.findIndex((p) => p.type === "AssignmentPattern" || p.type === "RestElement");
41
- this.length = optional === -1 ? parameters.length : optional;
42
- }
43
- }
44
- const MAX_ARRAY_LENGTH = 4_294_967_295;
45
- export const parseArrayIndex = (key) => {
46
- const property = String(key);
47
- if (!/^(0|[1-9]\d*)$/.test(property))
48
- return undefined;
49
- const index = Number(property);
50
- return index < MAX_ARRAY_LENGTH ? index : undefined;
51
- };
52
- const canonical = (key) => (typeof key === "symbol" ? key : String(key));
53
- const index = (target, key) => target instanceof ProgramArray && typeof key === "string" ? parseArrayIndex(key) : undefined;
54
- // Non-enumerable built-in properties: array length, function name and length.
55
- const builtin = (target, name) => (name === "length" && target instanceof ProgramArray) ||
56
- ((name === "name" || name === "length") && target instanceof ProgramFunction);
57
- export const hasOwn = (target, key) => {
58
- const name = canonical(key);
59
- const at = index(target, name);
60
- if (at !== undefined)
61
- return at in target.items;
62
- return builtin(target, name) || target.props.has(name);
63
- };
64
- export const getOwn = (target, key) => {
65
- const name = canonical(key);
66
- const at = index(target, name);
67
- if (at !== undefined)
68
- return target.items[at];
69
- if (target instanceof ProgramArray && name === "length")
70
- return target.items.length;
71
- if (target instanceof ProgramFunction && name === "name")
72
- return target.name;
73
- if (target instanceof ProgramFunction && name === "length")
74
- return target.length;
75
- return target.props.get(name);
76
- };
77
- export const get = (target, key) => {
78
- for (let current = target; current !== null; current = current.proto) {
79
- if (hasOwn(current, key))
80
- return getOwn(current, key);
81
- }
82
- return undefined;
83
- };
84
- export const has = (target, key) => {
85
- for (let current = target; current !== null; current = current.proto) {
86
- if (hasOwn(current, key))
87
- return true;
88
- }
89
- return false;
90
- };
91
- export const set = (target, key, value) => {
92
- const name = canonical(key);
93
- const at = index(target, name);
94
- if (at !== undefined) {
95
- ;
96
- target.items[at] = value;
97
- return true;
98
- }
99
- if (name === "length" && target instanceof ProgramArray) {
100
- const length = typeof value === "number" ? value : Number(value);
101
- if (!Number.isInteger(length) || length < 0 || length > 4_294_967_295)
102
- return false;
103
- target.items.length = length;
104
- return true;
105
- }
106
- if (builtin(target, name))
107
- return false;
108
- target.props.set(name, value);
109
- return true;
110
- };
111
- export const remove = (target, key) => {
112
- const name = canonical(key);
113
- const at = index(target, name);
114
- if (at !== undefined)
115
- return delete target.items[at];
116
- if (name === "length" && target instanceof ProgramArray)
117
- return false;
118
- if (builtin(target, name))
119
- return true;
120
- target.props.delete(name);
121
- return true;
122
- };
123
- // JS order: array indexes, integer-like keys ascending, other strings, then symbols.
124
- export const ownKeys = (target) => {
125
- const strings = [...target.props.keys()].filter((key) => typeof key === "string");
126
- const symbols = [...target.props.keys()].filter((key) => typeof key === "symbol");
127
- return [
128
- ...(target instanceof ProgramArray ? Object.keys(target.items) : []),
129
- ...strings.filter((key) => parseArrayIndex(key) !== undefined).sort((a, b) => Number(a) - Number(b)),
130
- ...strings.filter((key) => parseArrayIndex(key) === undefined),
131
- ...symbols,
132
- ];
133
- };
134
- export const ownEntries = (target) => ownKeys(target)
135
- .filter((key) => typeof key === "string")
136
- .map((key) => [key, getOwn(target, key)]);
137
- export const record = (entries) => {
138
- const target = new ProgramObject();
139
- for (const [key, value] of Object.entries(entries))
140
- set(target, key, value);
141
- return target;
142
- };
143
- export const assign = (target, source, skip) => {
144
- for (const key of ownKeys(source)) {
145
- if (skip?.has(key))
146
- continue;
147
- if (typeof key === "symbol" && key !== IteratorSymbol && key !== AsyncIteratorSymbol)
148
- continue;
149
- set(target, key, getOwn(source, key));
150
- }
151
- };
@@ -1,24 +0,0 @@
1
- import { Effect } from "effect";
2
- import { Values } from "../values.js";
3
- import { HostFunction } from "./host.js";
4
- import { type AstNode, IntrinsicReference } from "./model.js";
5
- import { ProgramFunction } from "./objects.js";
6
- export type IteratorCursor<R> = {
7
- readonly next: Effect.Effect<{
8
- readonly done: boolean;
9
- readonly value: unknown;
10
- }, unknown, R>;
11
- readonly close: Effect.Effect<void, unknown, R>;
12
- };
13
- /** Everything a host function needs to call back into the program. */
14
- export type Runner<R> = {
15
- readonly invokeFunction: (fn: ProgramFunction, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
16
- readonly invokeCallable: (callable: unknown, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
17
- readonly settlePromise: (promise: Values.Promise) => Effect.Effect<unknown, unknown, never>;
18
- readonly syncIterator: (value: unknown, node: AstNode) => Effect.Effect<IteratorCursor<R> | undefined, unknown, R>;
19
- };
20
- export declare const preserveConsumerError: <A, R>(cursor: IteratorCursor<R>, effect: Effect.Effect<A, unknown, R>) => Effect.Effect<A, unknown, R>;
21
- export declare const toPrimitive: <R>(runner: Runner<R>, value: unknown, hint: "number" | "string", node: AstNode) => Effect.Effect<unknown, unknown, R>;
22
- export type SupportedCallback = ProgramFunction | HostFunction<unknown> | IntrinsicReference;
23
- export declare const isSupportedCallback: (value: unknown) => value is SupportedCallback;
24
- export declare const applyCollectionCallback: <R>(runner: Runner<R>, callback: unknown, name: string, node: AstNode) => ((args: Array<unknown>) => Effect.Effect<unknown, unknown, R>);
@@ -1,45 +0,0 @@
1
- import { Effect, Exit } from "effect";
2
- import { Values } from "../values.js";
3
- import { coerceToString } from "../stdlib/value.js";
4
- import { HostFunction } from "./host.js";
5
- import { InterpreterRuntimeError, IntrinsicReference } from "./model.js";
6
- import { get, has, ProgramFunction, ProgramObject } from "./objects.js";
7
- import { typeofValue } from "./references.js";
8
- export const preserveConsumerError = (cursor, effect) => Effect.flatMap(Effect.exit(effect), (exit) => Exit.isSuccess(exit)
9
- ? Effect.succeed(exit.value)
10
- : Effect.andThen(Effect.exit(cursor.close), Effect.failCause(exit.cause)));
11
- export const toPrimitive = (runner, value, hint, node) => {
12
- if (value === null || typeof value !== "object")
13
- return Effect.succeed(value);
14
- if (Values.isValue(value)) {
15
- return Effect.succeed(value instanceof Values.Date && hint === "number" ? value.time : coerceToString(value));
16
- }
17
- if (!(value instanceof ProgramObject))
18
- return Effect.succeed(value);
19
- const order = hint === "number" ? ["valueOf", "toString"] : ["toString", "valueOf"];
20
- return Effect.gen(function* () {
21
- for (const method of order) {
22
- if (method === "toString" && !has(value, "toString"))
23
- return coerceToString(value);
24
- const callable = get(value, method);
25
- if (typeofValue(callable) !== "function")
26
- continue;
27
- const result = yield* runner.invokeCallable(callable, [], node);
28
- if (result === null || (typeof result !== "object" && typeof result !== "function"))
29
- return result;
30
- }
31
- throw new InterpreterRuntimeError("Cannot convert object to primitive value.", node).as("TypeError");
32
- });
33
- };
34
- export const isSupportedCallback = (value) => value instanceof ProgramFunction ||
35
- (value instanceof HostFunction && value.callback) ||
36
- value instanceof IntrinsicReference;
37
- export const applyCollectionCallback = (runner, callback, name, node) => {
38
- if (!isSupportedCallback(callback)) {
39
- if (typeofValue(callback) === "function") {
40
- throw new InterpreterRuntimeError(`${name} cannot use this callable as a callback; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`, node);
41
- }
42
- throw new InterpreterRuntimeError(`${name} expects a function callback.`, node).as("TypeError");
43
- }
44
- return (callbackArgs) => runner.invokeCallable(callback, callbackArgs, node);
45
- };
@@ -1,3 +0,0 @@
1
- import { HostFunction } from "../interpreter/host.js";
2
- import { type Runner } from "../interpreter/runner.js";
3
- export declare const arrayGlobal: <R>(runner: Runner<R>) => HostFunction<R>;
@@ -1,68 +0,0 @@
1
- import { Effect } from "effect";
2
- import { HostFunction, sync, syncCall } from "../interpreter/host.js";
3
- import { CodeModeGenerator, InterpreterRuntimeError } from "../interpreter/model.js";
4
- import { get, ProgramArray, ProgramObject } from "../interpreter/objects.js";
5
- import { describeValue } from "../interpreter/references.js";
6
- import { applyCollectionCallback, preserveConsumerError } from "../interpreter/runner.js";
7
- const constructArray = (args, node) => {
8
- if (args.length !== 1)
9
- return new ProgramArray([...args]);
10
- const first = args[0];
11
- if (typeof first !== "number")
12
- return new ProgramArray([first]);
13
- if (!Number.isInteger(first) || first < 0 || first > 4294967295) {
14
- throw new InterpreterRuntimeError("Invalid array length.", node).as("RangeError");
15
- }
16
- // Sparse like JS: Array(3) has holes, and combinator loops already skip them.
17
- return new ProgramArray(new Array(first));
18
- };
19
- const arrayLikeSource = (source, node) => {
20
- if (source instanceof ProgramObject && typeof get(source, "length") === "number") {
21
- const length = get(source, "length");
22
- const normalized = Number.isNaN(length) || length <= 0 ? 0 : Math.trunc(length);
23
- if (normalized > 4_294_967_295)
24
- throw new RangeError("Invalid array length");
25
- return { length: normalized, source };
26
- }
27
- throw new InterpreterRuntimeError(`Array.from expects an array, string, Map, Set, or array-like value, received ${describeValue(source)}.`, node, "InvalidDataValue");
28
- };
29
- const arrayFrom = (runner, args, node) => {
30
- const source = args[0];
31
- const apply = args.length < 2 || args[1] === undefined ? undefined : applyCollectionCallback(runner, args[1], "Array.from", node);
32
- return Effect.gen(function* () {
33
- const cursor = yield* runner.syncIterator(source, node);
34
- if (cursor === undefined) {
35
- if (source instanceof CodeModeGenerator) {
36
- throw new InterpreterRuntimeError("Array.from expects a synchronous iterable or array-like value.", node).as("TypeError");
37
- }
38
- const arrayLike = arrayLikeSource(source, node);
39
- const values = [];
40
- for (let index = 0; index < arrayLike.length; index += 1) {
41
- const item = get(arrayLike.source, index);
42
- values.push(apply === undefined ? item : yield* apply([item, index]));
43
- }
44
- return new ProgramArray(values);
45
- }
46
- const values = [];
47
- let index = 0;
48
- while (true) {
49
- const step = yield* cursor.next;
50
- if (step.done)
51
- return new ProgramArray(values);
52
- values.push(apply === undefined ? step.value : yield* preserveConsumerError(cursor, apply([step.value, index])));
53
- index += 1;
54
- }
55
- });
56
- };
57
- // Array constructs identically with or without new, like JS.
58
- export const arrayGlobal = (runner) => new HostFunction({
59
- name: "Array",
60
- call: syncCall(constructArray),
61
- construct: syncCall(constructArray),
62
- instanceOf: (value) => value instanceof ProgramArray,
63
- members: {
64
- isArray: sync("Array.isArray", (args) => args[0] instanceof ProgramArray),
65
- of: sync("Array.of", (args) => new ProgramArray([...args])),
66
- from: new HostFunction({ name: "Array.from", call: (args, node) => arrayFrom(runner, args, node) }),
67
- },
68
- });
@@ -1,4 +0,0 @@
1
- import { HostNamespace } from "../interpreter/host.js";
2
- export declare const atobGlobal: import("../interpreter/host.js").HostFunction<never>;
3
- export declare const btoaGlobal: import("../interpreter/host.js").HostFunction<never>;
4
- export declare const cryptoGlobal: HostNamespace;
@@ -1,20 +0,0 @@
1
- import { HostNamespace, sync } from "../interpreter/host.js";
2
- import { InterpreterRuntimeError } from "../interpreter/model.js";
3
- import { coerceToString } from "./value.js";
4
- // WebIDL DOMString conversion: a missing argument is a TypeError, anything else stringifies.
5
- const base64 = (name) => sync(name, (args, node) => {
6
- if (args.length === 0)
7
- throw new InterpreterRuntimeError(`${name} requires 1 argument (a string)`, node).as("TypeError");
8
- const input = coerceToString(args[0]);
9
- try {
10
- return name === "atob" ? atob(input) : btoa(input);
11
- }
12
- catch {
13
- throw new InterpreterRuntimeError("The string contains invalid characters.", node).as("InvalidCharacterError");
14
- }
15
- });
16
- export const atobGlobal = base64("atob");
17
- export const btoaGlobal = base64("btoa");
18
- export const cryptoGlobal = new HostNamespace("crypto", {
19
- randomUUID: sync("crypto.randomUUID", () => crypto.randomUUID()),
20
- });