@opencode/codemode 2.0.1 → 2.0.2

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 (61) hide show
  1. package/dist/data.d.ts +6 -5
  2. package/dist/data.js +44 -46
  3. package/dist/index.d.ts +0 -1
  4. package/dist/index.js +0 -1
  5. package/dist/interpreter/errors.d.ts +3 -4
  6. package/dist/interpreter/errors.js +40 -17
  7. package/dist/interpreter/execute.js +5 -3
  8. package/dist/interpreter/generators.d.ts +4 -0
  9. package/dist/interpreter/generators.js +25 -0
  10. package/dist/interpreter/globals.js +67 -46
  11. package/dist/interpreter/intrinsics.d.ts +8 -5
  12. package/dist/interpreter/intrinsics.js +59 -18
  13. package/dist/interpreter/model.d.ts +2 -32
  14. package/dist/interpreter/model.js +4 -38
  15. package/dist/interpreter/native.d.ts +19 -0
  16. package/dist/interpreter/native.js +40 -0
  17. package/dist/interpreter/objects.d.ts +105 -12
  18. package/dist/interpreter/objects.js +190 -66
  19. package/dist/interpreter/promises.d.ts +12 -13
  20. package/dist/interpreter/promises.js +52 -46
  21. package/dist/interpreter/references.d.ts +1 -0
  22. package/dist/interpreter/references.js +20 -33
  23. package/dist/interpreter/runner.d.ts +15 -13
  24. package/dist/interpreter/runner.js +19 -19
  25. package/dist/interpreter/runtime.d.ts +3 -3
  26. package/dist/interpreter/runtime.js +150 -314
  27. package/dist/stdlib/array.d.ts +4 -2
  28. package/dist/stdlib/array.js +423 -31
  29. package/dist/stdlib/collections.d.ts +3 -7
  30. package/dist/stdlib/collections.js +286 -114
  31. package/dist/stdlib/console.d.ts +3 -2
  32. package/dist/stdlib/console.js +35 -30
  33. package/dist/stdlib/date.d.ts +1 -7
  34. package/dist/stdlib/date.js +93 -188
  35. package/dist/stdlib/json.d.ts +2 -2
  36. package/dist/stdlib/json.js +24 -24
  37. package/dist/stdlib/math.d.ts +2 -2
  38. package/dist/stdlib/math.js +96 -76
  39. package/dist/stdlib/number.d.ts +3 -4
  40. package/dist/stdlib/number.js +94 -59
  41. package/dist/stdlib/object.d.ts +4 -4
  42. package/dist/stdlib/object.js +123 -49
  43. package/dist/stdlib/regexp.d.ts +6 -8
  44. package/dist/stdlib/regexp.js +71 -63
  45. package/dist/stdlib/string.d.ts +2 -2
  46. package/dist/stdlib/string.js +213 -50
  47. package/dist/stdlib/url.d.ts +5 -13
  48. package/dist/stdlib/url.js +196 -96
  49. package/dist/stdlib/value.d.ts +5 -4
  50. package/dist/stdlib/value.js +16 -16
  51. package/dist/stdlib/web.d.ts +4 -4
  52. package/dist/stdlib/web.js +8 -7
  53. package/dist/tool-runtime.d.ts +2 -1
  54. package/dist/tool-runtime.js +2 -2
  55. package/package.json +1 -1
  56. package/dist/interpreter/host.d.ts +0 -41
  57. package/dist/interpreter/host.js +0 -44
  58. package/dist/interpreter/methods.d.ts +0 -4
  59. package/dist/interpreter/methods.js +0 -837
  60. package/dist/values.d.ts +0 -37
  61. package/dist/values.js +0 -56
@@ -1,18 +1,12 @@
1
1
  import { ToolReference } from "../tool-runtime.js";
