@opencode/codemode 2.2.7 → 2.3.0

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 (197) hide show
  1. package/README.md +11 -13
  2. package/dist/{vm/async.d.ts → async.d.ts} +1 -1
  3. package/dist/codemode.d.ts +8 -22
  4. package/dist/codemode.js +17 -38
  5. package/dist/data.d.ts +4 -19
  6. package/dist/data.js +1 -82
  7. package/dist/extension.d.ts +8 -2
  8. package/dist/extension.js +9 -0
  9. package/dist/{vm/host.d.ts → host.d.ts} +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
  12. package/dist/{vm/prelude → prelude}/helpers.js +1 -1
  13. package/dist/{vm/runtime → runtime}/driver.d.ts +1 -1
  14. package/dist/{vm/runtime → runtime}/driver.js +1 -1
  15. package/dist/{vm/runtime → runtime}/errors.js +2 -2
  16. package/dist/{vm/runtime → runtime}/execute.d.ts +17 -8
  17. package/dist/{vm/runtime → runtime}/execute.js +11 -9
  18. package/dist/{vm/runtime → runtime}/extensions.d.ts +1 -1
  19. package/dist/{vm/runtime → runtime}/extensions.js +39 -4
  20. package/dist/{vm/runtime → runtime}/globals.js +1 -1
  21. package/dist/{vm/runtime → runtime}/interpreter.d.ts +1 -1
  22. package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
  23. package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
  24. package/dist/{vm/runtime → runtime}/objects.js +1 -1
  25. package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
  26. package/dist/{vm/runtime → runtime}/operations.js +1 -1
  27. package/dist/scan.js +2 -2
  28. package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +1 -1
  29. package/dist/{vm/snapshot → snapshot}/codec.d.ts +6 -5
  30. package/dist/{vm/snapshot → snapshot}/codec.js +12 -5
  31. package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +8 -5
  32. package/dist/{vm/snapshot → snapshot}/snapshot.js +14 -3
  33. package/dist/{vm/snapshot.js → snapshot.js} +1 -1
  34. package/dist/stdlib/array.d.ts +14 -9
  35. package/dist/stdlib/array.js +89 -276
  36. package/dist/stdlib/bytes.d.ts +11 -7
  37. package/dist/stdlib/bytes.js +112 -79
  38. package/dist/stdlib/collections.d.ts +7 -7
  39. package/dist/stdlib/collections.js +30 -339
  40. package/dist/stdlib/console.d.ts +2 -2
  41. package/dist/stdlib/console.js +3 -3
  42. package/dist/stdlib/date.d.ts +2 -2
  43. package/dist/stdlib/date.js +21 -30
  44. package/dist/stdlib/headers.d.ts +2 -2
  45. package/dist/stdlib/headers.js +15 -33
  46. package/dist/stdlib/iterator.d.ts +2 -2
  47. package/dist/stdlib/iterator.js +10 -302
  48. package/dist/stdlib/json.d.ts +2 -2
  49. package/dist/stdlib/json.js +15 -31
  50. package/dist/stdlib/math.d.ts +2 -7
  51. package/dist/stdlib/math.js +6 -30
  52. package/dist/stdlib/number.d.ts +3 -3
  53. package/dist/stdlib/number.js +8 -8
  54. package/dist/stdlib/object.d.ts +4 -4
  55. package/dist/stdlib/object.js +12 -30
  56. package/dist/stdlib/regexp.d.ts +6 -5
  57. package/dist/stdlib/regexp.js +18 -13
  58. package/dist/stdlib/string.d.ts +2 -2
  59. package/dist/stdlib/string.js +53 -53
  60. package/dist/stdlib/url.d.ts +21 -9
  61. package/dist/stdlib/url.js +55 -55
  62. package/dist/stdlib/value.d.ts +3 -4
  63. package/dist/stdlib/value.js +5 -6
  64. package/dist/stdlib/web.d.ts +4 -4
  65. package/dist/stdlib/web.js +7 -7
  66. package/dist/tool-runtime.d.ts +14 -13
  67. package/dist/tool-runtime.js +245 -53
  68. package/dist/tool-schema.d.ts +5 -0
  69. package/dist/tool-schema.js +36 -16
  70. package/dist/tool.d.ts +24 -9
  71. package/dist/tool.js +15 -22
  72. package/dist/{vm/vm.d.ts → vm.d.ts} +3 -3
  73. package/dist/{vm/vm.js → vm.js} +1 -1
  74. package/package.json +3 -5
  75. package/dist/interpreter/callback.d.ts +0 -35
  76. package/dist/interpreter/callback.js +0 -98
  77. package/dist/interpreter/errors.js +0 -173
  78. package/dist/interpreter/execute.d.ts +0 -14
  79. package/dist/interpreter/execute.js +0 -207
  80. package/dist/interpreter/extensions.d.ts +0 -9
  81. package/dist/interpreter/extensions.js +0 -248
  82. package/dist/interpreter/generators.d.ts +0 -3
  83. package/dist/interpreter/generators.js +0 -168
  84. package/dist/interpreter/globals.js +0 -144
  85. package/dist/interpreter/interpreter.d.ts +0 -48
  86. package/dist/interpreter/interpreter.js +0 -2253
  87. package/dist/interpreter/intrinsics.d.ts +0 -15
  88. package/dist/interpreter/intrinsics.js +0 -105
  89. package/dist/interpreter/model.d.ts +0 -68
  90. package/dist/interpreter/model.js +0 -54
  91. package/dist/interpreter/native.d.ts +0 -17
  92. package/dist/interpreter/native.js +0 -35
  93. package/dist/interpreter/objects.d.ts +0 -301
  94. package/dist/interpreter/objects.js +0 -592
  95. package/dist/interpreter/promises.d.ts +0 -18
  96. package/dist/interpreter/promises.js +0 -292
  97. package/dist/interpreter/runner.d.ts +0 -178
  98. package/dist/interpreter/runner.js +0 -480
  99. package/dist/interpreter/scope.d.ts +0 -16
  100. package/dist/interpreter/scope.js +0 -79
  101. package/dist/suspend-error.d.ts +0 -24
  102. package/dist/suspend-error.js +0 -25
  103. package/dist/vm/data.d.ts +0 -7
  104. package/dist/vm/data.js +0 -13
  105. package/dist/vm/index.d.ts +0 -6
  106. package/dist/vm/index.js +0 -6
  107. package/dist/vm/runtime/errors.d.ts +0 -23
  108. package/dist/vm/runtime/globals.d.ts +0 -6
  109. package/dist/vm/runtime/limits.d.ts +0 -10
  110. package/dist/vm/runtime/limits.js +0 -19
  111. package/dist/vm/runtime/modules.d.ts +0 -4
  112. package/dist/vm/runtime/modules.js +0 -33
  113. package/dist/vm/runtime/references.d.ts +0 -8
  114. package/dist/vm/runtime/references.js +0 -51
  115. package/dist/vm/stdlib/array.d.ts +0 -16
  116. package/dist/vm/stdlib/array.js +0 -311
  117. package/dist/vm/stdlib/bytes.d.ts +0 -11
  118. package/dist/vm/stdlib/bytes.js +0 -222
  119. package/dist/vm/stdlib/collections.d.ts +0 -7
  120. package/dist/vm/stdlib/collections.js +0 -188
  121. package/dist/vm/stdlib/console.d.ts +0 -6
  122. package/dist/vm/stdlib/console.js +0 -93
  123. package/dist/vm/stdlib/date.d.ts +0 -2
  124. package/dist/vm/stdlib/date.js +0 -123
  125. package/dist/vm/stdlib/headers.d.ts +0 -2
  126. package/dist/vm/stdlib/headers.js +0 -92
  127. package/dist/vm/stdlib/iterator.d.ts +0 -2
  128. package/dist/vm/stdlib/iterator.js +0 -30
  129. package/dist/vm/stdlib/json.d.ts +0 -3
  130. package/dist/vm/stdlib/json.js +0 -42
  131. package/dist/vm/stdlib/math.d.ts +0 -3
  132. package/dist/vm/stdlib/math.js +0 -73
  133. package/dist/vm/stdlib/number.d.ts +0 -3
  134. package/dist/vm/stdlib/number.js +0 -72
  135. package/dist/vm/stdlib/object.d.ts +0 -6
  136. package/dist/vm/stdlib/object.js +0 -201
  137. package/dist/vm/stdlib/regexp.d.ts +0 -8
  138. package/dist/vm/stdlib/regexp.js +0 -119
  139. package/dist/vm/stdlib/string.d.ts +0 -2
  140. package/dist/vm/stdlib/string.js +0 -272
  141. package/dist/vm/stdlib/url.d.ts +0 -25
  142. package/dist/vm/stdlib/url.js +0 -255
  143. package/dist/vm/stdlib/value.d.ts +0 -6
  144. package/dist/vm/stdlib/value.js +0 -32
  145. package/dist/vm/stdlib/web.d.ts +0 -5
  146. package/dist/vm/stdlib/web.js +0 -97
  147. /package/dist/{vm/async.js → async.js} +0 -0
  148. /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
  149. /package/dist/{vm/code.js → code.js} +0 -0
  150. /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
  151. /package/dist/{vm/compiler → compiler}/compile.js +0 -0
  152. /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
  153. /package/dist/{vm/compiler → compiler}/scope.js +0 -0
  154. /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
  155. /package/dist/{vm/generators.js → generators.js} +0 -0
  156. /package/dist/{vm/host.js → host.js} +0 -0
  157. /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
  158. /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
  159. /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
  160. /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
  161. /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
  162. /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
  163. /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
  164. /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
  165. /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
  166. /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
  167. /package/dist/{vm/prelude → prelude}/source.js +0 -0
  168. /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
  169. /package/dist/{vm/runtime → runtime}/callback.js +0 -0
  170. /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
  171. /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
  172. /package/dist/{vm/runtime → runtime}/generators.js +0 -0
  173. /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
  174. /package/dist/{vm/runtime → runtime}/interpreter.js +0 -0
  175. /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
  176. /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
  177. /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
  178. /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
  179. /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
  180. /package/dist/{interpreter → runtime}/limits.js +0 -0
  181. /package/dist/{vm/runtime → runtime}/model.js +0 -0
  182. /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
  183. /package/dist/{interpreter → runtime}/modules.js +0 -0
  184. /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
  185. /package/dist/{vm/runtime → runtime}/native.js +0 -0
  186. /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
  187. /package/dist/{vm/runtime → runtime}/promises.js +0 -0
  188. /package/dist/{interpreter → runtime}/references.d.ts +0 -0
  189. /package/dist/{interpreter → runtime}/references.js +0 -0
  190. /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
  191. /package/dist/{vm/runtime → runtime}/signals.js +0 -0
  192. /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
  193. /package/dist/{vm/runtime → runtime}/source.js +0 -0
  194. /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
  195. /package/dist/{vm/runtime → runtime}/table.js +0 -0
  196. /package/dist/{vm/scheduler.js → scheduler.js} +0 -0
  197. /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
