@opencode/codemode 2.2.0 → 2.2.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 (56) hide show
  1. package/README.md +33 -4
  2. package/dist/codemode.d.ts +9 -7
  3. package/dist/codemode.js +1 -1
  4. package/dist/data.d.ts +5 -4
  5. package/dist/data.js +7 -4
  6. package/dist/extension.d.ts +17 -4
  7. package/dist/extension.js +8 -0
  8. package/dist/interpreter/callback.d.ts +10 -10
  9. package/dist/interpreter/callback.js +43 -31
  10. package/dist/interpreter/errors.d.ts +1 -1
  11. package/dist/interpreter/errors.js +12 -15
  12. package/dist/interpreter/execute.js +59 -29
  13. package/dist/interpreter/extensions.d.ts +2 -2
  14. package/dist/interpreter/extensions.js +46 -18
  15. package/dist/interpreter/generators.js +162 -19
  16. package/dist/interpreter/globals.js +12 -9
  17. package/dist/interpreter/interpreter.d.ts +20 -13
  18. package/dist/interpreter/interpreter.js +1343 -1149
  19. package/dist/interpreter/intrinsics.js +1 -2
  20. package/dist/interpreter/model.d.ts +4 -7
  21. package/dist/interpreter/model.js +1 -11
  22. package/dist/interpreter/native.d.ts +7 -8
  23. package/dist/interpreter/native.js +4 -10
  24. package/dist/interpreter/objects.d.ts +47 -21
  25. package/dist/interpreter/objects.js +33 -12
  26. package/dist/interpreter/promises.d.ts +15 -53
  27. package/dist/interpreter/promises.js +226 -401
  28. package/dist/interpreter/runner.d.ts +188 -0
  29. package/dist/interpreter/runner.js +492 -0
  30. package/dist/stdlib/array.d.ts +3 -3
  31. package/dist/stdlib/array.js +106 -102
  32. package/dist/stdlib/bytes.d.ts +3 -3
  33. package/dist/stdlib/bytes.js +46 -41
  34. package/dist/stdlib/collections.d.ts +5 -5
  35. package/dist/stdlib/collections.js +160 -165
  36. package/dist/stdlib/date.d.ts +1 -1
  37. package/dist/stdlib/date.js +28 -20
  38. package/dist/stdlib/headers.d.ts +1 -1
  39. package/dist/stdlib/headers.js +14 -19
  40. package/dist/stdlib/iterator.d.ts +1 -1
  41. package/dist/stdlib/iterator.js +140 -122
  42. package/dist/stdlib/json.js +9 -12
  43. package/dist/stdlib/math.js +10 -10
  44. package/dist/stdlib/number.d.ts +2 -2
  45. package/dist/stdlib/object.d.ts +1 -1
  46. package/dist/stdlib/object.js +21 -22
  47. package/dist/stdlib/regexp.d.ts +1 -1
  48. package/dist/stdlib/regexp.js +2 -3
  49. package/dist/stdlib/string.d.ts +1 -1
  50. package/dist/stdlib/string.js +18 -20
  51. package/dist/stdlib/url.d.ts +5 -5
  52. package/dist/stdlib/url.js +32 -33
  53. package/dist/stdlib/value.d.ts +2 -2
  54. package/dist/stdlib/web.d.ts +2 -2
  55. package/dist/tool-schema.js +7 -3
  56. package/package.json +2 -2