2
- import { Values } from "../values.js";
3
- import { HostFunction, HostNamespace } from "./host.js";
4
- import { CodeModeGenerator, GeneratorMethodReference, InterpreterRuntimeError, IntrinsicReference, PromiseInstanceMethodReference, } from "./model.js";
5
- import { getOwn, ownKeys, ProgramArray, ProgramFunction, ProgramObject } from "./objects.js";
6
- export const isRuntimeReference = (value) => value instanceof HostFunction ||
7
- value instanceof HostNamespace ||
8
- value instanceof ProgramFunction ||
9
- value instanceof CodeModeGenerator ||
10
- value instanceof GeneratorMethodReference ||
2
+ import { InterpreterRuntimeError } from "./model.js";
3
+ import { Callable, getOwn, isWrapper, ownKeys, ProgramArray, ProgramDate, ProgramGenerator, ProgramMap, ProgramObject, ProgramPromise, ProgramRegExp, ProgramSet, ProgramURL, ProgramURLSearchParams, } from "./objects.js";
4
+ /** Values that cannot cross the data boundary. */
5
+ export const isRuntimeReference = (value) => value instanceof Callable ||
6
+ value instanceof ProgramGenerator ||
11
7
  value instanceof ToolReference ||
12
- value instanceof IntrinsicReference ||
13
- value instanceof PromiseInstanceMethodReference ||
14
- value instanceof Values.Promise ||
15
- Values.isValue(value);
8
+ value instanceof ProgramPromise ||
9
+ isWrapper(value);
16
10
  function* childValues(value) {
17
11
  if (!(value instanceof ProgramObject))
18
12
  return;
@@ -40,8 +34,8 @@ const find = (value, match, skip, seen) => {
40
34
  };
41
35
  const never = () => false;
42
36
  export const containsRuntimeReference = (value) => find(value, isRuntimeReference, never, new Set());
43
- // CodeMode values are data here, not opaque interpreter references.
44
- export const containsOpaqueReference = (value) => find(value, (current) => !Values.isValue(current) && isRuntimeReference(current), Values.isValue, new Set());
37
+ // Wrapper values are data here, not opaque interpreter references.
38
+ export const containsOpaqueReference = (value) => find(value, (current) => !isWrapper(current) && isRuntimeReference(current), isWrapper, new Set());
45
39
  // Reject cycles before mutation so later boundary walks remain safe.
46
40
  export const rejectCircularInsertion = (container, value, label, node, seen = new Set()) => {
47
41
  if (find(value, (current) => current === container, isRuntimeReference, seen)) {
@@ -49,27 +43,27 @@ export const rejectCircularInsertion = (container, value, label, node, seen = ne
49
43
  }
50
44
  };
51
45
  export const describeValue = (value) => {
52
- if (value === null)
53
- return "null";
46
+ if (value === null || value === undefined)
47
+ return String(value);
54
48
  if (value instanceof ProgramArray)
55
49
  return "an array";
56
- if (value instanceof Values.Promise)
50
+ if (value instanceof ProgramPromise)
57
51
  return "an un-awaited Promise";
58
52
  if (value instanceof ToolReference)
59
53
  return "a tool reference";
60
- if (value instanceof Values.Date)
54
+ if (value instanceof ProgramDate)
61
55
  return "a Date";
62
- if (value instanceof Values.RegExp)
56
+ if (value instanceof ProgramRegExp)
63
57
  return "a RegExp";
64
- if (value instanceof Values.Map)
58
+ if (value instanceof ProgramMap)
65
59
  return "a Map";
66
- if (value instanceof Values.Set)
60
+ if (value instanceof ProgramSet)
67
61
  return "a Set";
68
- if (value instanceof Values.URL)
62
+ if (value instanceof ProgramURL)
69
63
  return "a URL";
70
- if (value instanceof Values.URLSearchParams)
64
+ if (value instanceof ProgramURLSearchParams)
71
65
  return "a URLSearchParams";
72
- if (value instanceof CodeModeGenerator)
66
+ if (value instanceof ProgramGenerator)
73
67
  return "a generator";
74
68
  if (isRuntimeReference(value))
75
69
  return "a function";
@@ -78,15 +72,8 @@ export const describeValue = (value) => {
78
72
  return `a ${typeof value}`;
79
73
  };
80
74
  export const typeofValue = (value) => {
81
- if (value instanceof HostFunction ||
82
- value instanceof ProgramFunction ||
83
- value instanceof GeneratorMethodReference ||
84
- value instanceof IntrinsicReference ||
85
- value instanceof PromiseInstanceMethodReference) {
75
+ if (value instanceof Callable)
86
76
  return "function";
87
- }
88
- if (value instanceof HostNamespace)
89
- return "object";
90
77
  if (value instanceof ToolReference)
91
78
  return value.path.length > 0 ? "function" : "object";
92
79
  return typeof value;
@@ -1,9 +1,7 @@
1
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 type { Intrinsics } from "./intrinsics.js";
6
- import { ProgramFunction } from "./objects.js";
2
+ import type { Prototypes } from "./intrinsics.js";
3
+ import { type AstNode } from "./model.js";
4
+ import { Callable, ProgramPromise } from "./objects.js";
7
5
  export type IteratorCursor<R> = {
8
6
  readonly next: Effect.Effect<{
9
7
  readonly done: boolean;
@@ -11,16 +9,20 @@ export type IteratorCursor<R> = {
11
9
  }, unknown, R>;
12
10
  readonly close: Effect.Effect<void, unknown, R>;
13
11
  };
14
- /** Everything a host function needs from the realm: calling back into the program and its intrinsic objects. */
12
+ /** Everything a native function needs from the realm: calling back into the program and its intrinsic objects. */
15
13
  export type Runner<R> = {
16
- readonly invokeFunction: (fn: ProgramFunction, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
17
- readonly invokeCallable: (callable: unknown, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
18
- readonly settlePromise: (promise: Values.Promise) => Effect.Effect<unknown, unknown, never>;
14
+ readonly invokeCallable: (callable: unknown, thisValue: unknown, args: Array<unknown>, node: AstNode) => Effect.Effect<unknown, unknown, R>;
15
+ readonly settlePromise: (promise: ProgramPromise) => Effect.Effect<unknown, unknown, never>;
19
16
  readonly syncIterator: (value: unknown, node: AstNode) => Effect.Effect<IteratorCursor<R> | undefined, unknown, R>;
20
- readonly intrinsics: Intrinsics;
17
+ readonly prototypes: Prototypes;
21
18
  };
22
19
  export declare const preserveConsumerError: <A, R>(cursor: IteratorCursor<R>, effect: Effect.Effect<A, unknown, R>) => Effect.Effect<A, unknown, R>;
23
- export declare const toPrimitive: <R>(runner: Runner<R>, value: unknown, hint: "number" | "string", node: AstNode) => Effect.Effect<unknown, unknown, R>;
24
- export type SupportedCallback = ProgramFunction | HostFunction<unknown> | IntrinsicReference;
25
- export declare const isSupportedCallback: (value: unknown) => value is SupportedCallback;
20
+ /**
21
+ * ToPrimitive: calls `valueOf`/`toString` in hint order and returns the first primitive result. Dates treat the
22
+ * default hint as "string", like their `Symbol.toPrimitive`.
23
+ */
24
+ export declare const toPrimitive: <R>(runner: Runner<R>, value: unknown, hint: "number" | "string" | "default", node: AstNode) => Effect.Effect<unknown, unknown, R>;
25
+ export declare const toPrimitiveString: <R>(runner: Runner<R>, value: unknown, node: AstNode) => Effect.Effect<string, unknown, R>;
26
+ export declare const toPrimitiveNumber: <R>(runner: Runner<R>, value: unknown, node: AstNode) => Effect.Effect<number, unknown, R>;
27
+ export declare const isSupportedCallback: (value: unknown) => value is Callable;
26
28
  export declare const applyCollectionCallback: <R>(runner: Runner<R>, callback: unknown, name: string, node: AstNode) => ((args: Array<unknown>) => Effect.Effect<unknown, unknown, R>);
@@ -1,39 +1,39 @@
1
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";
2
+ import { coerceToNumber, coerceToString } from "../stdlib/value.js";
3
+ import { InterpreterRuntimeError } from "./model.js";
4
+ import { Callable, get, NativeFunction, ProgramDate, ProgramObject, ProgramPromise } from "./objects.js";
7
5
  import { typeofValue } from "./references.js";
8
6
  export const preserveConsumerError = (cursor, effect) => Effect.flatMap(Effect.exit(effect), (exit) => Exit.isSuccess(exit)
9
7
  ? Effect.succeed(exit.value)
10
8
  : Effect.andThen(Effect.exit(cursor.close), Effect.failCause(exit.cause)));
9
+ /**
10
+ * ToPrimitive: calls `valueOf`/`toString` in hint order and returns the first primitive result. Dates treat the
11
+ * default hint as "string", like their `Symbol.toPrimitive`.
12
+ */
11
13
  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
14
  if (!(value instanceof ProgramObject))
18
15
  return Effect.succeed(value);
19
- const order = hint === "number" ? ["valueOf", "toString"] : ["toString", "valueOf"];
16
+ const asString = hint === "string" || (hint === "default" && value instanceof ProgramDate);
17
+ const order = asString ? ["toString", "valueOf"] : ["valueOf", "toString"];
20
18
  return Effect.gen(function* () {
21
19
  for (const method of order) {
22
- if (method === "toString" && !has(value, "toString"))
23
- return coerceToString(value);
24
20
  const callable = get(value, method);
25
- if (typeofValue(callable) !== "function")
21
+ if (!(callable instanceof Callable))
26
22
  continue;
27
- const result = yield* runner.invokeCallable(callable, [], node);
23
+ const result = yield* runner.invokeCallable(callable, value, [], node);
28
24
  if (result === null || (typeof result !== "object" && typeof result !== "function"))
29
25
  return result;
30
26
  }
31
27
  throw new InterpreterRuntimeError("Cannot convert object to primitive value.", node);
32
28
  });
33
29
  };
34
- export const isSupportedCallback = (value) => value instanceof ProgramFunction ||
35
- (value instanceof HostFunction && value.callback) ||
36
- value instanceof IntrinsicReference;
30
+ export const toPrimitiveString = (runner, value, node) => Effect.map(toPrimitive(runner, value, "string", node), coerceToString);
31
+ export const toPrimitiveNumber = (runner, value, node) => Effect.map(toPrimitive(runner, value, "number", node), coerceToNumber);
32
+ // The single acceptance list for callbacks: collections, sort, string replacers,
33
+ // Array.from mappers, and promise reactions all admit exactly these callables.
34
+ // Admission means dispatchable, not necessarily invocable: new-requiring
35
+ // constructors pass the gate and throw a TypeError on call, like JS.
36
+ export const isSupportedCallback = (value) => value instanceof Callable && !(value instanceof NativeFunction && !value.callback);
37
37
  export const applyCollectionCallback = (runner, callback, name, node) => {
38
38
  if (!isSupportedCallback(callback)) {
39
39
  if (typeofValue(callback) === "function") {
@@ -41,5 +41,5 @@ export const applyCollectionCallback = (runner, callback, name, node) => {
41
41
  }
42
42
  throw new InterpreterRuntimeError(`${name} expects a function callback.`, node);
43
43
  }
44
- return (callbackArgs) => runner.invokeCallable(callback, callbackArgs, node);
44
+ return (callbackArgs) => runner.invokeCallable(callback, undefined, callbackArgs, node);
45
45
  };
@@ -1,5 +1,6 @@
1
1
  import type { Program } from "acorn";
2
2
  import { Effect } from "effect";
3
+ import type { Prototypes } from "./intrinsics.js";
3
4
  import { type Host } from "./globals.js";
4
5
  import { type Runner } from "./runner.js";
5
6
  import { PromiseRuntime } from "./promises.js";
@@ -9,11 +10,10 @@ export declare class Runtime<R> {
9
10
  readonly search: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>;
10
11
  readonly toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>;
11
12
  readonly promises: PromiseRuntime<R>;
13
+ readonly prototypes: Prototypes;
12
14
  readonly logs: Array<string>;
13
15
  readonly runner: Runner<R>;
14
- /** Built-in globals by name, unaffected by program shadowing. */
15
- readonly builtins: ReadonlyMap<string, unknown>;
16
16
  private readonly root;
17
- constructor(executeTool: (path: ReadonlyArray<string>, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, search: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>, promises: PromiseRuntime<R>, logs?: Array<string>, extraGlobals?: (host: Host<R>) => ReadonlyArray<readonly [string, unknown]>);
17
+ constructor(executeTool: (path: ReadonlyArray<string>, args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, search: (args: Array<unknown>) => Effect.Effect<unknown, unknown, R>, toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>, promises: PromiseRuntime<R>, prototypes: Prototypes, logs?: Array<string>, extraGlobals?: (host: Host<R>) => ReadonlyArray<readonly [string, unknown]>);
18
18
  run(program: Program): Effect.Effect<unknown, unknown, R>;
19
19
  }