@opencode/codemode 0.0.0-dev-20043 → 0.0.0-dev-20048

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,11 @@
1
1
  import { Effect } from "effect";
2
- import { type Value } from "../interpreter/objects.js";
2
+ import { type Method } from "../interpreter/native.js";
3
+ import { Obj, type Value } from "../interpreter/objects.js";
3
4
  import type { Interpreter } from "../interpreter/interpreter.js";
4
5
  export declare const sortArray: <R>(ctx: Interpreter<R>, target: Array<Value>, comparator: Value, name: string) => Effect.Effect<Array<Value>, unknown, R>;
5
6
  export declare const arrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
7
+ /**
8
+ * The callback methods Array and Uint8Array share. They fix the iteration length while reading existing elements
9
+ * live; `wrap` builds the collection `map` and `filter` return.
10
+ */
11
+ export declare const callbackMethods: <R, T extends Obj>(ctx: Interpreter<R>, label: string, self: (thisValue: Value, name: string) => T, elements: (target: T) => ArrayLike<Value>, wrap: (values: Array<Value>) => Value) => Array<Method>;
@@ -103,15 +103,6 @@ export const arrayGlobal = (ctx) => {
103
103
  ]);
104
104
  const self = (thisValue, name) => receiver(Arr, thisValue, `Array.prototype.${name}`);
105
105
  const optNumber = (value) => (value === undefined ? undefined : coerceToInteger(value));
106
- // Callback methods fix the iteration length while reading existing elements live.
107
- const iterate = (name, length, body) => [
108
- name,
109
- length,
110
- (thisValue, args) => {
111
- const target = self(thisValue, name);
112
- return body(target.items, target, applyCollectionCallback(ctx, args[0], `Array.${name}`), args);
113
- },
114
- ];
115
106
  methods(builtins, proto, [
116
107
  [
117
108
  "join",
@@ -144,7 +135,8 @@ export const arrayGlobal = (ctx) => {
144
135
  1,
145
136
  (thisValue, args) => {
146
137
  const target = self(thisValue, "lastIndexOf").items;
147
- return args[1] === undefined ? target.lastIndexOf(args[0]) : target.lastIndexOf(args[0], optNumber(args[1]));
138
+ // An explicit undefined is a fromIndex of 0, unlike omitting it.
139
+ return args.length < 2 ? target.lastIndexOf(args[0]) : target.lastIndexOf(args[0], optNumber(args[1]));
148
140
  },
149
141
  ],
150
142
  ["at", 1, (thisValue, args) => self(thisValue, "at").items.at(optNumber(args[0]) ?? 0)],
@@ -311,6 +303,47 @@ export const arrayGlobal = (ctx) => {
311
303
  .items.entries()
312
304
  .map(([index, item]) => wrap([index, item]))),
313
305
  ],
306
+ [
307
+ "flatMap",
308
+ 1,
309
+ (thisValue, args) => {
310
+ const target = self(thisValue, "flatMap");
311
+ const apply = applyCollectionCallback(ctx, args[0], "Array.flatMap");
312
+ return Effect.gen(function* () {
313
+ const length = target.items.length;
314
+ const values = [];
315
+ for (let index = 0; index < length; index += 1) {
316
+ if (!(index in target.items))
317
+ continue;
318
+ const mapped = yield* apply([target.items[index], index, target]);
319
+ if (mapped instanceof Arr)
320
+ values.push(...mapped.items);
321
+ else
322
+ values.push(mapped);
323
+ }
324
+ return wrap(values);
325
+ });
326
+ },
327
+ ],
328
+ ...callbackMethods(ctx, "Array", self, (target) => target.items, wrap),
329
+ ]);
330
+ define(proto, IteratorSymbol, get(proto, "values"), hidden);
331
+ return array;
332
+ };
333
+ /**
334
+ * The callback methods Array and Uint8Array share. They fix the iteration length while reading existing elements
335
+ * live; `wrap` builds the collection `map` and `filter` return.
336
+ */
337
+ export const callbackMethods = (ctx, label, self, elements, wrap) => {
338
+ const iterate = (name, length, body) => [
339
+ name,
340
+ length,
341
+ (thisValue, args) => {
342
+ const target = self(thisValue, name);
343
+ return body(elements(target), target, applyCollectionCallback(ctx, args[0], `${label}.${name}`), args);
344
+ },
345
+ ];
346
+ return [
314
347
  iterate("map", 1, (target, receiver, apply) => Effect.gen(function* () {
315
348
  const length = target.length;
316
349
  const values = [];
@@ -322,20 +355,6 @@ export const arrayGlobal = (ctx) => {
322
355
  }
323
356
  return wrap(values);
324
357
  })),
325
- iterate("flatMap", 1, (target, receiver, apply) => Effect.gen(function* () {
326
- const length = target.length;
327
- const values = [];
328
- for (let index = 0; index < length; index += 1) {
329
- if (!(index in target))
330
- continue;
331
- const mapped = yield* apply([target[index], index, receiver]);
332
- if (mapped instanceof Arr)
333
- values.push(...mapped.items);
334
- else
335
- values.push(mapped);
336
- }
337
- return wrap(values);
338
- })),
339
358
  iterate("filter", 1, (target, receiver, apply) => Effect.gen(function* () {
340
359
  const length = target.length;
341
360
  const values = [];
@@ -416,7 +435,7 @@ export const arrayGlobal = (ctx) => {
416
435
  while (start < length && !(start in target))
417
436
  start += 1;
418
437
  if (start === length) {
419
- throw typeError("Array.reduce of an empty array with no initial value.");
438
+ throw typeError(`${label}.reduce of an empty array with no initial value.`);
420
439
  }
421
440
  accumulator = target[start];
422
441
  start += 1;
@@ -435,7 +454,7 @@ export const arrayGlobal = (ctx) => {
435
454
  while (start >= 0 && !(start in target))
436
455
  start -= 1;
437
456
  if (start < 0) {
438
- throw typeError("Array.reduceRight of an empty array with no initial value.");
457
+ throw typeError(`${label}.reduceRight of an empty array with no initial value.`);
439
458
  }
440
459
  accumulator = target[start];
441
460
  start -= 1;
@@ -447,7 +466,5 @@ export const arrayGlobal = (ctx) => {
447
466
  }
448
467
  return accumulator;
449
468
  })),
450
- ]);
451
- define(proto, IteratorSymbol, get(proto, "values"), hidden);
452
- return array;
469
+ ];
453
470
  };
