@opencode/codemode 2.1.4 → 2.2.1
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.
- package/README.md +33 -4
- package/dist/codemode.d.ts +9 -7
- package/dist/codemode.js +1 -1
- package/dist/data.d.ts +5 -4
- package/dist/data.js +7 -4
- package/dist/extension.d.ts +17 -4
- package/dist/extension.js +8 -0
- package/dist/interpreter/callback.d.ts +10 -10
- package/dist/interpreter/callback.js +43 -31
- package/dist/interpreter/errors.d.ts +1 -1
- package/dist/interpreter/errors.js +12 -15
- package/dist/interpreter/execute.js +59 -29
- package/dist/interpreter/extensions.d.ts +2 -2
- package/dist/interpreter/extensions.js +46 -18
- package/dist/interpreter/generators.js +162 -19
- package/dist/interpreter/globals.js +12 -9
- package/dist/interpreter/interpreter.d.ts +20 -13
- package/dist/interpreter/interpreter.js +1329 -1147
- package/dist/interpreter/intrinsics.js +1 -2
- package/dist/interpreter/model.d.ts +4 -7
- package/dist/interpreter/model.js +1 -11
- package/dist/interpreter/native.d.ts +7 -8
- package/dist/interpreter/native.js +4 -10
- package/dist/interpreter/objects.d.ts +47 -21
- package/dist/interpreter/objects.js +33 -12
- package/dist/interpreter/promises.d.ts +15 -53
- package/dist/interpreter/promises.js +226 -401
- package/dist/interpreter/runner.d.ts +188 -0
- package/dist/interpreter/runner.js +492 -0
- package/dist/stdlib/array.d.ts +3 -3
- package/dist/stdlib/array.js +106 -102
- package/dist/stdlib/bytes.d.ts +3 -3
- package/dist/stdlib/bytes.js +46 -41
- package/dist/stdlib/collections.d.ts +5 -5
- package/dist/stdlib/collections.js +160 -165
- package/dist/stdlib/date.d.ts +1 -1
- package/dist/stdlib/date.js +28 -20
- package/dist/stdlib/headers.d.ts +1 -1
- package/dist/stdlib/headers.js +14 -19
- package/dist/stdlib/iterator.d.ts +1 -1
- package/dist/stdlib/iterator.js +140 -122
- package/dist/stdlib/json.js +9 -12
- package/dist/stdlib/math.js +10 -10
- package/dist/stdlib/number.d.ts +2 -2
- package/dist/stdlib/object.d.ts +1 -1
- package/dist/stdlib/object.js +21 -22
- package/dist/stdlib/regexp.d.ts +1 -1
- package/dist/stdlib/regexp.js +2 -3
- package/dist/stdlib/string.d.ts +1 -1
- package/dist/stdlib/string.js +18 -20
- package/dist/stdlib/url.d.ts +5 -5
- package/dist/stdlib/url.js +32 -33
- package/dist/stdlib/value.d.ts +2 -2
- package/dist/stdlib/web.d.ts +2 -2
- package/package.json +2 -1
package/dist/stdlib/iterator.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Cause, Effect, Exit } from "effect";
|
|
2
1
|
import { applyCollectionCallback } from "../interpreter/callback.js";
|
|
3
2
|
import { rangeError, typeError } from "../interpreter/model.js";
|
|
4
3
|
import { constructor, fn, methods, receiver, requiresNew } from "../interpreter/native.js";
|
|
5
|
-
import { Arr, coerceToNumber, define, hasPrototype, hidden, IteratorObj, Obj,
|
|
4
|
+
import { Arr, coerceToNumber, define, hasPrototype, hidden, iteratorResult, IteratorObj, Obj, } from "../interpreter/objects.js";
|
|
6
5
|
import { describeValue } from "../interpreter/references.js";
|
|
6
|
+
import { Interruption } from "../interpreter/runner.js";
|
|
7
7
|
const finished = { done: true, value: undefined };
|
|
8
8
|
// Every built-in iterator and helper shares the Iterator prototype; JS gives each collection its own, which is only
|
|
9
9
|
// observable through getPrototypeOf.
|
|
@@ -12,13 +12,22 @@ export const iteratorGlobals = (ctx) => {
|
|
|
12
12
|
const proto = builtins.Iterator;
|
|
13
13
|
const method = (name) => `Iterator.prototype.${name}`;
|
|
14
14
|
// A step that runs program code: when it throws, close the source first, as IteratorClose does.
|
|
15
|
-
const guarded = (source, body)
|
|
16
|
-
|
|
17
|
-
return
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
+
};
|
|
22
31
|
// A lazy helper: `pull` advances it once. Once it reports done, throws, or is closed, it stays done. A callback
|
|
23
32
|
// that re-enters its own helper is a TypeError, as for a running generator.
|
|
24
33
|
const helper = (source, pull, close = source.close) => {
|
|
@@ -30,34 +39,44 @@ export const iteratorGlobals = (ctx) => {
|
|
|
30
39
|
running = true;
|
|
31
40
|
};
|
|
32
41
|
return new IteratorObj(builtins.IteratorHelper, {
|
|
33
|
-
next:
|
|
42
|
+
next: function* () {
|
|
34
43
|
if (done)
|
|
35
|
-
return
|
|
44
|
+
return finished;
|
|
36
45
|
enter();
|
|
37
|
-
|
|
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 {
|
|
38
56
|
running = false;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}),
|
|
43
|
-
close: Effect.suspend(() => {
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
close: function* () {
|
|
44
60
|
if (done)
|
|
45
|
-
return
|
|
61
|
+
return;
|
|
46
62
|
enter();
|
|
47
63
|
done = true;
|
|
48
|
-
|
|
64
|
+
try {
|
|
65
|
+
yield* close();
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
49
68
|
running = false;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
52
71
|
});
|
|
53
72
|
};
|
|
54
73
|
// GetIteratorFlattenable: an iterable by `[Symbol.iterator]`, otherwise the object itself as an iterator.
|
|
55
|
-
const flattenable = (value, name, strings)
|
|
74
|
+
const flattenable = function* (value, name, strings) {
|
|
56
75
|
if (typeof value === "string" ? !strings : !(value instanceof Obj)) {
|
|
57
76
|
throw typeError(`${name} expects an iterable or iterator, received ${describeValue(value)}.`);
|
|
58
77
|
}
|
|
59
78
|
return (yield* ctx.iterate(value)) ?? ctx.iterateDirect(value);
|
|
60
|
-
}
|
|
79
|
+
};
|
|
61
80
|
const limit = (value, name) => {
|
|
62
81
|
const count = Math.trunc(coerceToNumber(value));
|
|
63
82
|
if (Number.isNaN(count) || count < 0) {
|
|
@@ -69,7 +88,10 @@ export const iteratorGlobals = (ctx) => {
|
|
|
69
88
|
[
|
|
70
89
|
"next",
|
|
71
90
|
0,
|
|
72
|
-
|
|
91
|
+
function* (thisValue) {
|
|
92
|
+
const step = yield* receiver(IteratorObj, thisValue, method("next")).cursor.next();
|
|
93
|
+
return iteratorResult(builtins.Object, step.value, step.done);
|
|
94
|
+
},
|
|
73
95
|
],
|
|
74
96
|
[
|
|
75
97
|
"map",
|
|
@@ -78,12 +100,12 @@ export const iteratorGlobals = (ctx) => {
|
|
|
78
100
|
const source = ctx.iterateDirect(thisValue);
|
|
79
101
|
const apply = applyCollectionCallback(ctx, args[0], method("map"));
|
|
80
102
|
let counter = 0;
|
|
81
|
-
return helper(source,
|
|
82
|
-
const step = yield* source.next;
|
|
103
|
+
return helper(source, function* () {
|
|
104
|
+
const step = yield* source.next();
|
|
83
105
|
if (step.done)
|
|
84
106
|
return finished;
|
|
85
107
|
return { done: false, value: yield* guarded(source, apply([step.value, counter++])) };
|
|
86
|
-
})
|
|
108
|
+
});
|
|
87
109
|
},
|
|
88
110
|
],
|
|
89
111
|
[
|
|
@@ -93,15 +115,15 @@ export const iteratorGlobals = (ctx) => {
|
|
|
93
115
|
const source = ctx.iterateDirect(thisValue);
|
|
94
116
|
const apply = applyCollectionCallback(ctx, args[0], method("filter"));
|
|
95
117
|
let counter = 0;
|
|
96
|
-
return helper(source,
|
|
118
|
+
return helper(source, function* () {
|
|
97
119
|
while (true) {
|
|
98
|
-
const step = yield* source.next;
|
|
120
|
+
const step = yield* source.next();
|
|
99
121
|
if (step.done)
|
|
100
122
|
return finished;
|
|
101
123
|
if (yield* guarded(source, apply([step.value, counter++])))
|
|
102
124
|
return step;
|
|
103
125
|
}
|
|
104
|
-
})
|
|
126
|
+
});
|
|
105
127
|
},
|
|
106
128
|
],
|
|
107
129
|
[
|
|
@@ -110,14 +132,14 @@ export const iteratorGlobals = (ctx) => {
|
|
|
110
132
|
(thisValue, args) => {
|
|
111
133
|
const source = ctx.iterateDirect(thisValue);
|
|
112
134
|
let remaining = limit(args[0], "take");
|
|
113
|
-
return helper(source,
|
|
135
|
+
return helper(source, function* () {
|
|
114
136
|
if (remaining === 0) {
|
|
115
|
-
yield* source.close;
|
|
137
|
+
yield* source.close();
|
|
116
138
|
return finished;
|
|
117
139
|
}
|
|
118
140
|
remaining -= 1;
|
|
119
|
-
return yield* source.next;
|
|
120
|
-
})
|
|
141
|
+
return yield* source.next();
|
|
142
|
+
});
|
|
121
143
|
},
|
|
122
144
|
],
|
|
123
145
|
[
|
|
@@ -126,15 +148,15 @@ export const iteratorGlobals = (ctx) => {
|
|
|
126
148
|
(thisValue, args) => {
|
|
127
149
|
const source = ctx.iterateDirect(thisValue);
|
|
128
150
|
let remaining = limit(args[0], "drop");
|
|
129
|
-
return helper(source,
|
|
151
|
+
return helper(source, function* () {
|
|
130
152
|
while (remaining > 0) {
|
|
131
153
|
remaining -= 1;
|
|
132
|
-
const step = yield* source.next;
|
|
154
|
+
const step = yield* source.next();
|
|
133
155
|
if (step.done)
|
|
134
156
|
return finished;
|
|
135
157
|
}
|
|
136
|
-
return yield* source.next;
|
|
137
|
-
})
|
|
158
|
+
return yield* source.next();
|
|
159
|
+
});
|
|
138
160
|
},
|
|
139
161
|
],
|
|
140
162
|
[
|
|
@@ -145,138 +167,131 @@ export const iteratorGlobals = (ctx) => {
|
|
|
145
167
|
const apply = applyCollectionCallback(ctx, args[0], method("flatMap"));
|
|
146
168
|
let counter = 0;
|
|
147
169
|
let inner;
|
|
148
|
-
return helper(source,
|
|
170
|
+
return helper(source, function* () {
|
|
149
171
|
while (true) {
|
|
150
172
|
if (inner !== undefined) {
|
|
151
|
-
const step = yield* guarded(source, inner.next);
|
|
173
|
+
const step = yield* guarded(source, inner.next());
|
|
152
174
|
if (!step.done)
|
|
153
175
|
return step;
|
|
154
176
|
inner = undefined;
|
|
155
177
|
}
|
|
156
|
-
const step = yield* source.next;
|
|
178
|
+
const step = yield* source.next();
|
|
157
179
|
if (step.done)
|
|
158
180
|
return finished;
|
|
159
|
-
|
|
181
|
+
const mapped = yield* guarded(source, apply([step.value, counter++]));
|
|
182
|
+
inner = yield* guarded(source, flattenable(mapped, method("flatMap"), false));
|
|
160
183
|
}
|
|
161
|
-
}
|
|
184
|
+
}, function* () {
|
|
185
|
+
if (inner !== undefined)
|
|
186
|
+
yield* inner.close();
|
|
187
|
+
yield* source.close();
|
|
188
|
+
});
|
|
162
189
|
},
|
|
163
190
|
],
|
|
164
191
|
[
|
|
165
192
|
"reduce",
|
|
166
193
|
1,
|
|
167
|
-
(thisValue, args)
|
|
194
|
+
function* (thisValue, args) {
|
|
168
195
|
const source = ctx.iterateDirect(thisValue);
|
|
169
196
|
const apply = applyCollectionCallback(ctx, args[0], method("reduce"));
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
});
|
|
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
|
+
}
|
|
187
212
|
},
|
|
188
213
|
],
|
|
189
214
|
[
|
|
190
215
|
"toArray",
|
|
191
216
|
0,
|
|
192
|
-
(thisValue)
|
|
217
|
+
function* (thisValue) {
|
|
193
218
|
const source = ctx.iterateDirect(thisValue);
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
});
|
|
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
|
+
}
|
|
203
226
|
},
|
|
204
227
|
],
|
|
205
228
|
[
|
|
206
229
|
"forEach",
|
|
207
230
|
1,
|
|
208
|
-
(thisValue, args)
|
|
231
|
+
function* (thisValue, args) {
|
|
209
232
|
const source = ctx.iterateDirect(thisValue);
|
|
210
233
|
const apply = applyCollectionCallback(ctx, args[0], method("forEach"));
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
});
|
|
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
|
+
}
|
|
220
241
|
},
|
|
221
242
|
],
|
|
222
243
|
[
|
|
223
244
|
"some",
|
|
224
245
|
1,
|
|
225
|
-
(thisValue, args)
|
|
246
|
+
function* (thisValue, args) {
|
|
226
247
|
const source = ctx.iterateDirect(thisValue);
|
|
227
248
|
const apply = applyCollectionCallback(ctx, args[0], method("some"));
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return true;
|
|
237
|
-
}
|
|
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;
|
|
238
257
|
}
|
|
239
|
-
}
|
|
258
|
+
}
|
|
240
259
|
},
|
|
241
260
|
],
|
|
242
261
|
[
|
|
243
262
|
"every",
|
|
244
263
|
1,
|
|
245
|
-
(thisValue, args)
|
|
264
|
+
function* (thisValue, args) {
|
|
246
265
|
const source = ctx.iterateDirect(thisValue);
|
|
247
266
|
const apply = applyCollectionCallback(ctx, args[0], method("every"));
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
return false;
|
|
257
|
-
}
|
|
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;
|
|
258
275
|
}
|
|
259
|
-
}
|
|
276
|
+
}
|
|
260
277
|
},
|
|
261
278
|
],
|
|
262
279
|
[
|
|
263
280
|
"find",
|
|
264
281
|
1,
|
|
265
|
-
(thisValue, args)
|
|
282
|
+
function* (thisValue, args) {
|
|
266
283
|
const source = ctx.iterateDirect(thisValue);
|
|
267
284
|
const apply = applyCollectionCallback(ctx, args[0], method("find"));
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return step.value;
|
|
277
|
-
}
|
|
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;
|
|
278
293
|
}
|
|
279
|
-
}
|
|
294
|
+
}
|
|
280
295
|
},
|
|
281
296
|
],
|
|
282
297
|
]);
|
|
@@ -285,21 +300,24 @@ export const iteratorGlobals = (ctx) => {
|
|
|
285
300
|
[
|
|
286
301
|
"return",
|
|
287
302
|
0,
|
|
288
|
-
|
|
303
|
+
function* (thisValue) {
|
|
304
|
+
yield* receiver(IteratorObj, thisValue, "Iterator.prototype.return").cursor.close();
|
|
305
|
+
return iteratorResult(builtins.Object, undefined, true);
|
|
306
|
+
},
|
|
289
307
|
],
|
|
290
308
|
]);
|
|
291
309
|
const iterator = constructor(builtins, proto, {
|
|
292
310
|
name: "Iterator",
|
|
293
311
|
length: 0,
|
|
294
312
|
call: requiresNew("Iterator"),
|
|
295
|
-
construct: () =>
|
|
313
|
+
construct: () => {
|
|
296
314
|
throw typeError("Iterator is abstract; use Iterator.from(...) or a built-in iterator.");
|
|
297
|
-
}
|
|
315
|
+
},
|
|
298
316
|
});
|
|
299
|
-
define(iterator, "from", fn(builtins, "from", 1, (_, args)
|
|
317
|
+
define(iterator, "from", fn(builtins, "from", 1, function* (_, args) {
|
|
300
318
|
if (args[0] instanceof Obj && hasPrototype(args[0], proto))
|
|
301
319
|
return args[0];
|
|
302
320
|
return new IteratorObj(builtins.IteratorHelper, yield* flattenable(args[0], "Iterator.from", true));
|
|
303
|
-
})
|
|
321
|
+
}), hidden);
|
|
304
322
|
return iterator;
|
|
305
323
|
};
|
package/dist/stdlib/json.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
1
|
import { methods } from "../interpreter/native.js";
|
|
3
2
|
import { applyCollectionCallback } from "../interpreter/callback.js";
|
|
4
3
|
import { checkStringLength } from "../interpreter/limits.js";
|
|
@@ -25,9 +24,9 @@ const parse = (ctx, args) => {
|
|
|
25
24
|
}
|
|
26
25
|
})();
|
|
27
26
|
if (typeofValue(args[1]) !== "function")
|
|
28
|
-
return
|
|
27
|
+
return parsed;
|
|
29
28
|
const apply = applyCollectionCallback(ctx, args[1], "JSON.parse");
|
|
30
|
-
const visit = (holder, key)
|
|
29
|
+
const visit = function* (holder, key) {
|
|
31
30
|
const value = get(holder, key);
|
|
32
31
|
if (value instanceof Obj) {
|
|
33
32
|
for (const name of keys(value)) {
|
|
@@ -39,10 +38,10 @@ const parse = (ctx, args) => {
|
|
|
39
38
|
}
|
|
40
39
|
}
|
|
41
40
|
return yield* apply([key, value], holder);
|
|
42
|
-
}
|
|
41
|
+
};
|
|
43
42
|
return visit(record(ctx.builtins.Object, { "": parsed }), "");
|
|
44
43
|
};
|
|
45
|
-
|
|
44
|
+
function* stringify(ctx, args) {
|
|
46
45
|
const space = args[2];
|
|
47
46
|
const indent = typeof space === "number" || typeof space === "string" ? space : undefined;
|
|
48
47
|
const replacer = args[1];
|
|
@@ -52,10 +51,8 @@ const stringify = (ctx, args) => {
|
|
|
52
51
|
.map(String)
|
|
53
52
|
: null;
|
|
54
53
|
const callback = typeofValue(replacer) === "function" ? applyCollectionCallback(ctx, replacer, "JSON.stringify") : undefined;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
});
|
|
61
|
-
};
|
|
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
|
+
}
|
package/dist/stdlib/math.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
1
|
import { constants, methods } from "../interpreter/native.js";
|
|
3
2
|
import { typeError } from "../interpreter/model.js";
|
|
4
3
|
import { Obj, coerceToNumber } from "../interpreter/objects.js";
|
|
@@ -73,25 +72,26 @@ export const mathGlobal = (ctx) => {
|
|
|
73
72
|
[
|
|
74
73
|
"sumPrecise",
|
|
75
74
|
1,
|
|
76
|
-
(_, args)
|
|
75
|
+
function* (_, args) {
|
|
77
76
|
const cursor = yield* ctx.iterate(args[0]);
|
|
78
77
|
if (cursor === undefined) {
|
|
79
78
|
throw typeError("Math.sumPrecise expects a synchronous iterable.");
|
|
80
79
|
}
|
|
81
80
|
const numbers = [];
|
|
82
81
|
while (true) {
|
|
83
|
-
const step = yield* cursor.next;
|
|
82
|
+
const step = yield* cursor.next();
|
|
84
83
|
if (step.done)
|
|
85
84
|
return Math.sumPrecise(numbers);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
numbers.push(step.value);
|
|
91
|
-
}));
|
|
85
|
+
if (typeof step.value !== "number") {
|
|
86
|
+
yield* preserveConsumerError(cursor.close, rejectNonNumber());
|
|
87
|
+
}
|
|
88
|
+
numbers.push(step.value);
|
|
92
89
|
}
|
|
93
|
-
}
|
|
90
|
+
},
|
|
94
91
|
],
|
|
95
92
|
]);
|
|
96
93
|
return math;
|
|
97
94
|
};
|
|
95
|
+
function* rejectNonNumber() {
|
|
96
|
+
throw typeError("Math.sumPrecise expects an iterable of numbers.");
|
|
97
|
+
}
|
package/dist/stdlib/number.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
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
|
|
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;
|
package/dist/stdlib/object.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ import { Obj, type Value } from "../interpreter/objects.js";
|
|
|
3
3
|
import type { Interpreter } from "../interpreter/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("../interpreter/objects.js").Native;
|
package/dist/stdlib/object.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
1
|
import { constructor, methods, receiver } from "../interpreter/native.js";
|
|
3
2
|
import { AsyncIteratorSymbol, invalidData, IteratorSymbol, typeError } from "../interpreter/model.js";
|
|
4
3
|
import { define, entries, enumerableKeys, getOwn, hasOwn, hasPrototype, hidden, keys, own, ownKeys, Arr, Bytes, Obj, PromiseObj, set, coerceToString, } from "../interpreter/objects.js";
|
|
@@ -44,26 +43,26 @@ export const objectAssign = (ctx, args) => {
|
|
|
44
43
|
}
|
|
45
44
|
return target;
|
|
46
45
|
};
|
|
47
|
-
|
|
46
|
+
function* objectFromEntries(ctx, source) {
|
|
48
47
|
const out = new Obj(ctx.builtins.Object);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
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
|
+
}
|
|
67
66
|
const classTag = (value) => {
|
|
68
67
|
if (value === null)
|
|
69
68
|
return "Null";
|
|
@@ -127,8 +126,8 @@ export const objectGlobal = (ctx) => {
|
|
|
127
126
|
const object = constructor(builtins, builtins.Object, {
|
|
128
127
|
name: "Object",
|
|
129
128
|
length: 1,
|
|
130
|
-
call: (_, args) =>
|
|
131
|
-
construct: (args) =>
|
|
129
|
+
call: (_, args) => construct(args),
|
|
130
|
+
construct: (args) => construct(args),
|
|
132
131
|
});
|
|
133
132
|
methods(builtins, object, [
|
|
134
133
|
[
|
package/dist/stdlib/regexp.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ import type { Interpreter } from "../interpreter/interpreter.js";
|
|
|
4
4
|
export declare const toHostRegex: (arg: Value, method: string, extraFlags?: string) => RegExp;
|
|
5
5
|
export declare const matchToValue: (builtins: Builtins, match: RegExpMatchArray) => Arr;
|
|
6
6
|
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
|
|
7
|
+
export declare const regexpGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
package/dist/stdlib/regexp.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
1
|
import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
|
|
3
2
|
import { syntaxError, typeError } from "../interpreter/model.js";
|
|
4
3
|
import { define, defineAccessor, Arr, Obj, RegExpObj, record, coerceToNumber, coerceToString, } from "../interpreter/objects.js";
|
|
@@ -63,8 +62,8 @@ export const regexpGlobal = (ctx) => {
|
|
|
63
62
|
const regexp = constructor(builtins, proto, {
|
|
64
63
|
name: "RegExp",
|
|
65
64
|
length: 2,
|
|
66
|
-
call: (_, args) =>
|
|
67
|
-
construct: (args, newTarget) =>
|
|
65
|
+
call: (_, args) => constructRegExp(builtins, args),
|
|
66
|
+
construct: (args, newTarget) => constructRegExp(builtins, args, prototypeFrom(newTarget, proto)),
|
|
68
67
|
});
|
|
69
68
|
methods(builtins, regexp, [
|
|
70
69
|
[
|
package/dist/stdlib/string.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
2
|
-
export declare const stringGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
2
|
+
export declare const stringGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|