@opencode/codemode 2.2.6 → 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 +115 -29
  2. package/dist/{vm/async.d.ts → async.d.ts} +1 -1
  3. package/dist/codemode.d.ts +13 -22
  4. package/dist/codemode.js +18 -39
  5. package/dist/data.d.ts +4 -19
  6. package/dist/data.js +1 -82
  7. package/dist/extension.d.ts +20 -1
  8. package/dist/extension.js +14 -1
  9. package/dist/{vm/host.d.ts → host.d.ts} +5 -2
  10. package/dist/{vm/host.js → host.js} +9 -4
  11. package/dist/index.d.ts +1 -1
  12. package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
  13. package/dist/{vm/prelude → prelude}/helpers.js +1 -1
  14. package/dist/{vm/runtime → runtime}/driver.d.ts +18 -8
  15. package/dist/{vm/runtime → runtime}/driver.js +90 -20
  16. package/dist/{vm/runtime → runtime}/errors.js +2 -2
  17. package/dist/{vm/runtime → runtime}/execute.d.ts +18 -9
  18. package/dist/{vm/runtime → runtime}/execute.js +45 -15
  19. package/dist/{vm/runtime → runtime}/extensions.d.ts +2 -2
  20. package/dist/{vm/runtime → runtime}/extensions.js +121 -19
  21. package/dist/{vm/runtime → runtime}/globals.js +1 -1
  22. package/dist/{vm/runtime → runtime}/interpreter.d.ts +4 -1
  23. package/dist/{vm/runtime → runtime}/interpreter.js +2 -0
  24. package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
  25. package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
  26. package/dist/{vm/runtime → runtime}/objects.js +1 -1
  27. package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
  28. package/dist/{vm/runtime → runtime}/operations.js +1 -1
  29. package/dist/scan.js +2 -2
  30. package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +16 -4
  31. package/dist/{vm/scheduler.js → scheduler.js} +13 -2
  32. package/dist/{vm/snapshot → snapshot}/codec.d.ts +37 -6
  33. package/dist/{vm/snapshot → snapshot}/codec.js +112 -8
  34. package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +13 -6
  35. package/dist/{vm/snapshot → snapshot}/snapshot.js +19 -4
  36. package/dist/{vm/snapshot.js → snapshot.js} +5 -2
  37. package/dist/stdlib/array.d.ts +14 -9
  38. package/dist/stdlib/array.js +89 -276
  39. package/dist/stdlib/bytes.d.ts +11 -7
  40. package/dist/stdlib/bytes.js +112 -79
  41. package/dist/stdlib/collections.d.ts +7 -7
  42. package/dist/stdlib/collections.js +30 -339
  43. package/dist/stdlib/console.d.ts +2 -2
  44. package/dist/stdlib/console.js +3 -3
  45. package/dist/stdlib/date.d.ts +2 -2
  46. package/dist/stdlib/date.js +21 -30
  47. package/dist/stdlib/headers.d.ts +2 -2
  48. package/dist/stdlib/headers.js +15 -33
  49. package/dist/stdlib/iterator.d.ts +2 -2
  50. package/dist/stdlib/iterator.js +10 -303
  51. package/dist/stdlib/json.d.ts +2 -2
  52. package/dist/stdlib/json.js +15 -31
  53. package/dist/stdlib/math.d.ts +2 -7
  54. package/dist/stdlib/math.js +6 -30
  55. package/dist/stdlib/number.d.ts +3 -3
  56. package/dist/stdlib/number.js +8 -8
  57. package/dist/stdlib/object.d.ts +4 -4
  58. package/dist/stdlib/object.js +12 -30
  59. package/dist/stdlib/regexp.d.ts +6 -5
  60. package/dist/stdlib/regexp.js +18 -13
  61. package/dist/stdlib/string.d.ts +2 -2
  62. package/dist/stdlib/string.js +53 -53
  63. package/dist/stdlib/url.d.ts +21 -9
  64. package/dist/stdlib/url.js +55 -55
  65. package/dist/stdlib/value.d.ts +3 -4
  66. package/dist/stdlib/value.js +5 -6
  67. package/dist/stdlib/web.d.ts +4 -4
  68. package/dist/stdlib/web.js +7 -7
  69. package/dist/tool-runtime.d.ts +34 -12
  70. package/dist/tool-runtime.js +350 -68
  71. package/dist/tool-schema.d.ts +5 -0
  72. package/dist/tool-schema.js +36 -16
  73. package/dist/tool.d.ts +60 -2
  74. package/dist/tool.js +17 -1
  75. package/dist/{vm/vm.d.ts → vm.d.ts} +4 -3
  76. package/dist/{vm/vm.js → vm.js} +1 -1
  77. package/package.json +5 -7
  78. package/dist/interpreter/callback.d.ts +0 -24
  79. package/dist/interpreter/callback.js +0 -86
  80. package/dist/interpreter/errors.js +0 -173
  81. package/dist/interpreter/execute.d.ts +0 -14
  82. package/dist/interpreter/execute.js +0 -207
  83. package/dist/interpreter/extensions.d.ts +0 -9
  84. package/dist/interpreter/extensions.js +0 -248
  85. package/dist/interpreter/generators.d.ts +0 -3
  86. package/dist/interpreter/generators.js +0 -168
  87. package/dist/interpreter/globals.js +0 -144
  88. package/dist/interpreter/interpreter.d.ts +0 -48
  89. package/dist/interpreter/interpreter.js +0 -2253
  90. package/dist/interpreter/intrinsics.d.ts +0 -15
  91. package/dist/interpreter/intrinsics.js +0 -105
  92. package/dist/interpreter/model.d.ts +0 -68
  93. package/dist/interpreter/model.js +0 -54
  94. package/dist/interpreter/native.d.ts +0 -17
  95. package/dist/interpreter/native.js +0 -35
  96. package/dist/interpreter/objects.d.ts +0 -301
  97. package/dist/interpreter/objects.js +0 -592
  98. package/dist/interpreter/promises.d.ts +0 -18
  99. package/dist/interpreter/promises.js +0 -292
  100. package/dist/interpreter/runner.d.ts +0 -188
  101. package/dist/interpreter/runner.js +0 -492
  102. package/dist/interpreter/scope.d.ts +0 -16
  103. package/dist/interpreter/scope.js +0 -79
  104. package/dist/suspend-error.d.ts +0 -23
  105. package/dist/suspend-error.js +0 -25
  106. package/dist/vm/data.d.ts +0 -7
  107. package/dist/vm/data.js +0 -13
  108. package/dist/vm/index.d.ts +0 -6
  109. package/dist/vm/index.js +0 -6
  110. package/dist/vm/runtime/errors.d.ts +0 -23
  111. package/dist/vm/runtime/globals.d.ts +0 -6
  112. package/dist/vm/runtime/limits.d.ts +0 -10
  113. package/dist/vm/runtime/limits.js +0 -19
  114. package/dist/vm/runtime/modules.d.ts +0 -4
  115. package/dist/vm/runtime/modules.js +0 -33
  116. package/dist/vm/runtime/references.d.ts +0 -8
  117. package/dist/vm/runtime/references.js +0 -51
  118. package/dist/vm/stdlib/array.d.ts +0 -16
  119. package/dist/vm/stdlib/array.js +0 -311
  120. package/dist/vm/stdlib/bytes.d.ts +0 -11
  121. package/dist/vm/stdlib/bytes.js +0 -222
  122. package/dist/vm/stdlib/collections.d.ts +0 -7
  123. package/dist/vm/stdlib/collections.js +0 -188
  124. package/dist/vm/stdlib/console.d.ts +0 -6
  125. package/dist/vm/stdlib/console.js +0 -93
  126. package/dist/vm/stdlib/date.d.ts +0 -2
  127. package/dist/vm/stdlib/date.js +0 -123
  128. package/dist/vm/stdlib/headers.d.ts +0 -2
  129. package/dist/vm/stdlib/headers.js +0 -92
  130. package/dist/vm/stdlib/iterator.d.ts +0 -2
  131. package/dist/vm/stdlib/iterator.js +0 -30
  132. package/dist/vm/stdlib/json.d.ts +0 -3
  133. package/dist/vm/stdlib/json.js +0 -42
  134. package/dist/vm/stdlib/math.d.ts +0 -3
  135. package/dist/vm/stdlib/math.js +0 -73
  136. package/dist/vm/stdlib/number.d.ts +0 -3
  137. package/dist/vm/stdlib/number.js +0 -72
  138. package/dist/vm/stdlib/object.d.ts +0 -6
  139. package/dist/vm/stdlib/object.js +0 -201
  140. package/dist/vm/stdlib/regexp.d.ts +0 -8
  141. package/dist/vm/stdlib/regexp.js +0 -119
  142. package/dist/vm/stdlib/string.d.ts +0 -2
  143. package/dist/vm/stdlib/string.js +0 -272
  144. package/dist/vm/stdlib/url.d.ts +0 -25
  145. package/dist/vm/stdlib/url.js +0 -255
  146. package/dist/vm/stdlib/value.d.ts +0 -6
  147. package/dist/vm/stdlib/value.js +0 -32
  148. package/dist/vm/stdlib/web.d.ts +0 -5
  149. package/dist/vm/stdlib/web.js +0 -97
  150. /package/dist/{vm/async.js → async.js} +0 -0
  151. /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
  152. /package/dist/{vm/code.js → code.js} +0 -0
  153. /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
  154. /package/dist/{vm/compiler → compiler}/compile.js +0 -0
  155. /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
  156. /package/dist/{vm/compiler → compiler}/scope.js +0 -0
  157. /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
  158. /package/dist/{vm/generators.js → generators.js} +0 -0
  159. /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
  160. /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
  161. /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
  162. /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
  163. /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
  164. /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
  165. /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
  166. /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
  167. /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
  168. /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
  169. /package/dist/{vm/prelude → prelude}/source.js +0 -0
  170. /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
  171. /package/dist/{vm/runtime → runtime}/callback.js +0 -0
  172. /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
  173. /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
  174. /package/dist/{vm/runtime → runtime}/generators.js +0 -0
  175. /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
  176. /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
  177. /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
  178. /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
  179. /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
  180. /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
  181. /package/dist/{interpreter → runtime}/limits.js +0 -0
  182. /package/dist/{vm/runtime → runtime}/model.js +0 -0
  183. /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
  184. /package/dist/{interpreter → runtime}/modules.js +0 -0
  185. /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
  186. /package/dist/{vm/runtime → runtime}/native.js +0 -0
  187. /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
  188. /package/dist/{vm/runtime → runtime}/promises.js +0 -0
  189. /package/dist/{interpreter → runtime}/references.d.ts +0 -0
  190. /package/dist/{interpreter → runtime}/references.js +0 -0
  191. /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
  192. /package/dist/{vm/runtime → runtime}/signals.js +0 -0
  193. /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
  194. /package/dist/{vm/runtime → runtime}/source.js +0 -0
  195. /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
  196. /package/dist/{vm/runtime → runtime}/table.js +0 -0
  197. /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