@@ -1,108 +1,28 @@
1
- import { constructor, fn, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
2
- import { invalidData, IteratorSymbol, typeError } from "../interpreter/model.js";
3
- import { define, defineAccessor, get, getOwn, hidden, Arr, coerceToString, hostCursor, hostIterator, MapObj, Obj, PromiseObj, SetObj, WeakMapObj, WeakSetObj, } from "../interpreter/objects.js";
4
- import { describeValue, isOpaque } from "../interpreter/references.js";
5
- import { applyCollectionCallback, isSupportedCallback, preserveConsumerError, toPrimitiveNumber, toPrimitiveString, } from "../interpreter/callback.js";
6
- function* coerceGroupByPropertyKey(ctx, result) {
7
- const value = yield* result;
8
- if (value instanceof PromiseObj)
9
- return coerceToString(value);
10
- if (isOpaque(value)) {
11
- throw invalidData(`Object.groupBy callback must return a data value, received ${describeValue(value)}.`);
12
- }
13
- return yield* toPrimitiveString(ctx, value);
14
- }
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, function* (_, args) {
17
- const builtins = ctx.builtins;
18
- const source = args[0];
19
- if (source === null || source === undefined) {
20
- throw typeError(`${namespace}.groupBy expects an iterable collection.`);
21
- }
22
- const apply = applyCollectionCallback(ctx, args[1], `${namespace}.groupBy`);
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);
29
- let index = 0;
30
- while (true) {
31
- const step = yield* cursor.next();
32
- if (step.done)
33
- return result;
34
- const item = step.value;
35
- const key = yield* preserveConsumerError(cursor.close, apply([item, index]));
36
- const group = result.map.get(key);
37
- if (group === undefined)
38
- result.map.set(key, new Arr(builtins.Array, [item]));
39
- else
40
- group.items.push(item);
41
- index += 1;
42
- }
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
- }
60
- });
61
- function* constructMap(ctx, init, proto) {
62
- const target = new MapObj(proto);
63
- if (init === undefined || init === null)
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) {
83
- const target = new SetObj(proto);
84
- if (init === undefined || init === null)
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
- }
1
+ import { Invoke } from "../runtime/signals.js";
2
+ import { constructor, methods, prototypeFrom, receiver, requiresNew, iteratorMethods } from "../runtime/native.js";
3
+ import { IteratorSymbol, typeError } from "../runtime/model.js";
4
+ import { define, defineAccessor, get, hidden, MapObj, Obj, SetObj, WeakMapObj, WeakSetObj, } from "../runtime/objects.js";
5
+ import { describeValue } from "../runtime/references.js";
6
+ import { requireCallback } from "../runtime/callback.js";
7
+ // A collection from an iterable is filled by the prelude, which iterates it.
8
+ const filled = (ctx, target, init, kind) => init === undefined || init === null
9
+ ? target
10
+ : new Invoke(ctx.prelude.fn("fillCollection"), undefined, [target, init, kind], undefined);
11
+ // The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself.
12
+ const getOrInsertComputed = (ctx, owner, key, callback) => {
13
+ const table = owner.map;
14
+ if (table.has(key))
15
+ return table.get(key);
16
+ return new Invoke(ctx.prelude.fn("insertComputed"), undefined, [owner, key === 0 ? 0 : key, callback], undefined);
17
+ };
97
18
  export const mapGlobal = (ctx) => {
98
19
  const builtins = ctx.builtins;
99
20
  const proto = builtins.Map;
100
21
  const map = constructor(builtins, proto, {
101
22
  name: "Map",
102
23
  call: requiresNew("Map"),
103
- construct: (args, newTarget) => constructMap(ctx, args[0], prototypeFrom(newTarget, proto)),
24
+ construct: (args, newTarget) => filled(ctx, new MapObj(prototypeFrom(newTarget, proto)), args[0], "Map"),
104
25
  });
105
- define(map, "groupBy", groupBy(ctx, "Map"), hidden);
106
26
  const self = (thisValue, name) => receiver(MapObj, thisValue, `Map.prototype.${name}`);
107
27
  defineAccessor(proto, "size", (thisValue) => receiver(MapObj, thisValue, "Map.prototype.size").map.size);
108
28
  methods(builtins, proto, [
@@ -130,15 +50,9 @@ export const mapGlobal = (ctx) => {
130
50
  [
131
51
  "getOrInsertComputed",
132
52
  2,
133
- function* (thisValue, args) {
134
- const target = self(thisValue, "getOrInsertComputed").map;
135
- const apply = applyCollectionCallback(ctx, args[1], "Map.getOrInsertComputed");
136
- if (target.has(args[0]))
137
- return target.get(args[0]);
138
- // The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself.
139
- const value = yield* apply([args[0] === 0 ? 0 : args[0]]);
140
- target.set(args[0], value);
141
- return value;
53
+ (thisValue, args) => {
54
+ const target = self(thisValue, "getOrInsertComputed");
55
+ return getOrInsertComputed(ctx, target, args[0], requireCallback(args[1], "Map.getOrInsertComputed"));
142
56
  },
143
57
  ],
144
58
  ["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])],
@@ -150,175 +64,20 @@ export const mapGlobal = (ctx) => {
150
64
  return undefined;
151
65
  },
152
66
  ],
153
- ["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").map.keys())],
154
- ["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").map.values())],
155
- ["entries", 0, (thisValue) => hostIterator(builtins, self(thisValue, "entries").iterator(builtins))],
156
- [
157
- "forEach",
158
- 1,
159
- function* (thisValue, args) {
160
- const target = self(thisValue, "forEach");
161
- const apply = applyCollectionCallback(ctx, args[0], "Map.forEach");
162
- for (const [key, item] of target.map.entries())
163
- yield* apply([item, key, target], args[1]);
164
- return undefined;
165
- },
166
- ],
67
+ ...iteratorMethods(builtins, self),
167
68
  ]);
