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