@opencode/codemode 0.0.0-dev-98 → 0.0.0-dev-106
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/data.d.ts +5 -4
- package/dist/data.js +7 -4
- package/dist/interpreter/callback.d.ts +10 -10
- package/dist/interpreter/callback.js +43 -31
- package/dist/interpreter/errors.d.ts +1 -1
- package/dist/interpreter/errors.js +9 -13
- package/dist/interpreter/execute.js +24 -21
- package/dist/interpreter/extensions.js +12 -4
- package/dist/interpreter/generators.js +162 -19
- package/dist/interpreter/globals.js +11 -8
- package/dist/interpreter/interpreter.d.ts +18 -13
- package/dist/interpreter/interpreter.js +1325 -1147
- package/dist/interpreter/intrinsics.js +1 -2
- package/dist/interpreter/model.d.ts +0 -6
- package/dist/interpreter/model.js +0 -5
- package/dist/interpreter/native.d.ts +7 -8
- package/dist/interpreter/native.js +4 -10
- package/dist/interpreter/objects.d.ts +47 -21
- package/dist/interpreter/objects.js +33 -12
- package/dist/interpreter/promises.d.ts +10 -53
- package/dist/interpreter/promises.js +196 -402
- package/dist/interpreter/runner.d.ts +184 -0
- package/dist/interpreter/runner.js +472 -0
- package/dist/stdlib/array.d.ts +3 -3
- package/dist/stdlib/array.js +106 -102
- package/dist/stdlib/bytes.d.ts +3 -3
- package/dist/stdlib/bytes.js +46 -41
- package/dist/stdlib/collections.d.ts +5 -5
- package/dist/stdlib/collections.js +160 -165
- package/dist/stdlib/date.d.ts +1 -1
- package/dist/stdlib/date.js +28 -20
- package/dist/stdlib/headers.d.ts +1 -1
- package/dist/stdlib/headers.js +14 -19
- package/dist/stdlib/iterator.d.ts +1 -1
- package/dist/stdlib/iterator.js +140 -122
- package/dist/stdlib/json.js +9 -12
- package/dist/stdlib/math.js +10 -10
- package/dist/stdlib/number.d.ts +2 -2
- package/dist/stdlib/object.d.ts +1 -1
- package/dist/stdlib/object.js +21 -22
- package/dist/stdlib/regexp.d.ts +1 -1
- package/dist/stdlib/regexp.js +2 -3
- package/dist/stdlib/string.d.ts +1 -1
- package/dist/stdlib/string.js +18 -20
- package/dist/stdlib/url.d.ts +5 -5
- package/dist/stdlib/url.js +32 -33
- package/dist/stdlib/value.d.ts +2 -2
- package/dist/stdlib/web.d.ts +2 -2
- package/package.json +1 -1
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { Cause, Deferred, Effect, Exit } from "effect";
|
|
2
1
|
import { fromJson, toBoundary } from "../data.js";
|
|
3
2
|
import { ToolReference } from "../tool-runtime.js";
|
|
4
|
-
import { AsyncIteratorSymbol,
|
|
3
|
+
import { AsyncIteratorSymbol, GeneratorReturn, IteratorSymbol, OptionalShortCircuit, invalidData, Throw, rangeError, typeError, unsupportedSyntax, } from "./model.js";
|
|
5
4
|
import { checkStringLength } from "./limits.js";
|
|
6
5
|
import { locate, materialize } from "./errors.js";
|
|
7
6
|
import { primitivePrototype } from "./intrinsics.js";
|
|
8
7
|
import { globals } from "./globals.js";
|
|
9
|
-
import { assign, Callable, define, frozen, get, hostCursor, IteratorObj, has, hidden, hasPrototype, keys, Native, parseArrayIndex, Arguments, Arr, Fn, GeneratorObj, Obj, PromiseObj,
|
|
8
|
+
import { assign, Callable, define, frozen, get, hostCursor, IteratorObj, has, hidden, hasPrototype, keys, Native, parseArrayIndex, Arguments, Arr, Fn, GeneratorObj, Obj, PromiseObj, iteratorResult, remove, set, coerceToNumber, coerceToString, } from "./objects.js";
|
|
10
9
|
import { preserveConsumerError, toPrimitive } from "./callback.js";
|
|
11
|
-
import {
|
|
10
|
+
import { AsyncBoundary, Await, Call, Interruption, isEval, MAX_CALL_DEPTH, Runner, Slice, Task, Yield, normal, } from "./runner.js";
|
|
12
11
|
import { describeValue, isOpaque, rejectCircularInsertion, typeofValue } from "./references.js";
|
|
12
|
+
import { fn } from "./native.js";
|
|
13
|
+
import { capability } from "./promises.js";
|
|
13
14
|
import { ScopeStack } from "./scope.js";
|
|
14
15
|
import { constructRegExp } from "../stdlib/regexp.js";
|
|
15
16
|
import { enumerableSource } from "../stdlib/object.js";
|
|
@@ -134,6 +135,24 @@ const usesArguments = (fn) => {
|
|
|
134
135
|
argumentsUse.set(fn.body, found);
|
|
135
136
|
return found;
|
|
136
137
|
};
|
|
138
|
+
// Whether evaluating a node can create a function, which could capture a loop's per-iteration bindings.
|
|
139
|
+
const closures = new WeakMap();
|
|
140
|
+
const createsClosure = (node) => {
|
|
141
|
+
const cached = closures.get(node);
|
|
142
|
+
if (cached !== undefined)
|
|
143
|
+
return cached;
|
|
144
|
+
const found = (function visit(item) {
|
|
145
|
+
if (item === null || typeof item !== "object")
|
|
146
|
+
return false;
|
|
147
|
+
if (item.type === "FunctionExpression" ||
|
|
148
|
+
item.type === "ArrowFunctionExpression" ||
|
|
149
|
+
item.type === "FunctionDeclaration")
|
|
150
|
+
return true;
|
|
151
|
+
return Object.values(item).some((child) => Array.isArray(child) ? child.some((entry) => visit(entry)) : visit(child));
|
|
152
|
+
})(node);
|
|
153
|
+
closures.set(node, found);
|
|
154
|
+
return found;
|
|
155
|
+
};
|
|
137
156
|
// `var` names declared anywhere in a function body except inside nested functions, which own theirs.
|
|
138
157
|
// Memoized per body: a function's var names never change, and hoisting runs on every call.
|
|
139
158
|
const varNames = new WeakMap();
|
|
@@ -197,10 +216,37 @@ const loopDeclaration = (left, statement) => {
|
|
|
197
216
|
lexical: kind !== "var",
|
|
198
217
|
};
|
|
199
218
|
};
|
|
200
|
-
|
|
219
|
+
const none = { kind: "none" };
|
|
220
|
+
/** Marks an operand a fast path has not evaluated yet, since `undefined` is a value. */
|
|
221
|
+
const unevaluated = Symbol("codemode.unevaluated");
|
|
222
|
+
// Parameter lists of plain names bind without evaluating anything, so a call needs no binding work.
|
|
223
|
+
const simpleParameters = new WeakMap();
|
|
224
|
+
const isSimple = (parameters) => {
|
|
225
|
+
const cached = simpleParameters.get(parameters);
|
|
226
|
+
if (cached !== undefined)
|
|
227
|
+
return cached;
|
|
228
|
+
const simple = parameters.every((parameter) => parameter.type === "Identifier");
|
|
229
|
+
simpleParameters.set(parameters, simple);
|
|
230
|
+
return simple;
|
|
231
|
+
};
|
|
232
|
+
// A block that declares nothing lexically needs no scope of its own.
|
|
233
|
+
const blockScopes = new WeakMap();
|
|
234
|
+
const declaresLexically = (statements) => {
|
|
235
|
+
const cached = blockScopes.get(statements);
|
|
236
|
+
if (cached !== undefined)
|
|
237
|
+
return cached;
|
|
238
|
+
const declares = statements.some((statement) => statement.type === "FunctionDeclaration" ||
|
|
239
|
+
(statement.type === "VariableDeclaration" && statement.kind !== "var"));
|
|
240
|
+
blockScopes.set(statements, declares);
|
|
241
|
+
return declares;
|
|
242
|
+
};
|
|
243
|
+
const isAbrupt = (result) => result.kind === "return" || result.kind === "break" || result.kind === "continue";
|
|
244
|
+
// A thenable the program's implicit async body adopts before copy-out.
|
|
245
|
+
const isThenable = (value) => value instanceof PromiseObj || (value instanceof Obj && typeofValue(get(value, "then")) === "function");
|
|
246
|
+
/** One program execution: the tool bridge, job queue, captured logs, and the global scope built once. */
|
|
201
247
|
export class Interpreter {
|
|
202
248
|
tools;
|
|
203
|
-
|
|
249
|
+
runner;
|
|
204
250
|
builtins;
|
|
205
251
|
logs;
|
|
206
252
|
/** Template objects by site: a tag sees the same `strings` array every time its literal is evaluated, as in JS. */
|
|
@@ -208,9 +254,9 @@ export class Interpreter {
|
|
|
208
254
|
root;
|
|
209
255
|
constructor(options) {
|
|
210
256
|
this.tools = options.tools;
|
|
211
|
-
this.pending = options.pending;
|
|
212
257
|
this.builtins = options.builtins;
|
|
213
258
|
this.logs = options.logs ?? [];
|
|
259
|
+
this.runner = new Runner(this, options.services);
|
|
214
260
|
// Program code has no receiver: top-level `this` is undefined, as in a module.
|
|
215
261
|
const globalScope = new Map([["this", { mutable: false, value: undefined }]]);
|
|
216
262
|
// Calling back into the program never reads frame state, so any frame serves; the root is always alive.
|
|
@@ -219,14 +265,17 @@ export class Interpreter {
|
|
|
219
265
|
globalScope.set(name, { mutable: false, value });
|
|
220
266
|
}
|
|
221
267
|
}
|
|
222
|
-
|
|
223
|
-
|
|
268
|
+
/** The program's body, as the first job: its completion value, copied out for the host. */
|
|
269
|
+
*main(program) {
|
|
270
|
+
return yield* toBoundary(this, yield* this.root.run(program));
|
|
224
271
|
}
|
|
225
|
-
call
|
|
226
|
-
|
|
272
|
+
/** Calls into the program; a built-in passes its own call site so failures inside are located there. */
|
|
273
|
+
call(callable, thisValue, args, site) {
|
|
274
|
+
return this.root.call(callable, thisValue, args, site);
|
|
227
275
|
}
|
|
228
|
-
|
|
229
|
-
|
|
276
|
+
/** Await: suspends the innermost async body until `value` settles. */
|
|
277
|
+
*awaitValue(value) {
|
|
278
|
+
return (yield new Await(value));
|
|
230
279
|
}
|
|
231
280
|
iterate(value) {
|
|
232
281
|
return this.root.iterate(value);
|
|
@@ -234,45 +283,39 @@ export class Interpreter {
|
|
|
234
283
|
iterateDirect(value) {
|
|
235
284
|
return this.root.iterateDirect(value);
|
|
236
285
|
}
|
|
237
|
-
/** Runs one host
|
|
238
|
-
tool(run, args) {
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
});
|
|
286
|
+
/** Runs one host call to completion before any other job: arguments cross as JSON, the result comes back as values. */
|
|
287
|
+
*tool(run, args) {
|
|
288
|
+
const json = [];
|
|
289
|
+
for (const arg of args)
|
|
290
|
+
json.push(yield* toBoundary(this, arg));
|
|
291
|
+
return fromJson(this, (yield this.runner.blocking(run(json))));
|
|
244
292
|
}
|
|
245
293
|
}
|
|
246
|
-
const MAX_CALL_DEPTH = 10_000;
|
|
247
294
|
/** One activation: the top-level program or a single function call, evaluating against its own scope chain. */
|
|
248
295
|
class Frame {
|
|
249
296
|
ctx;
|
|
250
297
|
scopes;
|
|
251
|
-
|
|
252
|
-
|
|
298
|
+
/** The body of a generator function, where `yield` suspends. */
|
|
299
|
+
generator = false;
|
|
253
300
|
generatorAsync = false;
|
|
254
|
-
constructor(ctx, scopes
|
|
255
|
-
/** Nested call depth; resets when an await resumes, since the continuation runs from the job queue. */
|
|
256
|
-
depth = 0) {
|
|
301
|
+
constructor(ctx, scopes) {
|
|
257
302
|
this.ctx = ctx;
|
|
258
303
|
this.scopes = scopes;
|
|
259
|
-
this.depth = depth;
|
|
260
304
|
}
|
|
261
|
-
run(program) {
|
|
262
|
-
const self = this;
|
|
305
|
+
*run(program) {
|
|
263
306
|
// Keep top-level declarations separate so they can shadow builtins.
|
|
264
307
|
this.scopes.push();
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
308
|
+
try {
|
|
309
|
+
this.predeclareLexical(program.body);
|
|
310
|
+
this.hoistFunctions(program.body);
|
|
311
|
+
this.hoistVars(program.body);
|
|
269
312
|
let value = undefined;
|
|
270
313
|
for (const [index, statement] of program.body.entries()) {
|
|
271
314
|
if (index === program.body.length - 1 && statement.type === "ExpressionStatement") {
|
|
272
|
-
value = yield*
|
|
315
|
+
value = yield* this.evaluateExpression(statement.expression);
|
|
273
316
|
break;
|
|
274
317
|
}
|
|
275
|
-
const result = yield*
|
|
318
|
+
const result = yield* this.evaluateStatement(statement);
|
|
276
319
|
if (result.kind === "return") {
|
|
277
320
|
value = result.value;
|
|
278
321
|
break;
|
|
@@ -282,40 +325,51 @@ class Frame {
|
|
|
282
325
|
}
|
|
283
326
|
}
|
|
284
327
|
// The implicit async body adopts returned promises before copy-out.
|
|
285
|
-
value
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
328
|
+
return isThenable(value) ? yield* this.awaitValue(value) : value;
|
|
329
|
+
}
|
|
330
|
+
finally {
|
|
331
|
+
this.scopes.pop();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// The call returns its promise at once. Arguments cross on the caller's stack, since `toJSON` is program code, and a
|
|
335
|
+
// promise argument is awaited first; the host call starts once they have crossed, so admission and hooks happen at
|
|
336
|
+
// the call site, and runs concurrently with the program.
|
|
337
|
+
*createToolCallPromise(path, args, site) {
|
|
338
|
+
const ctx = this.ctx;
|
|
339
|
+
const runner = ctx.runner;
|
|
340
|
+
const promise = runner.newPromise();
|
|
341
|
+
const resolving = runner.resolving(promise);
|
|
342
|
+
const body = function* () {
|
|
343
|
+
const json = [];
|
|
344
|
+
for (const arg of args)
|
|
345
|
+
json.push(yield* toBoundary(ctx, arg));
|
|
346
|
+
runner.host(ctx.tools.execute(path, json), (settled) => settled.ok
|
|
347
|
+
? resolving.resolve(fromJson(ctx, settled.value))
|
|
348
|
+
: resolving.reject(settled.error), site);
|
|
349
|
+
};
|
|
350
|
+
return (yield new Call(body(), new AsyncBoundary((completion) => {
|
|
351
|
+
if (!completion.ok)
|
|
352
|
+
resolving.reject(completion.error);
|
|
353
|
+
}, promise)));
|
|
354
|
+
}
|
|
355
|
+
awaitValue(value) {
|
|
356
|
+
return this.ctx.awaitValue(value);
|
|
302
357
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
return
|
|
358
|
+
*evaluateStatement(node) {
|
|
359
|
+
const result = this.execute(node);
|
|
360
|
+
return isEval(result) ? yield* result : result;
|
|
306
361
|
}
|
|
307
|
-
|
|
362
|
+
// Like `evaluate`: statements that cannot run program code complete without a generator.
|
|
363
|
+
execute(node) {
|
|
308
364
|
switch (node.type) {
|
|
309
|
-
case "ExpressionStatement":
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
return Effect.map(this.evaluateVariableDeclaration(node), () => ({ kind: "none" }));
|
|
313
|
-
case "ReturnStatement": {
|
|
314
|
-
const argumentNode = node.argument;
|
|
315
|
-
return argumentNode
|
|
316
|
-
? Effect.map(this.evaluateExpression(argumentNode), (value) => ({ kind: "return", value }))
|
|
317
|
-
: Effect.succeed({ kind: "return", value: undefined });
|
|
365
|
+
case "ExpressionStatement": {
|
|
366
|
+
const result = this.evaluate(node.expression);
|
|
367
|
+
return isEval(result) ? this.finishExpressionStatement(result) : none;
|
|
318
368
|
}
|
|
369
|
+
case "VariableDeclaration":
|
|
370
|
+
return this.evaluateVariableStatement(node);
|
|
371
|
+
case "ReturnStatement":
|
|
372
|
+
return this.evaluateReturnStatement(node);
|
|
319
373
|
case "BlockStatement":
|
|
320
374
|
return this.evaluateBlock(node);
|
|
321
375
|
case "IfStatement":
|
|
@@ -335,36 +389,59 @@ class Frame {
|
|
|
335
389
|
case "ForInStatement":
|
|
336
390
|
return this.evaluateForInStatement(node);
|
|
337
391
|
case "BreakStatement":
|
|
338
|
-
return
|
|
392
|
+
return this.evaluateBreakStatement(node);
|
|
339
393
|
case "ContinueStatement":
|
|
340
|
-
return
|
|
394
|
+
return this.evaluateContinueStatement(node);
|
|
341
395
|
case "ThrowStatement":
|
|
342
396
|
return this.evaluateThrowStatement(node);
|
|
343
397
|
case "TryStatement":
|
|
344
398
|
return this.evaluateTryStatement(node);
|
|
345
399
|
case "EmptyStatement":
|
|
346
|
-
return
|
|
400
|
+
return none;
|
|
347
401
|
case "FunctionDeclaration":
|
|
348
|
-
return
|
|
402
|
+
return none;
|
|
349
403
|
default:
|
|
350
404
|
throw unsupportedSyntax(node.type, node);
|
|
351
405
|
}
|
|
352
406
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
407
|
+
*finishExpressionStatement(work) {
|
|
408
|
+
yield* work;
|
|
409
|
+
return none;
|
|
410
|
+
}
|
|
411
|
+
*evaluateVariableStatement(node) {
|
|
412
|
+
yield* this.evaluateVariableDeclaration(node);
|
|
413
|
+
return none;
|
|
414
|
+
}
|
|
415
|
+
*evaluateReturnStatement(node) {
|
|
416
|
+
const argumentNode = node.argument;
|
|
417
|
+
if (!argumentNode)
|
|
418
|
+
return { kind: "return", value: undefined };
|
|
419
|
+
const evaluated = this.evaluate(argumentNode);
|
|
420
|
+
const value = isEval(evaluated) ? yield* evaluated : evaluated;
|
|
421
|
+
// An async generator awaits what it returns, as it awaits what it yields.
|
|
422
|
+
return { kind: "return", value: this.generatorAsync ? yield* this.awaitValue(value) : value };
|
|
423
|
+
}
|
|
424
|
+
*evaluateBlock(node) {
|
|
425
|
+
const body = node.body;
|
|
426
|
+
const scoped = declaresLexically(body);
|
|
427
|
+
if (scoped) {
|
|
428
|
+
this.scopes.push();
|
|
429
|
+
this.predeclareLexical(body);
|
|
430
|
+
this.hoistFunctions(body);
|
|
431
|
+
}
|
|
432
|
+
try {
|
|
360
433
|
for (const statement of body) {
|
|
361
|
-
const
|
|
362
|
-
|
|
434
|
+
const executed = this.execute(statement);
|
|
435
|
+
const result = isEval(executed) ? yield* executed : executed;
|
|
436
|
+
if (result.kind !== "none")
|
|
363
437
|
return result;
|
|
364
|
-
}
|
|
365
438
|
}
|
|
366
|
-
return
|
|
367
|
-
}
|
|
439
|
+
return none;
|
|
440
|
+
}
|
|
441
|
+
finally {
|
|
442
|
+
if (scoped)
|
|
443
|
+
this.scopes.pop();
|
|
444
|
+
}
|
|
368
445
|
}
|
|
369
446
|
createFunction(node, name = node.type === "ArrowFunctionExpression" ? "" : (node.id?.name ?? "")) {
|
|
370
447
|
const builtins = this.ctx.builtins;
|
|
@@ -379,11 +456,11 @@ class Frame {
|
|
|
379
456
|
return fn;
|
|
380
457
|
}
|
|
381
458
|
// NamedEvaluation: an anonymous function definition takes the name of what it is assigned to.
|
|
382
|
-
evaluateNamed(node, name) {
|
|
459
|
+
*evaluateNamed(node, name) {
|
|
383
460
|
if (node.type === "ArrowFunctionExpression" || (node.type === "FunctionExpression" && !node.id)) {
|
|
384
|
-
return
|
|
461
|
+
return this.createFunction(node, name);
|
|
385
462
|
}
|
|
386
|
-
return this.evaluateExpression(node);
|
|
463
|
+
return yield* this.evaluateExpression(node);
|
|
387
464
|
}
|
|
388
465
|
// Repeated `function` declarations and `var` clashes are legal: the last declaration wins.
|
|
389
466
|
hoistFunctions(statements) {
|
|
@@ -424,205 +501,337 @@ class Frame {
|
|
|
424
501
|
for (const name of collectPatternNames(pattern))
|
|
425
502
|
this.scopes.reserve(name, mutable, node);
|
|
426
503
|
}
|
|
427
|
-
evaluateIfStatement(node) {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
504
|
+
*evaluateIfStatement(node) {
|
|
505
|
+
const test = this.evaluate(node.test);
|
|
506
|
+
const branch = (isEval(test) ? yield* test : test) ? node.consequent : node.alternate;
|
|
507
|
+
if (!branch)
|
|
508
|
+
return none;
|
|
509
|
+
const result = this.execute(branch);
|
|
510
|
+
return isEval(result) ? yield* result : result;
|
|
433
511
|
}
|
|
434
|
-
evaluateSwitchStatement(node) {
|
|
435
|
-
const
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
defaultIndex = index;
|
|
450
|
-
continue;
|
|
451
|
-
}
|
|
452
|
-
if ((yield* self.evaluateExpression(test)) === discriminant) {
|
|
453
|
-
selected = index;
|
|
454
|
-
break;
|
|
455
|
-
}
|
|
512
|
+
*evaluateSwitchStatement(node) {
|
|
513
|
+
const discriminant = yield* this.evaluateExpression(node.discriminant);
|
|
514
|
+
this.scopes.push();
|
|
515
|
+
try {
|
|
516
|
+
const cases = node.cases;
|
|
517
|
+
const statements = cases.flatMap((branch) => branch.consequent);
|
|
518
|
+
this.predeclareLexical(statements);
|
|
519
|
+
this.hoistFunctions(statements);
|
|
520
|
+
let defaultIndex;
|
|
521
|
+
let selected;
|
|
522
|
+
for (const [index, branch] of cases.entries()) {
|
|
523
|
+
const test = branch.test;
|
|
524
|
+
if (!test) {
|
|
525
|
+
defaultIndex = index;
|
|
526
|
+
continue;
|
|
456
527
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
528
|
+
if ((yield* this.evaluateExpression(test)) === discriminant) {
|
|
529
|
+
selected = index;
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
const start = selected ?? defaultIndex;
|
|
534
|
+
if (start === undefined)
|
|
535
|
+
return none;
|
|
536
|
+
for (let index = start; index < cases.length; index += 1) {
|
|
537
|
+
for (const statement of cases[index].consequent) {
|
|
538
|
+
const result = yield* this.evaluateStatement(statement);
|
|
539
|
+
if (result.kind === "break") {
|
|
540
|
+
if (result.label === undefined)
|
|
541
|
+
return none;
|
|
542
|
+
return result;
|
|
470
543
|
}
|
|
544
|
+
if (result.kind === "return" || result.kind === "continue")
|
|
545
|
+
return result;
|
|
471
546
|
}
|
|
472
|
-
return { kind: "none" };
|
|
473
|
-
}).pipe(Effect.ensuring(Effect.sync(() => self.scopes.pop())));
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
evaluateWhileStatement(node, labels) {
|
|
477
|
-
const self = this;
|
|
478
|
-
return Effect.gen(function* () {
|
|
479
|
-
while (yield* self.evaluateExpression(node.test)) {
|
|
480
|
-
const exit = loopExit(yield* self.evaluateStatement(node.body), labels);
|
|
481
|
-
if (exit !== undefined)
|
|
482
|
-
return exit;
|
|
483
547
|
}
|
|
484
|
-
return
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
return Effect.gen(function* () {
|
|
490
|
-
do {
|
|
491
|
-
const exit = loopExit(yield* self.evaluateStatement(node.body), labels);
|
|
492
|
-
if (exit !== undefined)
|
|
493
|
-
return exit;
|
|
494
|
-
} while (yield* self.evaluateExpression(node.test));
|
|
495
|
-
return { kind: "none" };
|
|
496
|
-
});
|
|
548
|
+
return none;
|
|
549
|
+
}
|
|
550
|
+
finally {
|
|
551
|
+
this.scopes.pop();
|
|
552
|
+
}
|
|
497
553
|
}
|
|
498
|
-
|
|
554
|
+
*evaluateWhileStatement(node, labels) {
|
|
555
|
+
const runner = this.ctx.runner;
|
|
556
|
+
while (true) {
|
|
557
|
+
const test = this.evaluate(node.test);
|
|
558
|
+
if (!(isEval(test) ? yield* test : test))
|
|
559
|
+
break;
|
|
560
|
+
const executed = this.execute(node.body);
|
|
561
|
+
const exit = loopExit(isEval(executed) ? yield* executed : executed, labels);
|
|
562
|
+
if (exit !== undefined)
|
|
563
|
+
return exit;
|
|
564
|
+
if (runner.tick())
|
|
565
|
+
yield Slice;
|
|
566
|
+
}
|
|
567
|
+
return none;
|
|
568
|
+
}
|
|
569
|
+
*evaluateDoWhileStatement(node, labels) {
|
|
570
|
+
const runner = this.ctx.runner;
|
|
571
|
+
do {
|
|
572
|
+
const exit = loopExit(yield* this.evaluateStatement(node.body), labels);
|
|
573
|
+
if (exit !== undefined)
|
|
574
|
+
return exit;
|
|
575
|
+
if (runner.tick())
|
|
576
|
+
yield Slice;
|
|
577
|
+
} while (yield* this.evaluateExpression(node.test));
|
|
578
|
+
return none;
|
|
579
|
+
}
|
|
580
|
+
*evaluateForStatement(node, labels) {
|
|
581
|
+
const runner = this.ctx.runner;
|
|
499
582
|
this.scopes.push();
|
|
500
|
-
|
|
501
|
-
return Effect.gen(function* () {
|
|
583
|
+
try {
|
|
502
584
|
const initNode = node.init;
|
|
503
585
|
const testNode = node.test;
|
|
504
586
|
const updateNode = node.update;
|
|
505
587
|
if (initNode?.type === "VariableDeclaration" && initNode.kind !== "var") {
|
|
506
|
-
|
|
588
|
+
this.predeclareLexical([initNode]);
|
|
507
589
|
}
|
|
508
590
|
if (initNode) {
|
|
509
591
|
if (initNode.type === "VariableDeclaration") {
|
|
510
|
-
yield*
|
|
592
|
+
yield* this.evaluateVariableDeclaration(initNode);
|
|
511
593
|
}
|
|
512
594
|
else {
|
|
513
|
-
yield*
|
|
595
|
+
yield* this.evaluateExpression(initNode);
|
|
514
596
|
}
|
|
515
597
|
}
|
|
516
|
-
|
|
517
|
-
|
|
598
|
+
// Each iteration gets fresh copies of the loop's `let` bindings, but only closures can tell them apart.
|
|
599
|
+
const perIterationBindings = initNode?.type === "VariableDeclaration" &&
|
|
600
|
+
initNode.kind !== "var" &&
|
|
601
|
+
[node.init, node.test, node.update, node.body].some((part) => part && createsClosure(part))
|
|
602
|
+
? Array.from(this.scopes.current().keys())
|
|
518
603
|
: [];
|
|
519
604
|
const nextIteration = () => {
|
|
520
605
|
if (perIterationBindings.length === 0)
|
|
521
606
|
return;
|
|
522
|
-
const current =
|
|
523
|
-
|
|
524
|
-
|
|
607
|
+
const current = this.scopes.current();
|
|
608
|
+
this.scopes.pop();
|
|
609
|
+
this.scopes.push(new Map(perIterationBindings.map((name) => [name, { ...current.get(name) }])));
|
|
525
610
|
};
|
|
526
611
|
nextIteration();
|
|
527
|
-
while (
|
|
528
|
-
|
|
612
|
+
while (true) {
|
|
613
|
+
if (testNode) {
|
|
614
|
+
const test = this.evaluate(testNode);
|
|
615
|
+
if (!(isEval(test) ? yield* test : test))
|
|
616
|
+
break;
|
|
617
|
+
}
|
|
618
|
+
const executed = this.execute(node.body);
|
|
619
|
+
const exit = loopExit(isEval(executed) ? yield* executed : executed, labels);
|
|
529
620
|
if (exit !== undefined)
|
|
530
621
|
return exit;
|
|
531
622
|
nextIteration();
|
|
532
623
|
if (updateNode) {
|
|
533
|
-
|
|
624
|
+
const update = this.evaluate(updateNode);
|
|
625
|
+
if (isEval(update))
|
|
626
|
+
yield* update;
|
|
534
627
|
}
|
|
628
|
+
if (runner.tick())
|
|
629
|
+
yield Slice;
|
|
535
630
|
}
|
|
536
|
-
return
|
|
537
|
-
}
|
|
631
|
+
return none;
|
|
632
|
+
}
|
|
633
|
+
finally {
|
|
634
|
+
this.scopes.pop();
|
|
635
|
+
}
|
|
538
636
|
}
|
|
539
|
-
evaluateForOfStatement(node, labels) {
|
|
637
|
+
*evaluateForOfStatement(node, labels) {
|
|
638
|
+
const runner = this.ctx.runner;
|
|
540
639
|
const awaiting = node.await;
|
|
541
640
|
const left = node.left;
|
|
542
641
|
const declared = loopDeclaration(left, "for...of");
|
|
543
642
|
if (declared?.lexical)
|
|
544
643
|
this.scopes.push();
|
|
545
|
-
|
|
546
|
-
return Effect.gen(function* () {
|
|
644
|
+
try {
|
|
547
645
|
if (declared?.lexical)
|
|
548
|
-
|
|
549
|
-
const right = yield*
|
|
550
|
-
const
|
|
551
|
-
const
|
|
552
|
-
if (
|
|
646
|
+
this.predeclarePattern(declared.pattern, declared.mutable, left);
|
|
647
|
+
const right = yield* this.evaluateExpression(node.right);
|
|
648
|
+
const builtin = this.builtinCursor(right);
|
|
649
|
+
const custom = builtin === undefined ? yield* this.customIterator(right, node, awaiting) : undefined;
|
|
650
|
+
if (custom === undefined && builtin === undefined) {
|
|
553
651
|
throw invalidData(`${awaiting ? "for await...of" : "for...of"} requires an iterable value, received ${describeValue(right)}.`, node);
|
|
554
652
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
653
|
+
// `for await` over a synchronous iterator drives it through CreateAsyncFromSyncIterator, as JS does.
|
|
654
|
+
const iterator = awaiting && !custom?.asynchronous ? this.asyncFromSync(builtin, custom, node) : custom;
|
|
655
|
+
const cursor = iterator === undefined ? builtin : undefined;
|
|
656
|
+
const self = this;
|
|
657
|
+
const close = function* () {
|
|
658
|
+
if (iterator)
|
|
659
|
+
return yield* self.closeIterator(iterator, node);
|
|
660
|
+
yield* cursor.close();
|
|
661
|
+
};
|
|
560
662
|
if (left.type === "RestElement" || left.type === "AssignmentPattern") {
|
|
561
663
|
throw typeError("Unsupported for...of binding.", left);
|
|
562
664
|
}
|
|
563
665
|
const assignment = left.type === "VariableDeclaration" ? undefined : left;
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
666
|
+
// Each iteration gets a fresh binding, but only a closure in the body can tell; without one, a single
|
|
667
|
+
// `for (const x of ...)` binding is reused.
|
|
668
|
+
const reused = declared?.lexical && declared.pattern.type === "Identifier" && !createsClosure(node.body)
|
|
669
|
+
? { mutable: declared.mutable, value: undefined, initialized: false }
|
|
670
|
+
: undefined;
|
|
671
|
+
if (reused !== undefined)
|
|
672
|
+
this.scopes.push(new Map([[declared.pattern.name, reused]]));
|
|
673
|
+
try {
|
|
674
|
+
while (true) {
|
|
675
|
+
const step = iterator ? yield* this.nextIteratorResult(iterator, node) : yield* cursor.next();
|
|
676
|
+
if (step.done)
|
|
677
|
+
return none;
|
|
678
|
+
let result;
|
|
679
|
+
try {
|
|
680
|
+
if (reused !== undefined) {
|
|
681
|
+
reused.value = step.value;
|
|
682
|
+
reused.initialized = true;
|
|
683
|
+
const executed = this.execute(node.body);
|
|
684
|
+
result = isEval(executed) ? yield* executed : executed;
|
|
685
|
+
}
|
|
686
|
+
else if (declared?.lexical) {
|
|
687
|
+
this.scopes.push();
|
|
688
|
+
try {
|
|
689
|
+
this.predeclarePattern(declared.pattern, declared.mutable, left);
|
|
690
|
+
yield* this.declarePattern(declared.pattern, step.value, declared.mutable, left, true);
|
|
691
|
+
result = yield* this.evaluateStatement(node.body);
|
|
692
|
+
}
|
|
693
|
+
finally {
|
|
694
|
+
this.scopes.pop();
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
if (declared)
|
|
699
|
+
yield* this.assignPattern(declared.pattern, step.value, left);
|
|
700
|
+
else if (assignment)
|
|
701
|
+
yield* this.assignPattern(assignment, step.value, left);
|
|
702
|
+
result = yield* this.evaluateStatement(node.body);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
catch (error) {
|
|
706
|
+
return yield* preserveConsumerError(close, thrower(error));
|
|
707
|
+
}
|
|
708
|
+
const exit = loopExit(result, labels);
|
|
709
|
+
if (exit !== undefined) {
|
|
710
|
+
yield* close();
|
|
711
|
+
return exit;
|
|
712
|
+
}
|
|
713
|
+
if (runner.tick())
|
|
714
|
+
yield Slice;
|
|
599
715
|
}
|
|
600
716
|
}
|
|
601
|
-
|
|
717
|
+
finally {
|
|
718
|
+
if (reused !== undefined)
|
|
719
|
+
this.scopes.pop();
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
finally {
|
|
602
723
|
if (declared?.lexical)
|
|
603
|
-
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
awaitValue(value) {
|
|
607
|
-
return Effect.flatMap(resolvePromise(this.ctx, value), (promise) => Effect.ensuring(this.await(promise), Effect.sync(() => (this.depth = 0))));
|
|
724
|
+
this.scopes.pop();
|
|
725
|
+
}
|
|
608
726
|
}
|
|
609
|
-
|
|
727
|
+
// CreateAsyncFromSyncIterator: an async iterator over a synchronous one. Each `next()` promise settles a job after
|
|
728
|
+
// the value does, and a rejected value closes the synchronous iterator first; `return()` calls the synchronous
|
|
729
|
+
// iterator's own `return`, or settles at once when it has none.
|
|
730
|
+
asyncFromSync(builtin, custom, node) {
|
|
731
|
+
const ctx = this.ctx;
|
|
732
|
+
const runner = ctx.runner;
|
|
733
|
+
const builtins = ctx.builtins;
|
|
734
|
+
const result = (value, done) => iteratorResult(builtins.Object, value, done);
|
|
610
735
|
const self = this;
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
if (
|
|
614
|
-
return
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
736
|
+
// IteratorNext passes the value `next` received, when it received one.
|
|
737
|
+
const syncNext = function* (args) {
|
|
738
|
+
if (!custom)
|
|
739
|
+
return yield* builtin.next();
|
|
740
|
+
const object = self.requireIteratorObject(yield* self.call(custom.next, custom.iterator, args, node), "Iterator next() result", node);
|
|
741
|
+
return { done: Boolean(get(object, "done")), value: get(object, "value") };
|
|
742
|
+
};
|
|
743
|
+
// The synchronous iterator's `return`, looked up when it is needed; built-in collection iterators have none.
|
|
744
|
+
const syncReturn = () => (custom ? get(custom.iterator, "return") : undefined);
|
|
745
|
+
const settle = (step, closeOnRejection, made) => {
|
|
746
|
+
const rejected = (reason) => {
|
|
747
|
+
if (step.done || !closeOnRejection || reason instanceof Interruption)
|
|
748
|
+
return made.reject(reason);
|
|
749
|
+
const body = function* () {
|
|
750
|
+
try {
|
|
751
|
+
if (custom)
|
|
752
|
+
yield* self.closeIterator(custom, node);
|
|
753
|
+
else
|
|
754
|
+
yield* builtin.close();
|
|
755
|
+
}
|
|
756
|
+
catch {
|
|
757
|
+
// The rejection wins over a failing close.
|
|
758
|
+
}
|
|
759
|
+
made.reject(reason);
|
|
760
|
+
};
|
|
761
|
+
return { task: new Task([{ body: body(), boundary: undefined }]), input: normal(undefined) };
|
|
762
|
+
};
|
|
763
|
+
let wrapper;
|
|
764
|
+
try {
|
|
765
|
+
wrapper = runner.promiseResolve(step.value);
|
|
766
|
+
}
|
|
767
|
+
catch (error) {
|
|
768
|
+
const start = rejected(error);
|
|
769
|
+
if (start !== undefined)
|
|
770
|
+
runner.enqueue(() => start);
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
runner.then(wrapper, (settled) => settled.ok ? made.resolve(result(settled.value, step.done)) : rejected(settled.error));
|
|
774
|
+
};
|
|
775
|
+
const next = fn(builtins, "next", 1, function* (_, args) {
|
|
776
|
+
const made = capability(ctx, node);
|
|
777
|
+
try {
|
|
778
|
+
settle(yield* syncNext(args), true, made);
|
|
779
|
+
}
|
|
780
|
+
catch (error) {
|
|
781
|
+
made.reject(error);
|
|
782
|
+
}
|
|
783
|
+
return made.promise;
|
|
619
784
|
});
|
|
785
|
+
const close = fn(builtins, "return", 1, function* (_, args) {
|
|
786
|
+
const made = capability(ctx, node);
|
|
787
|
+
try {
|
|
788
|
+
const method = syncReturn();
|
|
789
|
+
if (method === undefined || method === null) {
|
|
790
|
+
if (builtin !== undefined)
|
|
791
|
+
yield* builtin.close();
|
|
792
|
+
made.resolve(result(args[0], true));
|
|
793
|
+
return made.promise;
|
|
794
|
+
}
|
|
795
|
+
const returned = self.requireIteratorObject(yield* self.call(self.requireIteratorMethod(method, "Iterator return", node), custom.iterator, args, node), "Iterator return() result", node);
|
|
796
|
+
settle({ done: Boolean(get(returned, "done")), value: get(returned, "value") }, false, made);
|
|
797
|
+
}
|
|
798
|
+
catch (error) {
|
|
799
|
+
made.reject(error);
|
|
800
|
+
}
|
|
801
|
+
return made.promise;
|
|
802
|
+
});
|
|
803
|
+
const raise = fn(builtins, "throw", 1, function* (_, args) {
|
|
804
|
+
const made = capability(ctx, node);
|
|
805
|
+
try {
|
|
806
|
+
const method = custom ? get(custom.iterator, "throw") : undefined;
|
|
807
|
+
if (method === undefined || method === null) {
|
|
808
|
+
if (custom)
|
|
809
|
+
yield* self.closeIterator(custom, node);
|
|
810
|
+
else
|
|
811
|
+
yield* builtin.close();
|
|
812
|
+
made.reject(typeError("The delegated iterator does not provide a throw() method.", node));
|
|
813
|
+
return made.promise;
|
|
814
|
+
}
|
|
815
|
+
const thrown = self.requireIteratorObject(yield* self.call(self.requireIteratorMethod(method, "Iterator throw", node), custom.iterator, args, node), "Iterator throw() result", node);
|
|
816
|
+
settle({ done: Boolean(get(thrown, "done")), value: get(thrown, "value") }, true, made);
|
|
817
|
+
}
|
|
818
|
+
catch (error) {
|
|
819
|
+
made.reject(error);
|
|
820
|
+
}
|
|
821
|
+
return made.promise;
|
|
822
|
+
});
|
|
823
|
+
const adapter = new Obj(builtins.Object);
|
|
824
|
+
define(adapter, "next", next, hidden);
|
|
825
|
+
define(adapter, "return", close, hidden);
|
|
826
|
+
define(adapter, "throw", raise, hidden);
|
|
827
|
+
return { iterator: adapter, next, asynchronous: true };
|
|
620
828
|
}
|
|
621
|
-
iterate(value, node) {
|
|
829
|
+
*iterate(value, node) {
|
|
622
830
|
const cursor = this.builtinCursor(value);
|
|
623
831
|
if (cursor !== undefined)
|
|
624
|
-
return
|
|
625
|
-
|
|
832
|
+
return cursor;
|
|
833
|
+
const iterator = yield* this.customIterator(value, node, false);
|
|
834
|
+
return iterator === undefined ? undefined : this.customCursor(iterator, node);
|
|
626
835
|
}
|
|
627
836
|
/** GetIteratorDirect: drive an iterator by its own `next`, without asking for `[Symbol.iterator]`. */
|
|
628
837
|
iterateDirect(value, node) {
|
|
@@ -639,12 +848,11 @@ class Frame {
|
|
|
639
848
|
}
|
|
640
849
|
customCursor(iterator, node) {
|
|
641
850
|
return {
|
|
642
|
-
next: this.nextIteratorResult(iterator, node
|
|
643
|
-
close:
|
|
851
|
+
next: () => this.nextIteratorResult(iterator, node),
|
|
852
|
+
close: () => this.closeIterator(iterator, node),
|
|
644
853
|
};
|
|
645
854
|
}
|
|
646
855
|
builtinCursor(value) {
|
|
647
|
-
// Natives build their cursors without knowing R, like `lift` in native.ts.
|
|
648
856
|
if (value instanceof IteratorObj)
|
|
649
857
|
return value.cursor;
|
|
650
858
|
const iterator = typeof value === "string"
|
|
@@ -654,80 +862,33 @@ class Frame {
|
|
|
654
862
|
: undefined;
|
|
655
863
|
return iterator === undefined ? undefined : hostCursor(iterator);
|
|
656
864
|
}
|
|
657
|
-
customIterator(value, node, allowAsync = true) {
|
|
865
|
+
*customIterator(value, node, allowAsync = true) {
|
|
658
866
|
if (!(value instanceof Obj))
|
|
659
|
-
return
|
|
867
|
+
return undefined;
|
|
660
868
|
const asyncMethod = allowAsync ? get(value, AsyncIteratorSymbol) : undefined;
|
|
661
869
|
const method = asyncMethod ?? get(value, IteratorSymbol);
|
|
662
870
|
if (method === undefined || method === null)
|
|
663
|
-
return
|
|
664
|
-
const
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
};
|
|
672
|
-
});
|
|
871
|
+
return undefined;
|
|
872
|
+
const iterator = yield* this.call(this.requireIteratorMethod(method, "Iterator method", node), value, [], node);
|
|
873
|
+
const object = this.requireIteratorObject(iterator, "Iterator method result", node);
|
|
874
|
+
return {
|
|
875
|
+
iterator: object,
|
|
876
|
+
next: this.requireIteratorMethod(get(object, "next"), "Iterator next", node),
|
|
877
|
+
asynchronous: asyncMethod !== undefined && asyncMethod !== null,
|
|
878
|
+
};
|
|
673
879
|
}
|
|
674
|
-
nextIteratorResult(iterator, node
|
|
675
|
-
const
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
const object = self.requireIteratorObject(yield* self.awaitValue(yield* self.call(iterator.next, iterator.iterator, [], node)), "Iterator next() result", node);
|
|
679
|
-
return { done: Boolean(get(object, "done")), value: get(object, "value") };
|
|
680
|
-
}
|
|
681
|
-
const called = yield* Effect.exit(self.call(iterator.next, iterator.iterator, [], node));
|
|
682
|
-
if (!Exit.isSuccess(called)) {
|
|
683
|
-
if (awaiting)
|
|
684
|
-
yield* self.nextJob();
|
|
685
|
-
return yield* Effect.failCause(called.cause);
|
|
686
|
-
}
|
|
687
|
-
const captured = yield* Effect.exit(Effect.sync(() => {
|
|
688
|
-
const object = self.requireIteratorObject(called.value, "Iterator next() result", node);
|
|
689
|
-
return { done: Boolean(get(object, "done")), value: get(object, "value") };
|
|
690
|
-
}));
|
|
691
|
-
if (!Exit.isSuccess(captured)) {
|
|
692
|
-
if (awaiting)
|
|
693
|
-
yield* self.nextJob();
|
|
694
|
-
return yield* Effect.failCause(captured.cause);
|
|
695
|
-
}
|
|
696
|
-
return {
|
|
697
|
-
done: captured.value.done,
|
|
698
|
-
value: awaiting
|
|
699
|
-
? yield* self.awaitAsyncFromSyncValue(iterator, captured.value.value, node, !captured.value.done)
|
|
700
|
-
: captured.value.value,
|
|
701
|
-
};
|
|
702
|
-
});
|
|
880
|
+
*nextIteratorResult(iterator, node) {
|
|
881
|
+
const called = yield* this.call(iterator.next, iterator.iterator, [], node);
|
|
882
|
+
const object = this.requireIteratorObject(iterator.asynchronous ? yield* this.awaitValue(called) : called, "Iterator next() result", node);
|
|
883
|
+
return { done: Boolean(get(object, "done")), value: get(object, "value") };
|
|
703
884
|
}
|
|
704
|
-
closeIterator(iterator, node
|
|
885
|
+
*closeIterator(iterator, node) {
|
|
705
886
|
const close = get(iterator.iterator, "return");
|
|
706
|
-
if (close === undefined || close === null)
|
|
707
|
-
return
|
|
708
|
-
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
const method = self.requireIteratorMethod(close, "Iterator return", node);
|
|
712
|
-
if (iterator.asynchronous) {
|
|
713
|
-
self.requireIteratorObject(yield* self.awaitValue(yield* self.call(method, iterator.iterator, [], node)), "Iterator return() result", node);
|
|
714
|
-
return;
|
|
715
|
-
}
|
|
716
|
-
const called = yield* Effect.exit(self.call(method, iterator.iterator, [], node));
|
|
717
|
-
if (!Exit.isSuccess(called)) {
|
|
718
|
-
if (awaiting)
|
|
719
|
-
yield* self.nextJob();
|
|
720
|
-
return yield* Effect.failCause(called.cause);
|
|
721
|
-
}
|
|
722
|
-
const captured = yield* Effect.exit(Effect.sync(() => get(self.requireIteratorObject(called.value, "Iterator return() result", node), "value")));
|
|
723
|
-
if (!Exit.isSuccess(captured)) {
|
|
724
|
-
if (awaiting)
|
|
725
|
-
yield* self.nextJob();
|
|
726
|
-
return yield* Effect.failCause(captured.cause);
|
|
727
|
-
}
|
|
728
|
-
if (awaiting)
|
|
729
|
-
yield* self.awaitValue(captured.value);
|
|
730
|
-
});
|
|
887
|
+
if (close === undefined || close === null)
|
|
888
|
+
return;
|
|
889
|
+
const method = this.requireIteratorMethod(close, "Iterator return", node);
|
|
890
|
+
const called = yield* this.call(method, iterator.iterator, [], node);
|
|
891
|
+
this.requireIteratorObject(iterator.asynchronous ? yield* this.awaitValue(called) : called, "Iterator return() result", node);
|
|
731
892
|
}
|
|
732
893
|
requireIteratorObject(value, context, node) {
|
|
733
894
|
if (value instanceof Obj)
|
|
@@ -747,48 +908,53 @@ class Frame {
|
|
|
747
908
|
return [];
|
|
748
909
|
return keys(enumerableSource(this.ctx, "for...in", value, node));
|
|
749
910
|
}
|
|
750
|
-
evaluateForInStatement(node, labels) {
|
|
911
|
+
*evaluateForInStatement(node, labels) {
|
|
912
|
+
const runner = this.ctx.runner;
|
|
751
913
|
const left = node.left;
|
|
752
914
|
const declared = loopDeclaration(left, "for...in");
|
|
753
915
|
if (declared?.lexical)
|
|
754
916
|
this.scopes.push();
|
|
755
|
-
|
|
756
|
-
return Effect.gen(function* () {
|
|
917
|
+
try {
|
|
757
918
|
if (declared?.lexical)
|
|
758
|
-
|
|
759
|
-
const right = yield*
|
|
760
|
-
const keys =
|
|
919
|
+
this.predeclarePattern(declared.pattern, declared.mutable, left);
|
|
920
|
+
const right = yield* this.evaluateExpression(node.right);
|
|
921
|
+
const keys = this.enumerableKeys(right, node.right);
|
|
761
922
|
if (left.type === "RestElement" || left.type === "AssignmentPattern") {
|
|
762
923
|
throw typeError("Unsupported for...in binding.", left);
|
|
763
924
|
}
|
|
764
925
|
const assignment = left.type === "VariableDeclaration" ? undefined : left;
|
|
765
926
|
for (const key of keys) {
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
yield* self.assignPattern(declared.pattern, key, left);
|
|
927
|
+
let result;
|
|
928
|
+
if (declared?.lexical) {
|
|
929
|
+
this.scopes.push();
|
|
930
|
+
try {
|
|
931
|
+
this.predeclarePattern(declared.pattern, declared.mutable, left);
|
|
932
|
+
yield* this.declarePattern(declared.pattern, key, declared.mutable, left, true);
|
|
933
|
+
result = yield* this.evaluateStatement(node.body);
|
|
774
934
|
}
|
|
775
|
-
|
|
776
|
-
|
|
935
|
+
finally {
|
|
936
|
+
this.scopes.pop();
|
|
777
937
|
}
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
if (declared
|
|
781
|
-
|
|
782
|
-
|
|
938
|
+
}
|
|
939
|
+
else {
|
|
940
|
+
if (declared)
|
|
941
|
+
yield* this.assignPattern(declared.pattern, key, left);
|
|
942
|
+
else if (assignment)
|
|
943
|
+
yield* this.assignPattern(assignment, key, left);
|
|
944
|
+
result = yield* this.evaluateStatement(node.body);
|
|
945
|
+
}
|
|
783
946
|
const exit = loopExit(result, labels);
|
|
784
947
|
if (exit !== undefined)
|
|
785
948
|
return exit;
|
|
949
|
+
if (runner.tick())
|
|
950
|
+
yield Slice;
|
|
786
951
|
}
|
|
787
|
-
return
|
|
788
|
-
}
|
|
952
|
+
return none;
|
|
953
|
+
}
|
|
954
|
+
finally {
|
|
789
955
|
if (declared?.lexical)
|
|
790
|
-
|
|
791
|
-
}
|
|
956
|
+
this.scopes.pop();
|
|
957
|
+
}
|
|
792
958
|
}
|
|
793
959
|
evaluateBreakStatement(node) {
|
|
794
960
|
return node.label ? { kind: "break", label: node.label.name } : { kind: "break" };
|
|
@@ -796,265 +962,314 @@ class Frame {
|
|
|
796
962
|
evaluateContinueStatement(node) {
|
|
797
963
|
return node.label ? { kind: "continue", label: node.label.name } : { kind: "continue" };
|
|
798
964
|
}
|
|
799
|
-
evaluateLabeledStatement(node) {
|
|
965
|
+
*evaluateLabeledStatement(node) {
|
|
800
966
|
const labels = new Set();
|
|
801
967
|
let body = node;
|
|
802
968
|
while (body.type === "LabeledStatement") {
|
|
803
969
|
labels.add(body.label.name);
|
|
804
970
|
body = body.body;
|
|
805
971
|
}
|
|
806
|
-
const
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
evaluateThrowStatement(node) {
|
|
824
|
-
return Effect.flatMap(this.evaluateExpression(node.argument), (value) => Effect.fail(new Throw(value)));
|
|
825
|
-
}
|
|
826
|
-
evaluateTryStatement(node) {
|
|
827
|
-
const body = node.block;
|
|
972
|
+
const result = body.type === "WhileStatement"
|
|
973
|
+
? yield* this.evaluateWhileStatement(body, labels)
|
|
974
|
+
: body.type === "DoWhileStatement"
|
|
975
|
+
? yield* this.evaluateDoWhileStatement(body, labels)
|
|
976
|
+
: body.type === "ForStatement"
|
|
977
|
+
? yield* this.evaluateForStatement(body, labels)
|
|
978
|
+
: body.type === "ForOfStatement"
|
|
979
|
+
? yield* this.evaluateForOfStatement(body, labels)
|
|
980
|
+
: body.type === "ForInStatement"
|
|
981
|
+
? yield* this.evaluateForInStatement(body, labels)
|
|
982
|
+
: yield* this.evaluateStatement(body);
|
|
983
|
+
return result.kind === "break" && result.label !== undefined && labels.has(result.label) ? none : result;
|
|
984
|
+
}
|
|
985
|
+
*evaluateThrowStatement(node) {
|
|
986
|
+
throw new Throw(yield* this.evaluateExpression(node.argument));
|
|
987
|
+
}
|
|
988
|
+
*evaluateTryStatement(node) {
|
|
828
989
|
const handler = node.handler;
|
|
829
990
|
const finalizer = node.finalizer;
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
991
|
+
let result = none;
|
|
992
|
+
let failure;
|
|
993
|
+
try {
|
|
994
|
+
result = yield* this.evaluateStatement(node.block);
|
|
995
|
+
}
|
|
996
|
+
catch (error) {
|
|
997
|
+
if (error instanceof Interruption)
|
|
998
|
+
throw error;
|
|
999
|
+
// A generator's return() unwinds through `finally` but is not an exception `catch` sees.
|
|
1000
|
+
if (error instanceof GeneratorReturn || !handler) {
|
|
1001
|
+
if (!finalizer)
|
|
1002
|
+
throw error;
|
|
1003
|
+
failure = { error };
|
|
1004
|
+
}
|
|
1005
|
+
else {
|
|
1006
|
+
const caught = materialize(this.ctx, error);
|
|
837
1007
|
const parameter = handler.param;
|
|
838
|
-
|
|
839
|
-
|
|
1008
|
+
this.scopes.push();
|
|
1009
|
+
try {
|
|
840
1010
|
if (parameter)
|
|
841
|
-
yield*
|
|
842
|
-
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
1011
|
+
yield* this.declarePattern(parameter, caught, true, handler);
|
|
1012
|
+
result = yield* this.evaluateStatement(handler.body);
|
|
1013
|
+
}
|
|
1014
|
+
catch (error) {
|
|
1015
|
+
if (!finalizer || error instanceof Interruption)
|
|
1016
|
+
throw error;
|
|
1017
|
+
failure = { error };
|
|
1018
|
+
}
|
|
1019
|
+
finally {
|
|
1020
|
+
this.scopes.pop();
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
847
1024
|
if (!finalizer)
|
|
848
|
-
return
|
|
849
|
-
const
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
});
|
|
1025
|
+
return result;
|
|
1026
|
+
const final = yield* this.evaluateStatement(finalizer);
|
|
1027
|
+
if (isAbrupt(final))
|
|
1028
|
+
return final;
|
|
1029
|
+
if (failure)
|
|
1030
|
+
throw failure.error;
|
|
1031
|
+
return result;
|
|
856
1032
|
}
|
|
857
|
-
evaluateVariableDeclaration(node) {
|
|
1033
|
+
*evaluateVariableDeclaration(node) {
|
|
858
1034
|
const kind = node.kind;
|
|
859
|
-
const
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
1035
|
+
for (const declaration of node.declarations) {
|
|
1036
|
+
if (declaration.type !== "VariableDeclarator") {
|
|
1037
|
+
throw typeError("Unsupported variable declaration shape.", declaration);
|
|
1038
|
+
}
|
|
1039
|
+
const init = declaration.init;
|
|
1040
|
+
// `var x` alone is a no-op: the binding was hoisted on function entry.
|
|
1041
|
+
const id = declaration.id;
|
|
1042
|
+
if (kind === "var") {
|
|
1043
|
+
if (init) {
|
|
1044
|
+
const value = id.type === "Identifier" ? yield* this.evaluateNamed(init, id.name) : yield* this.evaluateExpression(init);
|
|
1045
|
+
yield* this.assignPattern(id, value, declaration);
|
|
864
1046
|
}
|
|
865
|
-
|
|
866
|
-
// `var x` alone is a no-op: the binding was hoisted on function entry.
|
|
867
|
-
const id = declaration.id;
|
|
868
|
-
const evaluate = (init) => id.type === "Identifier" ? self.evaluateNamed(init, id.name) : self.evaluateExpression(init);
|
|
869
|
-
if (kind === "var") {
|
|
870
|
-
if (init)
|
|
871
|
-
yield* self.assignPattern(id, yield* evaluate(init), declaration);
|
|
872
|
-
continue;
|
|
873
|
-
}
|
|
874
|
-
const value = init ? yield* evaluate(init) : undefined;
|
|
875
|
-
yield* self.declarePattern(declaration.id, value, kind !== "const", declaration, true);
|
|
1047
|
+
continue;
|
|
876
1048
|
}
|
|
877
|
-
|
|
1049
|
+
const value = !init
|
|
1050
|
+
? undefined
|
|
1051
|
+
: id.type === "Identifier"
|
|
1052
|
+
? yield* this.evaluateNamed(init, id.name)
|
|
1053
|
+
: yield* this.evaluateExpression(init);
|
|
1054
|
+
yield* this.declarePattern(declaration.id, value, kind !== "const", declaration, true);
|
|
1055
|
+
}
|
|
878
1056
|
}
|
|
879
|
-
declarePattern(pattern, value, mutable, node, initialize = false) {
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
if (
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
yield* self.declarePattern(property.argument, rest, mutable, property, initialize);
|
|
905
|
-
continue;
|
|
906
|
-
}
|
|
907
|
-
const key = yield* self.destructuringPropertyKey(property);
|
|
908
|
-
consumed.add(typeof key === "symbol" ? key : String(key));
|
|
909
|
-
yield* self.declarePattern(property.value, self.destructuredProperty(value, key, property), mutable, property, initialize);
|
|
1057
|
+
*declarePattern(pattern, value, mutable, node, initialize = false) {
|
|
1058
|
+
if (pattern.type === "Identifier") {
|
|
1059
|
+
const name = pattern.name;
|
|
1060
|
+
if (initialize)
|
|
1061
|
+
this.scopes.initialize(name, value, node);
|
|
1062
|
+
else
|
|
1063
|
+
this.scopes.declare(name, value, mutable, node);
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
if (pattern.type === "AssignmentPattern") {
|
|
1067
|
+
const resolved = value === undefined ? yield* this.evaluateDefault(pattern) : value;
|
|
1068
|
+
yield* this.declarePattern(pattern.left, resolved, mutable, node, initialize);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
if (pattern.type === "ObjectPattern") {
|
|
1072
|
+
if (value === null || value === undefined) {
|
|
1073
|
+
throw typeError(`Cannot destructure ${describeValue(value)} as it is ${value}.`, pattern);
|
|
1074
|
+
}
|
|
1075
|
+
const consumed = new Set();
|
|
1076
|
+
for (const property of pattern.properties) {
|
|
1077
|
+
if (property.type === "RestElement") {
|
|
1078
|
+
const rest = new Obj(this.ctx.builtins.Object);
|
|
1079
|
+
assign(rest, enumerableSource(this.ctx, "Object destructuring", value, pattern), consumed);
|
|
1080
|
+
yield* this.declarePattern(property.argument, rest, mutable, property, initialize);
|
|
1081
|
+
continue;
|
|
910
1082
|
}
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
return yield* self.destructureArrayPattern(pattern, value, (target, item, context) => self.declarePattern(target, item, mutable, context, initialize));
|
|
1083
|
+
const key = yield* this.destructuringPropertyKey(property);
|
|
1084
|
+
consumed.add(typeof key === "symbol" ? key : String(key));
|
|
1085
|
+
yield* this.declarePattern(property.value, this.destructuredProperty(value, key, property), mutable, property, initialize);
|
|
915
1086
|
}
|
|
916
|
-
|
|
917
|
-
}
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
if (pattern.type === "ArrayPattern") {
|
|
1090
|
+
return yield* this.destructureArrayPattern(pattern, value, (target, item, context) => this.declarePattern(target, item, mutable, context, initialize));
|
|
1091
|
+
}
|
|
1092
|
+
throw typeError(`Unsupported binding pattern '${pattern.type}'.`, pattern);
|
|
918
1093
|
}
|
|
919
|
-
assignPattern(pattern, value, node) {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
yield* self.assignPattern(property.argument, rest, property);
|
|
945
|
-
continue;
|
|
946
|
-
}
|
|
947
|
-
const key = yield* self.destructuringPropertyKey(property);
|
|
948
|
-
consumed.add(typeof key === "symbol" ? key : String(key));
|
|
949
|
-
yield* self.assignPattern(property.value, self.destructuredProperty(value, key, property), property);
|
|
1094
|
+
*assignPattern(pattern, value, node) {
|
|
1095
|
+
if (pattern.type === "Identifier") {
|
|
1096
|
+
this.scopes.set(pattern.name, value, pattern);
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
if (pattern.type === "MemberExpression") {
|
|
1100
|
+
yield* this.writeMember(pattern, value);
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
if (pattern.type === "AssignmentPattern") {
|
|
1104
|
+
const resolved = value === undefined ? yield* this.evaluateDefault(pattern) : value;
|
|
1105
|
+
yield* this.assignPattern(pattern.left, resolved, node);
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
if (pattern.type === "ObjectPattern") {
|
|
1109
|
+
if (value === null || value === undefined) {
|
|
1110
|
+
throw typeError(`Cannot destructure ${describeValue(value)} as it is ${value}.`, pattern);
|
|
1111
|
+
}
|
|
1112
|
+
const consumed = new Set();
|
|
1113
|
+
for (const property of pattern.properties) {
|
|
1114
|
+
if (property.type === "RestElement") {
|
|
1115
|
+
const rest = new Obj(this.ctx.builtins.Object);
|
|
1116
|
+
assign(rest, enumerableSource(this.ctx, "Object destructuring", value, pattern), consumed);
|
|
1117
|
+
yield* this.assignPattern(property.argument, rest, property);
|
|
1118
|
+
continue;
|
|
950
1119
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
return yield* self.destructureArrayPattern(pattern, value, (target, item, context) => self.assignPattern(target, item, context));
|
|
1120
|
+
const key = yield* this.destructuringPropertyKey(property);
|
|
1121
|
+
consumed.add(typeof key === "symbol" ? key : String(key));
|
|
1122
|
+
yield* this.assignPattern(property.value, this.destructuredProperty(value, key, property), property);
|
|
955
1123
|
}
|
|
956
|
-
|
|
957
|
-
}
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
if (pattern.type === "ArrayPattern") {
|
|
1127
|
+
return yield* this.destructureArrayPattern(pattern, value, (target, item, context) => this.assignPattern(target, item, context));
|
|
1128
|
+
}
|
|
1129
|
+
throw typeError(`Unsupported assignment pattern '${pattern.type}'.`, node);
|
|
958
1130
|
}
|
|
959
1131
|
evaluateDefault(pattern) {
|
|
960
1132
|
return pattern.left.type === "Identifier"
|
|
961
1133
|
? this.evaluateNamed(pattern.right, pattern.left.name)
|
|
962
1134
|
: this.evaluateExpression(pattern.right);
|
|
963
1135
|
}
|
|
964
|
-
destructureArrayPattern(pattern, value, consume) {
|
|
965
|
-
const
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
for (const element of pattern.elements) {
|
|
973
|
-
if (done) {
|
|
974
|
-
if (element === null)
|
|
975
|
-
continue;
|
|
976
|
-
yield* consume(element.type === "RestElement" ? element.argument : element, element.type === "RestElement" ? new Arr(self.ctx.builtins.Array) : undefined, element);
|
|
977
|
-
if (element.type === "RestElement")
|
|
978
|
-
return;
|
|
979
|
-
continue;
|
|
980
|
-
}
|
|
981
|
-
const step = yield* cursor.next;
|
|
982
|
-
done = step.done;
|
|
1136
|
+
*destructureArrayPattern(pattern, value, consume) {
|
|
1137
|
+
const cursor = yield* this.iterate(value, pattern);
|
|
1138
|
+
if (cursor === undefined) {
|
|
1139
|
+
throw typeError("Array destructuring requires a supported iterable value.", pattern);
|
|
1140
|
+
}
|
|
1141
|
+
let done = false;
|
|
1142
|
+
for (const element of pattern.elements) {
|
|
1143
|
+
if (done) {
|
|
983
1144
|
if (element === null)
|
|
984
1145
|
continue;
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
if (!step.done)
|
|
988
|
-
rest.push(step.value);
|
|
989
|
-
while (!done) {
|
|
990
|
-
const next = yield* cursor.next;
|
|
991
|
-
done = next.done;
|
|
992
|
-
if (!done)
|
|
993
|
-
rest.push(next.value);
|
|
994
|
-
}
|
|
995
|
-
yield* consume(element.argument, new Arr(self.ctx.builtins.Array, rest), element);
|
|
1146
|
+
yield* consume(element.type === "RestElement" ? element.argument : element, element.type === "RestElement" ? new Arr(this.ctx.builtins.Array) : undefined, element);
|
|
1147
|
+
if (element.type === "RestElement")
|
|
996
1148
|
return;
|
|
1149
|
+
continue;
|
|
1150
|
+
}
|
|
1151
|
+
const step = yield* cursor.next();
|
|
1152
|
+
done = step.done;
|
|
1153
|
+
if (element === null)
|
|
1154
|
+
continue;
|
|
1155
|
+
if (element.type === "RestElement") {
|
|
1156
|
+
const rest = [];
|
|
1157
|
+
if (!step.done)
|
|
1158
|
+
rest.push(step.value);
|
|
1159
|
+
while (!done) {
|
|
1160
|
+
const next = yield* cursor.next();
|
|
1161
|
+
done = next.done;
|
|
1162
|
+
if (!done)
|
|
1163
|
+
rest.push(next.value);
|
|
997
1164
|
}
|
|
998
|
-
|
|
999
|
-
|
|
1165
|
+
yield* consume(element.argument, new Arr(this.ctx.builtins.Array, rest), element);
|
|
1166
|
+
return;
|
|
1000
1167
|
}
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
}
|
|
1168
|
+
const consumed = consume(element, step.done ? undefined : step.value, pattern);
|
|
1169
|
+
yield* step.done ? consumed : preserveConsumerError(cursor.close, consumed);
|
|
1170
|
+
}
|
|
1171
|
+
if (!done)
|
|
1172
|
+
yield* cursor.close();
|
|
1004
1173
|
}
|
|
1005
|
-
destructuringPropertyKey(property) {
|
|
1174
|
+
*destructuringPropertyKey(property) {
|
|
1006
1175
|
if (property.type !== "Property" || property.kind !== "init") {
|
|
1007
1176
|
throw typeError("Unsupported object destructuring property.", property);
|
|
1008
1177
|
}
|
|
1009
1178
|
const keyNode = property.key;
|
|
1010
|
-
if (property.computed)
|
|
1011
|
-
return
|
|
1012
|
-
}
|
|
1179
|
+
if (property.computed)
|
|
1180
|
+
return yield* this.toPropertyKey(yield* this.evaluateExpression(keyNode), keyNode);
|
|
1013
1181
|
if (keyNode.type === "Identifier")
|
|
1014
|
-
return
|
|
1182
|
+
return keyNode.name;
|
|
1015
1183
|
if (keyNode.type === "Literal")
|
|
1016
|
-
return
|
|
1184
|
+
return String(keyNode.value);
|
|
1017
1185
|
throw unsupportedSyntax(keyNode.type, keyNode);
|
|
1018
1186
|
}
|
|
1019
|
-
evaluateExpression(node) {
|
|
1187
|
+
*evaluateExpression(node) {
|
|
1188
|
+
const result = this.evaluate(node);
|
|
1189
|
+
return isEval(result) ? yield* result : result;
|
|
1190
|
+
}
|
|
1191
|
+
// Evaluates leaves directly and returns the work for anything that may run program code, so the common cases cost
|
|
1192
|
+
// no generator; callers finish the work with `yield*`.
|
|
1193
|
+
evaluate(node) {
|
|
1020
1194
|
switch (node.type) {
|
|
1021
1195
|
case "Literal": {
|
|
1022
1196
|
const regex = node.regex;
|
|
1023
1197
|
if (regex)
|
|
1024
|
-
return
|
|
1025
|
-
return
|
|
1198
|
+
return constructRegExp(this.ctx.builtins, [regex.pattern, regex.flags]);
|
|
1199
|
+
return literal(node);
|
|
1026
1200
|
}
|
|
1027
1201
|
case "Identifier":
|
|
1028
|
-
return
|
|
1202
|
+
return this.scopes.get(node.name, node);
|
|
1029
1203
|
case "ThisExpression":
|
|
1030
|
-
return
|
|
1031
|
-
case "BinaryExpression":
|
|
1032
|
-
|
|
1204
|
+
return this.scopes.get("this", node);
|
|
1205
|
+
case "BinaryExpression": {
|
|
1206
|
+
const operator = node.operator;
|
|
1207
|
+
if (node.left.type === "PrivateIdentifier")
|
|
1208
|
+
throw unsupportedSyntax(node.left.type, node.left);
|
|
1209
|
+
const left = this.evaluate(node.left);
|
|
1210
|
+
if (isEval(left))
|
|
1211
|
+
return this.evaluateBinaryExpression(node, left);
|
|
1212
|
+
const right = this.evaluate(node.right);
|
|
1213
|
+
if (isEval(right))
|
|
1214
|
+
return this.evaluateBinaryExpression(node, left, right);
|
|
1215
|
+
if (operator === "instanceof")
|
|
1216
|
+
return instanceofValue(left, right, node);
|
|
1217
|
+
if (left instanceof Obj || right instanceof Obj)
|
|
1218
|
+
return this.applyOperator(operator, left, right, node);
|
|
1219
|
+
return this.applyBinaryOperator(operator, left, right, node);
|
|
1220
|
+
}
|
|
1033
1221
|
case "LogicalExpression":
|
|
1034
1222
|
return this.evaluateLogicalExpression(node);
|
|
1035
1223
|
case "UnaryExpression":
|
|
1036
1224
|
return this.evaluateUnaryExpression(node);
|
|
1037
|
-
case "AssignmentExpression":
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
return
|
|
1047
|
-
}
|
|
1048
|
-
|
|
1225
|
+
case "AssignmentExpression": {
|
|
1226
|
+
const left = node.left;
|
|
1227
|
+
if (left.type !== "Identifier")
|
|
1228
|
+
return this.evaluateAssignmentExpression(node);
|
|
1229
|
+
if (node.operator === "=") {
|
|
1230
|
+
const right = node.right.type === "ArrowFunctionExpression" ||
|
|
1231
|
+
(node.right.type === "FunctionExpression" && !node.right.id)
|
|
1232
|
+
? this.createFunction(node.right, left.name)
|
|
1233
|
+
: this.evaluate(node.right);
|
|
1234
|
+
return isEval(right) ? this.assignAfter(left, right) : this.scopes.set(left.name, right, left);
|
|
1235
|
+
}
|
|
1236
|
+
if (!compoundOperators.has(node.operator))
|
|
1237
|
+
return this.evaluateAssignmentExpression(node);
|
|
1238
|
+
const current = this.scopes.get(left.name, left);
|
|
1239
|
+
const right = this.evaluate(node.right);
|
|
1240
|
+
if (isEval(right) || current instanceof Obj || right instanceof Obj)
|
|
1241
|
+
return this.compoundAfter(node, current, right);
|
|
1242
|
+
return this.scopes.set(left.name, this.applyBinaryOperator(node.operator.slice(0, -1), current, right, node), left);
|
|
1243
|
+
}
|
|
1244
|
+
case "SequenceExpression":
|
|
1245
|
+
return this.evaluateSequenceExpression(node);
|
|
1049
1246
|
case "CallExpression":
|
|
1050
1247
|
return this.evaluateCallExpression(node);
|
|
1051
1248
|
case "ArrowFunctionExpression":
|
|
1052
1249
|
case "FunctionExpression":
|
|
1053
|
-
return
|
|
1054
|
-
case "MemberExpression":
|
|
1055
|
-
|
|
1250
|
+
return this.createFunction(node);
|
|
1251
|
+
case "MemberExpression": {
|
|
1252
|
+
const objectNode = node.object;
|
|
1253
|
+
const propertyNode = node.property;
|
|
1254
|
+
if (objectNode.type === "Super")
|
|
1255
|
+
throw unsupportedSyntax(objectNode.type, objectNode);
|
|
1256
|
+
if (propertyNode.type === "PrivateIdentifier")
|
|
1257
|
+
throw unsupportedSyntax(propertyNode.type, propertyNode);
|
|
1258
|
+
const object = this.evaluate(objectNode);
|
|
1259
|
+
if (isEval(object))
|
|
1260
|
+
return this.readMember(node, object);
|
|
1261
|
+
if (object === OptionalShortCircuit)
|
|
1262
|
+
return OptionalShortCircuit;
|
|
1263
|
+
if ((object === null || object === undefined) && node.optional)
|
|
1264
|
+
return OptionalShortCircuit;
|
|
1265
|
+
const key = !node.computed && propertyNode.type === "Identifier" ? propertyNode.name : this.evaluate(propertyNode);
|
|
1266
|
+
if (isEval(key))
|
|
1267
|
+
return this.readMember(node, object, key);
|
|
1268
|
+
const reference = this.memberReference(node, object, key);
|
|
1269
|
+
return isEval(reference) ? this.readMember(node, object, key) : this.readResolved(node, reference);
|
|
1270
|
+
}
|
|
1056
1271
|
case "ChainExpression":
|
|
1057
|
-
return
|
|
1272
|
+
return this.evaluateChainExpression(node);
|
|
1058
1273
|
case "ObjectExpression":
|
|
1059
1274
|
return this.evaluateObjectExpression(node);
|
|
1060
1275
|
case "ArrayExpression":
|
|
@@ -1065,12 +1280,17 @@ class Frame {
|
|
|
1065
1280
|
return this.evaluateTaggedTemplate(node);
|
|
1066
1281
|
case "ConditionalExpression":
|
|
1067
1282
|
return this.evaluateConditionalExpression(node);
|
|
1068
|
-
case "UpdateExpression":
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1283
|
+
case "UpdateExpression": {
|
|
1284
|
+
const argument = node.argument;
|
|
1285
|
+
if (argument.type !== "Identifier")
|
|
1286
|
+
return this.evaluateUpdateExpression(node);
|
|
1287
|
+
const current = this.scopes.get(argument.name, argument);
|
|
1288
|
+
if (current instanceof Obj)
|
|
1289
|
+
return this.evaluateUpdateExpression(node);
|
|
1290
|
+
return this.update(node, argument, current);
|
|
1291
|
+
}
|
|
1292
|
+
case "AwaitExpression":
|
|
1293
|
+
return this.evaluateAwaitExpression(node);
|
|
1074
1294
|
case "YieldExpression":
|
|
1075
1295
|
return this.evaluateYieldExpression(node);
|
|
1076
1296
|
case "NewExpression":
|
|
@@ -1079,72 +1299,84 @@ class Frame {
|
|
|
1079
1299
|
throw unsupportedSyntax(node.type, node);
|
|
1080
1300
|
}
|
|
1081
1301
|
}
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
// `new` itself is supported, so a non-constructible callee is a TypeError like JS rather than
|
|
1090
|
-
// unsupported syntax. Built-ins like Number are real constructors in JS, so do not claim
|
|
1091
|
-
// otherwise; say `new` is unsupported for them and point at the plain call.
|
|
1092
|
-
const name = calleeDescription(node.callee) ?? "The called value";
|
|
1093
|
-
const message = callee instanceof Fn
|
|
1094
|
-
? `${name} cannot be constructed: user-defined constructors and classes are not supported. Call it as a function that returns a plain object instead.`
|
|
1095
|
-
: callee instanceof Native
|
|
1096
|
-
? `new ${name}(...) is not supported; call ${name}(...) without new instead.`
|
|
1097
|
-
: `${name} is not a constructor.`;
|
|
1098
|
-
throw typeError(message, node);
|
|
1099
|
-
}
|
|
1100
|
-
const args = yield* self.evaluateCallArguments(node.arguments);
|
|
1101
|
-
return yield* self.native(() => construct(args, callee), node);
|
|
1102
|
-
});
|
|
1302
|
+
*evaluateSequenceExpression(node) {
|
|
1303
|
+
let result;
|
|
1304
|
+
for (const expression of node.expressions) {
|
|
1305
|
+
const value = this.evaluate(expression);
|
|
1306
|
+
result = isEval(value) ? yield* value : value;
|
|
1307
|
+
}
|
|
1308
|
+
return result;
|
|
1103
1309
|
}
|
|
1104
|
-
|
|
1310
|
+
*evaluateChainExpression(node) {
|
|
1311
|
+
const value = yield* this.evaluateExpression(node.expression);
|
|
1312
|
+
return value === OptionalShortCircuit ? undefined : value;
|
|
1313
|
+
}
|
|
1314
|
+
*evaluateConditionalExpression(node) {
|
|
1315
|
+
const test = this.evaluate(node.test);
|
|
1316
|
+
const branch = this.evaluate((isEval(test) ? yield* test : test) ? node.consequent : node.alternate);
|
|
1317
|
+
return isEval(branch) ? yield* branch : branch;
|
|
1318
|
+
}
|
|
1319
|
+
*evaluateAwaitExpression(node) {
|
|
1320
|
+
// Await always suspends, including for plain values.
|
|
1321
|
+
return yield* this.awaitValue(yield* this.evaluateExpression(node.argument));
|
|
1322
|
+
}
|
|
1323
|
+
*evaluateNewExpression(node) {
|
|
1324
|
+
const callee = yield* this.evaluateExpression(node.callee);
|
|
1325
|
+
const construct = callee instanceof Native ? callee.construct : undefined;
|
|
1326
|
+
if (construct === undefined) {
|
|
1327
|
+
// `new` itself is supported, so a non-constructible callee is a TypeError like JS rather than
|
|
1328
|
+
// unsupported syntax. Built-ins like Number are real constructors in JS, so do not claim
|
|
1329
|
+
// otherwise; say `new` is unsupported for them and point at the plain call.
|
|
1330
|
+
const name = calleeDescription(node.callee) ?? "The called value";
|
|
1331
|
+
const message = callee instanceof Fn
|
|
1332
|
+
? `${name} cannot be constructed: user-defined constructors and classes are not supported. Call it as a function that returns a plain object instead.`
|
|
1333
|
+
: callee instanceof Native
|
|
1334
|
+
? `new ${name}(...) is not supported; call ${name}(...) without new instead.`
|
|
1335
|
+
: `${name} is not a constructor.`;
|
|
1336
|
+
throw typeError(message, node);
|
|
1337
|
+
}
|
|
1338
|
+
const args = yield* this.evaluateCallArguments(node.arguments);
|
|
1339
|
+
const result = this.native(() => construct(args, callee, node), node);
|
|
1340
|
+
return isEval(result) ? yield* result : result;
|
|
1341
|
+
}
|
|
1342
|
+
// Finishes a binary expression whose left operand (and maybe right) `evaluate` already started.
|
|
1343
|
+
*evaluateBinaryExpression(node, leftValue, started = unevaluated) {
|
|
1105
1344
|
const operator = node.operator;
|
|
1106
|
-
const
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
return instanceofValue(lhs, rhs, node);
|
|
1115
|
-
if (lhs instanceof Obj || rhs instanceof Obj)
|
|
1116
|
-
return yield* self.applyOperator(operator, lhs, rhs, node);
|
|
1117
|
-
return self.applyBinaryOperator(operator, lhs, rhs, node);
|
|
1118
|
-
});
|
|
1345
|
+
const lhs = isEval(leftValue) ? yield* leftValue : leftValue;
|
|
1346
|
+
const rightValue = started === unevaluated ? this.evaluate(node.right) : started;
|
|
1347
|
+
const rhs = isEval(rightValue) ? yield* rightValue : rightValue;
|
|
1348
|
+
if (operator === "instanceof")
|
|
1349
|
+
return instanceofValue(lhs, rhs, node);
|
|
1350
|
+
if (lhs instanceof Obj || rhs instanceof Obj)
|
|
1351
|
+
return yield* this.applyOperator(operator, lhs, rhs, node);
|
|
1352
|
+
return this.applyBinaryOperator(operator, lhs, rhs, node);
|
|
1119
1353
|
}
|
|
1120
1354
|
/** ToPrimitive for an operand: data objects run their own methods; opaque values stay for the data gates below. */
|
|
1121
|
-
toPrimitive(value, hint, node) {
|
|
1122
|
-
|
|
1355
|
+
*toPrimitive(value, hint, node) {
|
|
1356
|
+
const result = this.native(() => toPrimitive(this.ctx, value, hint), node);
|
|
1357
|
+
return isEval(result) ? yield* result : result;
|
|
1123
1358
|
}
|
|
1124
1359
|
// Arithmetic, relational, and bitwise operators convert both operands first, left then right, so a `valueOf`
|
|
1125
1360
|
// runs (and throws) in spec order; `+` asks for the default hint and the rest for a number.
|
|
1126
|
-
applyOperator(operator, lhs, rhs, node) {
|
|
1361
|
+
*applyOperator(operator, lhs, rhs, node) {
|
|
1127
1362
|
if (!(lhs instanceof Obj || rhs instanceof Obj))
|
|
1128
|
-
return
|
|
1363
|
+
return this.applyBinaryOperator(operator, lhs, rhs, node);
|
|
1129
1364
|
// IsLooselyEqual converts only an object facing a non-nullish primitive; two objects (including tool
|
|
1130
1365
|
// references, which are not Obj) compare by identity.
|
|
1131
1366
|
const equality = operator === "==" || operator === "!=";
|
|
1132
1367
|
// `in` checks the right operand before ToPropertyKey on the left, so a bad right side wins over a bad key.
|
|
1133
1368
|
if (operator === "in" && lhs instanceof Obj && !isOpaque(lhs) && rhs instanceof Obj) {
|
|
1134
|
-
return
|
|
1369
|
+
return has(rhs, yield* this.toPropertyKey(lhs, node));
|
|
1135
1370
|
}
|
|
1136
1371
|
const other = lhs instanceof Obj ? rhs : lhs;
|
|
1137
1372
|
const converts = primitiveOperators.has(operator) ||
|
|
1138
1373
|
(equality && other !== null && other !== undefined && typeof other !== "object");
|
|
1139
1374
|
if (!converts)
|
|
1140
|
-
return
|
|
1375
|
+
return this.applyBinaryOperator(operator, lhs, rhs, node);
|
|
1141
1376
|
const hint = operator === "+" || equality ? "default" : "number";
|
|
1142
|
-
const
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
const r = yield* self.toPrimitive(rhs, hint, node);
|
|
1146
|
-
return self.applyBinaryOperator(operator, l, r, node);
|
|
1147
|
-
});
|
|
1377
|
+
const l = yield* this.toPrimitive(lhs, hint, node);
|
|
1378
|
+
const r = yield* this.toPrimitive(rhs, hint, node);
|
|
1379
|
+
return this.applyBinaryOperator(operator, l, r, node);
|
|
1148
1380
|
}
|
|
1149
1381
|
applyBinaryOperator(operator, lhs, rhs, node) {
|
|
1150
1382
|
if (operator === "===")
|
|
@@ -1217,127 +1449,134 @@ class Frame {
|
|
|
1217
1449
|
// Data objects were converted by applyOperator, so only an opaque reference facing a primitive gets here.
|
|
1218
1450
|
throw invalidData("Binary operators require data values.", node);
|
|
1219
1451
|
}
|
|
1220
|
-
evaluateLogicalExpression(node) {
|
|
1452
|
+
*evaluateLogicalExpression(node) {
|
|
1221
1453
|
const operator = node.operator;
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
});
|
|
1231
|
-
}
|
|
1232
|
-
evaluateUnaryExpression(node) {
|
|
1454
|
+
const leftValue = this.evaluate(node.left);
|
|
1455
|
+
const left = isEval(leftValue) ? yield* leftValue : leftValue;
|
|
1456
|
+
if (operator === "&&")
|
|
1457
|
+
return left ? yield* this.evaluateExpression(node.right) : left;
|
|
1458
|
+
if (operator === "||")
|
|
1459
|
+
return left ? left : yield* this.evaluateExpression(node.right);
|
|
1460
|
+
if (operator === "??")
|
|
1461
|
+
return left !== null && left !== undefined ? left : yield* this.evaluateExpression(node.right);
|
|
1462
|
+
throw typeError(`Unsupported logical operator '${operator}'.`, node);
|
|
1463
|
+
}
|
|
1464
|
+
*evaluateUnaryExpression(node) {
|
|
1233
1465
|
const operator = node.operator;
|
|
1234
1466
|
const argument = node.argument;
|
|
1235
1467
|
if (operator === "delete")
|
|
1236
|
-
return this.evaluateDeleteExpression(argument);
|
|
1468
|
+
return yield* this.evaluateDeleteExpression(argument);
|
|
1237
1469
|
// Undeclared names short-circuit, but declared TDZ bindings must still throw.
|
|
1238
1470
|
if (operator === "typeof" && argument.type === "Identifier" && !this.scopes.resolve(argument.name)) {
|
|
1239
|
-
return
|
|
1471
|
+
return "undefined";
|
|
1472
|
+
}
|
|
1473
|
+
const value = yield* this.evaluateExpression(argument);
|
|
1474
|
+
if (operator === "typeof")
|
|
1475
|
+
return typeofValue(value);
|
|
1476
|
+
if (operator === "!")
|
|
1477
|
+
return !value;
|
|
1478
|
+
if (operator === "void")
|
|
1479
|
+
return undefined;
|
|
1480
|
+
const operand = yield* this.toPrimitive(value, "number", node);
|
|
1481
|
+
if (isOpaque(operand)) {
|
|
1482
|
+
throw invalidData("Unary operators require data values.", node);
|
|
1483
|
+
}
|
|
1484
|
+
switch (operator) {
|
|
1485
|
+
case "+":
|
|
1486
|
+
return +operand;
|
|
1487
|
+
case "-":
|
|
1488
|
+
return -operand;
|
|
1489
|
+
case "~":
|
|
1490
|
+
return ~operand;
|
|
1491
|
+
default:
|
|
1492
|
+
throw typeError(`Unsupported unary operator '${operator}'.`, node);
|
|
1240
1493
|
}
|
|
1241
|
-
const self = this;
|
|
1242
|
-
return Effect.gen(function* () {
|
|
1243
|
-
const value = yield* self.evaluateExpression(argument);
|
|
1244
|
-
if (operator === "typeof")
|
|
1245
|
-
return typeofValue(value);
|
|
1246
|
-
if (operator === "!")
|
|
1247
|
-
return !value;
|
|
1248
|
-
if (operator === "void")
|
|
1249
|
-
return undefined;
|
|
1250
|
-
const operand = yield* self.toPrimitive(value, "number", node);
|
|
1251
|
-
if (isOpaque(operand)) {
|
|
1252
|
-
throw invalidData("Unary operators require data values.", node);
|
|
1253
|
-
}
|
|
1254
|
-
let result;
|
|
1255
|
-
switch (operator) {
|
|
1256
|
-
case "+":
|
|
1257
|
-
result = +operand;
|
|
1258
|
-
break;
|
|
1259
|
-
case "-":
|
|
1260
|
-
result = -operand;
|
|
1261
|
-
break;
|
|
1262
|
-
case "~":
|
|
1263
|
-
result = ~operand;
|
|
1264
|
-
break;
|
|
1265
|
-
default:
|
|
1266
|
-
throw typeError(`Unsupported unary operator '${operator}'.`, node);
|
|
1267
|
-
}
|
|
1268
|
-
return result;
|
|
1269
|
-
});
|
|
1270
1494
|
}
|
|
1271
|
-
evaluateAssignmentExpression(node) {
|
|
1495
|
+
*evaluateAssignmentExpression(node) {
|
|
1272
1496
|
const left = node.left;
|
|
1273
1497
|
const operator = node.operator;
|
|
1274
1498
|
// The binary operator a compound assignment applies: `+=` is `+`.
|
|
1275
1499
|
const binary = operator.slice(0, -1);
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
}
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1500
|
+
if (operator === "??=" || operator === "||=" || operator === "&&=") {
|
|
1501
|
+
return yield* this.evaluateLogicalAssignment(node, left, operator);
|
|
1502
|
+
}
|
|
1503
|
+
if (operator !== "=" && !compoundOperators.has(operator)) {
|
|
1504
|
+
throw typeError(`Unsupported assignment operator '${operator}'.`, node);
|
|
1505
|
+
}
|
|
1506
|
+
if (operator === "=" && (left.type === "ObjectPattern" || left.type === "ArrayPattern")) {
|
|
1507
|
+
const rightValue = yield* this.evaluateExpression(node.right);
|
|
1508
|
+
yield* this.assignPattern(left, rightValue, node);
|
|
1509
|
+
return rightValue;
|
|
1510
|
+
}
|
|
1511
|
+
if (left.type === "Identifier") {
|
|
1512
|
+
const name = left.name;
|
|
1513
|
+
if (operator !== "=") {
|
|
1514
|
+
const current = this.scopes.get(name, left);
|
|
1515
|
+
const evaluated = this.evaluate(node.right);
|
|
1516
|
+
const rightValue = isEval(evaluated) ? yield* evaluated : evaluated;
|
|
1517
|
+
const next = current instanceof Obj || rightValue instanceof Obj
|
|
1518
|
+
? yield* this.applyOperator(binary, current, rightValue, node)
|
|
1519
|
+
: this.applyBinaryOperator(binary, current, rightValue, node);
|
|
1520
|
+
return this.scopes.set(name, next, left);
|
|
1521
|
+
}
|
|
1522
|
+
const rightValue = yield* this.evaluateNamed(node.right, name);
|
|
1523
|
+
return this.scopes.set(name, rightValue, left);
|
|
1524
|
+
}
|
|
1525
|
+
if (left.type === "MemberExpression") {
|
|
1526
|
+
const self = this;
|
|
1527
|
+
return yield* this.modifyMember(left, function* (current) {
|
|
1285
1528
|
const rightValue = yield* self.evaluateExpression(node.right);
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
const rightValue = yield* self.evaluateExpression(node.right);
|
|
1294
|
-
const next = current instanceof Obj || rightValue instanceof Obj
|
|
1295
|
-
? yield* self.applyOperator(binary, current, rightValue, node)
|
|
1296
|
-
: self.applyBinaryOperator(binary, current, rightValue, node);
|
|
1297
|
-
return self.scopes.set(name, next, left);
|
|
1298
|
-
}
|
|
1299
|
-
const rightValue = yield* self.evaluateNamed(node.right, name);
|
|
1300
|
-
return self.scopes.set(name, rightValue, left);
|
|
1301
|
-
}
|
|
1302
|
-
if (left.type === "MemberExpression") {
|
|
1303
|
-
return yield* self.modifyMember(left, (current) => Effect.flatMap(self.evaluateExpression(node.right), (rightValue) => {
|
|
1304
|
-
if (operator === "=")
|
|
1305
|
-
return Effect.succeed({ write: true, next: rightValue, result: rightValue });
|
|
1306
|
-
return Effect.map(self.applyOperator(binary, current, rightValue, node), (next) => ({
|
|
1307
|
-
write: true,
|
|
1308
|
-
next,
|
|
1309
|
-
result: next,
|
|
1310
|
-
}));
|
|
1311
|
-
}));
|
|
1312
|
-
}
|
|
1313
|
-
throw typeError("Assignment target must be an Identifier or MemberExpression.", left);
|
|
1314
|
-
});
|
|
1529
|
+
if (operator === "=")
|
|
1530
|
+
return { write: true, next: rightValue, result: rightValue };
|
|
1531
|
+
const next = yield* self.applyOperator(binary, current, rightValue, node);
|
|
1532
|
+
return { write: true, next, result: next };
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
throw typeError("Assignment target must be an Identifier or MemberExpression.", left);
|
|
1315
1536
|
}
|
|
1316
|
-
|
|
1317
|
-
|
|
1537
|
+
*assignAfter(left, right) {
|
|
1538
|
+
return this.scopes.set(left.name, yield* right, left);
|
|
1539
|
+
}
|
|
1540
|
+
*compoundAfter(node, current, started) {
|
|
1541
|
+
const left = node.left;
|
|
1542
|
+
const binary = node.operator.slice(0, -1);
|
|
1543
|
+
const right = isEval(started) ? yield* started : started;
|
|
1544
|
+
const next = current instanceof Obj || right instanceof Obj
|
|
1545
|
+
? yield* this.applyOperator(binary, current, right, node)
|
|
1546
|
+
: this.applyBinaryOperator(binary, current, right, node);
|
|
1547
|
+
return this.scopes.set(left.name, next, left);
|
|
1548
|
+
}
|
|
1549
|
+
// `++`/`--` on a name whose value is already primitive.
|
|
1550
|
+
update(node, argument, current) {
|
|
1551
|
+
if (isOpaque(current))
|
|
1552
|
+
throw invalidData(`'${node.operator}' requires a data value.`, argument);
|
|
1553
|
+
const before = coerceToNumber(current);
|
|
1554
|
+
const next = node.operator === "++" ? before + 1 : before - 1;
|
|
1555
|
+
this.scopes.set(argument.name, next, argument);
|
|
1556
|
+
return node.prefix ? next : before;
|
|
1557
|
+
}
|
|
1558
|
+
*evaluateLogicalAssignment(node, left, operator) {
|
|
1318
1559
|
const shouldAssign = (current) => operator === "??=" ? current === null || current === undefined : operator === "||=" ? !current : Boolean(current);
|
|
1319
1560
|
if (left.type === "Identifier") {
|
|
1320
1561
|
const name = left.name;
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
return self.scopes.set(name, rightValue, left);
|
|
1327
|
-
});
|
|
1562
|
+
const current = this.scopes.get(name, left);
|
|
1563
|
+
if (!shouldAssign(current))
|
|
1564
|
+
return current;
|
|
1565
|
+
const rightValue = yield* this.evaluateNamed(node.right, name);
|
|
1566
|
+
return this.scopes.set(name, rightValue, left);
|
|
1328
1567
|
}
|
|
1329
1568
|
if (left.type === "MemberExpression") {
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
next:
|
|
1334
|
-
|
|
1335
|
-
}
|
|
1336
|
-
|
|
1569
|
+
const self = this;
|
|
1570
|
+
return yield* this.modifyMember(left, function* (current) {
|
|
1571
|
+
if (!shouldAssign(current))
|
|
1572
|
+
return { write: false, next: current, result: current };
|
|
1573
|
+
const rightValue = yield* self.evaluateExpression(node.right);
|
|
1574
|
+
return { write: true, next: rightValue, result: rightValue };
|
|
1575
|
+
});
|
|
1337
1576
|
}
|
|
1338
1577
|
throw typeError("Assignment target must be an Identifier or MemberExpression.", left);
|
|
1339
1578
|
}
|
|
1340
|
-
evaluateUpdateExpression(node) {
|
|
1579
|
+
*evaluateUpdateExpression(node) {
|
|
1341
1580
|
const operator = node.operator;
|
|
1342
1581
|
const argument = node.argument;
|
|
1343
1582
|
const prefix = node.prefix;
|
|
@@ -1355,464 +1594,364 @@ class Frame {
|
|
|
1355
1594
|
if (argument.type === "Identifier") {
|
|
1356
1595
|
const name = argument.name;
|
|
1357
1596
|
const current = this.scopes.get(name, argument);
|
|
1358
|
-
const
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
return prefix ? next : before;
|
|
1363
|
-
};
|
|
1364
|
-
if (!(current instanceof Obj))
|
|
1365
|
-
return Effect.sync(() => update(current));
|
|
1366
|
-
return Effect.map(this.toPrimitive(current, "number", argument), update);
|
|
1597
|
+
const before = operand(current instanceof Obj ? yield* this.toPrimitive(current, "number", argument) : current);
|
|
1598
|
+
const next = before + increment;
|
|
1599
|
+
this.scopes.set(name, next, argument);
|
|
1600
|
+
return prefix ? next : before;
|
|
1367
1601
|
}
|
|
1368
1602
|
if (argument.type === "MemberExpression") {
|
|
1369
|
-
|
|
1370
|
-
|
|
1603
|
+
const self = this;
|
|
1604
|
+
return yield* this.modifyMember(argument, function* (current) {
|
|
1605
|
+
const value = operand(yield* self.toPrimitive(current, "number", argument));
|
|
1371
1606
|
const next = value + increment;
|
|
1372
1607
|
return { write: true, next, result: prefix ? next : value };
|
|
1373
|
-
})
|
|
1608
|
+
});
|
|
1374
1609
|
}
|
|
1375
1610
|
throw typeError("Update target must be an Identifier or MemberExpression.", argument);
|
|
1376
1611
|
}
|
|
1377
1612
|
// EvaluateCall: a member callee supplies its base object as `this`; anything else calls with undefined.
|
|
1378
|
-
evaluateCallExpression(node) {
|
|
1613
|
+
*evaluateCallExpression(node) {
|
|
1379
1614
|
const callee = node.callee;
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
call
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1615
|
+
if (callee.type === "Super")
|
|
1616
|
+
throw unsupportedSyntax(callee.type, callee);
|
|
1617
|
+
const { callable, thisValue } = callee.type === "MemberExpression"
|
|
1618
|
+
? yield* this.readMethod(callee)
|
|
1619
|
+
: { callable: yield* this.evaluateExpression(callee), thisValue: undefined };
|
|
1620
|
+
if (callable === OptionalShortCircuit)
|
|
1621
|
+
return OptionalShortCircuit;
|
|
1622
|
+
if ((callable === null || callable === undefined) && node.optional)
|
|
1623
|
+
return OptionalShortCircuit;
|
|
1624
|
+
const args = yield* this.evaluateCallArguments(node.arguments);
|
|
1625
|
+
const result = this.dispatch(callable, thisValue, args, node, callee);
|
|
1626
|
+
return isEval(result) ? yield* result : result instanceof Call ? (yield result) : result;
|
|
1627
|
+
}
|
|
1628
|
+
*readMethod(node) {
|
|
1629
|
+
const reference = yield* this.getMemberReference(node);
|
|
1630
|
+
if (reference === OptionalShortCircuit)
|
|
1631
|
+
return { callable: OptionalShortCircuit, thisValue: undefined };
|
|
1632
|
+
if (reference instanceof ToolReference)
|
|
1633
|
+
return { callable: reference, thisValue: undefined };
|
|
1634
|
+
if ("value" in reference)
|
|
1635
|
+
return { callable: reference.value, thisValue: undefined };
|
|
1636
|
+
return { callable: this.readReference(reference, node), thisValue: reference.receiver };
|
|
1637
|
+
}
|
|
1638
|
+
*call(callable, thisValue, args, node, callee) {
|
|
1639
|
+
const result = this.dispatch(callable, thisValue, args, node, callee);
|
|
1640
|
+
return isEval(result) ? yield* result : result instanceof Call ? (yield result) : result;
|
|
1641
|
+
}
|
|
1642
|
+
// The single dispatch for every invocation: call expressions and callbacks share it. A program function is a `Call`
|
|
1643
|
+
// for the caller to yield; a built-in that returns a value needs no generator.
|
|
1644
|
+
dispatch(callable, thisValue, args, node, callee) {
|
|
1645
|
+
if (callable instanceof Fn)
|
|
1646
|
+
return this.invokeFunction(callable, thisValue, args, node);
|
|
1647
|
+
if (callable instanceof Native) {
|
|
1648
|
+
// A loop made only of built-in calls has no program call or loop back-edge to count, so count the call.
|
|
1649
|
+
if (this.ctx.runner.tick())
|
|
1650
|
+
return afterSlice(() => this.native(() => callable.call(thisValue, args, node), node));
|
|
1651
|
+
return this.native(() => callable.call(thisValue, args, node), node);
|
|
1652
|
+
}
|
|
1653
|
+
if (callable instanceof ToolReference) {
|
|
1654
|
+
if (callable.path.length === 0) {
|
|
1655
|
+
throw typeError("The tools root is not callable.", callee ?? node);
|
|
1420
1656
|
}
|
|
1421
|
-
|
|
1422
|
-
}
|
|
1657
|
+
return this.createToolCallPromise(callable.path, args, node);
|
|
1658
|
+
}
|
|
1659
|
+
throw typeError(`${calleeDescription(callee) ?? "The called value"} is not a function.`, callee ?? node);
|
|
1423
1660
|
}
|
|
1424
|
-
// Built-ins throw without a location, synchronously or inside their
|
|
1661
|
+
// Built-ins throw without a location, synchronously or inside their work; the call site supplies it.
|
|
1425
1662
|
native(body, node) {
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1663
|
+
let result;
|
|
1664
|
+
try {
|
|
1665
|
+
result = body();
|
|
1666
|
+
}
|
|
1667
|
+
catch (error) {
|
|
1668
|
+
throw locate(error, node);
|
|
1669
|
+
}
|
|
1670
|
+
return isEval(result) ? located(result, node) : result;
|
|
1671
|
+
}
|
|
1672
|
+
*evaluateCallArguments(argNodes) {
|
|
1673
|
+
const args = [];
|
|
1674
|
+
for (const argNode of argNodes) {
|
|
1675
|
+
if (argNode.type === "SpreadElement") {
|
|
1676
|
+
const spread = yield* this.evaluateExpression(argNode.argument);
|
|
1677
|
+
const cursor = yield* this.iterate(spread, argNode);
|
|
1678
|
+
if (cursor === undefined)
|
|
1679
|
+
throw typeError("Spread arguments require a synchronous iterable.", argNode);
|
|
1680
|
+
while (true) {
|
|
1681
|
+
const step = yield* cursor.next();
|
|
1682
|
+
if (step.done)
|
|
1683
|
+
break;
|
|
1684
|
+
args.push(step.value);
|
|
1447
1685
|
}
|
|
1448
1686
|
}
|
|
1449
|
-
|
|
1450
|
-
|
|
1687
|
+
else {
|
|
1688
|
+
const value = this.evaluate(argNode);
|
|
1689
|
+
args.push(isEval(value) ? yield* value : value);
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
return args;
|
|
1451
1693
|
}
|
|
1452
|
-
// A
|
|
1694
|
+
// A program call runs as its own activation on the explicit stack, so it is a `Call` for the caller to yield. An
|
|
1695
|
+
// async call returns its promise when the body first suspends or ends; a generator call binds its parameters and
|
|
1696
|
+
// returns the generator without running the body.
|
|
1453
1697
|
invokeFunction(fn, thisValue, args, node) {
|
|
1454
|
-
const
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1698
|
+
const runner = this.ctx.runner;
|
|
1699
|
+
// The program's own activation is at the bottom of the stack.
|
|
1700
|
+
if (runner.depth > MAX_CALL_DEPTH)
|
|
1701
|
+
throw rangeError("Maximum call stack size exceeded", node);
|
|
1702
|
+
const invocation = new Frame(this.ctx, new ScopeStack([...fn.capturedScopes, new Map()]));
|
|
1703
|
+
const paramScope = invocation.scopes.current();
|
|
1704
|
+
// `this` and `arguments` are scope bindings so arrows resolve them lexically; a parameter named
|
|
1705
|
+
// `arguments` shadows the object, as in JS.
|
|
1706
|
+
if (!fn.arrow)
|
|
1707
|
+
paramScope.set("this", { mutable: false, value: thisValue, initialized: true });
|
|
1708
|
+
if (!fn.arrow && usesArguments(fn)) {
|
|
1709
|
+
paramScope.set("arguments", {
|
|
1710
|
+
mutable: true,
|
|
1711
|
+
value: new Arguments(this.ctx.builtins.Object, args),
|
|
1712
|
+
initialized: true,
|
|
1713
|
+
});
|
|
1714
|
+
}
|
|
1715
|
+
const simple = isSimple(fn.parameters);
|
|
1716
|
+
if (simple) {
|
|
1717
|
+
// A repeated name takes the last argument for it, as in JS.
|
|
1718
|
+
for (const [index, parameter] of fn.parameters.entries()) {
|
|
1719
|
+
paramScope.set(parameter.name, { mutable: true, value: args[index], initialized: true });
|
|
1471
1720
|
}
|
|
1721
|
+
}
|
|
1722
|
+
else {
|
|
1472
1723
|
// Seed all parameters first so defaults cannot fall through to same-named outer bindings.
|
|
1473
1724
|
for (const parameter of fn.parameters) {
|
|
1474
1725
|
for (const name of collectPatternNames(parameter)) {
|
|
1475
1726
|
paramScope.set(name, { mutable: true, value: undefined, initialized: false });
|
|
1476
1727
|
}
|
|
1477
1728
|
}
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
const result = (value, done) => record(builtins.Object, { value, done });
|
|
1516
|
-
const turn = this.ctx.pending.turn;
|
|
1517
|
-
const request = (kind, value) => {
|
|
1518
|
-
const request = { kind, value, response: Deferred.makeUnsafe() };
|
|
1519
|
-
// A body that ran on the caller's turn has answered or suspended when it hands the turn back. An answer still
|
|
1520
|
-
// pending can only be an async generator's: its promise settles when the body answers, without the turn.
|
|
1521
|
-
const answer = Effect.suspend(() => Deferred.isDoneUnsafe(request.response)
|
|
1522
|
-
? Deferred.await(request.response)
|
|
1523
|
-
: turn.leave(Deferred.await(request.response)));
|
|
1524
|
-
if (!asynchronous && state.active)
|
|
1525
|
-
return Effect.die(typeError("Generator is already running."));
|
|
1526
|
-
if (asynchronous && (state.completed || (!state.started && kind !== "next"))) {
|
|
1527
|
-
state.started = true;
|
|
1528
|
-
state.completed = true;
|
|
1529
|
-
state.pending.push(request);
|
|
1530
|
-
if (state.draining)
|
|
1531
|
-
return answer;
|
|
1532
|
-
state.draining = true;
|
|
1533
|
-
return Effect.andThen(this.ctx.pending.fork(invocation
|
|
1534
|
-
.completeGeneratorRequests(state, true)
|
|
1535
|
-
.pipe(Effect.ensuring(Effect.sync(() => (state.draining = false))))), answer);
|
|
1536
|
-
}
|
|
1537
|
-
if (state.completed) {
|
|
1538
|
-
if (kind === "throw")
|
|
1539
|
-
return Effect.fail(new Throw(value));
|
|
1540
|
-
return Effect.succeed(result(kind === "return" ? value : undefined, true));
|
|
1541
|
-
}
|
|
1542
|
-
if (!state.started && kind !== "next") {
|
|
1543
|
-
state.completed = true;
|
|
1544
|
-
if (kind === "throw")
|
|
1545
|
-
return Effect.fail(new Throw(value));
|
|
1546
|
-
return Effect.succeed(result(value, true));
|
|
1547
|
-
}
|
|
1548
|
-
state.pending.push(request);
|
|
1549
|
-
if (state.available) {
|
|
1550
|
-
// The body is parked waiting for a request: it runs this step on the caller's turn.
|
|
1551
|
-
const available = state.available;
|
|
1552
|
-
state.available = undefined;
|
|
1553
|
-
return Effect.andThen(turn.handOff(Effect.sync(() => Deferred.doneUnsafe(available, Exit.succeed(undefined)))), answer);
|
|
1554
|
-
}
|
|
1555
|
-
if (!state.started) {
|
|
1556
|
-
state.started = true;
|
|
1557
|
-
const body = Effect.gen(function* () {
|
|
1558
|
-
state.active = yield* invocation.takeGeneratorRequest(state);
|
|
1559
|
-
const exit = yield* Effect.exit(run.pipe(Effect.flatMap((result) => (asynchronous ? invocation.awaitValue(result) : Effect.succeed(result))), Effect.catch((error) => error instanceof GeneratorReturn
|
|
1560
|
-
? asynchronous
|
|
1561
|
-
? invocation.awaitValue(error.value)
|
|
1562
|
-
: Effect.succeed(error.value)
|
|
1563
|
-
: Effect.fail(error))));
|
|
1564
|
-
const active = state.active;
|
|
1565
|
-
state.active = undefined;
|
|
1566
|
-
if (active) {
|
|
1567
|
-
Deferred.doneUnsafe(active.response, Exit.isSuccess(exit) ? Exit.succeed(result(exit.value, true)) : exit);
|
|
1568
|
-
}
|
|
1569
|
-
yield* invocation.completeGeneratorRequests(state, asynchronous);
|
|
1570
|
-
state.completed = true;
|
|
1571
|
-
});
|
|
1572
|
-
return Effect.andThen(this.ctx.pending.fork(body), answer);
|
|
1573
|
-
}
|
|
1574
|
-
return answer;
|
|
1575
|
-
};
|
|
1576
|
-
const proto = get(fn, "prototype");
|
|
1577
|
-
return new GeneratorObj(proto instanceof Obj ? proto : builtins.Generator, asynchronous, request);
|
|
1578
|
-
}
|
|
1579
|
-
completeGeneratorRequests(state, asynchronous) {
|
|
1580
|
-
const self = this;
|
|
1581
|
-
const result = (value, done) => record(self.ctx.builtins.Object, { value, done });
|
|
1582
|
-
return Effect.gen(function* () {
|
|
1583
|
-
while (true) {
|
|
1584
|
-
const pending = self.dequeueGeneratorRequest(state);
|
|
1585
|
-
if (!pending)
|
|
1586
|
-
return;
|
|
1587
|
-
if (pending.kind === "throw") {
|
|
1588
|
-
Deferred.doneUnsafe(pending.response, Exit.fail(new Throw(pending.value)));
|
|
1589
|
-
continue;
|
|
1590
|
-
}
|
|
1591
|
-
if (asynchronous && pending.kind === "return") {
|
|
1592
|
-
const resolved = yield* Effect.exit(self.awaitValue(pending.value));
|
|
1593
|
-
Deferred.doneUnsafe(pending.response, Exit.isSuccess(resolved) ? Exit.succeed(result(resolved.value, true)) : resolved);
|
|
1729
|
+
}
|
|
1730
|
+
if (fn.generator) {
|
|
1731
|
+
invocation.generator = true;
|
|
1732
|
+
invocation.generatorAsync = fn.async;
|
|
1733
|
+
// The prototype is read after the parameters bind, since a default may replace it.
|
|
1734
|
+
const create = () => {
|
|
1735
|
+
const proto = get(fn, "prototype");
|
|
1736
|
+
return new GeneratorObj(proto instanceof Obj ? proto : fn.async ? this.ctx.builtins.AsyncGenerator : this.ctx.builtins.Generator, fn.async, generatorBody(invocation.functionBody(fn, paramScope)));
|
|
1737
|
+
};
|
|
1738
|
+
if (simple)
|
|
1739
|
+
return create();
|
|
1740
|
+
return (function* () {
|
|
1741
|
+
yield* invocation.bindParameters(fn, args);
|
|
1742
|
+
return create();
|
|
1743
|
+
})();
|
|
1744
|
+
}
|
|
1745
|
+
const body = simple ? invocation.functionBody(fn, paramScope) : invocation.enter(fn, args, paramScope);
|
|
1746
|
+
if (!fn.async)
|
|
1747
|
+
return new Call(body);
|
|
1748
|
+
const promise = runner.newPromise();
|
|
1749
|
+
const resolving = runner.resolving(promise);
|
|
1750
|
+
return new Call(body, new AsyncBoundary((completion) => completion.ok ? resolving.resolve(completion.value) : resolving.reject(completion.error), promise));
|
|
1751
|
+
}
|
|
1752
|
+
*enter(fn, args, paramScope) {
|
|
1753
|
+
yield* this.bindParameters(fn, args);
|
|
1754
|
+
return yield* this.functionBody(fn, paramScope);
|
|
1755
|
+
}
|
|
1756
|
+
*bindParameters(fn, args) {
|
|
1757
|
+
const parameters = fn.parameters;
|
|
1758
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
1759
|
+
if (parameter.type === "RestElement") {
|
|
1760
|
+
yield* this.declarePattern(parameter.argument, new Arr(this.ctx.builtins.Array, args.slice(index)), true, parameter, true);
|
|
1761
|
+
break;
|
|
1762
|
+
}
|
|
1763
|
+
if (parameter.type === "Identifier") {
|
|
1764
|
+
// A sloppy simple parameter list may repeat a name; the last occurrence wins, as in JS.
|
|
1765
|
+
if (parameters.findLastIndex((other) => other.type === "Identifier" && other.name === parameter.name) !== index)
|
|
1594
1766
|
continue;
|
|
1595
|
-
|
|
1596
|
-
|
|
1767
|
+
this.scopes.initialize(parameter.name, args[index], parameter);
|
|
1768
|
+
continue;
|
|
1597
1769
|
}
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
takeGeneratorRequest(state) {
|
|
1601
|
-
const next = this.dequeueGeneratorRequest(state);
|
|
1602
|
-
if (next)
|
|
1603
|
-
return Effect.succeed(next);
|
|
1604
|
-
state.available = Deferred.makeUnsafe();
|
|
1605
|
-
return Effect.andThen(this.ctx.pending.turn.park(Deferred.await(state.available)), Effect.sync(() => this.dequeueGeneratorRequest(state)));
|
|
1770
|
+
yield* this.declarePattern(parameter, args[index], true, parameter, true);
|
|
1771
|
+
}
|
|
1606
1772
|
}
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
state.pending = [];
|
|
1614
|
-
state.pendingIndex = 0;
|
|
1773
|
+
*functionBody(fn, paramScope) {
|
|
1774
|
+
if (fn.body.type === "BlockStatement") {
|
|
1775
|
+
this.scopes.push();
|
|
1776
|
+
this.hoistVars(fn.body.body, paramScope);
|
|
1777
|
+
const result = yield* this.evaluateBlock(fn.body);
|
|
1778
|
+
return result.kind === "return" ? result.value : undefined;
|
|
1615
1779
|
}
|
|
1616
|
-
|
|
1780
|
+
const result = this.evaluate(fn.body);
|
|
1781
|
+
return isEval(result) ? yield* result : result;
|
|
1617
1782
|
}
|
|
1618
|
-
evaluateYieldExpression(node) {
|
|
1783
|
+
*evaluateYieldExpression(node) {
|
|
1619
1784
|
const argument = node.argument;
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
});
|
|
1649
|
-
}
|
|
1650
|
-
delegateYield(value, node) {
|
|
1651
|
-
const self = this;
|
|
1652
|
-
return Effect.gen(function* () {
|
|
1653
|
-
const cursor = self.builtinCursor(value);
|
|
1654
|
-
if (cursor !== undefined) {
|
|
1655
|
-
while (true) {
|
|
1656
|
-
const step = yield* cursor.next;
|
|
1657
|
-
if (step.done)
|
|
1658
|
-
return undefined;
|
|
1659
|
-
const resumed = yield* Effect.exit(self.suspendGenerator(self.generatorAsync ? yield* self.awaitValue(step.value) : step.value, node));
|
|
1660
|
-
if (Exit.isSuccess(resumed))
|
|
1661
|
-
continue;
|
|
1662
|
-
const error = Cause.squash(resumed.cause);
|
|
1785
|
+
if (!this.generator)
|
|
1786
|
+
throw typeError("yield is only valid inside a generator.", node);
|
|
1787
|
+
const value = argument ? yield* this.evaluateExpression(argument) : undefined;
|
|
1788
|
+
if (node.delegate)
|
|
1789
|
+
return yield* this.delegateYield(value, node);
|
|
1790
|
+
return yield* this.suspendGenerator(this.generatorAsync ? yield* this.awaitValue(value) : value);
|
|
1791
|
+
}
|
|
1792
|
+
// Yield `value` and continue with the request that resumes the body: `next` supplies the value of the `yield`,
|
|
1793
|
+
// `throw` throws there, and `return` unwinds through `finally` blocks (an async generator awaits the value first).
|
|
1794
|
+
*suspendGenerator(value) {
|
|
1795
|
+
const request = (yield new Yield(value));
|
|
1796
|
+
if (request.kind === "next")
|
|
1797
|
+
return request.value;
|
|
1798
|
+
if (request.kind === "throw")
|
|
1799
|
+
throw new Throw(request.value);
|
|
1800
|
+
throw new GeneratorReturn(this.generatorAsync ? yield* this.awaitValue(request.value) : request.value);
|
|
1801
|
+
}
|
|
1802
|
+
*delegateYield(value, node) {
|
|
1803
|
+
const builtin = this.builtinCursor(value);
|
|
1804
|
+
if (builtin !== undefined && !this.generatorAsync) {
|
|
1805
|
+
while (true) {
|
|
1806
|
+
const step = yield* builtin.next();
|
|
1807
|
+
if (step.done)
|
|
1808
|
+
return undefined;
|
|
1809
|
+
try {
|
|
1810
|
+
yield* this.suspendGenerator(step.value);
|
|
1811
|
+
}
|
|
1812
|
+
catch (error) {
|
|
1663
1813
|
if (error instanceof GeneratorReturn) {
|
|
1664
|
-
yield*
|
|
1665
|
-
|
|
1814
|
+
yield* builtin.close();
|
|
1815
|
+
throw error;
|
|
1666
1816
|
}
|
|
1667
1817
|
if (error instanceof Throw) {
|
|
1668
|
-
yield*
|
|
1818
|
+
yield* builtin.close();
|
|
1669
1819
|
throw typeError("The delegated iterator does not provide a throw() method.", node);
|
|
1670
1820
|
}
|
|
1671
|
-
|
|
1821
|
+
throw error;
|
|
1672
1822
|
}
|
|
1673
1823
|
}
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
if (!(error instanceof GeneratorReturn) && !(error instanceof Throw))
|
|
1706
|
-
|
|
1707
|
-
}
|
|
1824
|
+
}
|
|
1825
|
+
const custom = builtin === undefined ? yield* this.customIterator(value, node, this.generatorAsync) : undefined;
|
|
1826
|
+
if (builtin === undefined && !custom)
|
|
1827
|
+
throw typeError("yield* requires a compatible iterable value.", node);
|
|
1828
|
+
// An async generator delegates to a synchronous iterator through CreateAsyncFromSyncIterator, as JS does.
|
|
1829
|
+
const iterator = this.generatorAsync && !custom?.asynchronous ? this.asyncFromSync(builtin, custom, node) : custom;
|
|
1830
|
+
let kind = "next";
|
|
1831
|
+
let input = undefined;
|
|
1832
|
+
while (true) {
|
|
1833
|
+
const method = kind === "next" ? iterator.next : get(iterator.iterator, kind);
|
|
1834
|
+
if (method === undefined || method === null) {
|
|
1835
|
+
if (kind === "return")
|
|
1836
|
+
throw new GeneratorReturn(this.generatorAsync ? yield* this.awaitValue(input) : input);
|
|
1837
|
+
yield* this.closeIterator(iterator, node);
|
|
1838
|
+
throw typeError("The delegated iterator does not provide a throw() method.", node);
|
|
1839
|
+
}
|
|
1840
|
+
const called = yield* this.call(this.requireIteratorMethod(method, `Iterator ${kind}`, node), iterator.iterator, [input], node);
|
|
1841
|
+
const result = this.requireIteratorObject(iterator.asynchronous ? yield* this.awaitValue(called) : called, `Iterator ${kind}() result`, node);
|
|
1842
|
+
const done = Boolean(get(result, "done"));
|
|
1843
|
+
const resultValue = get(result, "value");
|
|
1844
|
+
if (done) {
|
|
1845
|
+
if (kind === "return")
|
|
1846
|
+
throw new GeneratorReturn(resultValue);
|
|
1847
|
+
return resultValue;
|
|
1848
|
+
}
|
|
1849
|
+
try {
|
|
1850
|
+
// Delegated values are yielded as they are; an async generator's inner promise already settled them.
|
|
1851
|
+
input = yield* this.suspendGenerator(resultValue);
|
|
1852
|
+
kind = "next";
|
|
1853
|
+
}
|
|
1854
|
+
catch (error) {
|
|
1855
|
+
if (!(error instanceof GeneratorReturn) && !(error instanceof Throw))
|
|
1856
|
+
throw error;
|
|
1708
1857
|
kind = error instanceof GeneratorReturn ? "return" : "throw";
|
|
1709
1858
|
input = error.value;
|
|
1710
1859
|
}
|
|
1711
|
-
}
|
|
1860
|
+
}
|
|
1712
1861
|
}
|
|
1713
|
-
evaluateObjectExpression(node) {
|
|
1862
|
+
*evaluateObjectExpression(node) {
|
|
1714
1863
|
const objectValue = new Obj(this.ctx.builtins.Object);
|
|
1715
|
-
const
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
if (
|
|
1719
|
-
const spread = yield* self.evaluateExpression(property.argument);
|
|
1720
|
-
if (spread === null || spread === undefined)
|
|
1721
|
-
continue;
|
|
1722
|
-
assign(objectValue, enumerableSource(self.ctx, "Object spread", spread, property));
|
|
1864
|
+
for (const property of node.properties) {
|
|
1865
|
+
if (property.type === "SpreadElement") {
|
|
1866
|
+
const spread = yield* this.evaluateExpression(property.argument);
|
|
1867
|
+
if (spread === null || spread === undefined)
|
|
1723
1868
|
continue;
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
}
|
|
1869
|
+
assign(objectValue, enumerableSource(this.ctx, "Object spread", spread, property));
|
|
1870
|
+
continue;
|
|
1871
|
+
}
|
|
1872
|
+
if (property.kind !== "init") {
|
|
1873
|
+
throw typeError("Only init object properties are supported.", property);
|
|
1874
|
+
}
|
|
1875
|
+
const keyNode = property.key;
|
|
1876
|
+
let key;
|
|
1877
|
+
if (property.computed) {
|
|
1878
|
+
key = yield* this.toPropertyKey(yield* this.evaluateExpression(keyNode), keyNode);
|
|
1879
|
+
}
|
|
1880
|
+
else if (keyNode.type === "Identifier") {
|
|
1881
|
+
key = keyNode.name;
|
|
1882
|
+
}
|
|
1883
|
+
else if (keyNode.type === "Literal") {
|
|
1884
|
+
key = propertyKey(literal(keyNode));
|
|
1885
|
+
}
|
|
1886
|
+
else {
|
|
1887
|
+
throw typeError("Unsupported object property key shape.", keyNode);
|
|
1888
|
+
}
|
|
1889
|
+
const name = key === IteratorSymbol
|
|
1890
|
+
? "[Symbol.iterator]"
|
|
1891
|
+
: key === AsyncIteratorSymbol
|
|
1892
|
+
? "[Symbol.asyncIterator]"
|
|
1893
|
+
: String(key);
|
|
1894
|
+
define(objectValue, key, yield* this.evaluateNamed(property.value, name));
|
|
1895
|
+
}
|
|
1896
|
+
return objectValue;
|
|
1751
1897
|
}
|
|
1752
|
-
evaluateArrayExpression(node) {
|
|
1898
|
+
*evaluateArrayExpression(node) {
|
|
1753
1899
|
const values = [];
|
|
1754
|
-
const
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
break;
|
|
1771
|
-
values.push(step.value);
|
|
1772
|
-
}
|
|
1773
|
-
}
|
|
1774
|
-
else {
|
|
1775
|
-
values.push(yield* self.evaluateExpression(element));
|
|
1900
|
+
for (const element of node.elements) {
|
|
1901
|
+
if (element === null) {
|
|
1902
|
+
// A literal elision is a real hole, like JS: extend length without an own index.
|
|
1903
|
+
values.length += 1;
|
|
1904
|
+
continue;
|
|
1905
|
+
}
|
|
1906
|
+
if (element.type === "SpreadElement") {
|
|
1907
|
+
const spread = yield* this.evaluateExpression(element.argument);
|
|
1908
|
+
const cursor = yield* this.iterate(spread, element);
|
|
1909
|
+
if (cursor === undefined)
|
|
1910
|
+
throw typeError("Array spread requires a synchronous iterable.", element);
|
|
1911
|
+
while (true) {
|
|
1912
|
+
const step = yield* cursor.next();
|
|
1913
|
+
if (step.done)
|
|
1914
|
+
break;
|
|
1915
|
+
values.push(step.value);
|
|
1776
1916
|
}
|
|
1777
1917
|
}
|
|
1778
|
-
|
|
1779
|
-
|
|
1918
|
+
else {
|
|
1919
|
+
values.push(yield* this.evaluateExpression(element));
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
return new Arr(this.ctx.builtins.Array, values);
|
|
1780
1923
|
}
|
|
1781
|
-
evaluateTemplateLiteral(node) {
|
|
1924
|
+
*evaluateTemplateLiteral(node) {
|
|
1782
1925
|
const quasis = node.quasis;
|
|
1783
1926
|
const expressions = node.expressions;
|
|
1784
1927
|
let output = "";
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
for
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1928
|
+
for (let index = 0; index < quasis.length; index += 1) {
|
|
1929
|
+
const quasi = quasis[index];
|
|
1930
|
+
// acorn only omits `cooked` for invalid escapes, which are a parse error outside a tagged template.
|
|
1931
|
+
if (typeof quasi.value.cooked !== "string") {
|
|
1932
|
+
throw typeError("Invalid template literal quasi.", quasi);
|
|
1933
|
+
}
|
|
1934
|
+
output += quasi.value.cooked;
|
|
1935
|
+
checkStringLength(output.length);
|
|
1936
|
+
if (index < expressions.length) {
|
|
1937
|
+
const raw = yield* this.evaluateExpression(expressions[index]);
|
|
1938
|
+
output += coerceToString(yield* this.toPrimitive(raw, "string", expressions[index]));
|
|
1794
1939
|
checkStringLength(output.length);
|
|
1795
|
-
if (index < expressions.length) {
|
|
1796
|
-
const raw = yield* self.evaluateExpression(expressions[index]);
|
|
1797
|
-
output += coerceToString(yield* self.toPrimitive(raw, "string", expressions[index]));
|
|
1798
|
-
checkStringLength(output.length);
|
|
1799
|
-
}
|
|
1800
1940
|
}
|
|
1801
|
-
|
|
1802
|
-
|
|
1941
|
+
}
|
|
1942
|
+
return output;
|
|
1803
1943
|
}
|
|
1804
1944
|
// `tag\`a${x}b\`` is `tag(strings, x)`: the tag is read like a callee (a member keeps its receiver), the
|
|
1805
1945
|
// substitutions are evaluated in order, and `strings` carries the escaped source as `strings.raw`.
|
|
1806
|
-
evaluateTaggedTemplate(node) {
|
|
1807
|
-
const
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
});
|
|
1946
|
+
*evaluateTaggedTemplate(node) {
|
|
1947
|
+
const { callable, thisValue } = node.tag.type === "MemberExpression"
|
|
1948
|
+
? yield* this.readMethod(node.tag)
|
|
1949
|
+
: { callable: yield* this.evaluateExpression(node.tag), thisValue: undefined };
|
|
1950
|
+
const strings = this.ctx.templates.get(node) ?? this.createTemplateObject(node);
|
|
1951
|
+
const values = [];
|
|
1952
|
+
for (const expression of node.quasi.expressions)
|
|
1953
|
+
values.push(yield* this.evaluateExpression(expression));
|
|
1954
|
+
return yield* this.call(callable, thisValue, [strings, ...values], node, node.tag);
|
|
1816
1955
|
}
|
|
1817
1956
|
createTemplateObject(node) {
|
|
1818
1957
|
const array = this.ctx.builtins.Array;
|
|
@@ -1822,34 +1961,51 @@ class Frame {
|
|
|
1822
1961
|
this.ctx.templates.set(node, strings);
|
|
1823
1962
|
return strings;
|
|
1824
1963
|
}
|
|
1825
|
-
|
|
1826
|
-
return Effect.flatMap(this.evaluateExpression(node.test), (test) => this.evaluateExpression(test ? node.consequent : node.alternate));
|
|
1827
|
-
}
|
|
1828
|
-
getMemberReference(node) {
|
|
1964
|
+
*getMemberReference(node, object = unevaluated, key = unevaluated) {
|
|
1829
1965
|
const objectNode = node.object;
|
|
1830
1966
|
const propertyNode = node.property;
|
|
1831
1967
|
if (objectNode.type === "Super")
|
|
1832
1968
|
throw unsupportedSyntax(objectNode.type, objectNode);
|
|
1833
1969
|
if (propertyNode.type === "PrivateIdentifier")
|
|
1834
1970
|
throw unsupportedSyntax(propertyNode.type, propertyNode);
|
|
1835
|
-
const
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1971
|
+
const evaluated = object === unevaluated ? this.evaluate(objectNode) : object;
|
|
1972
|
+
const objectValue = isEval(evaluated) ? yield* evaluated : evaluated;
|
|
1973
|
+
if (objectValue === OptionalShortCircuit)
|
|
1974
|
+
return OptionalShortCircuit;
|
|
1975
|
+
if ((objectValue === null || objectValue === undefined) && node.optional)
|
|
1976
|
+
return OptionalShortCircuit;
|
|
1977
|
+
const computed = key !== unevaluated
|
|
1978
|
+
? key
|
|
1979
|
+
: !node.computed && propertyNode.type === "Identifier"
|
|
1843
1980
|
? propertyNode.name
|
|
1844
|
-
:
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1981
|
+
: this.evaluate(propertyNode);
|
|
1982
|
+
const keyValue = isEval(computed) ? yield* computed : computed;
|
|
1983
|
+
const reference = this.memberReference(node, objectValue, keyValue);
|
|
1984
|
+
return isEval(reference) ? yield* reference : reference;
|
|
1985
|
+
}
|
|
1986
|
+
// The reference once the object and key are evaluated: ToPropertyKey on an object key may run program code.
|
|
1987
|
+
// GetValue applies ToObject to the base before ToPropertyKey, so a nullish base throws before the key's own
|
|
1988
|
+
// toString runs.
|
|
1989
|
+
memberReference(node, objectValue, keyValue) {
|
|
1990
|
+
if (objectValue === null || objectValue === undefined) {
|
|
1991
|
+
throw typeError(`Cannot read properties of ${objectValue} (reading '${coerceToString(keyValue)}').`, node.object);
|
|
1992
|
+
}
|
|
1993
|
+
if (keyValue instanceof Obj)
|
|
1994
|
+
return this.resolveAfterKey(node, objectValue, keyValue);
|
|
1995
|
+
return this.resolveProperty(objectValue, propertyKey(keyValue), node.object, node.property);
|
|
1996
|
+
}
|
|
1997
|
+
*resolveAfterKey(node, objectValue, keyValue) {
|
|
1998
|
+
const key = yield* this.toPropertyKey(keyValue, node.property);
|
|
1999
|
+
return this.resolveProperty(objectValue, key, node.object, node.property);
|
|
2000
|
+
}
|
|
2001
|
+
readResolved(node, reference) {
|
|
2002
|
+
if (reference === OptionalShortCircuit)
|
|
2003
|
+
return OptionalShortCircuit;
|
|
2004
|
+
if (reference instanceof ToolReference)
|
|
2005
|
+
return reference;
|
|
2006
|
+
if ("value" in reference)
|
|
2007
|
+
return reference.value;
|
|
2008
|
+
return this.readReference(reference, node);
|
|
1853
2009
|
}
|
|
1854
2010
|
resolveProperty(objectValue, key, objectNode, propertyNode) {
|
|
1855
2011
|
if (objectValue instanceof ToolReference) {
|
|
@@ -1901,55 +2057,47 @@ class Frame {
|
|
|
1901
2057
|
throw locate(error, node);
|
|
1902
2058
|
}
|
|
1903
2059
|
}
|
|
1904
|
-
readMember(node) {
|
|
1905
|
-
return
|
|
1906
|
-
if (reference === OptionalShortCircuit)
|
|
1907
|
-
return OptionalShortCircuit;
|
|
1908
|
-
if (reference instanceof ToolReference)
|
|
1909
|
-
return reference;
|
|
1910
|
-
if ("value" in reference)
|
|
1911
|
-
return reference.value;
|
|
1912
|
-
return this.readReference(reference, node);
|
|
1913
|
-
});
|
|
2060
|
+
*readMember(node, object = unevaluated, key = unevaluated) {
|
|
2061
|
+
return this.readResolved(node, yield* this.getMemberReference(node, object, key));
|
|
1914
2062
|
}
|
|
1915
2063
|
writeMember(node, value) {
|
|
1916
|
-
return this.modifyMember(node, ()
|
|
2064
|
+
return this.modifyMember(node, function* () {
|
|
2065
|
+
return { write: true, next: value, result: value };
|
|
2066
|
+
});
|
|
1917
2067
|
}
|
|
1918
|
-
evaluateDeleteExpression(argument) {
|
|
2068
|
+
*evaluateDeleteExpression(argument) {
|
|
1919
2069
|
const target = argument.type === "ChainExpression" ? argument.expression : argument;
|
|
1920
2070
|
if (target.type === "Identifier")
|
|
1921
2071
|
throw typeError("Only data fields may be deleted.", argument);
|
|
1922
2072
|
// `delete <non-reference>` evaluates the operand and is true, as in JS.
|
|
1923
|
-
if (target.type !== "MemberExpression")
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
2073
|
+
if (target.type !== "MemberExpression") {
|
|
2074
|
+
yield* this.evaluateExpression(target);
|
|
2075
|
+
return true;
|
|
2076
|
+
}
|
|
2077
|
+
const reference = yield* this.getMemberReference(target);
|
|
2078
|
+
if (reference === OptionalShortCircuit)
|
|
2079
|
+
return true;
|
|
2080
|
+
if (reference instanceof ToolReference || "value" in reference || reference.receiver !== reference.target) {
|
|
2081
|
+
throw invalidData("Only data fields may be deleted.", target);
|
|
2082
|
+
}
|
|
2083
|
+
if (remove(reference.target, reference.key))
|
|
2084
|
+
return true;
|
|
2085
|
+
throw typeError(`Cannot delete property '${String(reference.key)}'.`, target);
|
|
1935
2086
|
}
|
|
1936
2087
|
// Resolve side-effecting object and key expressions exactly once.
|
|
1937
|
-
modifyMember(node, compute) {
|
|
1938
|
-
const
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
}
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
self.assignToReference(reference.target, key, next, node);
|
|
1951
|
-
return result;
|
|
1952
|
-
});
|
|
2088
|
+
*modifyMember(node, compute) {
|
|
2089
|
+
const reference = yield* this.getMemberReference(node);
|
|
2090
|
+
if (reference === OptionalShortCircuit || reference instanceof ToolReference || "value" in reference) {
|
|
2091
|
+
throw typeError("Only data fields may be assigned.", node);
|
|
2092
|
+
}
|
|
2093
|
+
if (reference.receiver !== reference.target) {
|
|
2094
|
+
throw typeError(`Cannot create property '${String(reference.key)}' on ${typeof reference.receiver} '${String(reference.receiver)}'.`, node);
|
|
2095
|
+
}
|
|
2096
|
+
const key = reference.key;
|
|
2097
|
+
const { write, next, result } = yield* compute(this.readReference(reference, node));
|
|
2098
|
+
if (write)
|
|
2099
|
+
this.assignToReference(reference.target, key, next, node);
|
|
2100
|
+
return result;
|
|
1953
2101
|
}
|
|
1954
2102
|
assignToReference(target, key, next, node) {
|
|
1955
2103
|
const written = (() => {
|
|
@@ -1967,9 +2115,39 @@ class Frame {
|
|
|
1967
2115
|
}
|
|
1968
2116
|
// ToPropertyKey: a data object converts through its own `toString`/`valueOf` first; anything else becomes its
|
|
1969
2117
|
// string form synchronously, so `counts[row.category]` works when the field is null.
|
|
1970
|
-
toPropertyKey(value, node) {
|
|
2118
|
+
*toPropertyKey(value, node) {
|
|
1971
2119
|
if (!(value instanceof Obj))
|
|
1972
|
-
return
|
|
1973
|
-
return
|
|
2120
|
+
return propertyKey(value);
|
|
2121
|
+
return propertyKey(yield* this.toPrimitive(value, "string", node));
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
// The body a generator object resumes: a `return()` request ends it with the returned value.
|
|
2125
|
+
function* generatorBody(body) {
|
|
2126
|
+
try {
|
|
2127
|
+
return yield* body;
|
|
1974
2128
|
}
|
|
2129
|
+
catch (error) {
|
|
2130
|
+
if (error instanceof GeneratorReturn)
|
|
2131
|
+
return error.value;
|
|
2132
|
+
throw error;
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
// Rethrows inside interpreter work, so a failure already caught can pass through `preserveConsumerError`.
|
|
2136
|
+
function* thrower(error) {
|
|
2137
|
+
throw error;
|
|
2138
|
+
}
|
|
2139
|
+
// Finishes a built-in's work, locating a failure at the call that started it.
|
|
2140
|
+
function* located(work, node) {
|
|
2141
|
+
try {
|
|
2142
|
+
return yield* work;
|
|
2143
|
+
}
|
|
2144
|
+
catch (error) {
|
|
2145
|
+
throw locate(error, node);
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
// Lets the host run first, then starts the work.
|
|
2149
|
+
function* afterSlice(start) {
|
|
2150
|
+
yield Slice;
|
|
2151
|
+
const result = start();
|
|
2152
|
+
return isEval(result) ? yield* result : result;
|
|
1975
2153
|
}
|