@opencode/codemode 0.0.0-dev-19566 → 0.0.0-dev-19568
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/dist/interpreter/errors.d.ts +8 -1
- package/dist/interpreter/errors.js +50 -33
- package/dist/interpreter/execute.js +2 -2
- package/dist/interpreter/generators.js +3 -3
- package/dist/interpreter/globals.js +6 -6
- package/dist/interpreter/model.d.ts +22 -13
- package/dist/interpreter/model.js +18 -13
- package/dist/interpreter/native.d.ts +4 -5
- package/dist/interpreter/native.js +9 -8
- package/dist/interpreter/objects.d.ts +7 -5
- package/dist/interpreter/objects.js +3 -1
- package/dist/interpreter/promises.d.ts +2 -4
- package/dist/interpreter/promises.js +46 -46
- package/dist/interpreter/references.d.ts +1 -2
- package/dist/interpreter/references.js +3 -3
- package/dist/interpreter/runner.d.ts +6 -7
- package/dist/interpreter/runner.js +10 -10
- package/dist/interpreter/runtime.js +86 -79
- package/dist/interpreter/scope.js +6 -6
- package/dist/stdlib/array.d.ts +1 -2
- package/dist/stdlib/array.js +80 -84
- package/dist/stdlib/collections.js +57 -57
- package/dist/stdlib/date.js +21 -21
- package/dist/stdlib/json.js +13 -13
- package/dist/stdlib/math.js +11 -15
- package/dist/stdlib/number.js +21 -25
- package/dist/stdlib/object.d.ts +2 -2
- package/dist/stdlib/object.js +32 -36
- package/dist/stdlib/regexp.d.ts +2 -3
- package/dist/stdlib/regexp.js +15 -15
- package/dist/stdlib/string.js +69 -65
- package/dist/stdlib/url.js +55 -55
- package/dist/stdlib/value.js +4 -4
- package/dist/stdlib/web.js +4 -4
- package/package.json +1 -1
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { constructor, fn, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
|
|
3
|
-
import {
|
|
3
|
+
import { invalidData, typeError } from "../interpreter/model.js";
|
|
4
4
|
import { define, defineAccessor, get, getOwn, hidden, isWrapper, ProgramArray, ProgramMap, ProgramObject, ProgramPromise, ProgramSet, } from "../interpreter/objects.js";
|
|
5
5
|
import { describeValue, isRuntimeReference } from "../interpreter/references.js";
|
|
6
6
|
import { applyCollectionCallback, isSupportedCallback, preserveConsumerError, toPrimitiveNumber, toPrimitiveString, } from "../interpreter/runner.js";
|
|
7
|
-
const coerceGroupByPropertyKey = (runner, value
|
|
7
|
+
const coerceGroupByPropertyKey = (runner, value) => {
|
|
8
8
|
if (value instanceof ProgramPromise)
|
|
9
9
|
return Effect.succeed("[object Promise]");
|
|
10
10
|
if (!isWrapper(value) && isRuntimeReference(value)) {
|
|
11
|
-
throw
|
|
11
|
+
throw invalidData(`Object.groupBy callback must return a data value, received ${describeValue(value)}.`);
|
|
12
12
|
}
|
|
13
|
-
return toPrimitiveString(runner, value
|
|
13
|
+
return toPrimitiveString(runner, value);
|
|
14
14
|
};
|
|
15
15
|
/** `Map.groupBy` and `Object.groupBy`: the same iteration, keyed into a Map or a data object. */
|
|
16
|
-
export const groupBy = (runner, namespace) => fn(runner.prototypes, "groupBy", 2, (_, args
|
|
16
|
+
export const groupBy = (runner, namespace) => fn(runner.prototypes, "groupBy", 2, (_, args) => {
|
|
17
17
|
const protos = runner.prototypes;
|
|
18
18
|
const source = args[0];
|
|
19
19
|
if (source === null || source === undefined) {
|
|
20
|
-
throw
|
|
20
|
+
throw typeError(`${namespace}.groupBy expects an iterable collection.`);
|
|
21
21
|
}
|
|
22
|
-
const apply = applyCollectionCallback(runner, args[1], `${namespace}.groupBy
|
|
22
|
+
const apply = applyCollectionCallback(runner, args[1], `${namespace}.groupBy`);
|
|
23
23
|
return Effect.gen(function* () {
|
|
24
|
-
const cursor = yield* runner.syncIterator(source
|
|
24
|
+
const cursor = yield* runner.syncIterator(source);
|
|
25
25
|
if (cursor === undefined) {
|
|
26
|
-
throw
|
|
26
|
+
throw typeError(`${namespace}.groupBy expects an iterable collection.`);
|
|
27
27
|
}
|
|
28
28
|
if (namespace === "Map") {
|
|
29
29
|
const result = new ProgramMap(protos.Map);
|
|
@@ -50,7 +50,7 @@ export const groupBy = (runner, namespace) => fn(runner.prototypes, "groupBy", 2
|
|
|
50
50
|
if (step.done)
|
|
51
51
|
return result;
|
|
52
52
|
const item = step.value;
|
|
53
|
-
const key = yield* preserveConsumerError(cursor, Effect.flatMap(apply([item, index]), (value) => coerceGroupByPropertyKey(runner, value
|
|
53
|
+
const key = yield* preserveConsumerError(cursor, Effect.flatMap(apply([item, index]), (value) => coerceGroupByPropertyKey(runner, value)));
|
|
54
54
|
const group = getOwn(result, key);
|
|
55
55
|
if (group === undefined)
|
|
56
56
|
define(result, key, new ProgramArray(protos.Array, [item]));
|
|
@@ -60,14 +60,14 @@ export const groupBy = (runner, namespace) => fn(runner.prototypes, "groupBy", 2
|
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
62
|
});
|
|
63
|
-
const constructMap = (runner, init, proto
|
|
63
|
+
const constructMap = (runner, init, proto) => {
|
|
64
64
|
const target = new ProgramMap(proto);
|
|
65
65
|
if (init === undefined || init === null)
|
|
66
66
|
return Effect.succeed(target);
|
|
67
67
|
return Effect.gen(function* () {
|
|
68
|
-
const cursor = yield* runner.syncIterator(init
|
|
68
|
+
const cursor = yield* runner.syncIterator(init);
|
|
69
69
|
if (cursor === undefined) {
|
|
70
|
-
throw
|
|
70
|
+
throw typeError("new Map(...) expects an iterable of [key, value] pairs or no argument.");
|
|
71
71
|
}
|
|
72
72
|
while (true) {
|
|
73
73
|
const step = yield* cursor.next;
|
|
@@ -75,21 +75,21 @@ const constructMap = (runner, init, proto, node) => {
|
|
|
75
75
|
return target;
|
|
76
76
|
yield* preserveConsumerError(cursor, Effect.sync(() => {
|
|
77
77
|
if (!(step.value instanceof ProgramObject)) {
|
|
78
|
-
throw
|
|
78
|
+
throw typeError("new Map(...) expects [key, value] pairs as entry objects.");
|
|
79
79
|
}
|
|
80
80
|
target.map.set(getOwn(step.value, 0), getOwn(step.value, 1));
|
|
81
81
|
}));
|
|
82
82
|
}
|
|
83
83
|
});
|
|
84
84
|
};
|
|
85
|
-
const constructSet = (runner, init, proto
|
|
85
|
+
const constructSet = (runner, init, proto) => {
|
|
86
86
|
const target = new ProgramSet(proto);
|
|
87
87
|
if (init === undefined || init === null)
|
|
88
88
|
return Effect.succeed(target);
|
|
89
89
|
return Effect.gen(function* () {
|
|
90
|
-
const cursor = yield* runner.syncIterator(init
|
|
90
|
+
const cursor = yield* runner.syncIterator(init);
|
|
91
91
|
if (cursor === undefined) {
|
|
92
|
-
throw
|
|
92
|
+
throw typeError("new Set(...) expects a synchronous iterable or no argument.");
|
|
93
93
|
}
|
|
94
94
|
while (true) {
|
|
95
95
|
const step = yield* cursor.next;
|
|
@@ -105,46 +105,46 @@ export const mapGlobal = (runner) => {
|
|
|
105
105
|
const map = constructor(protos, proto, {
|
|
106
106
|
name: "Map",
|
|
107
107
|
call: requiresNew("Map"),
|
|
108
|
-
construct: (args, newTarget
|
|
108
|
+
construct: (args, newTarget) => constructMap(runner, args[0], prototypeFrom(newTarget, proto)),
|
|
109
109
|
});
|
|
110
110
|
define(map, "groupBy", groupBy(runner, "Map"), hidden);
|
|
111
|
-
const self = (thisValue, name
|
|
111
|
+
const self = (thisValue, name) => receiver(ProgramMap, thisValue, `Map.prototype.${name}`);
|
|
112
112
|
const wrap = (items) => new ProgramArray(protos.Array, items);
|
|
113
113
|
defineAccessor(proto, "size", (thisValue) => receiver(ProgramMap, thisValue, "Map.prototype.size").map.size);
|
|
114
114
|
methods(protos, proto, [
|
|
115
|
-
["get", 1, (thisValue, args
|
|
116
|
-
["has", 1, (thisValue, args
|
|
115
|
+
["get", 1, (thisValue, args) => self(thisValue, "get").map.get(args[0])],
|
|
116
|
+
["has", 1, (thisValue, args) => self(thisValue, "has").map.has(args[0])],
|
|
117
117
|
[
|
|
118
118
|
"set",
|
|
119
119
|
2,
|
|
120
|
-
(thisValue, args
|
|
121
|
-
const target = self(thisValue, "set"
|
|
120
|
+
(thisValue, args) => {
|
|
121
|
+
const target = self(thisValue, "set");
|
|
122
122
|
target.map.set(args[0], args[1]);
|
|
123
123
|
return target;
|
|
124
124
|
},
|
|
125
125
|
],
|
|
126
|
-
["delete", 1, (thisValue, args
|
|
126
|
+
["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])],
|
|
127
127
|
[
|
|
128
128
|
"clear",
|
|
129
129
|
0,
|
|
130
|
-
(thisValue
|
|
131
|
-
self(thisValue, "clear"
|
|
130
|
+
(thisValue) => {
|
|
131
|
+
self(thisValue, "clear").map.clear();
|
|
132
132
|
return undefined;
|
|
133
133
|
},
|
|
134
134
|
],
|
|
135
|
-
["keys", 0, (thisValue
|
|
136
|
-
["values", 0, (thisValue
|
|
135
|
+
["keys", 0, (thisValue) => wrap(Array.from(self(thisValue, "keys").map.keys()))],
|
|
136
|
+
["values", 0, (thisValue) => wrap(Array.from(self(thisValue, "values").map.values()))],
|
|
137
137
|
[
|
|
138
138
|
"entries",
|
|
139
139
|
0,
|
|
140
|
-
(thisValue
|
|
140
|
+
(thisValue) => wrap(Array.from(self(thisValue, "entries").map.entries(), ([key, item]) => wrap([key, item]))),
|
|
141
141
|
],
|
|
142
142
|
[
|
|
143
143
|
"forEach",
|
|
144
144
|
1,
|
|
145
|
-
(thisValue, args
|
|
146
|
-
const target = self(thisValue, "forEach"
|
|
147
|
-
const apply = applyCollectionCallback(runner, args[0], "Map.forEach"
|
|
145
|
+
(thisValue, args) => {
|
|
146
|
+
const target = self(thisValue, "forEach");
|
|
147
|
+
const apply = applyCollectionCallback(runner, args[0], "Map.forEach");
|
|
148
148
|
return Effect.gen(function* () {
|
|
149
149
|
for (const [key, item] of Array.from(target.map.entries()))
|
|
150
150
|
yield* apply([item, key, target]);
|
|
@@ -155,7 +155,7 @@ export const mapGlobal = (runner) => {
|
|
|
155
155
|
]);
|
|
156
156
|
return map;
|
|
157
157
|
};
|
|
158
|
-
const loadSetRecord = (runner, source, name
|
|
158
|
+
const loadSetRecord = (runner, source, name) => {
|
|
159
159
|
if (source instanceof ProgramSet) {
|
|
160
160
|
return Effect.succeed({
|
|
161
161
|
size: source.set.size,
|
|
@@ -171,31 +171,31 @@ const loadSetRecord = (runner, source, name, node) => {
|
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
173
|
if (!(source instanceof ProgramObject)) {
|
|
174
|
-
throw
|
|
174
|
+
throw typeError(`Set.${name} expects a Set-like object.`);
|
|
175
175
|
}
|
|
176
176
|
return Effect.gen(function* () {
|
|
177
|
-
const size = yield* toPrimitiveNumber(runner, get(source, "size")
|
|
177
|
+
const size = yield* toPrimitiveNumber(runner, get(source, "size"));
|
|
178
178
|
if (Number.isNaN(size)) {
|
|
179
|
-
throw
|
|
179
|
+
throw typeError(`Set.${name} received a Set-like object with an invalid size.`);
|
|
180
180
|
}
|
|
181
181
|
const has = get(source, "has");
|
|
182
182
|
const keys = get(source, "keys");
|
|
183
183
|
if (!isSupportedCallback(has) || !isSupportedCallback(keys)) {
|
|
184
|
-
throw
|
|
184
|
+
throw typeError(`Set.${name} expects callable 'has' and 'keys' methods.`);
|
|
185
185
|
}
|
|
186
186
|
return {
|
|
187
187
|
size: Math.max(Math.trunc(size), 0),
|
|
188
|
-
has: (item) => Effect.map(runner.invokeCallable(has, source, [item]
|
|
189
|
-
keys: () => Effect.flatMap(runner.invokeCallable(keys, source, []
|
|
188
|
+
has: (item) => Effect.map(runner.invokeCallable(has, source, [item]), Boolean),
|
|
189
|
+
keys: () => Effect.flatMap(runner.invokeCallable(keys, source, []), (result) => {
|
|
190
190
|
if (result instanceof ProgramArray)
|
|
191
191
|
return Effect.succeed(result.items);
|
|
192
|
-
throw
|
|
192
|
+
throw typeError(`Set.${name} expected 'keys' to return an iterator.`);
|
|
193
193
|
}),
|
|
194
194
|
};
|
|
195
195
|
});
|
|
196
196
|
};
|
|
197
|
-
const setOperation = (runner, target, name, source
|
|
198
|
-
const other = yield* loadSetRecord(runner, source, name
|
|
197
|
+
const setOperation = (runner, target, name, source) => Effect.gen(function* () {
|
|
198
|
+
const other = yield* loadSetRecord(runner, source, name);
|
|
199
199
|
const copy = () => {
|
|
200
200
|
const result = new ProgramSet(runner.prototypes.Set);
|
|
201
201
|
for (const item of target.set.values())
|
|
@@ -283,49 +283,49 @@ export const setGlobal = (runner) => {
|
|
|
283
283
|
const set = constructor(protos, proto, {
|
|
284
284
|
name: "Set",
|
|
285
285
|
call: requiresNew("Set"),
|
|
286
|
-
construct: (args, newTarget
|
|
286
|
+
construct: (args, newTarget) => constructSet(runner, args[0], prototypeFrom(newTarget, proto)),
|
|
287
287
|
});
|
|
288
|
-
const self = (thisValue, name
|
|
288
|
+
const self = (thisValue, name) => receiver(ProgramSet, thisValue, `Set.prototype.${name}`);
|
|
289
289
|
const wrap = (items) => new ProgramArray(protos.Array, items);
|
|
290
290
|
const operation = (name) => [
|
|
291
291
|
name,
|
|
292
292
|
1,
|
|
293
|
-
(thisValue, args
|
|
293
|
+
(thisValue, args) => setOperation(runner, self(thisValue, name), name, args[0]),
|
|
294
294
|
];
|
|
295
295
|
defineAccessor(proto, "size", (thisValue) => receiver(ProgramSet, thisValue, "Set.prototype.size").set.size);
|
|
296
296
|
methods(protos, proto, [
|
|
297
|
-
["has", 1, (thisValue, args
|
|
297
|
+
["has", 1, (thisValue, args) => self(thisValue, "has").set.has(args[0])],
|
|
298
298
|
[
|
|
299
299
|
"add",
|
|
300
300
|
1,
|
|
301
|
-
(thisValue, args
|
|
302
|
-
const target = self(thisValue, "add"
|
|
301
|
+
(thisValue, args) => {
|
|
302
|
+
const target = self(thisValue, "add");
|
|
303
303
|
target.set.add(args[0]);
|
|
304
304
|
return target;
|
|
305
305
|
},
|
|
306
306
|
],
|
|
307
|
-
["delete", 1, (thisValue, args
|
|
307
|
+
["delete", 1, (thisValue, args) => self(thisValue, "delete").set.delete(args[0])],
|
|
308
308
|
[
|
|
309
309
|
"clear",
|
|
310
310
|
0,
|
|
311
|
-
(thisValue
|
|
312
|
-
self(thisValue, "clear"
|
|
311
|
+
(thisValue) => {
|
|
312
|
+
self(thisValue, "clear").set.clear();
|
|
313
313
|
return undefined;
|
|
314
314
|
},
|
|
315
315
|
],
|
|
316
|
-
["keys", 0, (thisValue
|
|
317
|
-
["values", 0, (thisValue
|
|
316
|
+
["keys", 0, (thisValue) => wrap(Array.from(self(thisValue, "keys").set.values()))],
|
|
317
|
+
["values", 0, (thisValue) => wrap(Array.from(self(thisValue, "values").set.values()))],
|
|
318
318
|
[
|
|
319
319
|
"entries",
|
|
320
320
|
0,
|
|
321
|
-
(thisValue
|
|
321
|
+
(thisValue) => wrap(Array.from(self(thisValue, "entries").set.values(), (item) => wrap([item, item]))),
|
|
322
322
|
],
|
|
323
323
|
[
|
|
324
324
|
"forEach",
|
|
325
325
|
1,
|
|
326
|
-
(thisValue, args
|
|
327
|
-
const target = self(thisValue, "forEach"
|
|
328
|
-
const apply = applyCollectionCallback(runner, args[0], "Set.forEach"
|
|
326
|
+
(thisValue, args) => {
|
|
327
|
+
const target = self(thisValue, "forEach");
|
|
328
|
+
const apply = applyCollectionCallback(runner, args[0], "Set.forEach");
|
|
329
329
|
return Effect.gen(function* () {
|
|
330
330
|
for (const item of Array.from(target.set.values()))
|
|
331
331
|
yield* apply([item, item, target]);
|
package/dist/stdlib/date.js
CHANGED
|
@@ -4,14 +4,14 @@ import { rangeError } from "../interpreter/model.js";
|
|
|
4
4
|
import { ProgramDate, ProgramObject } from "../interpreter/objects.js";
|
|
5
5
|
import { toPrimitive, toPrimitiveNumber } from "../interpreter/runner.js";
|
|
6
6
|
import { coerceToNumber, coerceToString } from "./value.js";
|
|
7
|
-
const constructDate = (runner, args, proto
|
|
7
|
+
const constructDate = (runner, args, proto) => {
|
|
8
8
|
if (args.length === 0)
|
|
9
9
|
return Effect.succeed(new ProgramDate(proto, Date.now()));
|
|
10
10
|
if (args.length === 1) {
|
|
11
11
|
const arg = args[0];
|
|
12
12
|
if (arg instanceof ProgramDate)
|
|
13
13
|
return Effect.succeed(new ProgramDate(proto, arg.time));
|
|
14
|
-
return Effect.map(toPrimitive(runner, arg, "default"
|
|
14
|
+
return Effect.map(toPrimitive(runner, arg, "default"), (value) => typeof value === "string"
|
|
15
15
|
? new ProgramDate(proto, Date.parse(value))
|
|
16
16
|
: new ProgramDate(proto, new Date(coerceToNumber(value)).getTime()));
|
|
17
17
|
}
|
|
@@ -62,45 +62,45 @@ export const dateGlobal = (runner) => {
|
|
|
62
62
|
length: 7,
|
|
63
63
|
// ISO instead of the host's locale string: date strings are deterministic and must not leak the host timezone.
|
|
64
64
|
call: () => Effect.sync(() => new Date().toISOString()),
|
|
65
|
-
construct: (args, newTarget
|
|
65
|
+
construct: (args, newTarget) => constructDate(runner, args, prototypeFrom(newTarget, proto)),
|
|
66
66
|
});
|
|
67
67
|
methods(protos, date, [
|
|
68
68
|
["now", 0, () => Date.now()],
|
|
69
69
|
["parse", 1, (_, args) => Date.parse(coerceToString(args[0]))],
|
|
70
70
|
["UTC", 7, (_, args) => Date.UTC(...args.map((arg) => coerceToNumber(arg)))],
|
|
71
71
|
]);
|
|
72
|
-
const self = (thisValue, name
|
|
73
|
-
const iso = (value
|
|
72
|
+
const self = (thisValue, name) => receiver(ProgramDate, thisValue, `Date.prototype.${name}`);
|
|
73
|
+
const iso = (value) => {
|
|
74
74
|
if (!Number.isFinite(value.time))
|
|
75
|
-
throw rangeError("Invalid time value."
|
|
75
|
+
throw rangeError("Invalid time value.");
|
|
76
76
|
return new Date(value.time).toISOString();
|
|
77
77
|
};
|
|
78
78
|
methods(protos, proto, [
|
|
79
|
-
["getTime", 0, (thisValue
|
|
80
|
-
["valueOf", 0, (thisValue
|
|
81
|
-
["toISOString", 0, (thisValue
|
|
79
|
+
["getTime", 0, (thisValue) => self(thisValue, "getTime").time],
|
|
80
|
+
["valueOf", 0, (thisValue) => self(thisValue, "valueOf").time],
|
|
81
|
+
["toISOString", 0, (thisValue) => iso(self(thisValue, "toISOString"))],
|
|
82
82
|
[
|
|
83
83
|
"toJSON",
|
|
84
84
|
1,
|
|
85
|
-
(thisValue
|
|
86
|
-
const value = self(thisValue, "toJSON"
|
|
87
|
-
return Number.isFinite(value.time) ? iso(value
|
|
85
|
+
(thisValue) => {
|
|
86
|
+
const value = self(thisValue, "toJSON");
|
|
87
|
+
return Number.isFinite(value.time) ? iso(value) : null;
|
|
88
88
|
},
|
|
89
89
|
],
|
|
90
|
-
["toString", 0, (thisValue
|
|
91
|
-
["toDateString", 0, (thisValue
|
|
92
|
-
["toTimeString", 0, (thisValue
|
|
93
|
-
["toUTCString", 0, (thisValue
|
|
94
|
-
["toGMTString", 0, (thisValue
|
|
95
|
-
...getters.map((name) => [name, 0, (thisValue
|
|
90
|
+
["toString", 0, (thisValue) => coerceToString(self(thisValue, "toString"))],
|
|
91
|
+
["toDateString", 0, (thisValue) => new Date(self(thisValue, "toDateString").time).toDateString()],
|
|
92
|
+
["toTimeString", 0, (thisValue) => new Date(self(thisValue, "toTimeString").time).toTimeString()],
|
|
93
|
+
["toUTCString", 0, (thisValue) => new Date(self(thisValue, "toUTCString").time).toUTCString()],
|
|
94
|
+
["toGMTString", 0, (thisValue) => new Date(self(thisValue, "toGMTString").time).toUTCString()],
|
|
95
|
+
...getters.map((name) => [name, 0, (thisValue) => new Date(self(thisValue, name).time)[name]()]),
|
|
96
96
|
...setters.map(([name, length]) => [
|
|
97
97
|
name,
|
|
98
98
|
length,
|
|
99
|
-
(thisValue, args
|
|
100
|
-
const target = self(thisValue, name
|
|
99
|
+
(thisValue, args) => {
|
|
100
|
+
const target = self(thisValue, name);
|
|
101
101
|
// Native setters read the current time before argument coercion, whose callbacks may mutate the Date.
|
|
102
102
|
const hosted = new Date(target.time);
|
|
103
|
-
return Effect.map(Effect.forEach(args.slice(0, length), (arg) => toPrimitiveNumber(runner, arg
|
|
103
|
+
return Effect.map(Effect.forEach(args.slice(0, length), (arg) => toPrimitiveNumber(runner, arg), {
|
|
104
104
|
concurrency: 1,
|
|
105
105
|
}), (values) => {
|
|
106
106
|
target.time = hosted[name](...values);
|
package/dist/stdlib/json.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { methods } from "../interpreter/native.js";
|
|
3
3
|
import { applyCollectionCallback } from "../interpreter/runner.js";
|
|
4
|
-
import {
|
|
4
|
+
import { syntaxError, typeError } from "../interpreter/model.js";
|
|
5
5
|
import { typeofValue } from "../interpreter/references.js";
|
|
6
6
|
import { fromData, toData, toProgram } from "../data.js";
|
|
7
7
|
import { Callable, get, keys, ProgramArray, ProgramObject, record, remove, set } from "../interpreter/objects.js";
|
|
8
8
|
export const jsonGlobal = (runner) => {
|
|
9
9
|
const json = new ProgramObject(runner.prototypes.Object);
|
|
10
10
|
methods(runner.prototypes, json, [
|
|
11
|
-
["parse", 2, (_, args
|
|
12
|
-
["stringify", 3, (_, args
|
|
11
|
+
["parse", 2, (_, args) => parse(runner, args)],
|
|
12
|
+
["stringify", 3, (_, args) => stringify(runner, args)],
|
|
13
13
|
]);
|
|
14
14
|
return json;
|
|
15
15
|
};
|
|
16
|
-
const parse = (runner, args
|
|
16
|
+
const parse = (runner, args) => {
|
|
17
17
|
const text = args[0];
|
|
18
18
|
if (typeof text !== "string")
|
|
19
|
-
throw
|
|
19
|
+
throw typeError("JSON.parse expects a string.");
|
|
20
20
|
const parsed = (() => {
|
|
21
21
|
try {
|
|
22
22
|
return fromData(runner.prototypes, JSON.parse(text), "JSON.parse result");
|
|
23
23
|
}
|
|
24
24
|
catch (error) {
|
|
25
|
-
throw syntaxError(`JSON.parse received invalid JSON: ${error instanceof Error ? error.message : String(error)}
|
|
25
|
+
throw syntaxError(`JSON.parse received invalid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
26
26
|
}
|
|
27
27
|
})();
|
|
28
28
|
if (typeofValue(args[1]) !== "function")
|
|
29
29
|
return Effect.succeed(parsed);
|
|
30
|
-
const apply = applyCollectionCallback(runner, args[1], "JSON.parse"
|
|
30
|
+
const apply = applyCollectionCallback(runner, args[1], "JSON.parse");
|
|
31
31
|
const visit = (holder, key) => Effect.gen(function* () {
|
|
32
32
|
const value = get(holder, key);
|
|
33
33
|
if (value instanceof ProgramObject) {
|
|
@@ -43,7 +43,7 @@ const parse = (runner, args, node) => {
|
|
|
43
43
|
});
|
|
44
44
|
return visit(record(runner.prototypes.Object, { "": parsed }), "");
|
|
45
45
|
};
|
|
46
|
-
const stringify = (runner, args
|
|
46
|
+
const stringify = (runner, args) => {
|
|
47
47
|
const space = args[2];
|
|
48
48
|
const indent = typeof space === "number" || typeof space === "string" ? space : undefined;
|
|
49
49
|
const replacer = args[1];
|
|
@@ -57,10 +57,10 @@ const stringify = (runner, args, node) => {
|
|
|
57
57
|
}
|
|
58
58
|
// Validate up front; the replacer walk below reads the original value.
|
|
59
59
|
toProgram(runner.prototypes, args[0], "JSON.stringify value");
|
|
60
|
-
const apply = applyCollectionCallback(runner, replacer, "JSON.stringify"
|
|
60
|
+
const apply = applyCollectionCallback(runner, replacer, "JSON.stringify");
|
|
61
61
|
const stack = new Set();
|
|
62
62
|
const visit = (holder, key) => Effect.gen(function* () {
|
|
63
|
-
const value = yield* apply([key, yield* toJSONValue(runner, get(holder, key), key
|
|
63
|
+
const value = yield* apply([key, yield* toJSONValue(runner, get(holder, key), key)]);
|
|
64
64
|
if (value === undefined || typeofValue(value) === "function")
|
|
65
65
|
return undefined;
|
|
66
66
|
toProgram(runner.prototypes, value, "JSON.stringify replacer result");
|
|
@@ -71,7 +71,7 @@ const stringify = (runner, args, node) => {
|
|
|
71
71
|
if (!(value instanceof ProgramObject))
|
|
72
72
|
return {};
|
|
73
73
|
if (stack.has(value))
|
|
74
|
-
throw
|
|
74
|
+
throw typeError("Converting circular structure to JSON.");
|
|
75
75
|
stack.add(value);
|
|
76
76
|
if (value instanceof ProgramArray) {
|
|
77
77
|
const result = [];
|
|
@@ -93,9 +93,9 @@ const stringify = (runner, args, node) => {
|
|
|
93
93
|
return Effect.map(visit(record(runner.prototypes.Object, { "": args[0] }), ""), (value) => JSON.stringify(value, null, indent));
|
|
94
94
|
};
|
|
95
95
|
// SerializeJSONProperty step 2: a callable `toJSON` decides the value, as Date and URL define.
|
|
96
|
-
const toJSONValue = (runner, value, key
|
|
96
|
+
const toJSONValue = (runner, value, key) => {
|
|
97
97
|
if (!(value instanceof ProgramObject))
|
|
98
98
|
return Effect.succeed(value);
|
|
99
99
|
const toJSON = get(value, "toJSON");
|
|
100
|
-
return toJSON instanceof Callable ? runner.invokeCallable(toJSON, value, [key]
|
|
100
|
+
return toJSON instanceof Callable ? runner.invokeCallable(toJSON, value, [key]) : Effect.succeed(value);
|
|
101
101
|
};
|
package/dist/stdlib/math.js
CHANGED
|
@@ -1,34 +1,30 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { constants, methods } from "../interpreter/native.js";
|
|
3
|
-
import {
|
|
3
|
+
import { typeError } from "../interpreter/model.js";
|
|
4
4
|
import { ProgramObject } from "../interpreter/objects.js";
|
|
5
5
|
import { preserveConsumerError } from "../interpreter/runner.js";
|
|
6
6
|
// Validate only the arguments a method consumes; like JS, extras are ignored
|
|
7
7
|
// (so built-ins work as callbacks receiving (element, index, array)).
|
|
8
|
-
const number = (name, args, index
|
|
8
|
+
const number = (name, args, index) => {
|
|
9
9
|
if (index >= args.length)
|
|
10
10
|
return Number.NaN;
|
|
11
11
|
const arg = args[index];
|
|
12
12
|
if (typeof arg !== "number")
|
|
13
|
-
throw
|
|
13
|
+
throw typeError(`Math.${name} expects number arguments.`);
|
|
14
14
|
return arg;
|
|
15
15
|
};
|
|
16
|
-
const unary = (name, op) => [
|
|
17
|
-
name,
|
|
18
|
-
1,
|
|
19
|
-
(_, args, node) => op(number(name, args, 0, node)),
|
|
20
|
-
];
|
|
16
|
+
const unary = (name, op) => [name, 1, (_, args) => op(number(name, args, 0))];
|
|
21
17
|
const binary = (name, op) => [
|
|
22
18
|
name,
|
|
23
19
|
2,
|
|
24
|
-
(_, args
|
|
20
|
+
(_, args) => op(number(name, args, 0), number(name, args, 1)),
|
|
25
21
|
];
|
|
26
22
|
const variadic = (name, op) => [
|
|
27
23
|
name,
|
|
28
24
|
2,
|
|
29
|
-
(_, args
|
|
25
|
+
(_, args) => op(...args.map((arg) => {
|
|
30
26
|
if (typeof arg !== "number")
|
|
31
|
-
throw
|
|
27
|
+
throw typeError(`Math.${name} expects number arguments.`);
|
|
32
28
|
return arg;
|
|
33
29
|
})),
|
|
34
30
|
];
|
|
@@ -85,10 +81,10 @@ export const mathGlobal = (runner) => {
|
|
|
85
81
|
[
|
|
86
82
|
"sumPrecise",
|
|
87
83
|
1,
|
|
88
|
-
(_, args
|
|
89
|
-
const cursor = yield* runner.syncIterator(args[0]
|
|
84
|
+
(_, args) => Effect.gen(function* () {
|
|
85
|
+
const cursor = yield* runner.syncIterator(args[0]);
|
|
90
86
|
if (cursor === undefined) {
|
|
91
|
-
throw
|
|
87
|
+
throw typeError("Math.sumPrecise expects a synchronous iterable.");
|
|
92
88
|
}
|
|
93
89
|
const numbers = [];
|
|
94
90
|
while (true) {
|
|
@@ -97,7 +93,7 @@ export const mathGlobal = (runner) => {
|
|
|
97
93
|
return Math.sumPrecise(numbers);
|
|
98
94
|
yield* preserveConsumerError(cursor, Effect.sync(() => {
|
|
99
95
|
if (typeof step.value !== "number") {
|
|
100
|
-
throw
|
|
96
|
+
throw typeError("Math.sumPrecise expects an iterable of numbers.");
|
|
101
97
|
}
|
|
102
98
|
numbers.push(step.value);
|
|
103
99
|
}));
|
package/dist/stdlib/number.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { constructor, constants, methods } from "../interpreter/native.js";
|
|
2
|
-
import {
|
|
2
|
+
import { rangeError, typeError } from "../interpreter/model.js";
|
|
3
3
|
import { coercion, coerceToString } from "./value.js";
|
|
4
4
|
export const numberGlobal = (runner) => {
|
|
5
5
|
const protos = runner.prototypes;
|
|
@@ -26,61 +26,57 @@ export const numberGlobal = (runner) => {
|
|
|
26
26
|
[
|
|
27
27
|
"parseInt",
|
|
28
28
|
2,
|
|
29
|
-
(_, args
|
|
29
|
+
(_, args) => {
|
|
30
30
|
const radix = args[1];
|
|
31
31
|
if (radix !== undefined && typeof radix !== "number") {
|
|
32
|
-
throw
|
|
32
|
+
throw typeError("Number.parseInt expects a numeric radix.");
|
|
33
33
|
}
|
|
34
34
|
return parseInt(coerceToString(args[0]), radix);
|
|
35
35
|
},
|
|
36
36
|
],
|
|
37
37
|
["parseFloat", 1, (_, args) => parseFloat(coerceToString(args[0]))],
|
|
38
38
|
]);
|
|
39
|
-
const self = (thisValue, name
|
|
39
|
+
const self = (thisValue, name) => {
|
|
40
40
|
if (typeof thisValue === "number")
|
|
41
41
|
return thisValue;
|
|
42
|
-
throw
|
|
42
|
+
throw typeError(`Number.prototype.${name} requires that 'this' be a Number.`);
|
|
43
43
|
};
|
|
44
|
-
const optNum = (name, arg
|
|
44
|
+
const optNum = (name, arg) => {
|
|
45
45
|
if (arg === undefined)
|
|
46
46
|
return undefined;
|
|
47
47
|
if (typeof arg !== "number")
|
|
48
|
-
throw
|
|
48
|
+
throw typeError(`Number.${name} expects a number argument.`);
|
|
49
49
|
return arg;
|
|
50
50
|
};
|
|
51
51
|
methods(protos, protos.Number, [
|
|
52
|
-
[
|
|
53
|
-
"toFixed",
|
|
54
|
-
1,
|
|
55
|
-
(thisValue, args, node) => self(thisValue, "toFixed", node).toFixed(optNum("toFixed", args[0], node)),
|
|
56
|
-
],
|
|
52
|
+
["toFixed", 1, (thisValue, args) => self(thisValue, "toFixed").toFixed(optNum("toFixed", args[0]))],
|
|
57
53
|
[
|
|
58
54
|
"toExponential",
|
|
59
55
|
1,
|
|
60
|
-
(thisValue, args
|
|
56
|
+
(thisValue, args) => self(thisValue, "toExponential").toExponential(optNum("toExponential", args[0])),
|
|
61
57
|
],
|
|
62
58
|
[
|
|
63
59
|
"toPrecision",
|
|
64
60
|
1,
|
|
65
|
-
(thisValue, args
|
|
66
|
-
const value = self(thisValue, "toPrecision"
|
|
67
|
-
const digits = optNum("toPrecision", args[0]
|
|
61
|
+
(thisValue, args) => {
|
|
62
|
+
const value = self(thisValue, "toPrecision");
|
|
63
|
+
const digits = optNum("toPrecision", args[0]);
|
|
68
64
|
return digits === undefined ? value.toString() : value.toPrecision(digits);
|
|
69
65
|
},
|
|
70
66
|
],
|
|
71
67
|
[
|
|
72
68
|
"toString",
|
|
73
69
|
1,
|
|
74
|
-
(thisValue, args
|
|
75
|
-
const value = self(thisValue, "toString"
|
|
76
|
-
const radix = optNum("toString", args[0]
|
|
70
|
+
(thisValue, args) => {
|
|
71
|
+
const value = self(thisValue, "toString");
|
|
72
|
+
const radix = optNum("toString", args[0]);
|
|
77
73
|
if (radix !== undefined && (radix < 2 || radix > 36)) {
|
|
78
|
-
throw rangeError("Number.toString radix must be between 2 and 36."
|
|
74
|
+
throw rangeError("Number.toString radix must be between 2 and 36.");
|
|
79
75
|
}
|
|
80
76
|
return value.toString(radix);
|
|
81
77
|
},
|
|
82
78
|
],
|
|
83
|
-
["valueOf", 0, (thisValue
|
|
79
|
+
["valueOf", 0, (thisValue) => self(thisValue, "valueOf")],
|
|
84
80
|
]);
|
|
85
81
|
return number;
|
|
86
82
|
};
|
|
@@ -91,14 +87,14 @@ export const booleanGlobal = (runner) => {
|
|
|
91
87
|
length: 1,
|
|
92
88
|
call: coercion(runner, "Boolean").call,
|
|
93
89
|
});
|
|
94
|
-
const self = (thisValue, name
|
|
90
|
+
const self = (thisValue, name) => {
|
|
95
91
|
if (typeof thisValue === "boolean")
|
|
96
92
|
return thisValue;
|
|
97
|
-
throw
|
|
93
|
+
throw typeError(`Boolean.prototype.${name} requires that 'this' be a Boolean.`);
|
|
98
94
|
};
|
|
99
95
|
methods(protos, protos.Boolean, [
|
|
100
|
-
["toString", 0, (thisValue
|
|
101
|
-
["valueOf", 0, (thisValue
|
|
96
|
+
["toString", 0, (thisValue) => String(self(thisValue, "toString"))],
|
|
97
|
+
["valueOf", 0, (thisValue) => self(thisValue, "valueOf")],
|
|
102
98
|
]);
|
|
103
99
|
return boolean;
|
|
104
100
|
};
|
package/dist/stdlib/object.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type AstNode } from "../interpreter/model.js";
|
|
2
2
|
import { ProgramObject } from "../interpreter/objects.js";
|
|
3
3
|
import { type Runner } from "../interpreter/runner.js";
|
|
4
|
-
export declare const enumerableSource: <R>(runner: Runner<R>, label: string, value: unknown, node
|
|
5
|
-
export declare const objectAssign: <R>(runner: Runner<R>, args: Array<unknown
|
|
4
|
+
export declare const enumerableSource: <R>(runner: Runner<R>, label: string, value: unknown, node?: AstNode) => ProgramObject;
|
|
5
|
+
export declare const objectAssign: <R>(runner: Runner<R>, args: Array<unknown>) => unknown;
|
|
6
6
|
export declare const classTag: (value: unknown) => string;
|
|
7
7
|
export declare const objectGlobal: <R>(runner: Runner<R>, toolKeys: (path: ReadonlyArray<string>) => ReadonlyArray<string>) => import("../interpreter/objects.js").NativeFunction<R>;
|