@opencode/codemode 0.0.0-dev-19907 → 0.0.0-dev-19909

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.
@@ -14,6 +14,8 @@ export declare const preserveConsumerError: <A, R>(cursor: IteratorCursor<R>, ef
14
14
  * default hint as "string", like their `Symbol.toPrimitive`.
15
15
  */
16
16
  export declare const toPrimitive: <R>(ctx: Interpreter<R>, value: unknown, hint: "number" | "string" | "default") => Effect.Effect<unknown, unknown, R>;
17
+ /** Invoke(value, name): calls the method the value would find through its prototype. */
18
+ export declare const invoke: <R>(ctx: Interpreter<R>, value: unknown, name: string, label: string) => Effect.Effect<unknown, unknown, R>;
17
19
  export declare const toPrimitiveString: <R>(ctx: Interpreter<R>, value: unknown) => Effect.Effect<string, unknown, R>;
18
20
  export declare const toPrimitiveNumber: <R>(ctx: Interpreter<R>, value: unknown) => Effect.Effect<number, unknown, R>;
19
21
  export declare const isSupportedCallback: (value: unknown) => value is Callable;
@@ -1,5 +1,6 @@
1
1
  import { Effect, Exit } from "effect";
2
2
  import { coerceToNumber, coerceToString } from "../stdlib/value.js";
3
+ import { primitivePrototype } from "./intrinsics.js";
3
4
  import { typeError } from "./model.js";
4
5
  import { Callable, get, Native, DateObj, Obj } from "./objects.js";
5
6
  import { typeofValue } from "./references.js";
@@ -27,6 +28,13 @@ export const toPrimitive = (ctx, value, hint) => {
27
28
  throw typeError("Cannot convert object to primitive value.");
28
29
  });
29
30
  };
31
+ /** Invoke(value, name): calls the method the value would find through its prototype. */
32
+ export const invoke = (ctx, value, name, label) => {
33
+ const target = value instanceof Obj ? value : primitivePrototype(ctx.builtins, value);
34
+ if (target === undefined)
35
+ throw typeError(`${label} called on null or undefined.`);
36
+ return ctx.call(get(target, name), value, []);
37
+ };
30
38
  export const toPrimitiveString = (ctx, value) => Effect.map(toPrimitive(ctx, value, "string"), coerceToString);
31
39
  export const toPrimitiveNumber = (ctx, value) => Effect.map(toPrimitive(ctx, value, "number"), coerceToNumber);
32
40
  // The single acceptance list for callbacks: collections, sort, string replacers,
@@ -150,6 +150,7 @@ export const errorGlobal = (type, ctx) => {
150
150
  methods(builtins, prototype, [
151
151
  ["toString", 0, (thisValue) => errorToString(receiver(Obj, thisValue, "Error.prototype.toString"))],
152
152
  ]);
153
+ methods(builtins, ctor, [["isError", 1, (_, args) => args[0] instanceof ErrorObj]]);
153
154
  }
154
155
  return ctor;
155
156
  };
@@ -2,7 +2,7 @@ import type { Program } from "acorn";
2
2
  import { Effect } from "effect";
3
3
  import { type Json } from "../data.js";
4
4
  import { type ToolRuntime } from "../tool-runtime.js";
5
- import type { Builtins } from "./intrinsics.js";
5
+ import { type Builtins } from "./intrinsics.js";
6
6
  import { PromiseObj } from "./objects.js";
7
7
  import { Pending } from "./promises.js";
8
8
  /** One program execution: the tool bridge, promise scheduler, captured logs, and the global scope built once. */
@@ -4,6 +4,7 @@ import { ToolReference } from "../tool-runtime.js";
4
4
  import { AsyncIteratorSymbol, CallSite, GeneratorReturn, IteratorSymbol, OptionalShortCircuit, invalidData, Throw, rangeError, typeError, unsupportedSyntax, } from "./model.js";
5
5
  import { checkStringLength } from "./limits.js";
6
6
  import { locate, materialize } from "./errors.js";
7
+ import { primitivePrototype } from "./intrinsics.js";
7
8
  import { globals } from "./globals.js";
8
9
  import { assign, Callable, define, get, has, hasPrototype, keys, Native, parseArrayIndex, Arr, Bytes, DateObj, Fn, GeneratorObj, IteratorObj, MapObj, Obj, PromiseObj, SetObj, URLSearchParamsObj, HeadersObj, record, remove, set, } from "./objects.js";
9
10
  import { preserveConsumerError } from "./callback.js";
@@ -1712,20 +1713,17 @@ class Frame {
1712
1713
  }
1713
1714
  if (objectValue instanceof Obj)
1714
1715
  return { target: objectValue, key, receiver: objectValue };