@@ -1,312 +1,25 @@
1
- import { applyCollectionCallback } from "../interpreter/callback.js";
2
- import { rangeError, typeError } from "../interpreter/model.js";
3
- import { constructor, fn, methods, receiver, requiresNew } from "../interpreter/native.js";
4
- import { Arr, coerceToNumber, define, hasPrototype, hidden, iteratorResult, IteratorObj, Obj, } from "../interpreter/objects.js";
5
- import { describeValue } from "../interpreter/references.js";
6
- import { Interruption } from "../interpreter/runner.js";
7
- const finished = { done: true, value: undefined };
1
+ import { typeError } from "../runtime/model.js";
2
+ import { constructor, methods, receiver, requiresNew } from "../runtime/native.js";
3
+ import { iteratorResult, IteratorObj, Position } from "../runtime/objects.js";
8
4
  // Every built-in iterator and helper shares the Iterator prototype; JS gives each collection its own, which is only
9
- // observable through getPrototypeOf.
5
+ // observable through getPrototypeOf. The helpers (`map`, `take`, `Iterator.from`, ...) are the prelude's.
10
6
  export const iteratorGlobals = (ctx) => {
11
7
  const builtins = ctx.builtins;
12
8
  const proto = builtins.Iterator;
13
- const method = (name) => `Iterator.prototype.${name}`;
14
- // A step that runs program code: when it throws, close the source first, as IteratorClose does.
15
- const guarded = function* (source, body) {
16
- try {
17
- return yield* body;
18
- }
19
- catch (error) {
20
- if (error instanceof Interruption)
21
- throw error;
22
- try {
23
- yield* source.close();
24
- }
25
- catch {
26
- // The step's failure wins over a failing close.
27
- }
28
- throw error;
29
- }
30
- };
31
- // A lazy helper: `pull` advances it once. Once it reports done, throws, or is closed, it stays done. A callback
32
- // that re-enters its own helper is a TypeError, as for a running generator.
33
- const helper = (source, pull, close = source.close) => {
34
- let done = false;
35
- let running = false;
36
- const enter = () => {
37
- if (running)
38
- throw typeError("Iterator helper is already running.");
39
- running = true;
40
- };
41
- return new IteratorObj(builtins.IteratorHelper, {
42
- next: function* () {
43
- if (done)
44
- return finished;
45
- enter();
46
- try {
47
- const step = yield* pull();
48
- done = step.done;
49
- return step;
50
- }
51
- catch (error) {
52
- done = true;
53
- throw error;
54
- }
55
- finally {
56
- running = false;
57
- }
58
- },
59
- close: function* () {
60
- if (done)
61
- return;
62
- enter();
63
- done = true;
64
- try {
65
- yield* close();
66
- }
67
- finally {
68
- running = false;
69
- }
70
- },
71
- });
72
- };
73
- // GetIteratorFlattenable: an iterable by `[Symbol.iterator]`, otherwise the object itself as an iterator.
74
- const flattenable = function* (value, name, strings) {
75
- if (typeof value === "string" ? !strings : !(value instanceof Obj)) {
76
- throw typeError(`${name} expects an iterable or iterator, received ${describeValue(value)}.`);
77
- }
78
- return (yield* ctx.iterate(value)) ?? ctx.iterateDirect(value);
79
- };
80
- const limit = (value, name) => {
81
- const count = Math.trunc(coerceToNumber(value));
82
- if (Number.isNaN(count) || count < 0) {
83
- throw rangeError(`${method(name)} expects a non-negative count, received ${count}.`);
84
- }
85
- return count;
86
- };
87
9
  methods(builtins, proto, [
88
10
  [
89
11
  "next",
90
12
  0,
91
- function* (thisValue) {
92
- const step = yield* receiver(IteratorObj, thisValue, method("next")).cursor.next();
13
+ (thisValue) => {
14
+ const cursor = receiver(IteratorObj, thisValue, "Iterator.prototype.next").cursor;
15
+ if (!(cursor instanceof Position))
16
+ throw typeError("Iterator.prototype.next called on an internal iterator.");
17
+ const step = cursor.step();
93
18
  return iteratorResult(builtins.Object, step.value, step.done);
94
19
  },
95
20
  ],
96
- [
97
- "map",
98
- 1,
99
- (thisValue, args) => {
100
- const source = ctx.iterateDirect(thisValue);
101
- const apply = applyCollectionCallback(ctx, args[0], method("map"));
102
- let counter = 0;
103
- return helper(source, function* () {
104
- const step = yield* source.next();
105
- if (step.done)
106
- return finished;
107
- return { done: false, value: yield* guarded(source, apply([step.value, counter++])) };
108
- });
109
- },
110
- ],
111
- [
112
- "filter",
113
- 1,
114
- (thisValue, args) => {
115
- const source = ctx.iterateDirect(thisValue);
116
- const apply = applyCollectionCallback(ctx, args[0], method("filter"));
117
- let counter = 0;
118
- return helper(source, function* () {
119
- while (true) {
120
- const step = yield* source.next();
121
- if (step.done)
122
- return finished;
123
- if (yield* guarded(source, apply([step.value, counter++])))
124
- return step;
125
- }
126
- });
127
- },
128
- ],
129
- [
130
- "take",
131
- 1,
132
- (thisValue, args) => {
133
- const source = ctx.iterateDirect(thisValue);
134
- let remaining = limit(args[0], "take");
135
- return helper(source, function* () {
136
- if (remaining === 0) {
137
- yield* source.close();
138
- return finished;
139
- }
140
- remaining -= 1;
141
- return yield* source.next();
142
- });
143
- },
144
- ],
145
- [
146
- "drop",
147
- 1,
148
- (thisValue, args) => {
149
- const source = ctx.iterateDirect(thisValue);
150
- let remaining = limit(args[0], "drop");
151
- return helper(source, function* () {
152
- while (remaining > 0) {
153
- remaining -= 1;
154
- const step = yield* source.next();
155
- if (step.done)
156
- return finished;
157
- }
158
- return yield* source.next();
159
- });
160
- },
161
- ],
162
- [
163
- "flatMap",
164
- 1,
165
- (thisValue, args) => {
166
- const source = ctx.iterateDirect(thisValue);
167
- const apply = applyCollectionCallback(ctx, args[0], method("flatMap"));
168
- let counter = 0;
169
- let inner;
170
- return helper(source, function* () {
171
- while (true) {
172
- if (inner !== undefined) {
173
- const step = yield* guarded(source, inner.next());
174
- if (!step.done)
175
- return step;
176
- inner = undefined;
177
- }
178
- const step = yield* source.next();
179
- if (step.done)
180
- return finished;
181
- const mapped = yield* guarded(source, apply([step.value, counter++]));
182
- inner = yield* guarded(source, flattenable(mapped, method("flatMap"), false));
183
- }
184
- }, function* () {
185
- if (inner !== undefined)
186
- yield* inner.close();
187
- yield* source.close();
188
- });
189
- },
190
- ],
191
- [
192
- "reduce",
193
- 1,
194
- function* (thisValue, args) {
195
- const source = ctx.iterateDirect(thisValue);
196
- const apply = applyCollectionCallback(ctx, args[0], method("reduce"));
197
- let counter = 0;
198
- let accumulator = args[1];
199
- if (args.length < 2) {
200
- const first = yield* source.next();
201
- if (first.done)
202
- throw typeError("Iterator.prototype.reduce of an empty iterator with no initial value.");
203
- accumulator = first.value;
204
- counter = 1;
205
- }
206
- while (true) {
207
- const step = yield* source.next();
208
- if (step.done)
209
- return accumulator;
210
- accumulator = yield* guarded(source, apply([accumulator, step.value, counter++]));
211
- }
212
- },
213
- ],
214
- [
215
- "toArray",
216
- 0,
217
- function* (thisValue) {
218
- const source = ctx.iterateDirect(thisValue);
219
- const items = [];
220
- while (true) {
221
- const step = yield* source.next();
222
- if (step.done)
223
- return new Arr(builtins.Array, items);
224
- items.push(step.value);
225
- }
226
- },
227
- ],
228
- [
229
- "forEach",
230
- 1,
231
- function* (thisValue, args) {
232
- const source = ctx.iterateDirect(thisValue);
233
- const apply = applyCollectionCallback(ctx, args[0], method("forEach"));
234
- let counter = 0;
235
- while (true) {
236
- const step = yield* source.next();
237
- if (step.done)
238
- return undefined;
239
- yield* guarded(source, apply([step.value, counter++]));
240
- }
241
- },
242
- ],
243
- [
244
- "some",
245
- 1,
246
- function* (thisValue, args) {
247
- const source = ctx.iterateDirect(thisValue);
248
- const apply = applyCollectionCallback(ctx, args[0], method("some"));
249
- let counter = 0;
250
- while (true) {
251
- const step = yield* source.next();
252
- if (step.done)
253
- return false;
254
- if (yield* guarded(source, apply([step.value, counter++]))) {
255
- yield* source.close();
256
- return true;
257
- }
258
- }
259
- },
260
- ],
261
- [
262
- "every",
263
- 1,
264
- function* (thisValue, args) {
265
- const source = ctx.iterateDirect(thisValue);
266
- const apply = applyCollectionCallback(ctx, args[0], method("every"));
267
- let counter = 0;
268
- while (true) {
269
- const step = yield* source.next();
270
- if (step.done)
271
- return true;
272
- if (!(yield* guarded(source, apply([step.value, counter++])))) {
273
- yield* source.close();
274
- return false;
275
- }
276
- }
277
- },
278
- ],
279
- [
280
- "find",
281
- 1,
282
- function* (thisValue, args) {
283
- const source = ctx.iterateDirect(thisValue);
284
- const apply = applyCollectionCallback(ctx, args[0], method("find"));
285
- let counter = 0;
286
- while (true) {
287
- const step = yield* source.next();
288
- if (step.done)
289
- return undefined;
290
- if (yield* guarded(source, apply([step.value, counter++]))) {
291
- yield* source.close();
292
- return step.value;
293
- }
294
- }
295
- },
296
- ],
297
- ]);
298
- // Helpers and `Iterator.from` wrappers can be closed; collection iterators, as in JS, cannot.
299
- methods(builtins, builtins.IteratorHelper, [
300
- [
301
- "return",
302
- 0,
303
- function* (thisValue) {
304
- yield* receiver(IteratorObj, thisValue, "Iterator.prototype.return").cursor.close();
305
- return iteratorResult(builtins.Object, undefined, true);
306
- },
307
- ],
308
21
  ]);
309
- const iterator = constructor(builtins, proto, {
22
+ return constructor(builtins, proto, {
310
23
  name: "Iterator",
311
24
  length: 0,
312
25
  call: requiresNew("Iterator"),
@@ -314,10 +27,4 @@ export const iteratorGlobals = (ctx) => {
314
27
  throw typeError("Iterator is abstract; use Iterator.from(...) or a built-in iterator.");
315
28
  },
316
29
  });
317
- define(iterator, "from", fn(builtins, "from", 1, function* (_, args) {
318
- if (args[0] instanceof Obj && hasPrototype(args[0], proto))
319
- return args[0];
320
- return new IteratorObj(builtins.IteratorHelper, yield* flattenable(args[0], "Iterator.from", true));
321
- }), hidden);
322
- return iterator;
323
30
  };
@@ -1,3 +1,3 @@
1
- import type { Interpreter } from "../interpreter/interpreter.js";
2
- import { Obj } from "../interpreter/objects.js";
1
+ import type { Interpreter } from "../runtime/interpreter.js";
2
+ import { Obj } from "../runtime/objects.js";
3
3
  export declare const jsonGlobal: <R>(ctx: Interpreter<R>) => Obj;
@@ -1,15 +1,15 @@
1
- import { methods } from "../interpreter/native.js";
2
- import { applyCollectionCallback } from "../interpreter/callback.js";
3
- import { checkStringLength } from "../interpreter/limits.js";
4
- import { syntaxError } from "../interpreter/model.js";
5
- import { typeofValue } from "../interpreter/references.js";
6
- import { fromJson, toJson } from "../data.js";
7
- import { get, keys, Arr, Obj, coerceToString, record, remove, set } from "../interpreter/objects.js";
1
+ import { methods } from "../runtime/native.js";
2
+ import { requireCallback } from "../runtime/callback.js";
3
+ import { syntaxError } from "../runtime/model.js";
4
+ import { typeofValue } from "../runtime/references.js";
5
+ import { fromJson } from "../data.js";
6
+ import { Arr, Obj, coerceToString, record } from "../runtime/objects.js";
7
+ import { Invoke, Push } from "../runtime/signals.js";
8
8
  export const jsonGlobal = (ctx) => {
9
9
  const json = new Obj(ctx.builtins.Object);
10
10
  methods(ctx.builtins, json, [
11
11
  ["parse", 2, (_, args) => parse(ctx, args)],
12
- ["stringify", 3, (_, args) => stringify(ctx, args)],
12
+ ["stringify", 3, (_, args) => stringify(args)],
13
13
  ]);
14
14
  return json;
15
15
  };
@@ -25,23 +25,10 @@ const parse = (ctx, args) => {
25
25
  })();
26
26
  if (typeofValue(args[1]) !== "function")
27
27
  return parsed;
28
- const apply = applyCollectionCallback(ctx, args[1], "JSON.parse");
29
- const visit = function* (holder, key) {
30
- const value = get(holder, key);
31
- if (value instanceof Obj) {
32
- for (const name of keys(value)) {
33
- const revived = yield* visit(value, name);
34
- if (revived === undefined)
35
- remove(value, name);
36
- else
37
- set(value, name, revived);
38
- }
39
- }
40
- return yield* apply([key, value], holder);
41
- };
42
- return visit(record(ctx.builtins.Object, { "": parsed }), "");
28
+ const reviver = requireCallback(args[1], "JSON.parse");
29
+ return new Invoke(ctx.prelude.fn("revive"), undefined, [record(ctx.builtins.Object, { "": parsed }), "", reviver], undefined);
43
30
  };
44
- function* stringify(ctx, args) {
31
+ const stringify = (args) => {
45
32
  const space = args[2];
46
33
  const indent = typeof space === "number" || typeof space === "string" ? space : undefined;
47
34
  const replacer = args[1];
@@ -49,10 +36,7 @@ function* stringify(ctx, args) {
49
36
  ? replacer.items
50
37
  .filter((item) => typeof item === "string" || typeof item === "number")
51
38
  .map(String)
52
- : null;
53
- const callback = typeofValue(replacer) === "function" ? applyCollectionCallback(ctx, replacer, "JSON.stringify") : undefined;
54
- const text = JSON.stringify(yield* toJson(ctx, args[0], callback), properties, indent);
55
- if (text !== undefined)
56
- checkStringLength(text.length);
57
- return text;
58
- }
39
+ : undefined;
40
+ const callback = typeofValue(replacer) === "function" ? requireCallback(replacer, "JSON.stringify") : undefined;
41
+ return new Push("JSON.stringify", [args[0], callback, properties, indent]);
42
+ };
@@ -1,8 +1,3 @@
1
- import { Obj } from "../interpreter/objects.js";
2
- import type { Interpreter } from "../interpreter/interpreter.js";
3
- declare global {
4
- interface Math {
5
- sumPrecise(values: Iterable<number>): number;
6
- }
7
- }
1
+ import { Obj } from "../runtime/objects.js";
2
+ import type { Interpreter } from "../runtime/interpreter.js";
8
3
  export declare const mathGlobal: <R>(ctx: Interpreter<R>) => Obj;
@@ -1,7 +1,6 @@
1
- import { constants, methods } from "../interpreter/native.js";
2
- import { typeError } from "../interpreter/model.js";
3
- import { Obj, coerceToNumber } from "../interpreter/objects.js";
4
- import { preserveConsumerError, withPrimitives } from "../interpreter/callback.js";
1
+ import { constants, methods } from "../runtime/native.js";
2
+ import { Obj, coerceToNumber } from "../runtime/objects.js";
3
+ import { withPrimitives } from "../runtime/callback.js";
5
4
  export const mathGlobal = (ctx) => {
6
5
  const builtins = ctx.builtins;
7
6
  const math = new Obj(builtins.Object);
@@ -10,17 +9,17 @@ export const mathGlobal = (ctx) => {
10
9
  const unary = (name, op) => [
11
10
  name,
12
11
  1,
13
- (_, args) => withPrimitives(ctx, "number", [args[0]], ([a]) => op(coerceToNumber(a))),
12
+ (_, args) => withPrimitives("number", [args[0]], ([a]) => op(coerceToNumber(a))),
14
13
  ];
15
14
  const binary = (name, op) => [
16
15
  name,
17
16
  2,
18
- (_, args) => withPrimitives(ctx, "number", [args[0], args[1]], ([a, b]) => op(coerceToNumber(a), coerceToNumber(b))),
17
+ (_, args) => withPrimitives("number", [args[0], args[1]], ([a, b]) => op(coerceToNumber(a), coerceToNumber(b))),
19
18
  ];
20
19
  const variadic = (name, op) => [
21
20
  name,
22
21
  2,
23
- (_, args) => withPrimitives(ctx, "number", args, (values) => op(...values.map(coerceToNumber))),
22
+ (_, args) => withPrimitives("number", args, (values) => op(...values.map(coerceToNumber))),
24
23
  ];
25
24
  constants(math, {
26
25
  PI: Math.PI,
@@ -69,29 +68,6 @@ export const mathGlobal = (ctx) => {
69
68
  unary("fround", Math.fround),
70
69
  unary("clz32", Math.clz32),
71
70
  binary("imul", Math.imul),
72
- [
73
- "sumPrecise",
74
- 1,
75
- function* (_, args) {
76
- const cursor = yield* ctx.iterate(args[0]);
77
- if (cursor === undefined) {
78
- throw typeError("Math.sumPrecise expects a synchronous iterable.");
79
- }
80
- const numbers = [];
81
- while (true) {
82
- const step = yield* cursor.next();
83
- if (step.done)
84
- return Math.sumPrecise(numbers);
85
- if (typeof step.value !== "number") {
86
- yield* preserveConsumerError(cursor.close, rejectNonNumber());
87
- }
88
- numbers.push(step.value);
89
- }
90
- },
91
- ],
92
71
  ]);
93
72
  return math;
94
73
  };
95
- function* rejectNonNumber() {
96
- throw typeError("Math.sumPrecise expects an iterable of numbers.");
97
- }
@@ -1,3 +1,3 @@
1
- import type { Interpreter } from "../interpreter/interpreter.js";
2
- export declare const numberGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
3
- export declare const booleanGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
1
+ import type { Interpreter } from "../runtime/interpreter.js";
2
+ export declare const numberGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
3
+ export declare const booleanGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -1,14 +1,14 @@
1
- import { constructor, constants, methods } from "../interpreter/native.js";
2
- import { coerceToNumber } from "../interpreter/objects.js";
3
- import { typeError } from "../interpreter/model.js";
4
- import { withPrimitives } from "../interpreter/callback.js";
5
- import { coerce, coercion } from "./value.js";
1
+ import { constructor, constants, methods } from "../runtime/native.js";
2
+ import { coerceToNumber } from "../runtime/objects.js";
3
+ import { typeError } from "../runtime/model.js";
4
+ import { withPrimitives } from "../runtime/callback.js";
5
+ import { coerce } from "./value.js";
6
6
  export const numberGlobal = (ctx) => {
7
7
  const builtins = ctx.builtins;
8
8
  const number = constructor(builtins, builtins.Number, {
9
9
  name: "Number",
10
10
  length: 1,
11
- call: coercion(ctx, "Number").call,
11
+ call: (_, args) => coerce(ctx, "Number", args),
12
12
  });
13
13
  constants(number, {
14
14
  MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER,
@@ -39,7 +39,7 @@ export const numberGlobal = (ctx) => {
39
39
  1,
40
40
  (thisValue, args) => {
41
41
  const value = self(thisValue, name);
42
- return withPrimitives(ctx, "number", [args[0]], ([digits]) => op(value, digits === undefined ? undefined : coerceToNumber(digits)));
42
+ return withPrimitives("number", [args[0]], ([digits]) => op(value, digits === undefined ? undefined : coerceToNumber(digits)));
43
43
  },
44
44
  ];
45
45
  methods(builtins, builtins.Number, [
@@ -57,7 +57,7 @@ export const booleanGlobal = (ctx) => {
57
57
  const boolean = constructor(builtins, builtins.Boolean, {
58
58
  name: "Boolean",
59
59
  length: 1,
60
- call: coercion(ctx, "Boolean").call,
60
+ call: (_, args) => coerce(ctx, "Boolean", args),
61
61
  });
62
62
  const self = (thisValue, name) => {
63
63
  if (typeof thisValue === "boolean")
@@ -1,6 +1,6 @@
1
- import { type AstNode } from "../interpreter/model.js";
2
- import { Obj, type Value } from "../interpreter/objects.js";
3
- import type { Interpreter } from "../interpreter/interpreter.js";
1
+ import { type AstNode } from "../runtime/model.js";
2
+ import { Obj, type Value } from "../runtime/objects.js";
3
+ import type { Interpreter } from "../runtime/interpreter.js";
4
4
  export declare const enumerableSource: <R>(ctx: Interpreter<R>, label: string, value: Value, node?: AstNode) => Obj;
5
5
  export declare const objectAssign: <R>(ctx: Interpreter<R>, args: Array<Value>) => Value;
6
- export declare const objectGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
6
+ export declare const objectGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -1,11 +1,10 @@
1
- import { constructor, methods, receiver } from "../interpreter/native.js";
2
- import { AsyncIteratorSymbol, invalidData, IteratorSymbol, typeError } from "../interpreter/model.js";
3
- import { define, entries, enumerableKeys, getOwn, hasOwn, hasPrototype, hidden, keys, own, ownKeys, Arr, Bytes, Obj, PromiseObj, set, coerceToString, } from "../interpreter/objects.js";
4
- import { primitivePrototype } from "../interpreter/intrinsics.js";
5
- import { containsOpaqueReference, describeValue, rejectCircularInsertion } from "../interpreter/references.js";
6
- import { invoke, preserveConsumerError } from "../interpreter/callback.js";
1
+ import { constructor, methods, receiver } from "../runtime/native.js";
2
+ import { AsyncIteratorSymbol, invalidData, IteratorSymbol, typeError } from "../runtime/model.js";
3
+ import { changed, entries, enumerableKeys, getOwn, hasOwn, hasPrototype, keys, own, ownKeys, Arr, Bytes, Obj, PromiseObj, set, coerceToString, } from "../runtime/objects.js";
4
+ import { primitivePrototype } from "../runtime/intrinsics.js";
5
+ import { describeValue, rejectCircularInsertion } from "../runtime/references.js";
6
+ import { invokeMethod } from "../runtime/callback.js";
7
7
  import { ToolReference } from "../tool-runtime.js";
8
- import { groupBy } from "./collections.js";
9
8
  // ToObject for enumeration.
10
9
  export const enumerableSource = (ctx, label, value, node) => {
11
10
  if (value === null || value === undefined) {
@@ -43,26 +42,6 @@ export const objectAssign = (ctx, args) => {
43
42
  }
44
43
  return target;
45
44
  };
46
- function* objectFromEntries(ctx, source) {
47
- const out = new Obj(ctx.builtins.Object);
48
- const cursor = yield* ctx.iterate(source);
49
- if (cursor === undefined) {
50
- throw typeError("Object.fromEntries expects a synchronous iterable of entries.");
51
- }
52
- while (true) {
53
- const step = yield* cursor.next();
54
- if (step.done)
55
- return out;
56
- yield* preserveConsumerError(cursor.close, defineEntry(out, step.value));
57
- }
58
- }
59
- // A step, so a rejected entry closes the source iterator.
60
- function* defineEntry(out, entry) {
61
- if (!(entry instanceof Obj) || containsOpaqueReference(entry)) {
62
- throw typeError("Object.fromEntries expects [key, value] entry objects.");
63
- }
64
- define(out, coerceToString(getOwn(entry, 0)), getOwn(entry, 1));
65
- }
66
45
  const classTag = (value) => {
67
46
  if (value === null)
68
47
  return "Null";
@@ -84,6 +63,7 @@ const propertyKey = (value) => value === AsyncIteratorSymbol || value === Iterat
84
63
  const restrict = (level, value) => {
85
64
  if (!(value instanceof Obj))
86
65
  return value;
66
+ changed(value);
87
67
  value.extensible = false;
88
68
  if (level === "preventExtensions")
89
69
  return value;
@@ -150,7 +130,6 @@ export const objectGlobal = (ctx) => {
150
130
  ["hasOwn", 2, (_, args) => hasOwn(enumerableSource(ctx, "Object.hasOwn(...)", args[0]), propertyKey(args[1]))],
151
131
  ["is", 2, (_, args) => Object.is(args[0], args[1])],
152
132
  ["assign", 2, (_, args) => objectAssign(ctx, args)],
153
- ["fromEntries", 1, (_, args) => objectFromEntries(ctx, args[0])],
154
133
  ["freeze", 1, (_, args) => restrict("freeze", args[0])],
155
134
  ["seal", 1, (_, args) => restrict("seal", args[0])],
156
135
  ["preventExtensions", 1, (_, args) => restrict("preventExtensions", args[0])],
@@ -184,7 +163,6 @@ export const objectGlobal = (ctx) => {
184
163
  },
185
164
  ],
186
165
  ]);
187
- define(object, "groupBy", groupBy(ctx, "Object"), hidden);
188
166
  methods(builtins, builtins.Object, [
189
167
  [
190
168
  "hasOwnProperty",
@@ -203,7 +181,11 @@ export const objectGlobal = (ctx) => {
203
181
  true,
204
182
  ],
205
183
  ["toString", 0, (thisValue) => `[object ${classTag(thisValue)}]`],
206
- ["toLocaleString", 0, (thisValue) => invoke(ctx, thisValue, "toString", "Object.prototype.toLocaleString")],
184
+ [
185
+ "toLocaleString",
186
+ 0,
187
+ (thisValue) => invokeMethod(ctx.builtins, thisValue, "toString", "Object.prototype.toLocaleString"),
188
+ ],
207
189
  [
208
190
  "valueOf",
209
191
  0,
@@ -1,7 +1,8 @@
1
- import type { Builtins } from "../interpreter/intrinsics.js";
2
- import { Arr, Obj, RegExpObj, type Value } from "../interpreter/objects.js";
3
- import type { Interpreter } from "../interpreter/interpreter.js";
4
- export declare const toHostRegex: (arg: Value, method: string, extraFlags?: string) => RegExp;
1
+ import type { Builtins } from "../runtime/intrinsics.js";
2
+ import { Arr, Obj, RegExpObj, type Value } from "../runtime/objects.js";
3
+ import type { Interpreter } from "../runtime/interpreter.js";
4
+ /** Runs host regex work on a RegExp argument, sharing its `lastIndex`, or on a pattern string compiled for the call. */
5
+ export declare const withHostRegex: <T>(arg: Value, method: string, extraFlags: string, work: (regex: RegExp) => T) => T;
5
6
  export declare const matchToValue: (builtins: Builtins, match: RegExpMatchArray) => Arr;
6
7
  export declare const constructRegExp: (builtins: Builtins, args: Array<Value>, proto?: Obj) => RegExpObj;
7
- export declare const regexpGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
8
+ export declare const regexpGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;