@@ -4,6 +4,7 @@ import { constructor, methods, prototypeFrom, receiver, requiresNew } from "../i
4
4
  import { IteratorSymbol, rangeError, syntaxError, typeError } from "../interpreter/model.js";
5
5
  import { define, defineAccessor, get, hidden, Arr, Bytes, hostIterator, Obj, coerceToInteger, coerceToNumber, coerceToString, } from "../interpreter/objects.js";
6
6
  import { describeValue } from "../interpreter/references.js";
7
+ import { callbackMethods, sortArray } from "./array.js";
7
8
  /** The bytes a Uint8Array, array, or other iterable of numbers describes; the host array clamps each value. */
8
9
  const collectBytes = (ctx, source, name) => {
9
10
  if (source instanceof Bytes)
@@ -110,6 +111,22 @@ export const uint8ArrayGlobal = (ctx) => {
110
111
  return target;
111
112
  },
112
113
  ],
114
+ [
115
+ "sort",
116
+ 1,
117
+ (thisValue, args) => {
118
+ const target = self(thisValue, "sort");
119
+ // Without a comparator, typed arrays sort numerically rather than by string.
120
+ if (args[0] === undefined) {
121
+ target.bytes.sort();
122
+ return target;
123
+ }
124
+ return Effect.map(sortArray(ctx, [...target.bytes], args[0], "Uint8Array.sort"), (sorted) => {
125
+ target.bytes.set(Uint8Array.from(sorted, coerceToNumber));
126
+ return target;
127
+ });
128
+ },
129
+ ],
113
130
  [
114
131
  "indexOf",
115
132
  1,
@@ -120,7 +137,7 @@ export const uint8ArrayGlobal = (ctx) => {
120
137
  1,
121
138
  (thisValue, args) => {
122
139
  const target = self(thisValue, "lastIndexOf").bytes;
123
- return args[1] === undefined
140
+ return args.length < 2
124
141
  ? target.lastIndexOf(coerceToNumber(args[0]))
125
142
  : target.lastIndexOf(coerceToNumber(args[0]), optNumber(args[1]));
126
143
  },
@@ -151,6 +168,7 @@ export const uint8ArrayGlobal = (ctx) => {
151
168
  .bytes.entries()
152
169
  .map(([index, byte]) => wrapAll([index, byte]))),
153
170
  ],
171
+ ...callbackMethods(ctx, "Uint8Array", self, (target) => target.bytes, (values) => wrap(Uint8Array.from(values, coerceToNumber))),
154
172
  ]);
155
173
  define(proto, IteratorSymbol, get(proto, "values"), hidden);
156
174
  return uint8Array;
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-20043",
4
+ "version": "0.0.0-dev-20048",
5
5
  "description": "Effect-native confined code execution over schema-described tools",
6
6
  "type": "module",
7
7
  "license": "MIT",