1715
- // Primitives read through their wrapper prototype without being boxed; strings own length and indexes.
1716
- const builtins = self.ctx.builtins;
1716
+ // Strings own length and indexes; every other primitive property reads through the wrapper prototype.
1717
1717
  if (typeof objectValue === "string") {
1718
1718
  if (key === "length")
1719
1719
  return { value: objectValue.length };
1720
1720
  const index = typeof key === "symbol" ? undefined : parseArrayIndex(key);
1721
1721
  if (index !== undefined)
1722
1722
  return { value: objectValue[index] };
1723
- return { target: builtins.String, key, receiver: objectValue };
1724
1723
  }
1725
- if (typeof objectValue === "number")
1726
- return { target: builtins.Number, key, receiver: objectValue };
1727
- if (typeof objectValue === "boolean")
1728
- return { target: builtins.Boolean, key, receiver: objectValue };
1724
+ const proto = primitivePrototype(self.ctx.builtins, objectValue);
1725
+ if (proto !== undefined)
1726
+ return { target: proto, key, receiver: objectValue };
1729
1727
  if (objectValue === null || objectValue === undefined) {
1730
1728
  throw typeError(`Cannot read properties of ${objectValue} (reading '${String(key)}').`, objectNode);
1731
1729
  }
@@ -9,5 +9,7 @@ declare const builtins: readonly ["Object", "Function", "Array", "String", "Numb
9
9
  */
10
10
  export type Builtins = Readonly<Record<(typeof builtins)[number] | ErrorType, Obj>>;
11
11
  export declare const createErrorValue: (prototype: Obj, message: string | undefined) => ErrorObj;
12
+ /** The prototype a primitive reads its methods from without being boxed; none for null, undefined, and symbols. */
13
+ export declare const primitivePrototype: (builtins: Builtins, value: unknown) => Obj | undefined;
12
14
  export declare const createBuiltins: () => Builtins;
13
15
  export {};
@@ -40,6 +40,16 @@ export const createErrorValue = (prototype, message) => {
40
40
  define(value, "message", message, hidden);
41
41
  return value;
42
42
  };
43
+ /** The prototype a primitive reads its methods from without being boxed; none for null, undefined, and symbols. */
44
+ export const primitivePrototype = (builtins, value) => {
45
+ if (typeof value === "string")
46
+ return builtins.String;
47
+ if (typeof value === "number")
48
+ return builtins.Number;
49
+ if (typeof value === "boolean")
50
+ return builtins.Boolean;
51
+ return undefined;
52
+ };
43
53
  export const createBuiltins = () => {
44
54
  const object = new Obj(null);
45
55
  // Function.prototype is itself callable and returns undefined.
@@ -4,7 +4,7 @@ import { checkArrayLength, checkStringLength, MAX_ARRAY_LENGTH } from "../interp
4
4
  import { invalidData, IteratorSymbol, rangeError, typeError } from "../interpreter/model.js";
5
5
  import { define, get, hidden, Arr, GeneratorObj, IteratorObj, Obj } from "../interpreter/objects.js";
6
6
  import { describeValue, rejectCircularInsertion } from "../interpreter/references.js";
7
- import { applyCollectionCallback, preserveConsumerError } from "../interpreter/callback.js";
7
+ import { applyCollectionCallback, invoke, preserveConsumerError } from "../interpreter/callback.js";
8
8
  import { compareText } from "../tool-runtime.js";
9
9
  import { coerceToNumber, coerceToString } from "./value.js";
10
10
  const arrayLikeSource = (source) => {
@@ -317,6 +317,13 @@ export const arrayGlobal = (ctx) => {
317
317
  return target;
318
318
  },
319
319
  ],
320
+ [
321
+ "toLocaleString",
322
+ 0,
323
+ (thisValue) => Effect.map(Effect.forEach(self(thisValue, "toLocaleString").items, (item) => item === null || item === undefined
324
+ ? Effect.succeed("")
325
+ : Effect.map(invoke(ctx, item, "toLocaleString", "Array.prototype.toLocaleString"), coerceToString)), (parts) => parts.join(",")),
326
+ ],
320
327
  ["keys", 0, (thisValue) => new IteratorObj(builtins.Iterator, self(thisValue, "keys").items.keys())],
321
328
  ["values", 0, (thisValue) => new IteratorObj(builtins.Iterator, self(thisValue, "values").items.values())],
322
329
  [
@@ -123,6 +123,31 @@ export const mapGlobal = (ctx) => {
123
123
  return target;
124
124
  },
125
125
  ],
126
+ [
127
+ "getOrInsert",
128
+ 2,
129
+ (thisValue, args) => {
130
+ const target = self(thisValue, "getOrInsert").map;
131
+ if (!target.has(args[0]))
132
+ target.set(args[0], args[1]);
133
+ return target.get(args[0]);
134
+ },
135
+ ],
136
+ [
137
+ "getOrInsertComputed",
138
+ 2,
139
+ (thisValue, args) => {
140
+ const target = self(thisValue, "getOrInsertComputed").map;
141
+ const apply = applyCollectionCallback(ctx, args[1], "Map.getOrInsertComputed");
142
+ if (target.has(args[0]))
143
+ return target.get(args[0]);
144
+ // The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself.
145
+ return Effect.map(apply([args[0] === 0 ? 0 : args[0]]), (value) => {
146
+ target.set(args[0], value);
147
+ return value;
148
+ });
149
+ },
150
+ ],
126
151
  ["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])],
127
152
  [
128
153
  "clear",
@@ -92,6 +92,11 @@ export const dateGlobal = (ctx) => {
92
92
  ["toTimeString", 0, (thisValue) => new Date(self(thisValue, "toTimeString").time).toTimeString()],
93
93
  ["toUTCString", 0, (thisValue) => new Date(self(thisValue, "toUTCString").time).toUTCString()],
94
94
  ["toGMTString", 0, (thisValue) => new Date(self(thisValue, "toGMTString").time).toUTCString()],
95
+ ...["toLocaleString", "toLocaleDateString", "toLocaleTimeString"].map((name) => [
96
+ name,
97
+ 0,
98
+ (thisValue) => new Date(self(thisValue, name).time)[name]("en-US", { timeZone: "UTC" }),
99
+ ]),
95
100
  ...getters.map((name) => [name, 0, (thisValue) => new Date(self(thisValue, name).time)[name]()]),
96
101
  ...setters.map(([name, length]) => [
97
102
  name,
@@ -50,6 +50,7 @@ export const numberGlobal = (ctx) => {
50
50
  };
51
51
  methods(builtins, builtins.Number, [
52
52
  ["toFixed", 1, (thisValue, args) => self(thisValue, "toFixed").toFixed(optNum("toFixed", args[0]))],
53
+ ["toLocaleString", 0, (thisValue) => self(thisValue, "toLocaleString").toLocaleString("en-US")],
53
54
  [
54
55
  "toExponential",
55
56
  1,
@@ -3,7 +3,7 @@ import { constructor, methods, receiver } from "../interpreter/native.js";
3
3
  import { AsyncIteratorSymbol, invalidData, IteratorSymbol, rangeError, typeError, } from "../interpreter/model.js";
4
4
  import { Callable, define, entries, enumerableKeys, getOwn, hasOwn, hasPrototype, hidden, keys, own, Arr, Bytes, DateObj, ErrorObj, Obj, PromiseObj, RegExpObj, set, } from "../interpreter/objects.js";
5
5
  import { containsOpaqueReference, describeValue, rejectCircularInsertion } from "../interpreter/references.js";
6
- import { preserveConsumerError } from "../interpreter/callback.js";
6
+ import { invoke, preserveConsumerError } from "../interpreter/callback.js";
7
7
  import { ToolReference } from "../tool-runtime.js";
8
8
  import { groupBy } from "./collections.js";
9
9
  import { coerceToString } from "./value.js";
@@ -161,7 +161,7 @@ export const objectGlobal = (ctx) => {
161
161
  true,
162
162
  ],
163
163
  ["toString", 0, (thisValue) => `[object ${classTag(thisValue)}]`],
164
- ["toLocaleString", 0, (thisValue) => `[object ${classTag(thisValue)}]`],
164
+ ["toLocaleString", 0, (thisValue) => invoke(ctx, thisValue, "toString", "Object.prototype.toLocaleString")],
165
165
  [
166
166
  "valueOf",
167
167
  0,
@@ -130,6 +130,8 @@ export const stringGlobal = (ctx) => {
130
130
  simple("valueOf", 0, (value) => value),
131
131
  simple("toLowerCase", 0, (value) => value.toLowerCase()),
132
132
  simple("toUpperCase", 0, (value) => value.toUpperCase()),
133
+ simple("toLocaleLowerCase", 0, (value) => value.toLowerCase()),
134
+ simple("toLocaleUpperCase", 0, (value) => value.toUpperCase()),
133
135
  simple("trim", 0, (value) => value.trim()),
134
136
  simple("trimStart", 0, (value) => value.trimStart()),
135
137
  simple("trimLeft", 0, (value) => value.trimStart()),
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-19907",
4
+ "version": "0.0.0-dev-19909",
5
5
  "description": "Effect-native confined code execution over schema-described tools",
6
6
  "type": "module",
7
7
  "license": "MIT",