168
69
  define(proto, IteratorSymbol, get(proto, "entries"), hidden);
169
70
  return map;
170
71
  };
171
- function* loadSetRecord(ctx, source, name) {
172
- if (source instanceof SetObj) {
173
- return {
174
- size: source.set.size,
175
- has: function* (item) {
176
- return source.set.has(item);
177
- },
178
- keys: function* () {
179
- return source.set.values();
180
- },
181
- };
182
- }
183
- if (source instanceof MapObj) {
184
- return {
185
- size: source.map.size,
186
- has: function* (item) {
187
- return source.map.has(item);
188
- },
189
- keys: function* () {
190
- return source.map.keys();
191
- },
192
- };
193
- }
194
- if (!(source instanceof Obj)) {
195
- throw typeError(`Set.${name} expects a Set-like object.`);
196
- }
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
- const other = yield* loadSetRecord(ctx, source, name);
226
- const copy = () => {
227
- const result = new SetObj(ctx.builtins.Set);
228
- for (const item of target.set.values())
229
- result.set.add(item);
230
- return result;
231
- };
232
- if (name === "union") {
233
- const result = copy();
234
- for (const item of yield* other.keys())
235
- result.set.add(item);
236
- return result;
237
- }
238
- if (name === "intersection") {
239
- const result = new SetObj(ctx.builtins.Set);
240
- if (target.set.size <= other.size) {
241
- for (const item of target.set.values()) {
242
- if (yield* other.has(item))
243
- result.set.add(item);
244
- }
245
- return result;
246
- }
247
- for (const item of yield* other.keys()) {
248
- if (target.set.has(item))
249
- result.set.add(item);
250
- }
251
- return result;
252
- }
253
- if (name === "difference") {
254
- const result = copy();
255
- if (target.set.size <= other.size) {
256
- for (const item of result.set.values()) {
257
- if (yield* other.has(item))
258
- result.set.delete(item);
259
- }
260
- return result;
261
- }
262
- for (const item of yield* other.keys())
263
- result.set.delete(item);
264
- return result;
265
- }
266
- if (name === "symmetricDifference") {
267
- const result = copy();
268
- for (const item of yield* other.keys()) {
269
- if (target.set.has(item))
270
- result.set.delete(item);
271
- else
272
- result.set.add(item);
273
- }
274
- return result;
275
- }
276
- if (name === "isSubsetOf") {
277
- if (target.set.size > other.size)
278
- return false;
279
- for (const item of target.set.values()) {
280
- if (!(yield* other.has(item)))
281
- return false;
282
- }
283
- return true;
284
- }
285
- if (name === "isSupersetOf") {
286
- if (target.set.size < other.size)
287
- return false;
288
- for (const item of yield* other.keys()) {
289
- if (!target.set.has(item))
290
- return false;
291
- }
292
- return true;
293
- }
294
- if (target.set.size <= other.size) {
295
- for (const item of target.set.values()) {
296
- if (yield* other.has(item))
297
- return false;
298
- }
299
- return true;
300
- }
301
- for (const item of yield* other.keys()) {
302
- if (target.set.has(item))
303
- return false;
304
- }
305
- return true;
306
- }
307
72
  export const setGlobal = (ctx) => {
308
73
  const builtins = ctx.builtins;
309
74
  const proto = builtins.Set;
310
75
  const set = constructor(builtins, proto, {
311
76
  name: "Set",
312
77
  call: requiresNew("Set"),
313
- construct: (args, newTarget) => constructSet(ctx, args[0], prototypeFrom(newTarget, proto)),
78
+ construct: (args, newTarget) => filled(ctx, new SetObj(prototypeFrom(newTarget, proto)), args[0], "Set"),
314
79
  });
315
80
  const self = (thisValue, name) => receiver(SetObj, thisValue, `Set.prototype.${name}`);
316
- const wrap = (items) => new Arr(builtins.Array, items);
317
- const operation = (name) => [
318
- name,
319
- 1,
320
- (thisValue, args) => setOperation(ctx, self(thisValue, name), name, args[0]),
321
- ];
322
81
  defineAccessor(proto, "size", (thisValue) => receiver(SetObj, thisValue, "Set.prototype.size").set.size);
323
82
  methods(builtins, proto, [
324
83
  ["has", 1, (thisValue, args) => self(thisValue, "has").set.has(args[0])],
@@ -340,39 +99,13 @@ export const setGlobal = (ctx) => {
340
99
  return undefined;
341
100
  },
342
101
  ],
343
- ["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").set.values())],
344
- ["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").set.values())],
345
- [
346
- "entries",
347
- 0,
348
- (thisValue) => hostIterator(builtins, self(thisValue, "entries")
349
- .set.values()
350
- .map((item) => wrap([item, item]))),
351
- ],
352
- [
353
- "forEach",
354
- 1,
355
- function* (thisValue, args) {
356
- const target = self(thisValue, "forEach");
357
- const apply = applyCollectionCallback(ctx, args[0], "Set.forEach");
358
- for (const item of target.set.values())
359
- yield* apply([item, item, target], args[1]);
360
- return undefined;
361
- },
362
- ],
363
- operation("union"),
364
- operation("intersection"),
365
- operation("difference"),
366
- operation("symmetricDifference"),
367
- operation("isSubsetOf"),
368
- operation("isSupersetOf"),
369
- operation("isDisjointFrom"),
102
+ ...iteratorMethods(builtins, self),
370
103
  ]);
