@opencode/codemode 0.0.0-dev-19565 → 0.0.0-dev-19567

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.
@@ -1,5 +1,4 @@
1
1
  import { Effect } from "effect";
2
- import { type AstNode } from "../interpreter/model.js";
3
2
  import { type Runner } from "../interpreter/runner.js";
4
- export declare const sortArray: <R>(runner: Runner<R>, target: Array<unknown>, comparator: unknown, name: string, node: AstNode) => Effect.Effect<Array<unknown>, unknown, R>;
3
+ export declare const sortArray: <R>(runner: Runner<R>, target: Array<unknown>, comparator: unknown, name: string) => Effect.Effect<Array<unknown>, unknown, R>;
5
4
  export declare const arrayGlobal: <R>(runner: Runner<R>) => import("../interpreter/objects.js").NativeFunction<R>;
@@ -1,13 +1,13 @@
1
1
  import { Effect } from "effect";
2
2
  import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
3
- import { InterpreterRuntimeError, rangeError } from "../interpreter/model.js";
3
+ import { invalidData, rangeError, typeError } from "../interpreter/model.js";
4
4
  import { get, ProgramArray, ProgramGenerator, ProgramObject } from "../interpreter/objects.js";
5
5
  import { describeValue, rejectCircularInsertion } from "../interpreter/references.js";
6
6
  import { applyCollectionCallback, preserveConsumerError } from "../interpreter/runner.js";
7
7
  import { compareText } from "../tool-runtime.js";
8
8
  import { coerceToNumber, coerceToString } from "./value.js";
9
9
  const MAX_LENGTH = 4_294_967_295;
