@opencode/codemode 0.0.0-dev-19355 → 0.0.0-dev-19358
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/codemode.d.ts +8 -11
- package/dist/codemode.js +4 -8
- package/dist/data.d.ts +28 -0
- package/dist/data.js +130 -0
- package/dist/interpreter/errors.d.ts +1 -1
- package/dist/interpreter/errors.js +2 -2
- package/dist/interpreter/execute.d.ts +3 -3
- package/dist/interpreter/execute.js +9 -15
- package/dist/interpreter/methods.js +9 -8
- package/dist/interpreter/model.d.ts +7 -27
- package/dist/interpreter/model.js +0 -31
- package/dist/interpreter/runtime.d.ts +2 -2
- package/dist/interpreter/runtime.js +200 -235
- package/dist/openapi/spec.js +1 -1
- package/dist/stdlib/console.js +4 -4
- package/dist/stdlib/json.js +13 -15
- package/dist/stdlib/number.js +3 -2
- package/dist/stdlib/object.js +5 -4
- package/dist/stdlib/regexp.js +1 -1
- package/dist/stdlib/url.js +3 -2
- package/dist/stdlib/value.d.ts +1 -2
- package/dist/stdlib/value.js +3 -4
- package/dist/tool-runtime.d.ts +15 -16
- package/dist/tool-runtime.js +12 -141
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Cause, Deferred, Effect, Exit } from "effect";
|
|
2
|
-
import { isBlockedMember,
|
|
3
|
-
import {
|
|
2
|
+
import { isBlockedMember, ToolRuntimeError, toProgram } from "../data.js";
|
|
3
|
+
import { ToolReference } from "../tool-runtime.js";
|
|
4
|
+
import { AsyncIteratorSymbol, CodeModeFunction, CodeModeGenerator, CoercionFunction, ComputedValue, ErrorConstructorReference, GlobalMethodReference, GlobalNamespace, GeneratorMethodReference, GeneratorReturn, IntrinsicReference, InterpreterRuntimeError, isRecord, IteratorSymbol, IteratorSymbols, JsonMethodReference, OptionalShortCircuit, PromiseCapabilityFunction, PromiseInstanceMethodReference, PromiseMethodReference, PromiseNamespace, ProgramThrow, SearchFunction, SymbolNamespace, unsupportedSyntax, UriFunction, } from "./model.js";
|
|
4
5
|
import { caughtErrorValue, constructAggregateErrorValue, constructErrorValue } from "./errors.js";
|
|
5
6
|
import { arrayStatics, invokeArrayFrom, invokeGlobalMethod, invokeGroupBy, invokeIntrinsic, toPrimitive, } from "./methods.js";
|
|
6
7
|
import { preserveConsumerError } from "./iterator.js";
|
|
@@ -18,7 +19,7 @@ import { promiseStatics } from "../stdlib/promise.js";
|
|
|
18
19
|
import { escapeRegexHint, regexpMethods, regexpProperties, regexpStatics, regexFailureReason, } from "../stdlib/regexp.js";
|
|
19
20
|
import { stringMethods, stringStatics } from "../stdlib/string.js";
|
|
20
21
|
import { urlMethods, urlProperties, urlSearchParamsMethods, urlStatics, urlWritableProperties, invokeUriFunction, uriArgument, urlArgument, } from "../stdlib/url.js";
|
|
21
|
-
import {
|
|
22
|
+
import { coerceToNumber, coerceToString, compoundOperators, errorBrandName, errorConstructors, invokeCoercion, valueConstructors, } from "../stdlib/value.js";
|
|
22
23
|
import { Values } from "../values.js";
|
|
23
24
|
const globalStaticMembers = {
|
|
24
25
|
Object: objectStatics,
|
|
@@ -39,18 +40,18 @@ const parseArrayIndex = (key) => {
|
|
|
39
40
|
return index < MAX_ARRAY_LENGTH ? index : undefined;
|
|
40
41
|
};
|
|
41
42
|
const calleeDescription = (callee) => {
|
|
42
|
-
if (callee
|
|
43
|
-
return
|
|
44
|
-
if (callee
|
|
45
|
-
const object =
|
|
46
|
-
const property =
|
|
47
|
-
const key = callee.computed
|
|
48
|
-
?
|
|
43
|
+
if (callee?.type === "Identifier")
|
|
44
|
+
return callee.name;
|
|
45
|
+
if (callee?.type === "MemberExpression") {
|
|
46
|
+
const object = callee.object;
|
|
47
|
+
const property = callee.property;
|
|
48
|
+
const key = !callee.computed && property.type === "Identifier"
|
|
49
|
+
? property.name
|
|
49
50
|
: property.type === "Literal" && typeof property.value === "string"
|
|
50
51
|
? property.value
|
|
51
52
|
: undefined;
|
|
52
53
|
if (object.type === "Identifier" && key !== undefined)
|
|
53
|
-
return `${
|
|
54
|
+
return `${object.name}.${key}`;
|
|
54
55
|
}
|
|
55
56
|
return "The called value";
|
|
56
57
|
};
|
|
@@ -89,24 +90,23 @@ const instanceofValue = (lhs, rhs, node) => {
|
|
|
89
90
|
const collectPatternNames = (pattern, out = []) => {
|
|
90
91
|
switch (pattern.type) {
|
|
91
92
|
case "Identifier":
|
|
92
|
-
out.push(
|
|
93
|
+
out.push(pattern.name);
|
|
93
94
|
break;
|
|
94
95
|
case "AssignmentPattern":
|
|
95
|
-
collectPatternNames(
|
|
96
|
+
collectPatternNames(pattern.left, out);
|
|
96
97
|
break;
|
|
97
98
|
case "RestElement":
|
|
98
|
-
collectPatternNames(
|
|
99
|
+
collectPatternNames(pattern.argument, out);
|
|
99
100
|
break;
|
|
100
101
|
case "ArrayPattern":
|
|
101
|
-
for (const element of
|
|
102
|
+
for (const element of pattern.elements) {
|
|
102
103
|
if (element !== null)
|
|
103
|
-
collectPatternNames(
|
|
104
|
+
collectPatternNames(element, out);
|
|
104
105
|
}
|
|
105
106
|
break;
|
|
106
107
|
case "ObjectPattern":
|
|
107
|
-
for (const
|
|
108
|
-
|
|
109
|
-
collectPatternNames(prop.type === "RestElement" ? getNode(prop, "argument") : getNode(prop, "value"), out);
|
|
108
|
+
for (const prop of pattern.properties) {
|
|
109
|
+
collectPatternNames(prop.type === "RestElement" ? prop.argument : prop.value, out);
|
|
110
110
|
}
|
|
111
111
|
break;
|
|
112
112
|
}
|
|
@@ -115,13 +115,13 @@ const collectPatternNames = (pattern, out = []) => {
|
|
|
115
115
|
const loopDeclaration = (left, statement) => {
|
|
116
116
|
if (left.type !== "VariableDeclaration")
|
|
117
117
|
return undefined;
|
|
118
|
-
const
|
|
119
|
-
if (
|
|
118
|
+
const declaration = left.declarations.length === 1 ? left.declarations[0] : undefined;
|
|
119
|
+
if (declaration === undefined) {
|
|
120
120
|
throw new InterpreterRuntimeError(`${statement} supports one declared binding.`, left);
|
|
121
121
|
}
|
|
122
|
-
const kind =
|
|
122
|
+
const kind = left.kind;
|
|
123
123
|
return {
|
|
124
|
-
pattern:
|
|
124
|
+
pattern: declaration.id,
|
|
125
125
|
mutable: kind !== "const",
|
|
126
126
|
lexical: kind !== "var",
|
|
127
127
|
};
|
|
@@ -139,7 +139,7 @@ const copyIteratorSymbols = (source, target, consumed) => {
|
|
|
139
139
|
Reflect.set(target, symbol, Reflect.get(source, symbol));
|
|
140
140
|
}
|
|
141
141
|
};
|
|
142
|
-
const promiseResolutionNode = { type: "PromiseResolution" };
|
|
142
|
+
const promiseResolutionNode = { type: "PromiseResolution", start: 0, end: 0 };
|
|
143
143
|
export class Interpreter {
|
|
144
144
|
scopes;
|
|
145
145
|
executeTool;
|
|
@@ -206,7 +206,7 @@ export class Interpreter {
|
|
|
206
206
|
let value = undefined;
|
|
207
207
|
for (const [index, statement] of program.body.entries()) {
|
|
208
208
|
if (index === program.body.length - 1 && statement.type === "ExpressionStatement") {
|
|
209
|
-
value = yield* self.evaluateExpression(
|
|
209
|
+
value = yield* self.evaluateExpression(statement.expression);
|
|
210
210
|
break;
|
|
211
211
|
}
|
|
212
212
|
const result = yield* self.evaluateStatement(statement);
|
|
@@ -241,11 +241,11 @@ export class Interpreter {
|
|
|
241
241
|
evaluateStatement(node) {
|
|
242
242
|
switch (node.type) {
|
|
243
243
|
case "ExpressionStatement":
|
|
244
|
-
return Effect.as(this.evaluateExpression(
|
|
244
|
+
return Effect.as(this.evaluateExpression(node.expression), { kind: "none" });
|
|
245
245
|
case "VariableDeclaration":
|
|
246
246
|
return Effect.map(this.evaluateVariableDeclaration(node), () => ({ kind: "none" }));
|
|
247
247
|
case "ReturnStatement": {
|
|
248
|
-
const argumentNode =
|
|
248
|
+
const argumentNode = node.argument;
|
|
249
249
|
return argumentNode
|
|
250
250
|
? Effect.map(this.evaluateExpression(argumentNode), (value) => ({ kind: "return", value }))
|
|
251
251
|
: Effect.succeed({ kind: "return", value: undefined });
|
|
@@ -288,11 +288,10 @@ export class Interpreter {
|
|
|
288
288
|
this.scopes.push();
|
|
289
289
|
const self = this;
|
|
290
290
|
return Effect.gen(function* () {
|
|
291
|
-
const body =
|
|
291
|
+
const body = node.body;
|
|
292
292
|
self.predeclareLexical(body);
|
|
293
293
|
self.hoistFunctions(body);
|
|
294
|
-
for (const
|
|
295
|
-
const statement = asNode(statementValue, "body");
|
|
294
|
+
for (const statement of body) {
|
|
296
295
|
const result = yield* self.evaluateStatement(statement);
|
|
297
296
|
if (result.kind !== "none") {
|
|
298
297
|
return result;
|
|
@@ -302,27 +301,24 @@ export class Interpreter {
|
|
|
302
301
|
}).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop())));
|
|
303
302
|
}
|
|
304
303
|
createFunction(node) {
|
|
305
|
-
return new CodeModeFunction(
|
|
304
|
+
return new CodeModeFunction(node.params, node.body, this.scopes.capture(), node.async, node.generator);
|
|
306
305
|
}
|
|
307
306
|
hoistFunctions(statements) {
|
|
308
|
-
for (const
|
|
309
|
-
if (
|
|
307
|
+
for (const node of statements) {
|
|
308
|
+
if (node.type !== "FunctionDeclaration")
|
|
310
309
|
continue;
|
|
311
|
-
|
|
312
|
-
this.scopes.declare(getString(getNode(node, "id"), "name"), this.createFunction(node), true, node);
|
|
310
|
+
this.scopes.declare(node.id.name, this.createFunction(node), true, node);
|
|
313
311
|
}
|
|
314
312
|
}
|
|
315
313
|
predeclareLexical(statements) {
|
|
316
|
-
for (const
|
|
317
|
-
if (
|
|
314
|
+
for (const statement of statements) {
|
|
315
|
+
if (statement.type !== "VariableDeclaration")
|
|
318
316
|
continue;
|
|
319
|
-
const
|
|
320
|
-
const kind = getString(statement, "kind");
|
|
317
|
+
const kind = statement.kind;
|
|
321
318
|
if (kind === "var")
|
|
322
319
|
continue;
|
|
323
|
-
for (const
|
|
324
|
-
const
|
|
325
|
-
for (const name of collectPatternNames(getNode(declaration, "id"))) {
|
|
320
|
+
for (const declaration of statement.declarations) {
|
|
321
|
+
for (const name of collectPatternNames(declaration.id)) {
|
|
326
322
|
this.scopes.reserve(name, kind !== "const", declaration);
|
|
327
323
|
}
|
|
328
324
|
}
|
|
@@ -333,30 +329,27 @@ export class Interpreter {
|
|
|
333
329
|
this.scopes.reserve(name, mutable, node);
|
|
334
330
|
}
|
|
335
331
|
evaluateIfStatement(node) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
? this.evaluateStatement(consequentNode)
|
|
341
|
-
: alternateNode
|
|
342
|
-
? this.evaluateStatement(alternateNode)
|
|
332
|
+
return Effect.flatMap(this.evaluateExpression(node.test), (test) => test
|
|
333
|
+
? this.evaluateStatement(node.consequent)
|
|
334
|
+
: node.alternate
|
|
335
|
+
? this.evaluateStatement(node.alternate)
|
|
343
336
|
: Effect.succeed({ kind: "none" }));
|
|
344
337
|
}
|
|
345
338
|
evaluateSwitchStatement(node) {
|
|
346
339
|
const self = this;
|
|
347
340
|
return Effect.gen(function* () {
|
|
348
|
-
const discriminant = yield* self.evaluateExpression(
|
|
341
|
+
const discriminant = yield* self.evaluateExpression(node.discriminant);
|
|
349
342
|
if (containsOpaqueReference(discriminant)) {
|
|
350
343
|
throw new InterpreterRuntimeError("Switch discriminants must be data values.", node, "InvalidDataValue");
|
|
351
344
|
}
|
|
352
345
|
self.scopes.push();
|
|
353
346
|
return yield* Effect.gen(function* () {
|
|
354
|
-
const cases =
|
|
355
|
-
self.predeclareLexical(cases.flatMap((branch) =>
|
|
347
|
+
const cases = node.cases;
|
|
348
|
+
self.predeclareLexical(cases.flatMap((branch) => branch.consequent));
|
|
356
349
|
let defaultIndex;
|
|
357
350
|
let selected;
|
|
358
351
|
for (const [index, branch] of cases.entries()) {
|
|
359
|
-
const test =
|
|
352
|
+
const test = branch.test;
|
|
360
353
|
if (!test) {
|
|
361
354
|
defaultIndex = index;
|
|
362
355
|
continue;
|
|
@@ -374,8 +367,8 @@ export class Interpreter {
|
|
|
374
367
|
if (start === undefined)
|
|
375
368
|
return { kind: "none" };
|
|
376
369
|
for (let index = start; index < cases.length; index += 1) {
|
|
377
|
-
for (const
|
|
378
|
-
const result = yield* self.evaluateStatement(
|
|
370
|
+
for (const statement of cases[index].consequent) {
|
|
371
|
+
const result = yield* self.evaluateStatement(statement);
|
|
379
372
|
if (result.kind === "break") {
|
|
380
373
|
if (result.label === undefined)
|
|
381
374
|
return { kind: "none" };
|
|
@@ -390,12 +383,10 @@ export class Interpreter {
|
|
|
390
383
|
});
|
|
391
384
|
}
|
|
392
385
|
evaluateWhileStatement(node, labels) {
|
|
393
|
-
const testNode = getNode(node, "test");
|
|
394
|
-
const bodyNode = getNode(node, "body");
|
|
395
386
|
const self = this;
|
|
396
387
|
return Effect.gen(function* () {
|
|
397
|
-
while (yield* self.evaluateExpression(
|
|
398
|
-
const result = yield* self.evaluateStatement(
|
|
388
|
+
while (yield* self.evaluateExpression(node.test)) {
|
|
389
|
+
const result = yield* self.evaluateStatement(node.body);
|
|
399
390
|
if (result.kind === "continue") {
|
|
400
391
|
if (result.label !== undefined && !labels?.has(result.label))
|
|
401
392
|
return result;
|
|
@@ -414,12 +405,10 @@ export class Interpreter {
|
|
|
414
405
|
});
|
|
415
406
|
}
|
|
416
407
|
evaluateDoWhileStatement(node, labels) {
|
|
417
|
-
const bodyNode = getNode(node, "body");
|
|
418
|
-
const testNode = getNode(node, "test");
|
|
419
408
|
const self = this;
|
|
420
409
|
return Effect.gen(function* () {
|
|
421
410
|
do {
|
|
422
|
-
const result = yield* self.evaluateStatement(
|
|
411
|
+
const result = yield* self.evaluateStatement(node.body);
|
|
423
412
|
if (result.kind === "continue") {
|
|
424
413
|
if (result.label !== undefined && !labels?.has(result.label))
|
|
425
414
|
return result;
|
|
@@ -433,7 +422,7 @@ export class Interpreter {
|
|
|
433
422
|
if (result.kind === "return") {
|
|
434
423
|
return result;
|
|
435
424
|
}
|
|
436
|
-
} while (yield* self.evaluateExpression(
|
|
425
|
+
} while (yield* self.evaluateExpression(node.test));
|
|
437
426
|
return { kind: "none" };
|
|
438
427
|
});
|
|
439
428
|
}
|
|
@@ -441,11 +430,10 @@ export class Interpreter {
|
|
|
441
430
|
this.scopes.push();
|
|
442
431
|
const self = this;
|
|
443
432
|
return Effect.gen(function* () {
|
|
444
|
-
const initNode =
|
|
445
|
-
const testNode =
|
|
446
|
-
const updateNode =
|
|
447
|
-
|
|
448
|
-
if (initNode?.type === "VariableDeclaration" && getString(initNode, "kind") !== "var") {
|
|
433
|
+
const initNode = node.init;
|
|
434
|
+
const testNode = node.test;
|
|
435
|
+
const updateNode = node.update;
|
|
436
|
+
if (initNode?.type === "VariableDeclaration" && initNode.kind !== "var") {
|
|
449
437
|
self.predeclareLexical([initNode]);
|
|
450
438
|
}
|
|
451
439
|
if (initNode) {
|
|
@@ -456,7 +444,7 @@ export class Interpreter {
|
|
|
456
444
|
yield* self.evaluateExpression(initNode);
|
|
457
445
|
}
|
|
458
446
|
}
|
|
459
|
-
const perIterationBindings = initNode?.type === "VariableDeclaration" &&
|
|
447
|
+
const perIterationBindings = initNode?.type === "VariableDeclaration" && initNode.kind !== "var"
|
|
460
448
|
? Array.from(self.scopes.current().keys())
|
|
461
449
|
: [];
|
|
462
450
|
const nextIteration = () => {
|
|
@@ -468,7 +456,7 @@ export class Interpreter {
|
|
|
468
456
|
};
|
|
469
457
|
nextIteration();
|
|
470
458
|
while (testNode ? yield* self.evaluateExpression(testNode) : true) {
|
|
471
|
-
const result = yield* self.evaluateStatement(
|
|
459
|
+
const result = yield* self.evaluateStatement(node.body);
|
|
472
460
|
if (result.kind === "return") {
|
|
473
461
|
return result;
|
|
474
462
|
}
|
|
@@ -491,8 +479,8 @@ export class Interpreter {
|
|
|
491
479
|
}).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop())));
|
|
492
480
|
}
|
|
493
481
|
evaluateForOfStatement(node, labels) {
|
|
494
|
-
const awaiting =
|
|
495
|
-
const left =
|
|
482
|
+
const awaiting = node.await;
|
|
483
|
+
const left = node.left;
|
|
496
484
|
const declared = loopDeclaration(left, "for...of");
|
|
497
485
|
if (declared?.lexical)
|
|
498
486
|
this.scopes.push();
|
|
@@ -500,8 +488,7 @@ export class Interpreter {
|
|
|
500
488
|
return Effect.gen(function* () {
|
|
501
489
|
if (declared?.lexical)
|
|
502
490
|
self.predeclarePattern(declared.pattern, declared.mutable, left);
|
|
503
|
-
const right = yield* self.evaluateExpression(
|
|
504
|
-
const body = getNode(node, "body");
|
|
491
|
+
const right = yield* self.evaluateExpression(node.right);
|
|
505
492
|
const iterator = yield* self.customIterator(right, node, awaiting);
|
|
506
493
|
const cursor = iterator === undefined ? yield* self.syncIterator(right, node) : undefined;
|
|
507
494
|
if (iterator === undefined && cursor === undefined) {
|
|
@@ -512,17 +499,10 @@ export class Interpreter {
|
|
|
512
499
|
: awaiting
|
|
513
500
|
? Effect.andThen(cursor?.close ?? Effect.void, Effect.yieldNow)
|
|
514
501
|
: (cursor?.close ?? Effect.void);
|
|
515
|
-
|
|
516
|
-
if (left.type !== "VariableDeclaration" &&
|
|
517
|
-
(left.type === "Identifier" ||
|
|
518
|
-
left.type === "MemberExpression" ||
|
|
519
|
-
left.type === "ArrayPattern" ||
|
|
520
|
-
left.type === "ObjectPattern")) {
|
|
521
|
-
assignment = left;
|
|
522
|
-
}
|
|
523
|
-
else if (left.type !== "VariableDeclaration") {
|
|
502
|
+
if (left.type === "RestElement" || left.type === "AssignmentPattern") {
|
|
524
503
|
throw new InterpreterRuntimeError("Unsupported for...of binding.", left);
|
|
525
504
|
}
|
|
505
|
+
const assignment = left.type === "VariableDeclaration" ? undefined : left;
|
|
526
506
|
const evaluateBody = (value) => Effect.gen(function* () {
|
|
527
507
|
if (declared) {
|
|
528
508
|
self.scopes.push();
|
|
@@ -533,7 +513,7 @@ export class Interpreter {
|
|
|
533
513
|
else if (assignment) {
|
|
534
514
|
yield* self.assignPattern(assignment, value, left);
|
|
535
515
|
}
|
|
536
|
-
return yield* self.evaluateStatement(body);
|
|
516
|
+
return yield* self.evaluateStatement(node.body);
|
|
537
517
|
}).pipe(Effect.ensuring(Effect.sync(() => {
|
|
538
518
|
if (declared)
|
|
539
519
|
self.scopes.pop();
|
|
@@ -733,7 +713,7 @@ export class Interpreter {
|
|
|
733
713
|
return undefined;
|
|
734
714
|
}
|
|
735
715
|
evaluateForInStatement(node, labels) {
|
|
736
|
-
const left =
|
|
716
|
+
const left = node.left;
|
|
737
717
|
const declared = loopDeclaration(left, "for...in");
|
|
738
718
|
if (declared?.lexical)
|
|
739
719
|
this.scopes.push();
|
|
@@ -741,19 +721,15 @@ export class Interpreter {
|
|
|
741
721
|
return Effect.gen(function* () {
|
|
742
722
|
if (declared?.lexical)
|
|
743
723
|
self.predeclarePattern(declared.pattern, declared.mutable, left);
|
|
744
|
-
const right = yield* self.evaluateExpression(
|
|
745
|
-
const body = getNode(node, "body");
|
|
724
|
+
const right = yield* self.evaluateExpression(node.right);
|
|
746
725
|
const keys = self.enumerableKeys(right);
|
|
747
726
|
if (keys === undefined) {
|
|
748
727
|
throw new InterpreterRuntimeError("for...in requires a plain object, array, or tools reference. Use for...of for arrays/strings/Maps/Sets, or Object.keys(value) for a key list.", node);
|
|
749
728
|
}
|
|
750
|
-
|
|
751
|
-
if (left.type === "Identifier") {
|
|
752
|
-
assignmentName = getString(left, "name");
|
|
753
|
-
}
|
|
754
|
-
else if (left.type !== "VariableDeclaration") {
|
|
729
|
+
if (left.type !== "Identifier" && left.type !== "VariableDeclaration") {
|
|
755
730
|
throw new InterpreterRuntimeError("Unsupported for...in binding.", left);
|
|
756
731
|
}
|
|
732
|
+
const assignmentName = left.type === "Identifier" ? left.name : undefined;
|
|
757
733
|
for (const key of keys) {
|
|
758
734
|
const result = yield* Effect.gen(function* () {
|
|
759
735
|
if (declared) {
|
|
@@ -765,7 +741,7 @@ export class Interpreter {
|
|
|
765
741
|
else if (assignmentName) {
|
|
766
742
|
self.scopes.set(assignmentName, key, left);
|
|
767
743
|
}
|
|
768
|
-
return yield* self.evaluateStatement(body);
|
|
744
|
+
return yield* self.evaluateStatement(node.body);
|
|
769
745
|
}).pipe(Effect.ensuring(Effect.sync(() => {
|
|
770
746
|
if (declared)
|
|
771
747
|
self.scopes.pop();
|
|
@@ -791,19 +767,17 @@ export class Interpreter {
|
|
|
791
767
|
})));
|
|
792
768
|
}
|
|
793
769
|
evaluateBreakStatement(node) {
|
|
794
|
-
|
|
795
|
-
return labelNode ? { kind: "break", label: getString(labelNode, "name") } : { kind: "break" };
|
|
770
|
+
return node.label ? { kind: "break", label: node.label.name } : { kind: "break" };
|
|
796
771
|
}
|
|
797
772
|
evaluateContinueStatement(node) {
|
|
798
|
-
|
|
799
|
-
return labelNode ? { kind: "continue", label: getString(labelNode, "name") } : { kind: "continue" };
|
|
773
|
+
return node.label ? { kind: "continue", label: node.label.name } : { kind: "continue" };
|
|
800
774
|
}
|
|
801
775
|
evaluateLabeledStatement(node) {
|
|
802
776
|
const labels = new Set();
|
|
803
777
|
let body = node;
|
|
804
778
|
while (body.type === "LabeledStatement") {
|
|
805
|
-
labels.add(
|
|
806
|
-
body =
|
|
779
|
+
labels.add(body.label.name);
|
|
780
|
+
body = body.body;
|
|
807
781
|
}
|
|
808
782
|
const evaluated = (() => {
|
|
809
783
|
if (body.type === "WhileStatement")
|
|
@@ -823,13 +797,12 @@ export class Interpreter {
|
|
|
823
797
|
: result);
|
|
824
798
|
}
|
|
825
799
|
evaluateThrowStatement(node) {
|
|
826
|
-
|
|
827
|
-
return Effect.flatMap(this.evaluateExpression(argument), (value) => Effect.fail(new ProgramThrow(value)));
|
|
800
|
+
return Effect.flatMap(this.evaluateExpression(node.argument), (value) => Effect.fail(new ProgramThrow(value)));
|
|
828
801
|
}
|
|
829
802
|
evaluateTryStatement(node) {
|
|
830
|
-
const body =
|
|
831
|
-
const handler =
|
|
832
|
-
const finalizer =
|
|
803
|
+
const body = node.block;
|
|
804
|
+
const handler = node.handler;
|
|
805
|
+
const finalizer = node.finalizer;
|
|
833
806
|
const self = this;
|
|
834
807
|
const attempted = Effect.matchCauseEffect(this.evaluateStatement(body), {
|
|
835
808
|
onFailure: (cause) => {
|
|
@@ -837,12 +810,12 @@ export class Interpreter {
|
|
|
837
810
|
return Effect.failCause(cause);
|
|
838
811
|
}
|
|
839
812
|
const caught = caughtErrorValue(Cause.squash(cause));
|
|
840
|
-
const parameter =
|
|
813
|
+
const parameter = handler.param;
|
|
841
814
|
self.scopes.push();
|
|
842
815
|
return Effect.gen(function* () {
|
|
843
816
|
if (parameter)
|
|
844
817
|
yield* self.declarePattern(parameter, caught, true, handler);
|
|
845
|
-
return yield* self.evaluateStatement(
|
|
818
|
+
return yield* self.evaluateStatement(handler.body);
|
|
846
819
|
}).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop())));
|
|
847
820
|
},
|
|
848
821
|
onSuccess: Effect.succeed,
|
|
@@ -858,18 +831,16 @@ export class Interpreter {
|
|
|
858
831
|
});
|
|
859
832
|
}
|
|
860
833
|
evaluateVariableDeclaration(node) {
|
|
861
|
-
const kind =
|
|
862
|
-
const declarations = getArray(node, "declarations");
|
|
834
|
+
const kind = node.kind;
|
|
863
835
|
const self = this;
|
|
864
836
|
return Effect.gen(function* () {
|
|
865
|
-
for (const
|
|
866
|
-
const declaration = asNode(declarationValue, "declarations");
|
|
837
|
+
for (const declaration of node.declarations) {
|
|
867
838
|
if (declaration.type !== "VariableDeclarator") {
|
|
868
839
|
throw new InterpreterRuntimeError("Unsupported variable declaration shape.", declaration);
|
|
869
840
|
}
|
|
870
|
-
const init =
|
|
841
|
+
const init = declaration.init;
|
|
871
842
|
const value = init ? yield* self.evaluateExpression(init) : undefined;
|
|
872
|
-
yield* self.declarePattern(
|
|
843
|
+
yield* self.declarePattern(declaration.id, value, kind !== "const", declaration, kind !== "var");
|
|
873
844
|
}
|
|
874
845
|
});
|
|
875
846
|
}
|
|
@@ -877,7 +848,7 @@ export class Interpreter {
|
|
|
877
848
|
const self = this;
|
|
878
849
|
return Effect.gen(function* () {
|
|
879
850
|
if (pattern.type === "Identifier") {
|
|
880
|
-
const name =
|
|
851
|
+
const name = pattern.name;
|
|
881
852
|
if (initialize)
|
|
882
853
|
self.scopes.initialize(name, value, node);
|
|
883
854
|
else
|
|
@@ -885,8 +856,8 @@ export class Interpreter {
|
|
|
885
856
|
return;
|
|
886
857
|
}
|
|
887
858
|
if (pattern.type === "AssignmentPattern") {
|
|
888
|
-
const resolved = value === undefined ? yield* self.evaluateExpression(
|
|
889
|
-
yield* self.declarePattern(
|
|
859
|
+
const resolved = value === undefined ? yield* self.evaluateExpression(pattern.right) : value;
|
|
860
|
+
yield* self.declarePattern(pattern.left, resolved, mutable, node, initialize);
|
|
890
861
|
return;
|
|
891
862
|
}
|
|
892
863
|
if (pattern.type === "ObjectPattern") {
|
|
@@ -894,8 +865,7 @@ export class Interpreter {
|
|
|
894
865
|
throw new InterpreterRuntimeError("Object destructuring requires a data object or array value.", pattern, "InvalidDataValue");
|
|
895
866
|
}
|
|
896
867
|
const consumed = new Set();
|
|
897
|
-
for (const
|
|
898
|
-
const property = asNode(propertyValue, "properties");
|
|
868
|
+
for (const property of pattern.properties) {
|
|
899
869
|
if (property.type === "RestElement") {
|
|
900
870
|
const rest = Object.create(null);
|
|
901
871
|
for (const [key, item] of Object.entries(value)) {
|
|
@@ -903,7 +873,7 @@ export class Interpreter {
|
|
|
903
873
|
rest[key] = item;
|
|
904
874
|
}
|
|
905
875
|
copyIteratorSymbols(value, rest, consumed);
|
|
906
|
-
yield* self.declarePattern(
|
|
876
|
+
yield* self.declarePattern(property.argument, rest, mutable, property, initialize);
|
|
907
877
|
continue;
|
|
908
878
|
}
|
|
909
879
|
const key = yield* self.destructuringPropertyKey(property);
|
|
@@ -911,7 +881,7 @@ export class Interpreter {
|
|
|
911
881
|
throw new InterpreterRuntimeError(`Property '${String(key)}' is not available.`, property);
|
|
912
882
|
}
|
|
913
883
|
consumed.add(typeof key === "symbol" ? key : String(key));
|
|
914
|
-
yield* self.declarePattern(
|
|
884
|
+
yield* self.declarePattern(property.value, self.destructuringPropertyValue(value, key), mutable, property, initialize);
|
|
915
885
|
}
|
|
916
886
|
return;
|
|
917
887
|
}
|
|
@@ -925,7 +895,7 @@ export class Interpreter {
|
|
|
925
895
|
const self = this;
|
|
926
896
|
return Effect.gen(function* () {
|
|
927
897
|
if (pattern.type === "Identifier") {
|
|
928
|
-
self.scopes.set(
|
|
898
|
+
self.scopes.set(pattern.name, value, pattern);
|
|
929
899
|
return;
|
|
930
900
|
}
|
|
931
901
|
if (pattern.type === "MemberExpression") {
|
|
@@ -933,8 +903,8 @@ export class Interpreter {
|
|
|
933
903
|
return;
|
|
934
904
|
}
|
|
935
905
|
if (pattern.type === "AssignmentPattern") {
|
|
936
|
-
const resolved = value === undefined ? yield* self.evaluateExpression(
|
|
937
|
-
yield* self.assignPattern(
|
|
906
|
+
const resolved = value === undefined ? yield* self.evaluateExpression(pattern.right) : value;
|
|
907
|
+
yield* self.assignPattern(pattern.left, resolved, node);
|
|
938
908
|
return;
|
|
939
909
|
}
|
|
940
910
|
if (pattern.type === "ObjectPattern") {
|
|
@@ -943,8 +913,7 @@ export class Interpreter {
|
|
|
943
913
|
}
|
|
944
914
|
const source = value;
|
|
945
915
|
const consumed = new Set();
|
|
946
|
-
for (const
|
|
947
|
-
const property = asNode(propertyValue, "properties");
|
|
916
|
+
for (const property of pattern.properties) {
|
|
948
917
|
if (property.type === "RestElement") {
|
|
949
918
|
const rest = Object.create(null);
|
|
950
919
|
for (const [key, item] of Object.entries(source)) {
|
|
@@ -952,7 +921,7 @@ export class Interpreter {
|
|
|
952
921
|
rest[key] = item;
|
|
953
922
|
}
|
|
954
923
|
copyIteratorSymbols(source, rest, consumed);
|
|
955
|
-
yield* self.assignPattern(
|
|
924
|
+
yield* self.assignPattern(property.argument, rest, property);
|
|
956
925
|
continue;
|
|
957
926
|
}
|
|
958
927
|
const key = yield* self.destructuringPropertyKey(property);
|
|
@@ -960,7 +929,7 @@ export class Interpreter {
|
|
|
960
929
|
throw new InterpreterRuntimeError(`Property '${String(key)}' is not available.`, property);
|
|
961
930
|
}
|
|
962
931
|
consumed.add(typeof key === "symbol" ? key : String(key));
|
|
963
|
-
yield* self.assignPattern(
|
|
932
|
+
yield* self.assignPattern(property.value, self.destructuringPropertyValue(source, key), property);
|
|
964
933
|
}
|
|
965
934
|
return;
|
|
966
935
|
}
|
|
@@ -978,21 +947,19 @@ export class Interpreter {
|
|
|
978
947
|
throw new InterpreterRuntimeError("Array destructuring requires a supported iterable value.", pattern).as("TypeError");
|
|
979
948
|
}
|
|
980
949
|
let done = false;
|
|
981
|
-
for (const
|
|
950
|
+
for (const element of pattern.elements) {
|
|
982
951
|
if (done) {
|
|
983
|
-
if (
|
|
952
|
+
if (element === null)
|
|
984
953
|
continue;
|
|
985
|
-
|
|
986
|
-
yield* consume(element.type === "RestElement" ? getNode(element, "argument") : element, element.type === "RestElement" ? [] : undefined, element);
|
|
954
|
+
yield* consume(element.type === "RestElement" ? element.argument : element, element.type === "RestElement" ? [] : undefined, element);
|
|
987
955
|
if (element.type === "RestElement")
|
|
988
956
|
return;
|
|
989
957
|
continue;
|
|
990
958
|
}
|
|
991
959
|
const step = yield* cursor.next;
|
|
992
960
|
done = step.done;
|
|
993
|
-
if (
|
|
961
|
+
if (element === null)
|
|
994
962
|
continue;
|
|
995
|
-
const element = asNode(item, `elements[${index}]`);
|
|
996
963
|
if (element.type === "RestElement") {
|
|
997
964
|
const rest = [];
|
|
998
965
|
if (!step.done)
|
|
@@ -1003,7 +970,7 @@ export class Interpreter {
|
|
|
1003
970
|
if (!done)
|
|
1004
971
|
rest.push(next.value);
|
|
1005
972
|
}
|
|
1006
|
-
yield* consume(
|
|
973
|
+
yield* consume(element.argument, rest, element);
|
|
1007
974
|
return;
|
|
1008
975
|
}
|
|
1009
976
|
const consumed = consume(element, step.done ? undefined : step.value, pattern);
|
|
@@ -1014,14 +981,18 @@ export class Interpreter {
|
|
|
1014
981
|
});
|
|
1015
982
|
}
|
|
1016
983
|
destructuringPropertyKey(property) {
|
|
1017
|
-
if (property.type !== "Property" ||
|
|
984
|
+
if (property.type !== "Property" || property.kind !== "init") {
|
|
1018
985
|
throw new InterpreterRuntimeError("Unsupported object destructuring property.", property);
|
|
1019
986
|
}
|
|
1020
|
-
const keyNode =
|
|
1021
|
-
if (
|
|
987
|
+
const keyNode = property.key;
|
|
988
|
+
if (property.computed) {
|
|
1022
989
|
return Effect.map(this.evaluateExpression(keyNode), (value) => this.toPropertyKey(value, keyNode));
|
|
1023
990
|
}
|
|
1024
|
-
|
|
991
|
+
if (keyNode.type === "Identifier")
|
|
992
|
+
return Effect.succeed(keyNode.name);
|
|
993
|
+
if (keyNode.type === "Literal")
|
|
994
|
+
return Effect.succeed(String(keyNode.value));
|
|
995
|
+
throw unsupportedSyntax(keyNode.type, keyNode);
|
|
1025
996
|
}
|
|
1026
997
|
destructuringPropertyValue(source, key) {
|
|
1027
998
|
if (!Array.isArray(source))
|
|
@@ -1040,13 +1011,12 @@ export class Interpreter {
|
|
|
1040
1011
|
switch (node.type) {
|
|
1041
1012
|
case "Literal": {
|
|
1042
1013
|
const regex = node.regex;
|
|
1043
|
-
if (
|
|
1044
|
-
return Effect.sync(() => this.constructRegExp([regex.pattern,
|
|
1045
|
-
|
|
1046
|
-
return Effect.sync(() => boundedData(node.value, "Literal"));
|
|
1014
|
+
if (regex)
|
|
1015
|
+
return Effect.sync(() => this.constructRegExp([regex.pattern, regex.flags], node));
|
|
1016
|
+
return Effect.sync(() => toProgram(node.value, "Literal"));
|
|
1047
1017
|
}
|
|
1048
1018
|
case "Identifier":
|
|
1049
|
-
return Effect.sync(() => this.scopes.get(
|
|
1019
|
+
return Effect.sync(() => this.scopes.get(node.name, node));
|
|
1050
1020
|
case "BinaryExpression":
|
|
1051
1021
|
return this.evaluateBinaryExpression(node);
|
|
1052
1022
|
case "LogicalExpression":
|
|
@@ -1059,8 +1029,8 @@ export class Interpreter {
|
|
|
1059
1029
|
const self = this;
|
|
1060
1030
|
return Effect.gen(function* () {
|
|
1061
1031
|
let result;
|
|
1062
|
-
for (const expression of
|
|
1063
|
-
result = yield* self.evaluateExpression(
|
|
1032
|
+
for (const expression of node.expressions) {
|
|
1033
|
+
result = yield* self.evaluateExpression(expression);
|
|
1064
1034
|
}
|
|
1065
1035
|
return result;
|
|
1066
1036
|
});
|
|
@@ -1073,7 +1043,7 @@ export class Interpreter {
|
|
|
1073
1043
|
case "MemberExpression":
|
|
1074
1044
|
return this.readMember(node);
|
|
1075
1045
|
case "ChainExpression":
|
|
1076
|
-
return Effect.map(this.evaluateExpression(
|
|
1046
|
+
return Effect.map(this.evaluateExpression(node.expression), (value) => value === OptionalShortCircuit ? undefined : value);
|
|
1077
1047
|
case "ObjectExpression":
|
|
1078
1048
|
return this.evaluateObjectExpression(node);
|
|
1079
1049
|
case "ArrayExpression":
|
|
@@ -1086,7 +1056,7 @@ export class Interpreter {
|
|
|
1086
1056
|
return this.evaluateUpdateExpression(node);
|
|
1087
1057
|
case "AwaitExpression": {
|
|
1088
1058
|
// Await always suspends, including for plain values.
|
|
1089
|
-
return Effect.flatMap(this.evaluateExpression(
|
|
1059
|
+
return Effect.flatMap(this.evaluateExpression(node.argument), (value) => this.awaitValue(value, node));
|
|
1090
1060
|
}
|
|
1091
1061
|
case "YieldExpression":
|
|
1092
1062
|
return this.evaluateYieldExpression(node);
|
|
@@ -1097,12 +1067,12 @@ export class Interpreter {
|
|
|
1097
1067
|
}
|
|
1098
1068
|
}
|
|
1099
1069
|
evaluateNewExpression(node) {
|
|
1100
|
-
const callee =
|
|
1070
|
+
const callee = node.callee;
|
|
1101
1071
|
if (callee.type !== "Identifier") {
|
|
1102
1072
|
throw unsupportedSyntax("NewExpression", node);
|
|
1103
1073
|
}
|
|
1104
|
-
const name =
|
|
1105
|
-
const argNodes =
|
|
1074
|
+
const name = callee.name;
|
|
1075
|
+
const argNodes = node.arguments;
|
|
1106
1076
|
const self = this;
|
|
1107
1077
|
if (name === "Promise") {
|
|
1108
1078
|
return Effect.flatMap(this.evaluateCallArguments(argNodes), (args) => constructPromise(self.runner, self.promises, args[0], node));
|
|
@@ -1278,7 +1248,7 @@ export class Interpreter {
|
|
|
1278
1248
|
}
|
|
1279
1249
|
if (Values.isValue(init))
|
|
1280
1250
|
return new Values.URLSearchParams(new URLSearchParams());
|
|
1281
|
-
const data =
|
|
1251
|
+
const data = toProgram(init, "new URLSearchParams input");
|
|
1282
1252
|
if (data === null || typeof data !== "object") {
|
|
1283
1253
|
throw new InterpreterRuntimeError("new URLSearchParams(...) expects a query string, data object, iterable pairs, or URLSearchParams.", node).as("TypeError");
|
|
1284
1254
|
}
|
|
@@ -1302,14 +1272,17 @@ export class Interpreter {
|
|
|
1302
1272
|
});
|
|
1303
1273
|
}
|
|
1304
1274
|
evaluateBinaryExpression(node) {
|
|
1305
|
-
const operator =
|
|
1275
|
+
const operator = node.operator;
|
|
1276
|
+
const left = node.left;
|
|
1277
|
+
if (left.type === "PrivateIdentifier")
|
|
1278
|
+
throw unsupportedSyntax(left.type, left);
|
|
1306
1279
|
const self = this;
|
|
1307
1280
|
return Effect.gen(function* () {
|
|
1308
|
-
const lhs = yield* self.evaluateExpression(
|
|
1309
|
-
const rhs = yield* self.evaluateExpression(
|
|
1281
|
+
const lhs = yield* self.evaluateExpression(left);
|
|
1282
|
+
const rhs = yield* self.evaluateExpression(node.right);
|
|
1310
1283
|
if (operator === "instanceof")
|
|
1311
1284
|
return instanceofValue(lhs, rhs, node);
|
|
1312
|
-
return
|
|
1285
|
+
return toProgram(self.applyBinaryOperator(operator, lhs, rhs, node), "Binary expression result");
|
|
1313
1286
|
});
|
|
1314
1287
|
}
|
|
1315
1288
|
applyBinaryOperator(operator, lhs, rhs, node) {
|
|
@@ -1379,26 +1352,24 @@ export class Interpreter {
|
|
|
1379
1352
|
}
|
|
1380
1353
|
}
|
|
1381
1354
|
evaluateLogicalExpression(node) {
|
|
1382
|
-
const operator =
|
|
1383
|
-
return Effect.flatMap(this.evaluateExpression(
|
|
1355
|
+
const operator = node.operator;
|
|
1356
|
+
return Effect.flatMap(this.evaluateExpression(node.left), (left) => {
|
|
1384
1357
|
if (operator === "&&")
|
|
1385
|
-
return left ? this.evaluateExpression(
|
|
1358
|
+
return left ? this.evaluateExpression(node.right) : Effect.succeed(left);
|
|
1386
1359
|
if (operator === "||")
|
|
1387
|
-
return left ? Effect.succeed(left) : this.evaluateExpression(
|
|
1360
|
+
return left ? Effect.succeed(left) : this.evaluateExpression(node.right);
|
|
1388
1361
|
if (operator === "??")
|
|
1389
|
-
return left !== null && left !== undefined
|
|
1390
|
-
? Effect.succeed(left)
|
|
1391
|
-
: this.evaluateExpression(getNode(node, "right"));
|
|
1362
|
+
return left !== null && left !== undefined ? Effect.succeed(left) : this.evaluateExpression(node.right);
|
|
1392
1363
|
throw new InterpreterRuntimeError(`Unsupported logical operator '${operator}'.`, node);
|
|
1393
1364
|
});
|
|
1394
1365
|
}
|
|
1395
1366
|
evaluateUnaryExpression(node) {
|
|
1396
|
-
const operator =
|
|
1397
|
-
const argument =
|
|
1367
|
+
const operator = node.operator;
|
|
1368
|
+
const argument = node.argument;
|
|
1398
1369
|
if (operator === "delete")
|
|
1399
1370
|
return this.evaluateDeleteExpression(argument);
|
|
1400
1371
|
// Undeclared names short-circuit, but declared TDZ bindings must still throw.
|
|
1401
|
-
if (operator === "typeof" && argument.type === "Identifier" && !this.scopes.resolve(
|
|
1372
|
+
if (operator === "typeof" && argument.type === "Identifier" && !this.scopes.resolve(argument.name)) {
|
|
1402
1373
|
return Effect.succeed("undefined");
|
|
1403
1374
|
}
|
|
1404
1375
|
return Effect.map(this.evaluateExpression(argument), (value) => {
|
|
@@ -1430,38 +1401,38 @@ export class Interpreter {
|
|
|
1430
1401
|
default:
|
|
1431
1402
|
throw new InterpreterRuntimeError(`Unsupported unary operator '${operator}'.`, node);
|
|
1432
1403
|
}
|
|
1433
|
-
return
|
|
1404
|
+
return toProgram(result, "Unary expression result");
|
|
1434
1405
|
});
|
|
1435
1406
|
}
|
|
1436
1407
|
evaluateAssignmentExpression(node) {
|
|
1437
|
-
const left =
|
|
1438
|
-
const operator =
|
|
1408
|
+
const left = node.left;
|
|
1409
|
+
const operator = node.operator;
|
|
1439
1410
|
const self = this;
|
|
1440
1411
|
return Effect.gen(function* () {
|
|
1441
1412
|
if (operator === "??=" || operator === "||=" || operator === "&&=") {
|
|
1442
1413
|
return yield* self.evaluateLogicalAssignment(node, left, operator);
|
|
1443
1414
|
}
|
|
1444
1415
|
if (operator === "=" && (left.type === "ObjectPattern" || left.type === "ArrayPattern")) {
|
|
1445
|
-
const rightValue = yield* self.evaluateExpression(
|
|
1416
|
+
const rightValue = yield* self.evaluateExpression(node.right);
|
|
1446
1417
|
yield* self.assignPattern(left, rightValue, node);
|
|
1447
1418
|
return rightValue;
|
|
1448
1419
|
}
|
|
1449
1420
|
if (left.type === "Identifier") {
|
|
1450
|
-
const name =
|
|
1421
|
+
const name = left.name;
|
|
1451
1422
|
if (operator !== "=") {
|
|
1452
1423
|
const current = self.scopes.get(name, left);
|
|
1453
|
-
const rightValue = yield* self.evaluateExpression(
|
|
1454
|
-
const next =
|
|
1424
|
+
const rightValue = yield* self.evaluateExpression(node.right);
|
|
1425
|
+
const next = toProgram(self.applyCompoundAssignment(operator, current, rightValue, node), "Assignment result");
|
|
1455
1426
|
return self.scopes.set(name, next, left);
|
|
1456
1427
|
}
|
|
1457
|
-
const rightValue = yield* self.evaluateExpression(
|
|
1428
|
+
const rightValue = yield* self.evaluateExpression(node.right);
|
|
1458
1429
|
return self.scopes.set(name, rightValue, left);
|
|
1459
1430
|
}
|
|
1460
1431
|
if (left.type === "MemberExpression") {
|
|
1461
|
-
return yield* self.modifyMember(left, (current) => Effect.map(self.evaluateExpression(
|
|
1432
|
+
return yield* self.modifyMember(left, (current) => Effect.map(self.evaluateExpression(node.right), (rightValue) => {
|
|
1462
1433
|
if (operator === "=")
|
|
1463
1434
|
return { write: true, next: rightValue, result: rightValue };
|
|
1464
|
-
const next =
|
|
1435
|
+
const next = toProgram(self.applyCompoundAssignment(operator, current, rightValue, node), "Assignment result");
|
|
1465
1436
|
return { write: true, next, result: next };
|
|
1466
1437
|
}));
|
|
1467
1438
|
}
|
|
@@ -1472,18 +1443,18 @@ export class Interpreter {
|
|
|
1472
1443
|
const self = this;
|
|
1473
1444
|
const shouldAssign = (current) => operator === "??=" ? current === null || current === undefined : operator === "||=" ? !current : Boolean(current);
|
|
1474
1445
|
if (left.type === "Identifier") {
|
|
1475
|
-
const name =
|
|
1446
|
+
const name = left.name;
|
|
1476
1447
|
return Effect.gen(function* () {
|
|
1477
1448
|
const current = self.scopes.get(name, left);
|
|
1478
1449
|
if (!shouldAssign(current))
|
|
1479
1450
|
return current;
|
|
1480
|
-
const rightValue = yield* self.evaluateExpression(
|
|
1451
|
+
const rightValue = yield* self.evaluateExpression(node.right);
|
|
1481
1452
|
return self.scopes.set(name, rightValue, left);
|
|
1482
1453
|
});
|
|
1483
1454
|
}
|
|
1484
1455
|
if (left.type === "MemberExpression") {
|
|
1485
1456
|
return self.modifyMember(left, (current) => shouldAssign(current)
|
|
1486
|
-
? Effect.map(self.evaluateExpression(
|
|
1457
|
+
? Effect.map(self.evaluateExpression(node.right), (rightValue) => ({
|
|
1487
1458
|
write: true,
|
|
1488
1459
|
next: rightValue,
|
|
1489
1460
|
result: rightValue,
|
|
@@ -1493,9 +1464,9 @@ export class Interpreter {
|
|
|
1493
1464
|
throw new InterpreterRuntimeError("Assignment target must be an Identifier or MemberExpression.", left);
|
|
1494
1465
|
}
|
|
1495
1466
|
evaluateUpdateExpression(node) {
|
|
1496
|
-
const operator =
|
|
1497
|
-
const argument =
|
|
1498
|
-
const prefix =
|
|
1467
|
+
const operator = node.operator;
|
|
1468
|
+
const argument = node.argument;
|
|
1469
|
+
const prefix = node.prefix;
|
|
1499
1470
|
const increment = operator === "++" ? 1 : operator === "--" ? -1 : undefined;
|
|
1500
1471
|
if (increment === undefined) {
|
|
1501
1472
|
throw new InterpreterRuntimeError(`Unsupported update operator '${operator}'.`, node);
|
|
@@ -1510,7 +1481,7 @@ export class Interpreter {
|
|
|
1510
1481
|
};
|
|
1511
1482
|
if (argument.type === "Identifier") {
|
|
1512
1483
|
return Effect.sync(() => {
|
|
1513
|
-
const name =
|
|
1484
|
+
const name = argument.name;
|
|
1514
1485
|
const current = operand(this.scopes.get(name, argument));
|
|
1515
1486
|
const next = current + increment;
|
|
1516
1487
|
this.scopes.set(name, next, argument);
|
|
@@ -1527,26 +1498,28 @@ export class Interpreter {
|
|
|
1527
1498
|
throw new InterpreterRuntimeError("Update target must be an Identifier or MemberExpression.", argument);
|
|
1528
1499
|
}
|
|
1529
1500
|
evaluateCallExpression(node) {
|
|
1530
|
-
const callee =
|
|
1531
|
-
const argNodes = getArray(node, "arguments");
|
|
1501
|
+
const callee = node.callee;
|
|
1532
1502
|
const self = this;
|
|
1533
1503
|
return Effect.gen(function* () {
|
|
1504
|
+
if (callee.type === "Super")
|
|
1505
|
+
throw unsupportedSyntax(callee.type, callee);
|
|
1534
1506
|
const callable = yield* self.evaluateExpression(callee);
|
|
1535
1507
|
if (callable === OptionalShortCircuit)
|
|
1536
1508
|
return OptionalShortCircuit;
|
|
1537
|
-
if ((callable === null || callable === undefined) && node.optional
|
|
1509
|
+
if ((callable === null || callable === undefined) && node.optional)
|
|
1538
1510
|
return OptionalShortCircuit;
|
|
1539
|
-
const args = yield* self.evaluateCallArguments(
|
|
1511
|
+
const args = yield* self.evaluateCallArguments(node.arguments);
|
|
1540
1512
|
return yield* self.invokeCallable(callable, args, node, callee);
|
|
1541
1513
|
});
|
|
1542
1514
|
}
|
|
1543
1515
|
// The single dispatch for every invocation: call expressions and callbacks share it.
|
|
1544
|
-
invokeCallable(callable, args, node, callee
|
|
1516
|
+
invokeCallable(callable, args, node, callee) {
|
|
1545
1517
|
const self = this;
|
|
1546
1518
|
return Effect.gen(function* () {
|
|
1547
1519
|
if (callable instanceof ToolReference) {
|
|
1548
|
-
if (callable.path.length === 0)
|
|
1549
|
-
throw new InterpreterRuntimeError("The tools root is not callable.", callee);
|
|
1520
|
+
if (callable.path.length === 0) {
|
|
1521
|
+
throw new InterpreterRuntimeError("The tools root is not callable.", callee ?? node);
|
|
1522
|
+
}
|
|
1550
1523
|
return yield* self.createToolCallPromise(callable.path, args);
|
|
1551
1524
|
}
|
|
1552
1525
|
if (callable instanceof PromiseMethodReference) {
|
|
@@ -1590,13 +1563,13 @@ export class Interpreter {
|
|
|
1590
1563
|
if (callable.namespace === "Array" && callable.name === "of") {
|
|
1591
1564
|
return invokeGlobalMethod(callable, args, node);
|
|
1592
1565
|
}
|
|
1593
|
-
return
|
|
1566
|
+
return toProgram(invokeGlobalMethod(callable, args, node), `${callable.namespace}.${callable.name} result`);
|
|
1594
1567
|
}
|
|
1595
1568
|
if (callable instanceof JsonMethodReference) {
|
|
1596
1569
|
return yield* invokeJsonMethod(self.runner, callable.name, args, node);
|
|
1597
1570
|
}
|
|
1598
1571
|
if (callable instanceof CoercionFunction) {
|
|
1599
|
-
return
|
|
1572
|
+
return toProgram(invokeCoercion(callable, args, node), `${callable.name} result`);
|
|
1600
1573
|
}
|
|
1601
1574
|
if (callable instanceof UriFunction) {
|
|
1602
1575
|
return invokeUriFunction(callable, args, node);
|
|
@@ -1637,14 +1610,14 @@ export class Interpreter {
|
|
|
1637
1610
|
return undefined;
|
|
1638
1611
|
}
|
|
1639
1612
|
if (callable === undefined || callable === null) {
|
|
1640
|
-
throw new InterpreterRuntimeError(`${calleeDescription(callee)} is not a function.`, callee).as("TypeError");
|
|
1613
|
+
throw new InterpreterRuntimeError(`${calleeDescription(callee)} is not a function.`, callee ?? node).as("TypeError");
|
|
1641
1614
|
}
|
|
1642
|
-
throw new InterpreterRuntimeError("Only tools are callable here.", callee);
|
|
1615
|
+
throw new InterpreterRuntimeError("Only tools are callable here.", callee ?? node);
|
|
1643
1616
|
});
|
|
1644
1617
|
}
|
|
1645
1618
|
invokeObjectMethodOnTools(name, ref, node) {
|
|
1646
1619
|
if (name === "keys") {
|
|
1647
|
-
return
|
|
1620
|
+
return toProgram(this.enumerableKeys(ref), "Object.keys result");
|
|
1648
1621
|
}
|
|
1649
1622
|
throw new InterpreterRuntimeError(`Object.${name}(...) cannot read tool references: they are not plain data. Use Object.keys(tools) for names, or search({ query }) for signatures.`, node, "InvalidDataValue");
|
|
1650
1623
|
}
|
|
@@ -1658,10 +1631,9 @@ export class Interpreter {
|
|
|
1658
1631
|
const self = this;
|
|
1659
1632
|
return Effect.gen(function* () {
|
|
1660
1633
|
const args = [];
|
|
1661
|
-
for (const
|
|
1662
|
-
const argNode = asNode(arg, `arguments[${index}]`);
|
|
1634
|
+
for (const argNode of argNodes) {
|
|
1663
1635
|
if (argNode.type === "SpreadElement") {
|
|
1664
|
-
const spread = yield* self.evaluateExpression(
|
|
1636
|
+
const spread = yield* self.evaluateExpression(argNode.argument);
|
|
1665
1637
|
const cursor = yield* self.syncIterator(spread, argNode);
|
|
1666
1638
|
if (cursor === undefined)
|
|
1667
1639
|
throw new InterpreterRuntimeError("Spread arguments require a synchronous iterable.", argNode).as("TypeError");
|
|
@@ -1692,7 +1664,7 @@ export class Interpreter {
|
|
|
1692
1664
|
}
|
|
1693
1665
|
for (const [index, parameter] of fn.parameters.entries()) {
|
|
1694
1666
|
if (parameter.type === "RestElement") {
|
|
1695
|
-
yield* invocation.declarePattern(
|
|
1667
|
+
yield* invocation.declarePattern(parameter.argument, args.slice(index), true, parameter, true);
|
|
1696
1668
|
break;
|
|
1697
1669
|
}
|
|
1698
1670
|
yield* invocation.declarePattern(parameter, args[index], true, parameter, true);
|
|
@@ -1813,12 +1785,12 @@ export class Interpreter {
|
|
|
1813
1785
|
return request;
|
|
1814
1786
|
}
|
|
1815
1787
|
evaluateYieldExpression(node) {
|
|
1816
|
-
const argument =
|
|
1788
|
+
const argument = node.argument;
|
|
1817
1789
|
const self = this;
|
|
1818
1790
|
return Effect.gen(function* () {
|
|
1819
1791
|
if (!self.generatorState)
|
|
1820
1792
|
throw new InterpreterRuntimeError("yield is only valid inside a generator.", node);
|
|
1821
|
-
if (node.delegate
|
|
1793
|
+
if (node.delegate) {
|
|
1822
1794
|
const value = argument ? yield* self.evaluateExpression(argument) : undefined;
|
|
1823
1795
|
return yield* self.delegateYield(value, node);
|
|
1824
1796
|
}
|
|
@@ -1919,13 +1891,11 @@ export class Interpreter {
|
|
|
1919
1891
|
}
|
|
1920
1892
|
evaluateObjectExpression(node) {
|
|
1921
1893
|
const objectValue = Object.create(null);
|
|
1922
|
-
const properties = getArray(node, "properties");
|
|
1923
1894
|
const self = this;
|
|
1924
1895
|
return Effect.gen(function* () {
|
|
1925
|
-
for (const
|
|
1926
|
-
const property = asNode(propertyValue, "properties");
|
|
1896
|
+
for (const property of node.properties) {
|
|
1927
1897
|
if (property.type === "SpreadElement") {
|
|
1928
|
-
const spread = yield* self.evaluateExpression(
|
|
1898
|
+
const spread = yield* self.evaluateExpression(property.argument);
|
|
1929
1899
|
if (spread === null || spread === undefined || Values.isValue(spread))
|
|
1930
1900
|
continue;
|
|
1931
1901
|
if (typeof spread !== "object" || Array.isArray(spread) || isRuntimeReference(spread)) {
|
|
@@ -1939,21 +1909,16 @@ export class Interpreter {
|
|
|
1939
1909
|
copyIteratorSymbols(spread, objectValue);
|
|
1940
1910
|
continue;
|
|
1941
1911
|
}
|
|
1942
|
-
if (property.
|
|
1943
|
-
throw new InterpreterRuntimeError("Only standard object properties are supported.", property);
|
|
1944
|
-
}
|
|
1945
|
-
if (getString(property, "kind") !== "init") {
|
|
1912
|
+
if (property.kind !== "init") {
|
|
1946
1913
|
throw new InterpreterRuntimeError("Only init object properties are supported.", property);
|
|
1947
1914
|
}
|
|
1948
|
-
const keyNode =
|
|
1949
|
-
const valueNode = getNode(property, "value");
|
|
1950
|
-
const computed = getBoolean(property, "computed");
|
|
1915
|
+
const keyNode = property.key;
|
|
1951
1916
|
let key;
|
|
1952
|
-
if (computed) {
|
|
1917
|
+
if (property.computed) {
|
|
1953
1918
|
key = self.toPropertyKey(yield* self.evaluateExpression(keyNode), keyNode);
|
|
1954
1919
|
}
|
|
1955
1920
|
else if (keyNode.type === "Identifier") {
|
|
1956
|
-
key =
|
|
1921
|
+
key = keyNode.name;
|
|
1957
1922
|
}
|
|
1958
1923
|
else if (keyNode.type === "Literal") {
|
|
1959
1924
|
key = self.toPropertyKey(keyNode.value, keyNode);
|
|
@@ -1964,25 +1929,23 @@ export class Interpreter {
|
|
|
1964
1929
|
if (isBlockedMember(String(key))) {
|
|
1965
1930
|
throw new InterpreterRuntimeError(`Property '${String(key)}' is not available.`, keyNode);
|
|
1966
1931
|
}
|
|
1967
|
-
Reflect.set(objectValue, key, yield* self.evaluateExpression(
|
|
1932
|
+
Reflect.set(objectValue, key, yield* self.evaluateExpression(property.value));
|
|
1968
1933
|
}
|
|
1969
1934
|
return objectValue;
|
|
1970
1935
|
});
|
|
1971
1936
|
}
|
|
1972
1937
|
evaluateArrayExpression(node) {
|
|
1973
|
-
const elements = getArray(node, "elements");
|
|
1974
1938
|
const values = [];
|
|
1975
1939
|
const self = this;
|
|
1976
1940
|
return Effect.gen(function* () {
|
|
1977
|
-
for (const
|
|
1978
|
-
if (
|
|
1941
|
+
for (const element of node.elements) {
|
|
1942
|
+
if (element === null) {
|
|
1979
1943
|
// A literal elision is a real hole, like JS: extend length without an own index.
|
|
1980
1944
|
values.length += 1;
|
|
1981
1945
|
continue;
|
|
1982
1946
|
}
|
|
1983
|
-
const element = asNode(elementValue, "elements");
|
|
1984
1947
|
if (element.type === "SpreadElement") {
|
|
1985
|
-
const spread = yield* self.evaluateExpression(
|
|
1948
|
+
const spread = yield* self.evaluateExpression(element.argument);
|
|
1986
1949
|
const cursor = yield* self.syncIterator(spread, element);
|
|
1987
1950
|
if (cursor === undefined)
|
|
1988
1951
|
throw new InterpreterRuntimeError("Array spread requires a synchronous iterable.", element).as("TypeError");
|
|
@@ -2001,28 +1964,28 @@ export class Interpreter {
|
|
|
2001
1964
|
});
|
|
2002
1965
|
}
|
|
2003
1966
|
evaluateTemplateLiteral(node) {
|
|
2004
|
-
const quasis =
|
|
2005
|
-
const expressions =
|
|
1967
|
+
const quasis = node.quasis;
|
|
1968
|
+
const expressions = node.expressions;
|
|
2006
1969
|
let output = "";
|
|
2007
1970
|
const self = this;
|
|
2008
1971
|
return Effect.gen(function* () {
|
|
2009
1972
|
for (let index = 0; index < quasis.length; index += 1) {
|
|
2010
|
-
const quasi =
|
|
2011
|
-
|
|
2012
|
-
if (
|
|
1973
|
+
const quasi = quasis[index];
|
|
1974
|
+
// acorn only omits `cooked` for invalid escapes in tagged templates, which are unsupported.
|
|
1975
|
+
if (typeof quasi.value.cooked !== "string") {
|
|
2013
1976
|
throw new InterpreterRuntimeError("Invalid template literal quasi.", quasi);
|
|
2014
1977
|
}
|
|
2015
|
-
output +=
|
|
1978
|
+
output += quasi.value.cooked;
|
|
2016
1979
|
if (index < expressions.length) {
|
|
2017
|
-
const raw = yield* self.evaluateExpression(
|
|
2018
|
-
output += coerceToString(
|
|
1980
|
+
const raw = yield* self.evaluateExpression(expressions[index]);
|
|
1981
|
+
output += coerceToString(toProgram(raw, "Template interpolation"));
|
|
2019
1982
|
}
|
|
2020
1983
|
}
|
|
2021
1984
|
return output;
|
|
2022
1985
|
});
|
|
2023
1986
|
}
|
|
2024
1987
|
evaluateConditionalExpression(node) {
|
|
2025
|
-
return Effect.flatMap(this.evaluateExpression(
|
|
1988
|
+
return Effect.flatMap(this.evaluateExpression(node.test), (test) => this.evaluateExpression(test ? node.consequent : node.alternate));
|
|
2026
1989
|
}
|
|
2027
1990
|
applyCompoundAssignment(operator, current, incoming, node) {
|
|
2028
1991
|
if (!compoundOperators.has(operator)) {
|
|
@@ -2031,21 +1994,23 @@ export class Interpreter {
|
|
|
2031
1994
|
return this.applyBinaryOperator(operator.slice(0, -1), current, incoming, node);
|
|
2032
1995
|
}
|
|
2033
1996
|
getMemberReference(node, operation = "read") {
|
|
2034
|
-
const objectNode =
|
|
2035
|
-
const propertyNode =
|
|
2036
|
-
|
|
2037
|
-
|
|
1997
|
+
const objectNode = node.object;
|
|
1998
|
+
const propertyNode = node.property;
|
|
1999
|
+
if (objectNode.type === "Super")
|
|
2000
|
+
throw unsupportedSyntax(objectNode.type, objectNode);
|
|
2001
|
+
if (propertyNode.type === "PrivateIdentifier")
|
|
2002
|
+
throw unsupportedSyntax(propertyNode.type, propertyNode);
|
|
2038
2003
|
const self = this;
|
|
2039
2004
|
return Effect.gen(function* () {
|
|
2040
2005
|
const objectValue = yield* self.evaluateExpression(objectNode);
|
|
2041
2006
|
if (objectValue === OptionalShortCircuit)
|
|
2042
2007
|
return OptionalShortCircuit;
|
|
2043
|
-
if ((objectValue === null || objectValue === undefined) && optional)
|
|
2008
|
+
if ((objectValue === null || objectValue === undefined) && node.optional)
|
|
2044
2009
|
return OptionalShortCircuit;
|
|
2045
|
-
const key = computed
|
|
2010
|
+
const key = node.computed
|
|
2046
2011
|
? self.toPropertyKey(yield* self.evaluateExpression(propertyNode), propertyNode)
|
|
2047
2012
|
: propertyNode.type === "Identifier"
|
|
2048
|
-
?
|
|
2013
|
+
? propertyNode.name
|
|
2049
2014
|
: self.toPropertyKey(yield* self.evaluateExpression(propertyNode), propertyNode);
|
|
2050
2015
|
if (objectValue instanceof ToolReference) {
|
|
2051
2016
|
if (typeof key !== "string") {
|
|
@@ -2233,7 +2198,7 @@ export class Interpreter {
|
|
|
2233
2198
|
return this.modifyMember(node, () => Effect.succeed({ write: true, next: value, result: value }));
|
|
2234
2199
|
}
|
|
2235
2200
|
evaluateDeleteExpression(argument) {
|
|
2236
|
-
const target = argument.type === "ChainExpression" ?
|
|
2201
|
+
const target = argument.type === "ChainExpression" ? argument.expression : argument;
|
|
2237
2202
|
if (target.type !== "MemberExpression") {
|
|
2238
2203
|
throw new InterpreterRuntimeError("Only data fields may be deleted.", argument);
|
|
2239
2204
|
}
|