@@ -1,104 +1,99 @@
1
- import { Effect } from "effect";
2
1
  import { constructor, fn, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
3
2
  import { invalidData, IteratorSymbol, typeError } from "../interpreter/model.js";
4
3
  import { define, defineAccessor, get, getOwn, hidden, Arr, coerceToString, hostCursor, hostIterator, MapObj, Obj, PromiseObj, SetObj, WeakMapObj, WeakSetObj, } from "../interpreter/objects.js";
5
4
  import { describeValue, isOpaque } from "../interpreter/references.js";
6
5
  import { applyCollectionCallback, isSupportedCallback, preserveConsumerError, toPrimitiveNumber, toPrimitiveString, } from "../interpreter/callback.js";
7
- const coerceGroupByPropertyKey = (ctx, value) => {
6
+ function* coerceGroupByPropertyKey(ctx, result) {
7
+ const value = yield* result;
8
8
  if (value instanceof PromiseObj)
9
- return Effect.succeed(coerceToString(value));
9
+ return coerceToString(value);
10
10
  if (isOpaque(value)) {
11
11
  throw invalidData(`Object.groupBy callback must return a data value, received ${describeValue(value)}.`);
12
12
  }
13
- return toPrimitiveString(ctx, value);
14
- };
13
+ return yield* toPrimitiveString(ctx, value);
14
+ }
15
15
  /** `Map.groupBy` and `Object.groupBy`: the same iteration, keyed into a Map or a data object. */
16
- export const groupBy = (ctx, namespace) => fn(ctx.builtins, "groupBy", 2, (_, args) => {
16
+ export const groupBy = (ctx, namespace) => fn(ctx.builtins, "groupBy", 2, function* (_, args) {
17
17
  const builtins = ctx.builtins;
18
18
  const source = args[0];
19
19
  if (source === null || source === undefined) {
20
20
  throw typeError(`${namespace}.groupBy expects an iterable collection.`);
21
21
  }
22
22
  const apply = applyCollectionCallback(ctx, args[1], `${namespace}.groupBy`);
23
- return Effect.gen(function* () {
24
- const cursor = yield* ctx.iterate(source);
25
- if (cursor === undefined) {
26
- throw typeError(`${namespace}.groupBy expects an iterable collection.`);
27
- }
28
- if (namespace === "Map") {
29
- const result = new MapObj(builtins.Map);
30
- let index = 0;
31
- while (true) {
32
- const step = yield* cursor.next;
33
- if (step.done)
34
- return result;
35
- const item = step.value;
36
- const key = yield* preserveConsumerError(cursor.close, apply([item, index]));
37
- const group = result.map.get(key);
38
- if (group === undefined)
39
- result.map.set(key, new Arr(builtins.Array, [item]));
40
- else
41
- group.items.push(item);
42
- index += 1;
43
- }
44
- }
45
- // Object.groupBy returns a null-prototype object, so group names never collide with inherited methods.
46
- const result = new Obj(null);
23
+ const cursor = yield* ctx.iterate(source);
24
+ if (cursor === undefined) {
25
+ throw typeError(`${namespace}.groupBy expects an iterable collection.`);
26
+ }
27
+ if (namespace === "Map") {
28
+ const result = new MapObj(builtins.Map);
47
29
  let index = 0;
48
30
  while (true) {
49
- const step = yield* cursor.next;
31
+ const step = yield* cursor.next();
50
32
  if (step.done)
51
33
  return result;
52
34
  const item = step.value;
53
- const key = yield* preserveConsumerError(cursor.close, Effect.flatMap(apply([item, index]), (value) => coerceGroupByPropertyKey(ctx, value)));
54
- const group = getOwn(result, key);
35
+ const key = yield* preserveConsumerError(cursor.close, apply([item, index]));
36
+ const group = result.map.get(key);
55
37
  if (group === undefined)
56
- define(result, key, new Arr(builtins.Array, [item]));
38
+ result.map.set(key, new Arr(builtins.Array, [item]));
57
39
  else
58
40
  group.items.push(item);
59
41
  index += 1;
60
42
  }
61
- });
43
+ }
44
+ // Object.groupBy returns a null-prototype object, so group names never collide with inherited methods.
45
+ const result = new Obj(null);
46
+ let index = 0;
47
+ while (true) {
48
+ const step = yield* cursor.next();
49
+ if (step.done)
50
+ return result;
51
+ const item = step.value;
52
+ const key = yield* preserveConsumerError(cursor.close, coerceGroupByPropertyKey(ctx, apply([item, index])));
53
+ const group = getOwn(result, key);
54
+ if (group === undefined)
55
+ define(result, key, new Arr(builtins.Array, [item]));
56
+ else
57
+ group.items.push(item);
58
+ index += 1;
59
+ }
62
60
  });
63
- const constructMap = (ctx, init, proto) => {
61
+ function* constructMap(ctx, init, proto) {
64
62
  const target = new MapObj(proto);
65
63
  if (init === undefined || init === null)
66
- return Effect.succeed(target);
67
- return Effect.gen(function* () {
68
- const cursor = yield* ctx.iterate(init);
69
- if (cursor === undefined) {
70
- throw typeError(`new Map(...) expects an iterable of [key, value] pairs, received ${describeValue(init)}.`);
71
- }
72
- while (true) {
73
- const step = yield* cursor.next;
74
- if (step.done)
75
- return target;
76
- yield* preserveConsumerError(cursor.close, Effect.sync(() => {
77
- if (!(step.value instanceof Obj)) {
78
- throw typeError("new Map(...) expects [key, value] pairs as entry objects.");
79
- }
80
- target.map.set(getOwn(step.value, 0), getOwn(step.value, 1));
81
- }));
82
- }
83
- });
84
- };
85
- const constructSet = (ctx, init, proto) => {
64
+ return target;
65
+ const cursor = yield* ctx.iterate(init);
66
+ if (cursor === undefined) {
67
+ throw typeError(`new Map(...) expects an iterable of [key, value] pairs, received ${describeValue(init)}.`);
68
+ }
69
+ while (true) {
70
+ const step = yield* cursor.next();
71
+ if (step.done)
72
+ return target;
73
+ yield* preserveConsumerError(cursor.close, addMapEntry(target, step.value));
74
+ }
75
+ }
76
+ function* addMapEntry(target, entry) {
77
+ if (!(entry instanceof Obj)) {
78
+ throw typeError("new Map(...) expects [key, value] pairs as entry objects.");
79
+ }
80
+ target.map.set(getOwn(entry, 0), getOwn(entry, 1));
81
+ }
82
+ function* constructSet(ctx, init, proto) {
86
83
  const target = new SetObj(proto);
87
84
  if (init === undefined || init === null)
88
- return Effect.succeed(target);
89
- return Effect.gen(function* () {
90
- const cursor = yield* ctx.iterate(init);
91
- if (cursor === undefined) {
92
- throw typeError(`new Set(...) expects a synchronous iterable, received ${describeValue(init)}.`);
93
- }
94
- while (true) {
95
- const step = yield* cursor.next;
96
- if (step.done)
97
- return target;
98
- target.set.add(step.value);
99
- }
100
- });
101
- };
85
+ return target;
86
+ const cursor = yield* ctx.iterate(init);
87
+ if (cursor === undefined) {
88
+ throw typeError(`new Set(...) expects a synchronous iterable, received ${describeValue(init)}.`);
89
+ }
90
+ while (true) {
91
+ const step = yield* cursor.next();
92
+ if (step.done)
93
+ return target;
94
+ target.set.add(step.value);
95
+ }
96
+ }
102
97
  export const mapGlobal = (ctx) => {
103
98
  const builtins = ctx.builtins;
104
99
  const proto = builtins.Map;
@@ -135,16 +130,15 @@ export const mapGlobal = (ctx) => {
135
130
  [
136
131
  "getOrInsertComputed",
137
132
  2,
138
- (thisValue, args) => {
133
+ function* (thisValue, args) {
139
134
  const target = self(thisValue, "getOrInsertComputed").map;
140
135
  const apply = applyCollectionCallback(ctx, args[1], "Map.getOrInsertComputed");
141
136
  if (target.has(args[0]))
142
137
  return target.get(args[0]);
143
138
  // The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself.
144
- return Effect.map(apply([args[0] === 0 ? 0 : args[0]]), (value) => {
145
- target.set(args[0], value);
146
- return value;
147
- });
139
+ const value = yield* apply([args[0] === 0 ? 0 : args[0]]);
140
+ target.set(args[0], value);
141
+ return value;
148
142
  },
149
143
  ],
150
144
  ["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])],
@@ -162,66 +156,72 @@ export const mapGlobal = (ctx) => {
162
156
  [
163
157
  "forEach",
164
158
  1,
165
- (thisValue, args) => {
159
+ function* (thisValue, args) {
166
160
  const target = self(thisValue, "forEach");
167
161
  const apply = applyCollectionCallback(ctx, args[0], "Map.forEach");
168
- return Effect.gen(function* () {
169
- for (const [key, item] of target.map.entries())
170
- yield* apply([item, key, target], args[1]);
171
- return undefined;
172
- });
162
+ for (const [key, item] of target.map.entries())
163
+ yield* apply([item, key, target], args[1]);
164
+ return undefined;
173
165
  },
174
166
  ],
175
167
  ]);
176
168
  define(proto, IteratorSymbol, get(proto, "entries"), hidden);
177
169
  return map;
178
170
  };
179
- const loadSetRecord = (ctx, source, name) => {
171
+ function* loadSetRecord(ctx, source, name) {
180
172
  if (source instanceof SetObj) {
181
- return Effect.succeed({
173
+ return {
182
174
  size: source.set.size,
183
- has: (item) => Effect.succeed(source.set.has(item)),
184
- keys: () => Effect.succeed(source.set.values()),
185
- });
175
+ has: function* (item) {
176
+ return source.set.has(item);
177
+ },
178
+ keys: function* () {
179
+ return source.set.values();
180
+ },
181
+ };
186
182
  }
187
183
  if (source instanceof MapObj) {
188
- return Effect.succeed({
184
+ return {
189
185
  size: source.map.size,
190
- has: (item) => Effect.succeed(source.map.has(item)),
191
- keys: () => Effect.succeed(source.map.keys()),
192
- });
186
+ has: function* (item) {
187
+ return source.map.has(item);
188
+ },
189
+ keys: function* () {
190
+ return source.map.keys();
191
+ },
192
+ };
193
193
  }
194
194
  if (!(source instanceof Obj)) {
195
195
  throw typeError(`Set.${name} expects a Set-like object.`);
196
196
  }
197
- return Effect.gen(function* () {
198
- const size = yield* toPrimitiveNumber(ctx, get(source, "size"));
199
- if (Number.isNaN(size)) {
200
- throw typeError(`Set.${name} received a Set-like object with an invalid size.`);
201
- }
202
- const has = get(source, "has");
203
- const keys = get(source, "keys");
204
- if (!isSupportedCallback(has) || !isSupportedCallback(keys)) {
205
- throw typeError(`Set.${name} expects callable 'has' and 'keys' methods.`);
206
- }
207
- return {
208
- size: Math.max(Math.trunc(size), 0),
209
- has: (item) => Effect.map(ctx.call(has, source, [item]), Boolean),
210
- keys: () => Effect.gen(function* () {
211
- const result = yield* ctx.call(keys, source, []);
212
- const cursor = result instanceof Arr ? hostCursor(result.items.values()) : ctx.iterateDirect(result);
213
- const items = [];
214
- while (true) {
215
- const step = yield* cursor.next;
216
- if (step.done)
217
- return items;
218
- items.push(step.value);
219
- }
220
- }),
221
- };
222
- });
223
- };
224
- const setOperation = (ctx, target, name, source) => Effect.gen(function* () {
197
+ const size = yield* toPrimitiveNumber(ctx, get(source, "size"));
198
+ if (Number.isNaN(size)) {
199
+ throw typeError(`Set.${name} received a Set-like object with an invalid size.`);
200
+ }
201
+ const has = get(source, "has");
202
+ const keys = get(source, "keys");
203
+ if (!isSupportedCallback(has) || !isSupportedCallback(keys)) {
204
+ throw typeError(`Set.${name} expects callable 'has' and 'keys' methods.`);
205
+ }
206
+ return {
207
+ size: Math.max(Math.trunc(size), 0),
208
+ has: function* (item) {
209
+ return Boolean(yield* ctx.call(has, source, [item]));
210
+ },
211
+ keys: function* () {
212
+ const result = yield* ctx.call(keys, source, []);
213
+ const cursor = result instanceof Arr ? hostCursor(result.items.values()) : ctx.iterateDirect(result);
214
+ const items = [];
215
+ while (true) {
216
+ const step = yield* cursor.next();
217
+ if (step.done)
218
+ return items;
219
+ items.push(step.value);
220
+ }
221
+ },
222
+ };
223
+ }
224
+ function* setOperation(ctx, target, name, source) {
225
225
  const other = yield* loadSetRecord(ctx, source, name);
226
226
  const copy = () => {
227
227
  const result = new SetObj(ctx.builtins.Set);
@@ -303,7 +303,7 @@ const setOperation = (ctx, target, name, source) => Effect.gen(function* () {
303
303
  return false;
304
304
  }
305
305
  return true;
306
- });
306
+ }
307
307
  export const setGlobal = (ctx) => {
308
308
  const builtins = ctx.builtins;
309
309
  const proto = builtins.Set;
@@ -352,14 +352,12 @@ export const setGlobal = (ctx) => {
352
352
  [
353
353
  "forEach",
354
354
  1,
355
- (thisValue, args) => {
355
+ function* (thisValue, args) {
356
356
  const target = self(thisValue, "forEach");
357
357
  const apply = applyCollectionCallback(ctx, args[0], "Set.forEach");
358
- return Effect.gen(function* () {
359
- for (const item of target.set.values())
360
- yield* apply([item, item, target], args[1]);
361
- return undefined;
362
- });
358
+ for (const item of target.set.values())
359
+ yield* apply([item, item, target], args[1]);
360
+ return undefined;
363
361
  },
364
362
  ],
365
363
  operation("union"),
@@ -385,27 +383,20 @@ export const weakMapGlobal = (ctx) => {
385
383
  const weakMap = constructor(builtins, proto, {
386
384
  name: "WeakMap",
387
385
  call: requiresNew("WeakMap"),
388
- construct: (args, newTarget) => {
386
+ construct: function* (args, newTarget) {
389
387
  const target = new WeakMapObj(prototypeFrom(newTarget, proto));
390
388
  if (args[0] === undefined || args[0] === null)
391
- return Effect.succeed(target);
392
- return Effect.gen(function* () {
393
- const cursor = yield* ctx.iterate(args[0]);
394
- if (cursor === undefined) {
395
- throw typeError(`new WeakMap(...) expects an iterable of [key, value] pairs, received ${describeValue(args[0])}.`);
396
- }
397
- while (true) {
398
- const step = yield* cursor.next;
399
- if (step.done)
400
- return target;
401
- yield* preserveConsumerError(cursor.close, Effect.sync(() => {
402
- if (!(step.value instanceof Obj)) {
403
- throw typeError("new WeakMap(...) expects [key, value] pairs as entry objects.");
404
- }
405
- target.map.set(weakKey(getOwn(step.value, 0), "as weak map key"), getOwn(step.value, 1));
406
- }));
407
- }
408
- });
389
+ return target;
390
+ const cursor = yield* ctx.iterate(args[0]);
391
+ if (cursor === undefined) {
392
+ throw typeError(`new WeakMap(...) expects an iterable of [key, value] pairs, received ${describeValue(args[0])}.`);
393
+ }
394
+ while (true) {
395
+ const step = yield* cursor.next();
396
+ if (step.done)
397
+ return target;
398
+ yield* preserveConsumerError(cursor.close, addWeakMapEntry(target, step.value));
399
+ }
409
400
  },
410
401
  });
411
402
  const self = (thisValue, name) => receiver(WeakMapObj, thisValue, `WeakMap.prototype.${name}`).map;
@@ -444,45 +435,46 @@ export const weakMapGlobal = (ctx) => {
444
435
  [
445
436
  "getOrInsertComputed",
446
437
  2,
447
- (thisValue, args) => {
438
+ function* (thisValue, args) {
448
439
  const target = self(thisValue, "getOrInsertComputed");
449
440
  const k = key(args[0]);
450
441
  const apply = applyCollectionCallback(ctx, args[1], "WeakMap.getOrInsertComputed");
451
442
  if (target.has(k))
452
443
  return target.get(k);
453
- return Effect.map(apply([k]), (value) => {
454
- target.set(k, value);
455
- return value;
456
- });
444
+ const value = yield* apply([k]);
445
+ target.set(k, value);
446
+ return value;
457
447
  },
458
448
  ],
459
449
  ]);
460
450
  return weakMap;
461
451
  };
452
+ function* addWeakMapEntry(target, entry) {
453
+ if (!(entry instanceof Obj)) {
454
+ throw typeError("new WeakMap(...) expects [key, value] pairs as entry objects.");
455
+ }
456
+ target.map.set(weakKey(getOwn(entry, 0), "as weak map key"), getOwn(entry, 1));
457
+ }
462
458
  export const weakSetGlobal = (ctx) => {
463
459
  const builtins = ctx.builtins;
464
460
  const proto = builtins.WeakSet;
465
461
  const weakSet = constructor(builtins, proto, {
466
462
  name: "WeakSet",
467
463
  call: requiresNew("WeakSet"),
468
- construct: (args, newTarget) => {
464
+ construct: function* (args, newTarget) {
469
465
  const target = new WeakSetObj(prototypeFrom(newTarget, proto));
470
466
  if (args[0] === undefined || args[0] === null)
471
- return Effect.succeed(target);
472
- return Effect.gen(function* () {
473
- const cursor = yield* ctx.iterate(args[0]);
474
- if (cursor === undefined) {
475
- throw typeError(`new WeakSet(...) expects a synchronous iterable, received ${describeValue(args[0])}.`);
476
- }
477
- while (true) {
478
- const step = yield* cursor.next;
479
- if (step.done)
480
- return target;
481
- yield* preserveConsumerError(cursor.close, Effect.sync(() => {
482
- target.set.add(weakKey(step.value, "in weak set"));
483
- }));
484
- }
485
- });
467
+ return target;
468
+ const cursor = yield* ctx.iterate(args[0]);
469
+ if (cursor === undefined) {
470
+ throw typeError(`new WeakSet(...) expects a synchronous iterable, received ${describeValue(args[0])}.`);
471
+ }
472
+ while (true) {
473
+ const step = yield* cursor.next();
474
+ if (step.done)
475
+ return target;
476
+ yield* preserveConsumerError(cursor.close, addWeakSetValue(target, step.value));
477
+ }
486
478
  },
487
479
  });
488
480
  const self = (thisValue, name) => receiver(WeakSetObj, thisValue, `WeakSet.prototype.${name}`).set;
@@ -500,3 +492,6 @@ export const weakSetGlobal = (ctx) => {
500
492
  ]);
501
493
  return weakSet;
502
494
  };
495
+ function* addWeakSetValue(target, value) {
496
+ target.set.add(weakKey(value, "in weak set"));
497
+ }
@@ -1,2 +1,2 @@
1
1
  import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const dateGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
2
+ export declare const dateGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
@@ -1,22 +1,30 @@
1
- import { Effect } from "effect";
2
1
  import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
3
2
  import { rangeError } from "../interpreter/model.js";
4
3
  import { DateObj, Obj, coerceToNumber, coerceToString } from "../interpreter/objects.js";
5
4
  import { toPrimitive, toPrimitiveNumber } from "../interpreter/callback.js";
6
- const constructDate = (ctx, args, proto) => {
5
+ function* constructDate(ctx, args, proto) {
7
6
  if (args.length === 0)
8
- return Effect.succeed(new DateObj(proto, Date.now()));
7
+ return new DateObj(proto, Date.now());
9
8
  if (args.length === 1) {
10
9
  const arg = args[0];
11
10
  if (arg instanceof DateObj)
12
- return Effect.succeed(new DateObj(proto, arg.time));
13
- return Effect.map(toPrimitive(ctx, arg, "default"), (value) => typeof value === "string"
11
+ return new DateObj(proto, arg.time);
12
+ const value = yield* toPrimitive(ctx, arg, "default");
13
+ return typeof value === "string"
14
14
  ? new DateObj(proto, Date.parse(value))
15
- : new DateObj(proto, new Date(coerceToNumber(value)).getTime()));
15
+ : new DateObj(proto, new Date(coerceToNumber(value)).getTime());
16
16
  }
17
17
  // The spec converts at most seven components, in order, so extra arguments never run program code.
18
- return Effect.map(Effect.forEach(args.slice(0, 7), (arg) => toPrimitiveNumber(ctx, arg), { concurrency: 1 }), (parts) => new DateObj(proto, new Date(...parts).getTime()));
19
- };
18
+ const parts = yield* toNumbers(ctx, args.slice(0, 7));
19
+ return new DateObj(proto, new Date(...parts).getTime());
20
+ }
21
+ // ToNumber on each value in turn, so a symbol throws before any later value's conversion runs program code.
22
+ function* toNumbers(ctx, values) {
23
+ const numbers = [];
24
+ for (const value of values)
25
+ numbers.push(yield* toPrimitiveNumber(ctx, value));
26
+ return numbers;
27
+ }
20
28
  const getters = [
21
29
  "getFullYear",
22
30
  "getMonth",
@@ -60,7 +68,7 @@ export const dateGlobal = (ctx) => {
60
68
  name: "Date",
61
69
  length: 7,
62
70
  // ISO instead of the host's locale string: date strings are deterministic and must not leak the host timezone.
63
- call: () => Effect.sync(() => new Date().toISOString()),
71
+ call: () => new Date().toISOString(),
64
72
  construct: (args, newTarget) => constructDate(ctx, args, prototypeFrom(newTarget, proto)),
65
73
  });
66
74
  methods(builtins, date, [
@@ -69,7 +77,10 @@ export const dateGlobal = (ctx) => {
69
77
  [
70
78
  "UTC",
71
79
  7,
72
- (_, args) => Effect.map(Effect.forEach(args.slice(0, 7), (arg) => toPrimitiveNumber(ctx, arg), { concurrency: 1 }), (parts) => Date.UTC(...parts)),
80
+ function* (_, args) {
81
+ const parts = yield* toNumbers(ctx, args.slice(0, 7));
82
+ return Date.UTC(...parts);
83
+ },
73
84
  ],
74
85
  ]);
75
86
  const self = (thisValue, name) => receiver(DateObj, thisValue, `Date.prototype.${name}`);
@@ -104,19 +115,16 @@ export const dateGlobal = (ctx) => {
104
115
  ...setters.map(([name, length]) => [
105
116
  name,
106
117
  length,
107
- (thisValue, args) => {
118
+ function* (thisValue, args) {
108
119
  const target = self(thisValue, name);
109
120
  // Native setters read the current time before argument coercion, whose callbacks may mutate the Date.
110
121
  const hosted = new Date(target.time);
111
- return Effect.map(Effect.forEach(args.slice(0, length), (arg) => toPrimitiveNumber(ctx, arg), {
112
- concurrency: 1,
113
- }), (values) => {
114
- // Every setter but setTime and setFullYear leaves an invalid Date untouched and answers NaN.
115
- if (Number.isNaN(hosted.getTime()) && name !== "setTime" && !name.endsWith("FullYear"))
116
- return NaN;
117
- target.time = hosted[name](...values);
118
- return target.time;
119
- });
122
+ const values = yield* toNumbers(ctx, args.slice(0, length));
123
+ // Every setter but setTime and setFullYear leaves an invalid Date untouched and answers NaN.
124
+ if (Number.isNaN(hosted.getTime()) && name !== "setTime" && !name.endsWith("FullYear"))
125
+ return NaN;
126
+ target.time = hosted[name](...values);
127
+ return target.time;
120
128
  },
121
129
  ]),
122
130
  ]);
@@ -1,2 +1,2 @@
1
1
  import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const headersGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
2
+ export declare const headersGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
@@ -1,4 +1,3 @@
1
- import { Effect } from "effect";
2
1
  import { constructor, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
3
2
  import { IteratorSymbol, typeError } from "../interpreter/model.js";
4
3
  import { define, entries, get, hidden, Arr, HeadersObj, hostIterator, Obj, coerceToString, isRuntimeReference, } from "../interpreter/objects.js";
@@ -13,20 +12,18 @@ const attempt = (run) => {
13
12
  throw typeError(error instanceof Error ? error.message : String(error));
14
13
  }
15
14
  };
16
- const constructHeaders = (ctx, init, proto) => {
15
+ function* constructHeaders(ctx, init, proto) {
17
16
  const wrap = (headers) => new HeadersObj(proto, headers);
18
17
  if (init === undefined)
19
- return Effect.succeed(wrap(new Headers()));
20
- return Effect.gen(function* () {
21
- const pairs = init instanceof Obj ? yield* readPairs(ctx, init, "new Headers(...)") : undefined;
22
- if (pairs !== undefined)
23
- return wrap(attempt(() => new Headers(pairs)));
24
- if (!(init instanceof Obj) || isRuntimeReference(init)) {
25
- throw typeError("new Headers(...) expects a record of names to values, iterable [name, value] pairs, or Headers.");
26
- }
27
- return wrap(attempt(() => new Headers(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)])))));
28
- });
29
- };
18
+ return wrap(new Headers());
19
+ const pairs = init instanceof Obj ? yield* readPairs(ctx, init, "new Headers(...)") : undefined;
20
+ if (pairs !== undefined)
21
+ return wrap(attempt(() => new Headers(pairs)));
22
+ if (!(init instanceof Obj) || isRuntimeReference(init)) {
23
+ throw typeError("new Headers(...) expects a record of names to values, iterable [name, value] pairs, or Headers.");
24
+ }
25
+ return wrap(attempt(() => new Headers(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)])))));
26
+ }
30
27
  export const headersGlobal = (ctx) => {
31
28
  const builtins = ctx.builtins;
32
29
  const proto = builtins.Headers;
@@ -98,15 +95,13 @@ export const headersGlobal = (ctx) => {
98
95
  [
99
96
  "forEach",
100
97
  1,
101
- (thisValue, args) => {
98
+ function* (thisValue, args) {
102
99
  requireArgs("forEach", args, 1);
103
100
  const target = self(thisValue, "forEach");
104
101
  const apply = applyCollectionCallback(ctx, args[0], "Headers.forEach");
105
- return Effect.gen(function* () {
106
- for (const [key, value] of Array.from(target.headers.entries()))
107
- yield* apply([value, key, target], args[1]);
108
- return undefined;
109
- });
102
+ for (const [key, value] of Array.from(target.headers.entries()))
103
+ yield* apply([value, key, target], args[1]);
104
+ return undefined;
110
105
  },
111
106
  ],
112
107
  ]);
@@ -1,2 +1,2 @@
1
1
  import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const iteratorGlobals: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
2
+ export declare const iteratorGlobals: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;