371
104
  define(proto, IteratorSymbol, get(proto, "values"), hidden);
372
105
  return set;
373
106
  };
374
107
  // CanBeHeldWeakly: only program objects; tool references are rebuilt on every access, so they could never be found again.
375
- const weakKey = (value, label) => {
108
+ export const weakKey = (value, label) => {
376
109
  if (value instanceof Obj)
377
110
  return value;
378
111
  throw typeError(`Invalid value used ${label}: ${describeValue(value)} cannot be held weakly.`);
@@ -383,21 +116,7 @@ export const weakMapGlobal = (ctx) => {
383
116
  const weakMap = constructor(builtins, proto, {
384
117
  name: "WeakMap",
385
118
  call: requiresNew("WeakMap"),
386
- construct: function* (args, newTarget) {
387
- const target = new WeakMapObj(prototypeFrom(newTarget, proto));
388
- if (args[0] === undefined || args[0] === null)
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
- }
400
- },
119
+ construct: (args, newTarget) => filled(ctx, new WeakMapObj(prototypeFrom(newTarget, proto)), args[0], "WeakMap"),
401
120
  });
402
121
  const self = (thisValue, name) => receiver(WeakMapObj, thisValue, `WeakMap.prototype.${name}`).map;
403
122
  // Lookups pass any key through: the host collection answers false for a non-object, as the spec requires.
@@ -435,47 +154,22 @@ export const weakMapGlobal = (ctx) => {
435
154
  [
436
155
  "getOrInsertComputed",
437
156
  2,
438
- function* (thisValue, args) {
439
- const target = self(thisValue, "getOrInsertComputed");
157
+ (thisValue, args) => {
158
+ const target = receiver(WeakMapObj, thisValue, "WeakMap.prototype.getOrInsertComputed");
440
159
  const k = key(args[0]);
441
- const apply = applyCollectionCallback(ctx, args[1], "WeakMap.getOrInsertComputed");
442
- if (target.has(k))
443
- return target.get(k);
444
- const value = yield* apply([k]);
445
- target.set(k, value);
446
- return value;
160
+ return getOrInsertComputed(ctx, target, k, requireCallback(args[1], "WeakMap.getOrInsertComputed"));
447
161
  },
448
162
  ],
449
163
  ]);
450
164
  return weakMap;
451
165
  };
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
- }
458
166
  export const weakSetGlobal = (ctx) => {
459
167
  const builtins = ctx.builtins;
460
168
  const proto = builtins.WeakSet;
461
169
  const weakSet = constructor(builtins, proto, {
462
170
  name: "WeakSet",
463
171
  call: requiresNew("WeakSet"),
464
- construct: function* (args, newTarget) {
465
- const target = new WeakSetObj(prototypeFrom(newTarget, proto));
466
- if (args[0] === undefined || args[0] === null)
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
- }
478
- },
172
+ construct: (args, newTarget) => filled(ctx, new WeakSetObj(prototypeFrom(newTarget, proto)), args[0], "WeakSet"),
479
173
  });