10
- const arrayLikeSource = (source, node) => {
10
+ const arrayLikeSource = (source) => {
11
11
  if (source instanceof ProgramObject && typeof get(source, "length") === "number") {
12
12
  const length = get(source, "length");
13
13
  const normalized = Number.isNaN(length) || length <= 0 ? 0 : Math.trunc(length);
@@ -15,19 +15,19 @@ const arrayLikeSource = (source, node) => {
15
15
  throw new RangeError("Invalid array length");
16
16
  return { length: normalized, source };
17
17
  }
18
- throw new InterpreterRuntimeError(`Array.from expects an array, string, Map, Set, or array-like value, received ${describeValue(source)}.`, node, "InvalidDataValue");
18
+ throw invalidData(`Array.from expects an array, string, Map, Set, or array-like value, received ${describeValue(source)}.`);
19
19
  };
20
- const arrayFrom = (runner, args, node) => {
20
+ const arrayFrom = (runner, args) => {
21
21
  const source = args[0];
22
22
  const proto = runner.prototypes.Array;
23
- const apply = args.length < 2 || args[1] === undefined ? undefined : applyCollectionCallback(runner, args[1], "Array.from", node);
23
+ const apply = args.length < 2 || args[1] === undefined ? undefined : applyCollectionCallback(runner, args[1], "Array.from");
24
24
  return Effect.gen(function* () {
25
- const cursor = yield* runner.syncIterator(source, node);
25
+ const cursor = yield* runner.syncIterator(source);
26
26
  if (cursor === undefined) {
27
27
  if (source instanceof ProgramGenerator) {
28
- throw new InterpreterRuntimeError("Array.from expects a synchronous iterable or array-like value.", node);
28
+ throw typeError("Array.from expects a synchronous iterable or array-like value.");
29
29
  }
30
- const arrayLike = arrayLikeSource(source, node);
30
+ const arrayLike = arrayLikeSource(source);
31
31
  const values = [];
32
32
  for (let index = 0; index < arrayLike.length; index += 1) {
33
33
  const item = get(arrayLike.source, index);
@@ -46,11 +46,11 @@ const arrayFrom = (runner, args, node) => {
46
46
  }
47
47
  });
48
48
  };
49
- export const sortArray = (runner, target, comparator, name, node) => {
49
+ export const sortArray = (runner, target, comparator, name) => {
50
50
  if (comparator === undefined) {
51
51
  return Effect.sync(() => [...target].sort((a, b) => compareText(coerceToString(a), coerceToString(b))));
52
52
  }
53
- const apply = applyCollectionCallback(runner, comparator, name, node);
53
+ const apply = applyCollectionCallback(runner, comparator, name);
54
54
  const mergeSort = (items) => {
55
55
  if (items.length <= 1)
56
56
  return Effect.succeed(items);
@@ -81,34 +81,34 @@ export const arrayGlobal = (runner) => {
81
81
  const protos = runner.prototypes;
82
82
  const proto = protos.Array;
83
83
  const wrap = (items) => new ProgramArray(proto, items);
84
- const construct = (args, into, node) => {
84
+ const construct = (args, into) => {
85
85
  if (args.length !== 1)
86
86
  return new ProgramArray(into, [...args]);
87
87
  const first = args[0];
88
88
  if (typeof first !== "number")
89
89
  return new ProgramArray(into, [first]);
90
90
  if (!Number.isInteger(first) || first < 0 || first > MAX_LENGTH)
91
- throw rangeError("Invalid array length.", node);
91
+ throw rangeError("Invalid array length.");
92
92
  // Sparse like JS: Array(3) has holes, and combinator loops already skip them.
93
93
  return new ProgramArray(into, new Array(first));
94
94
  };
95
95
  const array = constructor(protos, proto, {
96
96
  name: "Array",
97
97
  length: 1,
98
- call: (_, args, node) => Effect.sync(() => construct(args, proto, node)),
99
- construct: (args, newTarget, node) => Effect.sync(() => construct(args, prototypeFrom(newTarget, proto), node)),
98
+ call: (_, args) => Effect.sync(() => construct(args, proto)),
99
+ construct: (args, newTarget) => Effect.sync(() => construct(args, prototypeFrom(newTarget, proto))),
100
100
  });
101
101
  methods(protos, array, [
102
102
  ["isArray", 1, (_, args) => args[0] instanceof ProgramArray],
103
103
  ["of", 0, (_, args) => wrap([...args])],
104
- ["from", 1, (_, args, node) => arrayFrom(runner, args, node)],
104
+ ["from", 1, (_, args) => arrayFrom(runner, args)],
105
105
  ]);
106
- const self = (thisValue, name, node) => receiver(ProgramArray, thisValue, `Array.prototype.${name}`, node);
107
- const optNumber = (name, value, label, node) => {
106
+ const self = (thisValue, name) => receiver(ProgramArray, thisValue, `Array.prototype.${name}`);
107
+ const optNumber = (name, value, label) => {
108
108
  if (value === undefined)
109
109
  return undefined;
110
110
  if (typeof value !== "number") {
111
- throw new InterpreterRuntimeError(`Array.${name} expects ${label} to be a number.`, node);
111
+ throw typeError(`Array.${name} expects ${label} to be a number.`);
112
112
  }
113
113
  return value;
114
114
  };
@@ -116,19 +116,19 @@ export const arrayGlobal = (runner) => {
116
116
  const iterate = (name, length, body) => [
117
117
  name,
118
118
  length,
119
- (thisValue, args, node) => {
120
- const target = self(thisValue, name, node);
121
- return body(target.items, target, applyCollectionCallback(runner, args[0], `Array.${name}`, node), args, node);
119
+ (thisValue, args) => {
120
+ const target = self(thisValue, name);
121
+ return body(target.items, target, applyCollectionCallback(runner, args[0], `Array.${name}`), args);
122
122
  },
123
123
  ];
124
124
  methods(protos, proto, [
125
125
  [
126
126
  "join",
127
127
  1,
128
- (thisValue, args, node) => {
129
- const target = self(thisValue, "join", node).items;
128
+ (thisValue, args) => {
129
+ const target = self(thisValue, "join").items;
130
130
  if (args.length > 1 || (args.length === 1 && typeof args[0] !== "string")) {
131
- throw new InterpreterRuntimeError("Array.join expects zero arguments or one string separator.", node);
131
+ throw typeError("Array.join expects zero arguments or one string separator.");
132
132
  }
133
133
  return target.map((item) => coerceToString(item ?? "")).join(args.length === 0 ? "," : args[0]);
134
134
  },
@@ -136,64 +136,60 @@ export const arrayGlobal = (runner) => {
136
136
  [
137
137
  "toString",
138
138
  0,
139
- (thisValue, _, node) => self(thisValue, "toString", node)
139
+ (thisValue) => self(thisValue, "toString")
140
140
  .items.map((item) => coerceToString(item ?? ""))
141
141
  .join(","),
142
142
  ],
143
143
  [
144
144
  "includes",
145
145
  1,
146
- (thisValue, args, node) => {
147
- const target = self(thisValue, "includes", node).items;
146
+ (thisValue, args) => {
147
+ const target = self(thisValue, "includes").items;
148
148
  if (args.length === 0 || args.length > 2) {
149
- throw new InterpreterRuntimeError("Array.includes expects a value and optional start index.", node);
149
+ throw typeError("Array.includes expects a value and optional start index.");
150
150
  }
151
- return target.includes(args[0], optNumber("includes", args[1], "start index", node));
151
+ return target.includes(args[0], optNumber("includes", args[1], "start index"));
152
152
  },
153
153
  ],
154
154
  [
155
155
  "indexOf",
156
156
  1,
157
- (thisValue, args, node) => self(thisValue, "indexOf", node).items.indexOf(args[0], optNumber("indexOf", args[1], "start index", node)),
157
+ (thisValue, args) => self(thisValue, "indexOf").items.indexOf(args[0], optNumber("indexOf", args[1], "start index")),
158
158
  ],
159
159
  [
160
160
  "lastIndexOf",
161
161
  1,
162
- (thisValue, args, node) => {
163
- const target = self(thisValue, "lastIndexOf", node).items;
162
+ (thisValue, args) => {
163
+ const target = self(thisValue, "lastIndexOf").items;
164
164
  return args[1] === undefined
165
165
  ? target.lastIndexOf(args[0])
166
- : target.lastIndexOf(args[0], optNumber("lastIndexOf", args[1], "start index", node));
166
+ : target.lastIndexOf(args[0], optNumber("lastIndexOf", args[1], "start index"));
167
167
  },
168
168
  ],
169
- [
170
- "at",
171
- 1,
172
- (thisValue, args, node) => self(thisValue, "at", node).items.at(optNumber("at", args[0], "index", node) ?? 0),
173
- ],
169
+ ["at", 1, (thisValue, args) => self(thisValue, "at").items.at(optNumber("at", args[0], "index") ?? 0)],
174
170
  [
175
171
  "slice",
176
172
  2,
177
- (thisValue, args, node) => wrap(self(thisValue, "slice", node).items.slice(optNumber("slice", args[0], "start", node), optNumber("slice", args[1], "end", node))),
173
+ (thisValue, args) => wrap(self(thisValue, "slice").items.slice(optNumber("slice", args[0], "start"), optNumber("slice", args[1], "end"))),
178
174
  ],
179
175
  [
180
176
  "concat",
181
177
  1,
182
- (thisValue, args, node) => wrap(self(thisValue, "concat", node).items.concat(...args.map((item) => (item instanceof ProgramArray ? item.items : item)))),
178
+ (thisValue, args) => wrap(self(thisValue, "concat").items.concat(...args.map((item) => (item instanceof ProgramArray ? item.items : item)))),
183
179
  ],
184
180
  [
185
181
  "flat",
186
182
  0,
187
- (thisValue, args, node) => {
183
+ (thisValue, args) => {
188
184
  const flatten = (items, depth) => items.flatMap((item) => (item instanceof ProgramArray && depth > 0 ? flatten(item.items, depth - 1) : [item]));
189
- return wrap(flatten(self(thisValue, "flat", node).items, optNumber("flat", args[0], "depth", node) ?? 1));
185
+ return wrap(flatten(self(thisValue, "flat").items, optNumber("flat", args[0], "depth") ?? 1));
190
186
  },
191
187
  ],
192
188
  [
193
189
  "reverse",
194
190
  0,
195
- (thisValue, _, node) => {
196
- const target = self(thisValue, "reverse", node);
191
+ (thisValue) => {
192
+ const target = self(thisValue, "reverse");
197
193
  target.items.reverse();
198
194
  return target;
199
195
  },
@@ -201,13 +197,13 @@ export const arrayGlobal = (runner) => {
201
197
  [
202
198
  "sort",
203
199
  1,
204
- (thisValue, args, node) => {
205
- const target = self(thisValue, "sort", node);
200
+ (thisValue, args) => {
201
+ const target = self(thisValue, "sort");
206
202
  const items = target.items;
207
203
  const length = items.length;
208
204
  const holeCount = Array.from({ length }, (_, index) => Object.hasOwn(items, index)).filter((o) => !o).length;
209
205
  const itemCount = length - holeCount;
210
- return Effect.map(sortArray(runner, items, args[0], "Array.sort", node), (sorted) => {
206
+ return Effect.map(sortArray(runner, items, args[0], "Array.sort"), (sorted) => {
211
207
  sorted.slice(0, itemCount).forEach((item, index) => {
212
208
  items[index] = item;
213
209
  });
@@ -221,18 +217,18 @@ export const arrayGlobal = (runner) => {
221
217
  [
222
218
  "toSorted",
223
219
  1,
224
- (thisValue, args, node) => Effect.map(sortArray(runner, self(thisValue, "toSorted", node).items, args[0], "Array.toSorted", node), wrap),
220
+ (thisValue, args) => Effect.map(sortArray(runner, self(thisValue, "toSorted").items, args[0], "Array.toSorted"), wrap),
225
221
  ],
226
- ["toReversed", 0, (thisValue, _, node) => wrap([...self(thisValue, "toReversed", node).items].reverse())],
222
+ ["toReversed", 0, (thisValue) => wrap([...self(thisValue, "toReversed").items].reverse())],
227
223
  [
228
224
  "with",
229
225
  2,
230
- (thisValue, args, node) => {
231
- const target = self(thisValue, "with", node).items;
232
- const index = optNumber("with", args[0], "index", node) ?? 0;
226
+ (thisValue, args) => {
227
+ const target = self(thisValue, "with").items;
228
+ const index = optNumber("with", args[0], "index") ?? 0;
233
229
  const resolved = index < 0 ? target.length + index : index;
234
230
  if (resolved < 0 || resolved >= target.length)
235
- throw rangeError("Array.with index is out of range.", node);
231
+ throw rangeError("Array.with index is out of range.");
236
232
  const copied = [...target];
237
233
  copied[resolved] = args[1];
238
234
  return wrap(copied);
@@ -241,83 +237,83 @@ export const arrayGlobal = (runner) => {
241
237
  [
242
238
  "push",
243
239
  1,
244
- (thisValue, args, node) => {
245
- const target = self(thisValue, "push", node);
240
+ (thisValue, args) => {
241
+ const target = self(thisValue, "push");
246
242
  // Validate all insertions before mutating to avoid partial cyclic updates.
247
243
  for (const item of args)
248
- rejectCircularInsertion(target, item, "Array.push result", node);
244
+ rejectCircularInsertion(target, item, "Array.push result");
249
245
  return target.items.push(...args);
250
246
  },
251
247
  ],
252
248
  [
253
249
  "unshift",
254
250
  1,
255
- (thisValue, args, node) => {
256
- const target = self(thisValue, "unshift", node);
251
+ (thisValue, args) => {
252
+ const target = self(thisValue, "unshift");
257
253
  for (const item of args)
258
- rejectCircularInsertion(target, item, "Array.unshift result", node);
254
+ rejectCircularInsertion(target, item, "Array.unshift result");
259
255
  return target.items.unshift(...args);
260
256
  },
261
257
  ],
262
- ["pop", 0, (thisValue, _, node) => self(thisValue, "pop", node).items.pop()],
263
- ["shift", 0, (thisValue, _, node) => self(thisValue, "shift", node).items.shift()],
258
+ ["pop", 0, (thisValue) => self(thisValue, "pop").items.pop()],
259
+ ["shift", 0, (thisValue) => self(thisValue, "shift").items.shift()],
264
260
  [
265
261
  "splice",
266
262
  2,
267
- (thisValue, args, node) => {
268
- const target = self(thisValue, "splice", node);
263
+ (thisValue, args) => {
264
+ const target = self(thisValue, "splice");
269
265
  if (args.length === 0)
270
266
  return wrap(target.items.splice(0, 0));
271
- const start = optNumber("splice", args[0], "start", node) ?? 0;
267
+ const start = optNumber("splice", args[0], "start") ?? 0;
272
268
  if (args.length === 1)
273
269
  return wrap(target.items.splice(start));
274
- const deleteCount = optNumber("splice", args[1], "delete count", node) ?? 0;
270
+ const deleteCount = optNumber("splice", args[1], "delete count") ?? 0;
275
271
  const inserted = args.slice(2);
276
272
  for (const item of inserted)
277
- rejectCircularInsertion(target, item, "Array.splice result", node);
273
+ rejectCircularInsertion(target, item, "Array.splice result");
278
274
  return wrap(target.items.splice(start, deleteCount, ...inserted));
279
275
  },
280
276
  ],
281
277
  [
282
278
  "toSpliced",
283
279
  2,
284
- (thisValue, args, node) => {
285
- const copied = [...self(thisValue, "toSpliced", node).items];
280
+ (thisValue, args) => {
281
+ const copied = [...self(thisValue, "toSpliced").items];
286
282
  if (args.length === 0)
287
283
  return wrap(copied);
288
- const start = optNumber("toSpliced", args[0], "start", node) ?? 0;
284
+ const start = optNumber("toSpliced", args[0], "start") ?? 0;
289
285
  if (args.length === 1)
290
286
  copied.splice(start);
291
287
  else
292
- copied.splice(start, optNumber("toSpliced", args[1], "delete count", node) ?? 0, ...args.slice(2));
288
+ copied.splice(start, optNumber("toSpliced", args[1], "delete count") ?? 0, ...args.slice(2));
293
289
  return wrap(copied);
294
290
  },
295
291
  ],
296
292
  [
297
293
  "fill",
298
294
  1,
299
- (thisValue, args, node) => {
300
- const target = self(thisValue, "fill", node);
301
- rejectCircularInsertion(target, args[0], "Array.fill result", node);
302
- target.items.fill(args[0], optNumber("fill", args[1], "start", node), optNumber("fill", args[2], "end", node));
295
+ (thisValue, args) => {
296
+ const target = self(thisValue, "fill");
297
+ rejectCircularInsertion(target, args[0], "Array.fill result");
298
+ target.items.fill(args[0], optNumber("fill", args[1], "start"), optNumber("fill", args[2], "end"));
303
299
  return target;
304
300
  },
305
301
  ],
306
302
  [
307
303
  "copyWithin",
308
304
  2,
309
- (thisValue, args, node) => {
310
- const target = self(thisValue, "copyWithin", node);
311
- target.items.copyWithin(optNumber("copyWithin", args[0], "target index", node) ?? 0, optNumber("copyWithin", args[1], "start", node) ?? 0, optNumber("copyWithin", args[2], "end", node));
305
+ (thisValue, args) => {
306
+ const target = self(thisValue, "copyWithin");
307
+ target.items.copyWithin(optNumber("copyWithin", args[0], "target index") ?? 0, optNumber("copyWithin", args[1], "start") ?? 0, optNumber("copyWithin", args[2], "end"));
312
308
  return target;
313
309
  },
314
310
  ],
315
- ["keys", 0, (thisValue, _, node) => wrap(Array.from(self(thisValue, "keys", node).items.keys()))],
316
- ["values", 0, (thisValue, _, node) => wrap([...self(thisValue, "values", node).items])],
311
+ ["keys", 0, (thisValue) => wrap(Array.from(self(thisValue, "keys").items.keys()))],
312
+ ["values", 0, (thisValue) => wrap([...self(thisValue, "values").items])],
317
313
  [
318
314
  "entries",
319
315
  0,
320
- (thisValue, _, node) => wrap(Array.from(self(thisValue, "entries", node).items.entries(), ([index, item]) => wrap([index, item]))),
316
+ (thisValue) => wrap(Array.from(self(thisValue, "entries").items.entries(), ([index, item]) => wrap([index, item]))),
321
317
  ],
322
318
  iterate("map", 1, (target, receiver, apply) => Effect.gen(function* () {
323
319
  const length = target.length;
@@ -416,7 +412,7 @@ export const arrayGlobal = (runner) => {
416
412
  }
417
413
  return undefined;
418
414
  })),
419
- iterate("reduce", 1, (target, receiver, apply, args, node) => Effect.gen(function* () {
415
+ iterate("reduce", 1, (target, receiver, apply, args) => Effect.gen(function* () {
420
416
  const length = target.length;
421
417
  let start = 0;
422
418
  let accumulator = args[1];
@@ -424,7 +420,7 @@ export const arrayGlobal = (runner) => {
424
420
  while (start < length && !(start in target))
425
421
  start += 1;
426
422
  if (start === length) {
427
- throw new InterpreterRuntimeError("Array.reduce of an empty array with no initial value.", node);
423
+ throw typeError("Array.reduce of an empty array with no initial value.");
428
424
  }
429
425
  accumulator = target[start];
430
426
  start += 1;
@@ -436,14 +432,14 @@ export const arrayGlobal = (runner) => {
436
432
  }
437
433
  return accumulator;
438
434
  })),
439
- iterate("reduceRight", 1, (target, receiver, apply, args, node) => Effect.gen(function* () {
435
+ iterate("reduceRight", 1, (target, receiver, apply, args) => Effect.gen(function* () {
440
436
  let start = target.length - 1;
441
437
  let accumulator = args[1];
442
438
  if (args.length < 2) {
443
439
  while (start >= 0 && !(start in target))
444
440
  start -= 1;
445
441
  if (start < 0) {
446
- throw new InterpreterRuntimeError("Array.reduceRight of an empty array with no initial value.", node);
442
+ throw typeError("Array.reduceRight of an empty array with no initial value.");
447
443
  }
448
444
  accumulator = target[start];
449
445
  start -= 1;