@opencode/codemode 0.0.0-dev-1180 → 0.0.0-dev-1189
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 +6 -12
- package/dist/codemode.d.ts +7 -21
- package/dist/codemode.js +17 -38
- package/dist/extension.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/scan.js +2 -2
- package/dist/tool-runtime.d.ts +8 -11
- package/dist/tool-runtime.js +13 -11
- package/dist/tool.d.ts +18 -9
- package/dist/tool.js +15 -23
- package/dist/vm/data.d.ts +1 -1
- package/dist/vm/runtime/execute.d.ts +16 -6
- package/dist/vm/runtime/execute.js +10 -7
- package/dist/vm/snapshot/codec.d.ts +3 -2
- package/dist/vm/snapshot/codec.js +3 -2
- package/dist/vm/snapshot/snapshot.d.ts +6 -3
- package/dist/vm/snapshot/snapshot.js +13 -2
- package/dist/vm/vm.d.ts +2 -2
- package/package.json +3 -5
- package/dist/data.d.ts +0 -21
- package/dist/data.js +0 -93
- package/dist/interpreter/callback.d.ts +0 -35
- package/dist/interpreter/callback.js +0 -98
- package/dist/interpreter/errors.d.ts +0 -23
- package/dist/interpreter/errors.js +0 -173
- package/dist/interpreter/execute.d.ts +0 -14
- package/dist/interpreter/execute.js +0 -207
- package/dist/interpreter/extensions.d.ts +0 -9
- package/dist/interpreter/extensions.js +0 -248
- package/dist/interpreter/generators.d.ts +0 -3
- package/dist/interpreter/generators.js +0 -168
- package/dist/interpreter/globals.d.ts +0 -6
- package/dist/interpreter/globals.js +0 -144
- package/dist/interpreter/interpreter.d.ts +0 -48
- package/dist/interpreter/interpreter.js +0 -2253
- package/dist/interpreter/intrinsics.d.ts +0 -15
- package/dist/interpreter/intrinsics.js +0 -105
- package/dist/interpreter/limits.d.ts +0 -10
- package/dist/interpreter/limits.js +0 -19
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/modules.d.ts +0 -4
- package/dist/interpreter/modules.js +0 -33
- package/dist/interpreter/native.d.ts +0 -17
- package/dist/interpreter/native.js +0 -35
- package/dist/interpreter/objects.d.ts +0 -301
- package/dist/interpreter/objects.js +0 -592
- package/dist/interpreter/promises.d.ts +0 -18
- package/dist/interpreter/promises.js +0 -292
- package/dist/interpreter/references.d.ts +0 -8
- package/dist/interpreter/references.js +0 -51
- package/dist/interpreter/runner.d.ts +0 -178
- package/dist/interpreter/runner.js +0 -480
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/stdlib/array.d.ts +0 -11
- package/dist/stdlib/array.js +0 -498
- package/dist/stdlib/bytes.d.ts +0 -10
- package/dist/stdlib/bytes.js +0 -253
- package/dist/stdlib/collections.d.ts +0 -7
- package/dist/stdlib/collections.js +0 -497
- package/dist/stdlib/console.d.ts +0 -6
- package/dist/stdlib/console.js +0 -93
- package/dist/stdlib/date.d.ts +0 -2
- package/dist/stdlib/date.js +0 -132
- package/dist/stdlib/headers.d.ts +0 -2
- package/dist/stdlib/headers.js +0 -110
- package/dist/stdlib/iterator.d.ts +0 -2
- package/dist/stdlib/iterator.js +0 -322
- package/dist/stdlib/json.d.ts +0 -3
- package/dist/stdlib/json.js +0 -58
- package/dist/stdlib/math.d.ts +0 -8
- package/dist/stdlib/math.js +0 -97
- package/dist/stdlib/number.d.ts +0 -3
- package/dist/stdlib/number.js +0 -72
- package/dist/stdlib/object.d.ts +0 -6
- package/dist/stdlib/object.js +0 -219
- package/dist/stdlib/regexp.d.ts +0 -7
- package/dist/stdlib/regexp.js +0 -114
- package/dist/stdlib/string.d.ts +0 -2
- package/dist/stdlib/string.js +0 -272
- package/dist/stdlib/url.d.ts +0 -13
- package/dist/stdlib/url.js +0 -255
- package/dist/stdlib/value.d.ts +0 -7
- package/dist/stdlib/value.js +0 -33
- package/dist/stdlib/web.d.ts +0 -5
- package/dist/stdlib/web.js +0 -97
- package/dist/suspend-error.d.ts +0 -24
- package/dist/suspend-error.js +0 -25
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
|
@@ -1,497 +0,0 @@
|
|
|
1
|
-
import { constructor, fn, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
|
|
2
|
-
import { invalidData, IteratorSymbol, typeError } from "../interpreter/model.js";
|
|
3
|
-
import { define, defineAccessor, get, getOwn, hidden, Arr, coerceToString, hostCursor, hostIterator, MapObj, Obj, PromiseObj, SetObj, WeakMapObj, WeakSetObj, } from "../interpreter/objects.js";
|
|
4
|
-
import { describeValue, isOpaque } from "../interpreter/references.js";
|
|
5
|
-
import { applyCollectionCallback, isSupportedCallback, preserveConsumerError, toPrimitiveNumber, toPrimitiveString, } from "../interpreter/callback.js";
|
|
6
|
-
function* coerceGroupByPropertyKey(ctx, result) {
|
|
7
|
-
const value = yield* result;
|
|
8
|
-
if (value instanceof PromiseObj)
|
|
9
|
-
return coerceToString(value);
|
|
10
|
-
if (isOpaque(value)) {
|
|
11
|
-
throw invalidData(`Object.groupBy callback must return a data value, received ${describeValue(value)}.`);
|
|
12
|
-
}
|
|
13
|
-
return yield* toPrimitiveString(ctx, value);
|
|
14
|
-
}
|
|
15
|
-
/** `Map.groupBy` and `Object.groupBy`: the same iteration, keyed into a Map or a data object. */
|
|
16
|
-
export const groupBy = (ctx, namespace) => fn(ctx.builtins, "groupBy", 2, function* (_, args) {
|
|
17
|
-
const builtins = ctx.builtins;
|
|
18
|
-
const source = args[0];
|
|
19
|
-
if (source === null || source === undefined) {
|
|
20
|
-
throw typeError(`${namespace}.groupBy expects an iterable collection.`);
|
|
21
|
-
}
|
|
22
|
-
const apply = applyCollectionCallback(ctx, args[1], `${namespace}.groupBy`);
|
|
23
|
-
const cursor = yield* ctx.iterate(source);
|
|
24
|
-
if (cursor === undefined) {
|
|
25
|
-
throw typeError(`${namespace}.groupBy expects an iterable collection.`);
|
|
26
|
-
}
|
|
27
|
-
if (namespace === "Map") {
|
|
28
|
-
const result = new MapObj(builtins.Map);
|
|
29
|
-
let index = 0;
|
|
30
|
-
while (true) {
|
|
31
|
-
const step = yield* cursor.next();
|
|
32
|
-
if (step.done)
|
|
33
|
-
return result;
|
|
34
|
-
const item = step.value;
|
|
35
|
-
const key = yield* preserveConsumerError(cursor.close, apply([item, index]));
|
|
36
|
-
const group = result.map.get(key);
|
|
37
|
-
if (group === undefined)
|
|
38
|
-
result.map.set(key, new Arr(builtins.Array, [item]));
|
|
39
|
-
else
|
|
40
|
-
group.items.push(item);
|
|
41
|
-
index += 1;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// Object.groupBy returns a null-prototype object, so group names never collide with inherited methods.
|
|
45
|
-
const result = new Obj(null);
|
|
46
|
-
let index = 0;
|
|
47
|
-
while (true) {
|
|
48
|
-
const step = yield* cursor.next();
|
|
49
|
-
if (step.done)
|
|
50
|
-
return result;
|
|
51
|
-
const item = step.value;
|
|
52
|
-
const key = yield* preserveConsumerError(cursor.close, coerceGroupByPropertyKey(ctx, apply([item, index])));
|
|
53
|
-
const group = getOwn(result, key);
|
|
54
|
-
if (group === undefined)
|
|
55
|
-
define(result, key, new Arr(builtins.Array, [item]));
|
|
56
|
-
else
|
|
57
|
-
group.items.push(item);
|
|
58
|
-
index += 1;
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
function* constructMap(ctx, init, proto) {
|
|
62
|
-
const target = new MapObj(proto);
|
|
63
|
-
if (init === undefined || init === null)
|
|
64
|
-
return target;
|
|
65
|
-
const cursor = yield* ctx.iterate(init);
|
|
66
|
-
if (cursor === undefined) {
|
|
67
|
-
throw typeError(`new Map(...) expects an iterable of [key, value] pairs, received ${describeValue(init)}.`);
|
|
68
|
-
}
|
|
69
|
-
while (true) {
|
|
70
|
-
const step = yield* cursor.next();
|
|
71
|
-
if (step.done)
|
|
72
|
-
return target;
|
|
73
|
-
yield* preserveConsumerError(cursor.close, addMapEntry(target, step.value));
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function* addMapEntry(target, entry) {
|
|
77
|
-
if (!(entry instanceof Obj)) {
|
|
78
|
-
throw typeError("new Map(...) expects [key, value] pairs as entry objects.");
|
|
79
|
-
}
|
|
80
|
-
target.map.set(getOwn(entry, 0), getOwn(entry, 1));
|
|
81
|
-
}
|
|
82
|
-
function* constructSet(ctx, init, proto) {
|
|
83
|
-
const target = new SetObj(proto);
|
|
84
|
-
if (init === undefined || init === null)
|
|
85
|
-
return target;
|
|
86
|
-
const cursor = yield* ctx.iterate(init);
|
|
87
|
-
if (cursor === undefined) {
|
|
88
|
-
throw typeError(`new Set(...) expects a synchronous iterable, received ${describeValue(init)}.`);
|
|
89
|
-
}
|
|
90
|
-
while (true) {
|
|
91
|
-
const step = yield* cursor.next();
|
|
92
|
-
if (step.done)
|
|
93
|
-
return target;
|
|
94
|
-
target.set.add(step.value);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
export const mapGlobal = (ctx) => {
|
|
98
|
-
const builtins = ctx.builtins;
|
|
99
|
-
const proto = builtins.Map;
|
|
100
|
-
const map = constructor(builtins, proto, {
|
|
101
|
-
name: "Map",
|
|
102
|
-
call: requiresNew("Map"),
|
|
103
|
-
construct: (args, newTarget) => constructMap(ctx, args[0], prototypeFrom(newTarget, proto)),
|
|
104
|
-
});
|
|
105
|
-
define(map, "groupBy", groupBy(ctx, "Map"), hidden);
|
|
106
|
-
const self = (thisValue, name) => receiver(MapObj, thisValue, `Map.prototype.${name}`);
|
|
107
|
-
defineAccessor(proto, "size", (thisValue) => receiver(MapObj, thisValue, "Map.prototype.size").map.size);
|
|
108
|
-
methods(builtins, proto, [
|
|
109
|
-
["get", 1, (thisValue, args) => self(thisValue, "get").map.get(args[0])],
|
|
110
|
-
["has", 1, (thisValue, args) => self(thisValue, "has").map.has(args[0])],
|
|
111
|
-
[
|
|
112
|
-
"set",
|
|
113
|
-
2,
|
|
114
|
-
(thisValue, args) => {
|
|
115
|
-
const target = self(thisValue, "set");
|
|
116
|
-
target.map.set(args[0], args[1]);
|
|
117
|
-
return target;
|
|
118
|
-
},
|
|
119
|
-
],
|
|
120
|
-
[
|
|
121
|
-
"getOrInsert",
|
|
122
|
-
2,
|
|
123
|
-
(thisValue, args) => {
|
|
124
|
-
const target = self(thisValue, "getOrInsert").map;
|
|
125
|
-
if (!target.has(args[0]))
|
|
126
|
-
target.set(args[0], args[1]);
|
|
127
|
-
return target.get(args[0]);
|
|
128
|
-
},
|
|
129
|
-
],
|
|
130
|
-
[
|
|
131
|
-
"getOrInsertComputed",
|
|
132
|
-
2,
|
|
133
|
-
function* (thisValue, args) {
|
|
134
|
-
const target = self(thisValue, "getOrInsertComputed").map;
|
|
135
|
-
const apply = applyCollectionCallback(ctx, args[1], "Map.getOrInsertComputed");
|
|
136
|
-
if (target.has(args[0]))
|
|
137
|
-
return target.get(args[0]);
|
|
138
|
-
// The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself.
|
|
139
|
-
const value = yield* apply([args[0] === 0 ? 0 : args[0]]);
|
|
140
|
-
target.set(args[0], value);
|
|
141
|
-
return value;
|
|
142
|
-
},
|
|
143
|
-
],
|
|
144
|
-
["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])],
|
|
145
|
-
[
|
|
146
|
-
"clear",
|
|
147
|
-
0,
|
|
148
|
-
(thisValue) => {
|
|
149
|
-
self(thisValue, "clear").map.clear();
|
|
150
|
-
return undefined;
|
|
151
|
-
},
|
|
152
|
-
],
|
|
153
|
-
["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").map.keys())],
|
|
154
|
-
["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").map.values())],
|
|
155
|
-
["entries", 0, (thisValue) => hostIterator(builtins, self(thisValue, "entries").iterator(builtins))],
|
|
156
|
-
[
|
|
157
|
-
"forEach",
|
|
158
|
-
1,
|
|
159
|
-
function* (thisValue, args) {
|
|
160
|
-
const target = self(thisValue, "forEach");
|
|
161
|
-
const apply = applyCollectionCallback(ctx, args[0], "Map.forEach");
|
|
162
|
-
for (const [key, item] of target.map.entries())
|
|
163
|
-
yield* apply([item, key, target], args[1]);
|
|
164
|
-
return undefined;
|
|
165
|
-
},
|
|
166
|
-
],
|
|
167
|
-
]);
|
|
168
|
-
define(proto, IteratorSymbol, get(proto, "entries"), hidden);
|
|
169
|
-
return map;
|
|
170
|
-
};
|
|
171
|
-
function* loadSetRecord(ctx, source, name) {
|
|
172
|
-
if (source instanceof SetObj) {
|
|
173
|
-
return {
|
|
174
|
-
size: source.set.size,
|
|
175
|
-
has: function* (item) {
|
|
176
|
-
return source.set.has(item);
|
|
177
|
-
},
|
|
178
|
-
keys: function* () {
|
|
179
|
-
return source.set.values();
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
if (source instanceof MapObj) {
|
|
184
|
-
return {
|
|
185
|
-
size: source.map.size,
|
|
186
|
-
has: function* (item) {
|
|
187
|
-
return source.map.has(item);
|
|
188
|
-
},
|
|
189
|
-
keys: function* () {
|
|
190
|
-
return source.map.keys();
|
|
191
|
-
},
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
if (!(source instanceof Obj)) {
|
|
195
|
-
throw typeError(`Set.${name} expects a Set-like object.`);
|
|
196
|
-
}
|
|
197
|
-
const size = yield* toPrimitiveNumber(ctx, get(source, "size"));
|
|
198
|
-
if (Number.isNaN(size)) {
|
|
199
|
-
throw typeError(`Set.${name} received a Set-like object with an invalid size.`);
|
|
200
|
-
}
|
|
201
|
-
const has = get(source, "has");
|
|
202
|
-
const keys = get(source, "keys");
|
|
203
|
-
if (!isSupportedCallback(has) || !isSupportedCallback(keys)) {
|
|
204
|
-
throw typeError(`Set.${name} expects callable 'has' and 'keys' methods.`);
|
|
205
|
-
}
|
|
206
|
-
return {
|
|
207
|
-
size: Math.max(Math.trunc(size), 0),
|
|
208
|
-
has: function* (item) {
|
|
209
|
-
return Boolean(yield* ctx.call(has, source, [item]));
|
|
210
|
-
},
|
|
211
|
-
keys: function* () {
|
|
212
|
-
const result = yield* ctx.call(keys, source, []);
|
|
213
|
-
const cursor = result instanceof Arr ? hostCursor(result.items.values()) : ctx.iterateDirect(result);
|
|
214
|
-
const items = [];
|
|
215
|
-
while (true) {
|
|
216
|
-
const step = yield* cursor.next();
|
|
217
|
-
if (step.done)
|
|
218
|
-
return items;
|
|
219
|
-
items.push(step.value);
|
|
220
|
-
}
|
|
221
|
-
},
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
function* setOperation(ctx, target, name, source) {
|
|
225
|
-
const other = yield* loadSetRecord(ctx, source, name);
|
|
226
|
-
const copy = () => {
|
|
227
|
-
const result = new SetObj(ctx.builtins.Set);
|
|
228
|
-
for (const item of target.set.values())
|
|
229
|
-
result.set.add(item);
|
|
230
|
-
return result;
|
|
231
|
-
};
|
|
232
|
-
if (name === "union") {
|
|
233
|
-
const result = copy();
|
|
234
|
-
for (const item of yield* other.keys())
|
|
235
|
-
result.set.add(item);
|
|
236
|
-
return result;
|
|
237
|
-
}
|
|
238
|
-
if (name === "intersection") {
|
|
239
|
-
const result = new SetObj(ctx.builtins.Set);
|
|
240
|
-
if (target.set.size <= other.size) {
|
|
241
|
-
for (const item of target.set.values()) {
|
|
242
|
-
if (yield* other.has(item))
|
|
243
|
-
result.set.add(item);
|
|
244
|
-
}
|
|
245
|
-
return result;
|
|
246
|
-
}
|
|
247
|
-
for (const item of yield* other.keys()) {
|
|
248
|
-
if (target.set.has(item))
|
|
249
|
-
result.set.add(item);
|
|
250
|
-
}
|
|
251
|
-
return result;
|
|
252
|
-
}
|
|
253
|
-
if (name === "difference") {
|
|
254
|
-
const result = copy();
|
|
255
|
-
if (target.set.size <= other.size) {
|
|
256
|
-
for (const item of result.set.values()) {
|
|
257
|
-
if (yield* other.has(item))
|
|
258
|
-
result.set.delete(item);
|
|
259
|
-
}
|
|
260
|
-
return result;
|
|
261
|
-
}
|
|
262
|
-
for (const item of yield* other.keys())
|
|
263
|
-
result.set.delete(item);
|
|
264
|
-
return result;
|
|
265
|
-
}
|
|
266
|
-
if (name === "symmetricDifference") {
|
|
267
|
-
const result = copy();
|
|
268
|
-
for (const item of yield* other.keys()) {
|
|
269
|
-
if (target.set.has(item))
|
|
270
|
-
result.set.delete(item);
|
|
271
|
-
else
|
|
272
|
-
result.set.add(item);
|
|
273
|
-
}
|
|
274
|
-
return result;
|
|
275
|
-
}
|
|
276
|
-
if (name === "isSubsetOf") {
|
|
277
|
-
if (target.set.size > other.size)
|
|
278
|
-
return false;
|
|
279
|
-
for (const item of target.set.values()) {
|
|
280
|
-
if (!(yield* other.has(item)))
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
return true;
|
|
284
|
-
}
|
|
285
|
-
if (name === "isSupersetOf") {
|
|
286
|
-
if (target.set.size < other.size)
|
|
287
|
-
return false;
|
|
288
|
-
for (const item of yield* other.keys()) {
|
|
289
|
-
if (!target.set.has(item))
|
|
290
|
-
return false;
|
|
291
|
-
}
|
|
292
|
-
return true;
|
|
293
|
-
}
|
|
294
|
-
if (target.set.size <= other.size) {
|
|
295
|
-
for (const item of target.set.values()) {
|
|
296
|
-
if (yield* other.has(item))
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
299
|
-
return true;
|
|
300
|
-
}
|
|
301
|
-
for (const item of yield* other.keys()) {
|
|
302
|
-
if (target.set.has(item))
|
|
303
|
-
return false;
|
|
304
|
-
}
|
|
305
|
-
return true;
|
|
306
|
-
}
|
|
307
|
-
export const setGlobal = (ctx) => {
|
|
308
|
-
const builtins = ctx.builtins;
|
|
309
|
-
const proto = builtins.Set;
|
|
310
|
-
const set = constructor(builtins, proto, {
|
|
311
|
-
name: "Set",
|
|
312
|
-
call: requiresNew("Set"),
|
|
313
|
-
construct: (args, newTarget) => constructSet(ctx, args[0], prototypeFrom(newTarget, proto)),
|
|
314
|
-
});
|
|
315
|
-
const self = (thisValue, name) => receiver(SetObj, thisValue, `Set.prototype.${name}`);
|
|
316
|
-
const wrap = (items) => new Arr(builtins.Array, items);
|
|
317
|
-
const operation = (name) => [
|
|
318
|
-
name,
|
|
319
|
-
1,
|
|
320
|
-
(thisValue, args) => setOperation(ctx, self(thisValue, name), name, args[0]),
|
|
321
|
-
];
|
|
322
|
-
defineAccessor(proto, "size", (thisValue) => receiver(SetObj, thisValue, "Set.prototype.size").set.size);
|
|
323
|
-
methods(builtins, proto, [
|
|
324
|
-
["has", 1, (thisValue, args) => self(thisValue, "has").set.has(args[0])],
|
|
325
|
-
[
|
|
326
|
-
"add",
|
|
327
|
-
1,
|
|
328
|
-
(thisValue, args) => {
|
|
329
|
-
const target = self(thisValue, "add");
|
|
330
|
-
target.set.add(args[0]);
|
|
331
|
-
return target;
|
|
332
|
-
},
|
|
333
|
-
],
|
|
334
|
-
["delete", 1, (thisValue, args) => self(thisValue, "delete").set.delete(args[0])],
|
|
335
|
-
[
|
|
336
|
-
"clear",
|
|
337
|
-
0,
|
|
338
|
-
(thisValue) => {
|
|
339
|
-
self(thisValue, "clear").set.clear();
|
|
340
|
-
return undefined;
|
|
341
|
-
},
|
|
342
|
-
],
|
|
343
|
-
["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").set.values())],
|
|
344
|
-
["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").set.values())],
|
|
345
|
-
[
|
|
346
|
-
"entries",
|
|
347
|
-
0,
|
|
348
|
-
(thisValue) => hostIterator(builtins, self(thisValue, "entries")
|
|
349
|
-
.set.values()
|
|
350
|
-
.map((item) => wrap([item, item]))),
|
|
351
|
-
],
|
|
352
|
-
[
|
|
353
|
-
"forEach",
|
|
354
|
-
1,
|
|
355
|
-
function* (thisValue, args) {
|
|
356
|
-
const target = self(thisValue, "forEach");
|
|
357
|
-
const apply = applyCollectionCallback(ctx, args[0], "Set.forEach");
|
|
358
|
-
for (const item of target.set.values())
|
|
359
|
-
yield* apply([item, item, target], args[1]);
|
|
360
|
-
return undefined;
|
|
361
|
-
},
|
|
362
|
-
],
|
|
363
|
-
operation("union"),
|
|
364
|
-
operation("intersection"),
|
|
365
|
-
operation("difference"),
|
|
366
|
-
operation("symmetricDifference"),
|
|
367
|
-
operation("isSubsetOf"),
|
|
368
|
-
operation("isSupersetOf"),
|
|
369
|
-
operation("isDisjointFrom"),
|
|
370
|
-
]);
|
|
371
|
-
define(proto, IteratorSymbol, get(proto, "values"), hidden);
|
|
372
|
-
return set;
|
|
373
|
-
};
|
|
374
|
-
// CanBeHeldWeakly: only program objects; tool references are rebuilt on every access, so they could never be found again.
|
|
375
|
-
const weakKey = (value, label) => {
|
|
376
|
-
if (value instanceof Obj)
|
|
377
|
-
return value;
|
|
378
|
-
throw typeError(`Invalid value used ${label}: ${describeValue(value)} cannot be held weakly.`);
|
|
379
|
-
};
|
|
380
|
-
export const weakMapGlobal = (ctx) => {
|
|
381
|
-
const builtins = ctx.builtins;
|
|
382
|
-
const proto = builtins.WeakMap;
|
|
383
|
-
const weakMap = constructor(builtins, proto, {
|
|
384
|
-
name: "WeakMap",
|
|
385
|
-
call: requiresNew("WeakMap"),
|
|
386
|
-
construct: function* (args, newTarget) {
|
|
387
|
-
const target = new WeakMapObj(prototypeFrom(newTarget, proto));
|
|
388
|
-
if (args[0] === undefined || args[0] === null)
|
|
389
|
-
return target;
|
|
390
|
-
const cursor = yield* ctx.iterate(args[0]);
|
|
391
|
-
if (cursor === undefined) {
|
|
392
|
-
throw typeError(`new WeakMap(...) expects an iterable of [key, value] pairs, received ${describeValue(args[0])}.`);
|
|
393
|
-
}
|
|
394
|
-
while (true) {
|
|
395
|
-
const step = yield* cursor.next();
|
|
396
|
-
if (step.done)
|
|
397
|
-
return target;
|
|
398
|
-
yield* preserveConsumerError(cursor.close, addWeakMapEntry(target, step.value));
|
|
399
|
-
}
|
|
400
|
-
},
|
|
401
|
-
});
|
|
402
|
-
const self = (thisValue, name) => receiver(WeakMapObj, thisValue, `WeakMap.prototype.${name}`).map;
|
|
403
|
-
// Lookups pass any key through: the host collection answers false for a non-object, as the spec requires.
|
|
404
|
-
const key = (value) => weakKey(value, "as weak map key");
|
|
405
|
-
methods(builtins, proto, [
|
|
406
|
-
[
|
|
407
|
-
"get",
|
|
408
|
-
1,
|
|
409
|
-
(thisValue, args) => {
|
|
410
|
-
const target = self(thisValue, "get");
|
|
411
|
-
return args[0] instanceof Obj ? target.get(args[0]) : undefined;
|
|
412
|
-
},
|
|
413
|
-
],
|
|
414
|
-
["has", 1, (thisValue, args) => self(thisValue, "has").has(args[0])],
|
|
415
|
-
["delete", 1, (thisValue, args) => self(thisValue, "delete").delete(args[0])],
|
|
416
|
-
[
|
|
417
|
-
"set",
|
|
418
|
-
2,
|
|
419
|
-
(thisValue, args) => {
|
|
420
|
-
self(thisValue, "set").set(key(args[0]), args[1]);
|
|
421
|
-
return thisValue;
|
|
422
|
-
},
|
|
423
|
-
],
|
|
424
|
-
[
|
|
425
|
-
"getOrInsert",
|
|
426
|
-
2,
|
|
427
|
-
(thisValue, args) => {
|
|
428
|
-
const target = self(thisValue, "getOrInsert");
|
|
429
|
-
const k = key(args[0]);
|
|
430
|
-
if (!target.has(k))
|
|
431
|
-
target.set(k, args[1]);
|
|
432
|
-
return target.get(k);
|
|
433
|
-
},
|
|
434
|
-
],
|
|
435
|
-
[
|
|
436
|
-
"getOrInsertComputed",
|
|
437
|
-
2,
|
|
438
|
-
function* (thisValue, args) {
|
|
439
|
-
const target = self(thisValue, "getOrInsertComputed");
|
|
440
|
-
const k = key(args[0]);
|
|
441
|
-
const apply = applyCollectionCallback(ctx, args[1], "WeakMap.getOrInsertComputed");
|
|
442
|
-
if (target.has(k))
|
|
443
|
-
return target.get(k);
|
|
444
|
-
const value = yield* apply([k]);
|
|
445
|
-
target.set(k, value);
|
|
446
|
-
return value;
|
|
447
|
-
},
|
|
448
|
-
],
|
|
449
|
-
]);
|
|
450
|
-
return weakMap;
|
|
451
|
-
};
|
|
452
|
-
function* addWeakMapEntry(target, entry) {
|
|
453
|
-
if (!(entry instanceof Obj)) {
|
|
454
|
-
throw typeError("new WeakMap(...) expects [key, value] pairs as entry objects.");
|
|
455
|
-
}
|
|
456
|
-
target.map.set(weakKey(getOwn(entry, 0), "as weak map key"), getOwn(entry, 1));
|
|
457
|
-
}
|
|
458
|
-
export const weakSetGlobal = (ctx) => {
|
|
459
|
-
const builtins = ctx.builtins;
|
|
460
|
-
const proto = builtins.WeakSet;
|
|
461
|
-
const weakSet = constructor(builtins, proto, {
|
|
462
|
-
name: "WeakSet",
|
|
463
|
-
call: requiresNew("WeakSet"),
|
|
464
|
-
construct: function* (args, newTarget) {
|
|
465
|
-
const target = new WeakSetObj(prototypeFrom(newTarget, proto));
|
|
466
|
-
if (args[0] === undefined || args[0] === null)
|
|
467
|
-
return target;
|
|
468
|
-
const cursor = yield* ctx.iterate(args[0]);
|
|
469
|
-
if (cursor === undefined) {
|
|
470
|
-
throw typeError(`new WeakSet(...) expects a synchronous iterable, received ${describeValue(args[0])}.`);
|
|
471
|
-
}
|
|
472
|
-
while (true) {
|
|
473
|
-
const step = yield* cursor.next();
|
|
474
|
-
if (step.done)
|
|
475
|
-
return target;
|
|
476
|
-
yield* preserveConsumerError(cursor.close, addWeakSetValue(target, step.value));
|
|
477
|
-
}
|
|
478
|
-
},
|
|
479
|
-
});
|
|
480
|
-
const self = (thisValue, name) => receiver(WeakSetObj, thisValue, `WeakSet.prototype.${name}`).set;
|
|
481
|
-
methods(builtins, proto, [
|
|
482
|
-
["has", 1, (thisValue, args) => self(thisValue, "has").has(args[0])],
|
|
483
|
-
["delete", 1, (thisValue, args) => self(thisValue, "delete").delete(args[0])],
|
|
484
|
-
[
|
|
485
|
-
"add",
|
|
486
|
-
1,
|
|
487
|
-
(thisValue, args) => {
|
|
488
|
-
self(thisValue, "add").add(weakKey(args[0], "in weak set"));
|
|
489
|
-
return thisValue;
|
|
490
|
-
},
|
|
491
|
-
],
|
|
492
|
-
]);
|
|
493
|
-
return weakSet;
|
|
494
|
-
};
|
|
495
|
-
function* addWeakSetValue(target, value) {
|
|
496
|
-
target.set.add(weakKey(value, "in weak set"));
|
|
497
|
-
}
|
package/dist/stdlib/console.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Obj, type Value } from "../interpreter/objects.js";
|
|
2
|
-
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
3
|
-
/** Captured console: every method appends one formatted line to `logs`. */
|
|
4
|
-
export declare const consoleGlobal: <R>(ctx: Interpreter<R>) => Obj;
|
|
5
|
-
/** One value as `console.log` shows it. */
|
|
6
|
-
export declare const formatValue: (value: Value) => string;
|
package/dist/stdlib/console.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { methods } from "../interpreter/native.js";
|
|
2
|
-
import { entries, get, Arr, Obj } from "../interpreter/objects.js";
|
|
3
|
-
import { ToolReference } from "../tool-runtime.js";
|
|
4
|
-
import { containsOpaqueReference } from "../interpreter/references.js";
|
|
5
|
-
const consoleMethods = ["log", "info", "debug", "warn", "error", "dir", "table"];
|
|
6
|
-
/** Captured console: every method appends one formatted line to `logs`. */
|
|
7
|
-
export const consoleGlobal = (ctx) => {
|
|
8
|
-
const builtins = ctx.builtins;
|
|
9
|
-
const console = new Obj(builtins.Object);
|
|
10
|
-
methods(builtins, console, consoleMethods.map((name) => [
|
|
11
|
-
name,
|
|
12
|
-
0,
|
|
13
|
-
(_, args) => {
|
|
14
|
-
ctx.logs.push(formatConsoleMessage(name, args));
|
|
15
|
-
return undefined;
|
|
16
|
-
},
|
|
17
|
-
]));
|
|
18
|
-
return console;
|
|
19
|
-
};
|
|
20
|
-
const MAX_CONSOLE_DEPTH = 32;
|
|
21
|
-
const formatConsoleMessage = (name, args) => {
|
|
22
|
-
if (name === "dir")
|
|
23
|
-
return args.length === 0 ? "undefined" : formatValue(args[0]);
|
|
24
|
-
if (name === "table")
|
|
25
|
-
return formatConsoleTable(args[0], args[1]);
|
|
26
|
-
const prefix = name === "warn" ? "[warn] " : name === "error" ? "[error] " : name === "debug" ? "[debug] " : "";
|
|
27
|
-
return `${prefix}${args.map((arg) => formatValue(arg)).join(" ")}`;
|
|
28
|
-
};
|
|
29
|
-
/** One value as `console.log` shows it. */
|
|
30
|
-
export const formatValue = (value) => {
|
|
31
|
-
if (value === undefined)
|
|
32
|
-
return "undefined";
|
|
33
|
-
if (typeof value === "string")
|
|
34
|
-
return value;
|
|
35
|
-
return formatConsoleValue(value, new Set(), 0);
|
|
36
|
-
};
|
|
37
|
-
const formatConsoleValue = (value, seen, depth) => {
|
|
38
|
-
if (value === null || value === undefined)
|
|
39
|
-
return "null";
|
|
40
|
-
if (typeof value === "string")
|
|
41
|
-
return JSON.stringify(value);
|
|
42
|
-
if (!(value instanceof Obj))
|
|
43
|
-
return value instanceof ToolReference ? "[opaque reference]" : String(value);
|
|
44
|
-
if (depth > MAX_CONSOLE_DEPTH)
|
|
45
|
-
return "...";
|
|
46
|
-
if (seen.has(value))
|
|
47
|
-
return "[Circular]";
|
|
48
|
-
seen.add(value);
|
|
49
|
-
try {
|
|
50
|
-
return value.inspect((item) => formatConsoleValue(item, seen, depth + 1));
|
|
51
|
-
}
|
|
52
|
-
finally {
|
|
53
|
-
seen.delete(value);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
const formatConsoleTable = (value, columnsArgument) => {
|
|
57
|
-
if (value === undefined)
|
|
58
|
-
return "undefined";
|
|
59
|
-
if (containsOpaqueReference(value))
|
|
60
|
-
return "[opaque reference]";
|
|
61
|
-
const columns = columnsArgument instanceof Arr ? columnsArgument.items.map(String) : undefined;
|
|
62
|
-
const rows = consoleTableRows(value, columns);
|
|
63
|
-
const keys = columns ?? Array.from(new Set(rows.flatMap((row) => Object.keys(row.values))));
|
|
64
|
-
const header = ["(index)", ...keys].join("\t");
|
|
65
|
-
return [
|
|
66
|
-
header,
|
|
67
|
-
...rows.map((row) => [row.index, ...keys.map((key) => formatConsoleTableCell(row.values[key]))].join("\t")),
|
|
68
|
-
].join("\n");
|
|
69
|
-
};
|
|
70
|
-
const consoleTableRows = (data, columns) => {
|
|
71
|
-
if (data instanceof Arr) {
|
|
72
|
-
return data.items.map((item, index) => ({ index: String(index), values: consoleTableValues(item, columns) }));
|
|
73
|
-
}
|
|
74
|
-
if (data instanceof Obj) {
|
|
75
|
-
return entries(data).map(([index, item]) => ({ index, values: consoleTableValues(item, columns) }));
|
|
76
|
-
}
|
|
77
|
-
return [{ index: "0", values: { Value: data } }];
|
|
78
|
-
};
|
|
79
|
-
const consoleTableValues = (value, columns) => {
|
|
80
|
-
if (value instanceof Obj && !(value instanceof Arr)) {
|
|
81
|
-
if (columns !== undefined)
|
|
82
|
-
return Object.fromEntries(columns.map((column) => [column, get(value, column)]));
|
|
83
|
-
return Object.fromEntries(entries(value));
|
|
84
|
-
}
|
|
85
|
-
return { Value: value };
|
|
86
|
-
};
|
|
87
|
-
const formatConsoleTableCell = (value) => {
|
|
88
|
-
if (value === undefined)
|
|
89
|
-
return "";
|
|
90
|
-
if (typeof value === "string")
|
|
91
|
-
return value;
|
|
92
|
-
return formatConsoleValue(value, new Set(), 0);
|
|
93
|
-
};
|
package/dist/stdlib/date.d.ts
DELETED