480
174
  const self = (thisValue, name) => receiver(WeakSetObj, thisValue, `WeakSet.prototype.${name}`).set;
481
175
  methods(builtins, proto, [
@@ -492,6 +186,3 @@ export const weakSetGlobal = (ctx) => {
492
186
  ]);
493
187
  return weakSet;
494
188
  };
495
- function* addWeakSetValue(target, value) {
496
- target.set.add(weakKey(value, "in weak set"));
497
- }
@@ -1,5 +1,5 @@
1
- import { Obj, type Value } from "../interpreter/objects.js";
2
- import type { Interpreter } from "../interpreter/interpreter.js";
1
+ import { Obj, type Value } from "../runtime/objects.js";
2
+ import type { Interpreter } from "../runtime/interpreter.js";
3
3
  /** Captured console: every method appends one formatted line to `logs`. */
4
4
  export declare const consoleGlobal: <R>(ctx: Interpreter<R>) => Obj;
5
5
  /** One value as `console.log` shows it. */
@@ -1,7 +1,7 @@
1
- import { methods } from "../interpreter/native.js";
2
- import { entries, get, Arr, Obj } from "../interpreter/objects.js";
1
+ import { methods } from "../runtime/native.js";
2
+ import { entries, get, Arr, Obj } from "../runtime/objects.js";
3
3
  import { ToolReference } from "../tool-runtime.js";
4
- import { containsOpaqueReference } from "../interpreter/references.js";
4
+ import { containsOpaqueReference } from "../runtime/references.js";
5
5
  const consoleMethods = ["log", "info", "debug", "warn", "error", "dir", "table"];
6
6
  /** Captured console: every method appends one formatted line to `logs`. */
7
7
  export const consoleGlobal = (ctx) => {
@@ -1,2 +1,2 @@
1
- import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const dateGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
1
+ import type { Interpreter } from "../runtime/interpreter.js";
2
+ export declare const dateGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -1,30 +1,23 @@
1
- import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
2
- import { rangeError } from "../interpreter/model.js";
3
- import { DateObj, Obj, coerceToNumber, coerceToString } from "../interpreter/objects.js";
4
- import { toPrimitive, toPrimitiveNumber } from "../interpreter/callback.js";
5
- function* constructDate(ctx, args, proto) {
1
+ import { constructor, methods, prototypeFrom, receiver } from "../runtime/native.js";
2
+ import { rangeError } from "../runtime/model.js";
3
+ import { DateObj, Obj, coerceToNumber, coerceToString } from "../runtime/objects.js";
4
+ import { withPrimitives } from "../runtime/callback.js";
5
+ const constructDate = (args, proto) => {
6
6
  if (args.length === 0)
7
7
  return new DateObj(proto, Date.now());
8
8
  if (args.length === 1) {
9
9
  const arg = args[0];
10
10
  if (arg instanceof DateObj)
11
11
  return new DateObj(proto, arg.time);
12
- const value = yield* toPrimitive(ctx, arg, "default");
13
- return typeof value === "string"
12
+ return withPrimitives("default", [arg], ([value]) => typeof value === "string"
14
13
  ? new DateObj(proto, Date.parse(value))
15
- : new DateObj(proto, new Date(coerceToNumber(value)).getTime());
14
+ : new DateObj(proto, new Date(coerceToNumber(value)).getTime()));
16
15
  }
17
16
  // The spec converts at most seven components, in order, so extra arguments never run program code.
18
- const parts = yield* toNumbers(ctx, args.slice(0, 7));
19
- return new DateObj(proto, new Date(...parts).getTime());
20
- }
17
+ return toNumbers(args.slice(0, 7), (parts) => new DateObj(proto, new Date(...parts).getTime()));
18
+ };
21
19
  // 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
+ const toNumbers = (values, body) => withPrimitives("number", values, (primitives) => body(primitives.map(coerceToNumber)), true);
28
21
  const getters = [
29
22
  "getFullYear",
30
23
  "getMonth",
@@ -69,7 +62,7 @@ export const dateGlobal = (ctx) => {
69
62
  length: 7,
70
63
  // ISO instead of the host's locale string: date strings are deterministic and must not leak the host timezone.
71
64
  call: () => new Date().toISOString(),
72
- construct: (args, newTarget) => constructDate(ctx, args, prototypeFrom(newTarget, proto)),
65
+ construct: (args, newTarget) => constructDate(args, prototypeFrom(newTarget, proto)),
73
66
  });
74
67
  methods(builtins, date, [
75
68
  ["now", 0, () => Date.now()],
@@ -77,10 +70,7 @@ export const dateGlobal = (ctx) => {
77
70
  [
78
71
  "UTC",
79
72
  7,
80
- function* (_, args) {
81
- const parts = yield* toNumbers(ctx, args.slice(0, 7));
82
- return Date.UTC(...parts);
83
- },
73
+ (_, args) => toNumbers(args.slice(0, 7), (parts) => Date.UTC(...parts)),
84
74
  ],
85
75
  ]);
86
76
  const self = (thisValue, name) => receiver(DateObj, thisValue, `Date.prototype.${name}`);
@@ -115,16 +105,17 @@ export const dateGlobal = (ctx) => {
115
105
  ...setters.map(([name, length]) => [
116
106
  name,
117
107
  length,
118
- function* (thisValue, args) {
108
+ (thisValue, args) => {
119
109
  const target = self(thisValue, name);
120
110
  // Native setters read the current time before argument coercion, whose callbacks may mutate the Date.
121
- const hosted = new Date(target.time);
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;
111
+ return toNumbers([target.time, ...args.slice(0, length)], ([time, ...values]) => {
112
+ const hosted = new Date(time);
113
+ // Every setter but setTime and setFullYear leaves an invalid Date untouched and answers NaN.
114
+ if (Number.isNaN(hosted.getTime()) && name !== "setTime" && !name.endsWith("FullYear"))
115
+ return NaN;
116
+ target.time = hosted[name](...values);
117
+ return target.time;
118
+ });
128
119
  },
129
120
  ]),
130
121
  ]);
@@ -1,2 +1,2 @@
1
- import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const headersGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
1
+ import type { Interpreter } from "../runtime/interpreter.js";
2
+ export declare const headersGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -1,29 +1,15 @@
1
- import { constructor, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
2
- import { IteratorSymbol, typeError } from "../interpreter/model.js";
3
- import { define, entries, get, hidden, Arr, HeadersObj, hostIterator, Obj, coerceToString, isRuntimeReference, } from "../interpreter/objects.js";
4
- import { applyCollectionCallback } from "../interpreter/callback.js";
5
- import { readPairs } from "./url.js";
6
- // The host validates header names and values and throws its own TypeError; the program gets one of its own.
7
- const attempt = (run) => {
8
- try {
9
- return run();
10
- }
11
- catch (error) {
12
- throw typeError(error instanceof Error ? error.message : String(error));
13
- }
14
- };
15
- function* constructHeaders(ctx, init, proto) {
16
- const wrap = (headers) => new HeadersObj(proto, headers);
1
+ import { constructor, methods, prototypeFrom, receiver, requiresNew, iteratorMethods } from "../runtime/native.js";
2
+ import { IteratorSymbol, typeError } from "../runtime/model.js";
3
+ import { define, get, hidden, Arr, HeadersObj, Obj, coerceToString } from "../runtime/objects.js";
4
+ import { requireCallback } from "../runtime/callback.js";
5
+ import { attempt, pairsForEach, readPairs } from "./url.js";
6
+ const constructHeaders = (ctx, init, proto) => {
17
7
  if (init === undefined)
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
- }
8
+ return new HeadersObj(proto, new Headers());
9
+ if (init instanceof Obj)
10
+ return readPairs(ctx, init, "new Headers(...)", { kind: "Headers", proto });
11
+ throw typeError("new Headers(...) expects a record of names to values, iterable [name, value] pairs, or Headers.");
12
+ };
27
13
  export const headersGlobal = (ctx) => {
28
14
  const builtins = ctx.builtins;
29
15
  const proto = builtins.Headers;
@@ -89,19 +75,15 @@ export const headersGlobal = (ctx) => {
89
75
  return undefined;
90
76
  },
91
77
  ],
92
- ["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").headers.keys())],
93
- ["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").headers.values())],
94
- ["entries", 0, (thisValue) => hostIterator(builtins, self(thisValue, "entries").iterator(builtins))],
78
+ ...iteratorMethods(builtins, self),
95
79
  [
96
80
  "forEach",
97
81
  1,
98
- function* (thisValue, args) {
82
+ (thisValue, args) => {
99
83
  requireArgs("forEach", args, 1);
100
84
  const target = self(thisValue, "forEach");
101
- const apply = applyCollectionCallback(ctx, args[0], "Headers.forEach");
102
- for (const [key, value] of Array.from(target.headers.entries()))
103
- yield* apply([value, key, target], args[1]);
104
- return undefined;
85
+ const callback = requireCallback(args[0], "Headers.forEach");
86
+ return pairsForEach(ctx, target, Array.from(target.headers.entries()), callback, args[1]);
105
87
  },
106
88
  ],
107
89
  ]);
@@ -1,2 +1,2 @@
1
- import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const iteratorGlobals: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
1
+ import type { Interpreter } from "../runtime/interpreter.js";
2
+ export declare const iteratorGlobals: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;