@next-core/cook 2.2.18 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/cook.js CHANGED
@@ -11,6 +11,7 @@ var _traverse = require("./traverse.js");
11
11
  /** For next-core internal usage only. */
12
12
  function cook(rootAst, codeSource, {
13
13
  rules,
14
+ debug,
14
15
  globalVariables = {},
15
16
  hooks = {}
16
17
  } = {}) {
@@ -46,9 +47,14 @@ function cook(rootAst, codeSource, {
46
47
  TemplateMap.set(templateLiteral, template);
47
48
  return template;
48
49
  }
49
- function Evaluate(node, optionalChainRef) {
50
- var _hooks$beforeEvaluate, _hooks$beforeBranch, _hooks$beforeBranch2;
50
+ let currentNode;
51
+ function* Evaluate(node, optionalChainRef, forceYield) {
52
+ var _hooks$beforeEvaluate, _hooks$beforeBranch2;
51
53
  (_hooks$beforeEvaluate = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate === void 0 || _hooks$beforeEvaluate.call(hooks, node);
54
+ currentNode = node;
55
+ if (debug && (forceYield || node.type.endsWith("Statement") && node.type !== "TryStatement" && node.type !== "BlockStatement" && node.type !== "ForStatement" && node.type !== "ForInStatement" && node.type !== "ForOfStatement")) {
56
+ yield;
57
+ }
52
58
  // Expressions:
53
59
  switch (node.type) {
54
60
  case "ArrayExpression":
@@ -59,10 +65,10 @@ function cook(rootAst, codeSource, {
59
65
  if (!element) {
60
66
  array.length += 1;
61
67
  } else if (element.type === "SpreadElement") {
62
- const spreadValues = (0, _contextFree.GetValue)(Evaluate(element.argument));
68
+ const spreadValues = (0, _contextFree.GetValue)(yield* Evaluate(element.argument));
63
69
  array.push(...spreadValues);
64
70
  } else {
65
- array.push((0, _contextFree.GetValue)(Evaluate(element)));
71
+ array.push((0, _contextFree.GetValue)(yield* Evaluate(element)));
66
72
  }
67
73
  }
68
74
  return (0, _ExecutionContext.NormalCompletion)(array);
@@ -76,9 +82,9 @@ function cook(rootAst, codeSource, {
76
82
  }
77
83
  case "BinaryExpression":
78
84
  {
79
- const leftRef = Evaluate(node.left);
85
+ const leftRef = yield* Evaluate(node.left);
80
86
  const leftValue = (0, _contextFree.GetValue)(leftRef);
81
- const rightRef = Evaluate(node.right).Value;
87
+ const rightRef = (yield* Evaluate(node.right)).Value;
82
88
  const rightValue = (0, _contextFree.GetValue)(rightRef);
83
89
  if (expressionOnly && node.operator === "|>") {
84
90
  // Minimal pipeline operator is supported only in expression-only mode.
@@ -103,21 +109,22 @@ function cook(rootAst, codeSource, {
103
109
  case "CallExpression":
104
110
  {
105
111
  // https://tc39.es/ecma262/#sec-function-calls
106
- const ref = Evaluate(node.callee, optionalChainRef).Value;
112
+ const ref = (yield* Evaluate(node.callee, optionalChainRef)).Value;
107
113
  const func = (0, _contextFree.GetValue)(ref);
108
114
  if ((func === undefined || func === null) && (node.optional || optionalChainRef !== null && optionalChainRef !== void 0 && optionalChainRef.skipped)) {
109
115
  optionalChainRef.skipped = true;
110
116
  return (0, _ExecutionContext.NormalCompletion)(undefined);
111
117
  }
112
118
  (0, _sanitize.sanitize)(func);
113
- return EvaluateCall(func, ref, node.arguments, node.callee);
119
+ if (debug) yield;
120
+ return yield* EvaluateCall(func, ref, node.arguments, node.callee);
114
121
  }
115
122
  case "ChainExpression":
116
123
  // https://tc39.es/ecma262/#sec-optional-chains
117
- return Evaluate(node.expression, {});
124
+ return yield* Evaluate(node.expression, {});
118
125
  case "ConditionalExpression":
119
126
  // https://tc39.es/ecma262/#sec-conditional-operator
120
- return (0, _ExecutionContext.NormalCompletion)((0, _contextFree.GetValue)(Evaluate((0, _contextFree.GetValue)(Evaluate(node.test)) ? node.consequent : node.alternate)));
127
+ return (0, _ExecutionContext.NormalCompletion)((0, _contextFree.GetValue)(yield* Evaluate((0, _contextFree.GetValue)(yield* Evaluate(node.test)) ? node.consequent : node.alternate)));
121
128
  case "Identifier":
122
129
  // https://tc39.es/ecma262/#sec-identifiers
123
130
  return (0, _ExecutionContext.NormalCompletion)(ResolveBinding(node.name));
@@ -139,14 +146,14 @@ function cook(rootAst, codeSource, {
139
146
  case "LogicalExpression":
140
147
  {
141
148
  // https://tc39.es/ecma262/#sec-binary-logical-operators
142
- const leftValue = (0, _contextFree.GetValue)(Evaluate(node.left));
149
+ const leftValue = (0, _contextFree.GetValue)(yield* Evaluate(node.left));
143
150
  switch (node.operator) {
144
151
  case "&&":
145
- return (0, _ExecutionContext.NormalCompletion)(leftValue && (0, _contextFree.GetValue)(Evaluate(node.right)));
152
+ return (0, _ExecutionContext.NormalCompletion)(leftValue && (0, _contextFree.GetValue)(yield* Evaluate(node.right)));
146
153
  case "||":
147
- return (0, _ExecutionContext.NormalCompletion)(leftValue || (0, _contextFree.GetValue)(Evaluate(node.right)));
154
+ return (0, _ExecutionContext.NormalCompletion)(leftValue || (0, _contextFree.GetValue)(yield* Evaluate(node.right)));
148
155
  case "??":
149
- return (0, _ExecutionContext.NormalCompletion)(leftValue ?? (0, _contextFree.GetValue)(Evaluate(node.right)));
156
+ return (0, _ExecutionContext.NormalCompletion)(leftValue ?? (0, _contextFree.GetValue)(yield* Evaluate(node.right)));
150
157
  // istanbul ignore next
151
158
  default:
152
159
  throw new SyntaxError(
@@ -158,37 +165,41 @@ function cook(rootAst, codeSource, {
158
165
  case "MemberExpression":
159
166
  {
160
167
  // https://tc39.es/ecma262/#sec-property-accessors
161
- const baseReference = Evaluate(node.object, optionalChainRef).Value;
168
+ const baseReference = (yield* Evaluate(node.object, optionalChainRef)).Value;
162
169
  const baseValue = (0, _contextFree.GetValue)(baseReference);
163
170
  if ((baseValue === undefined || baseValue === null) && (node.optional || optionalChainRef !== null && optionalChainRef !== void 0 && optionalChainRef.skipped)) {
164
171
  optionalChainRef.skipped = true;
165
172
  return (0, _ExecutionContext.NormalCompletion)(undefined);
166
173
  }
167
174
  (0, _sanitize.sanitize)(baseValue);
168
- const result = node.computed ? EvaluatePropertyAccessWithExpressionKey(baseValue, node.property, true) : EvaluatePropertyAccessWithIdentifierKey(baseValue, node.property, true);
175
+ const result = node.computed ? yield* EvaluatePropertyAccessWithExpressionKey(baseValue, node.property, true) : EvaluatePropertyAccessWithIdentifierKey(baseValue, node.property, true);
169
176
  (0, _sanitize.sanitize)(result);
170
177
  return (0, _ExecutionContext.NormalCompletion)(result);
171
178
  }
172
179
  case "NewExpression":
173
180
  // https://tc39.es/ecma262/#sec-new-operator
174
- return EvaluateNew(node.callee, node.arguments);
181
+ return yield* EvaluateNew(node.callee, node.arguments);
175
182
  case "ObjectExpression":
176
183
  {
177
184
  // https://tc39.es/ecma262/#sec-object-initializer
178
185
  const object = {};
179
186
  for (const prop of node.properties) {
180
187
  if (prop.type === "SpreadElement") {
181
- const fromValue = (0, _contextFree.GetValue)(Evaluate(prop.argument));
188
+ const fromValue = (0, _contextFree.GetValue)(yield* Evaluate(prop.argument));
182
189
  (0, _contextFree.CopyDataProperties)(object, fromValue, new Set());
183
190
  } else {
184
191
  if (prop.kind !== "init") {
185
192
  throw new SyntaxError("Unsupported object getter/setter");
186
193
  }
187
- const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : EvaluateComputedPropertyName(prop.key);
194
+ const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : yield* EvaluateComputedPropertyName(prop.key);
188
195
  if (propName === "__proto__") {
189
196
  throw new TypeError("Setting '__proto__' property is not allowed");
190
197
  }
191
- object[propName] = (0, _contextFree.GetValue)(Evaluate(prop.value));
198
+ const propValue = (0, _contextFree.GetValue)(yield* Evaluate(prop.value));
199
+ if (prop.method && typeof propValue === "function") {
200
+ SetFunctionName(propValue, propName);
201
+ }
202
+ object[propName] = propValue;
192
203
  }
193
204
  }
194
205
  return (0, _ExecutionContext.NormalCompletion)(object);
@@ -198,7 +209,7 @@ function cook(rootAst, codeSource, {
198
209
  // https://tc39.es/ecma262/#sec-comma-operator
199
210
  let result;
200
211
  for (const expr of node.expressions) {
201
- result = (0, _ExecutionContext.NormalCompletion)((0, _contextFree.GetValue)(Evaluate(expr)));
212
+ result = (0, _ExecutionContext.NormalCompletion)((0, _contextFree.GetValue)(yield* Evaluate(expr)));
202
213
  }
203
214
  return result;
204
215
  }
@@ -208,7 +219,7 @@ function cook(rootAst, codeSource, {
208
219
  const chunks = [node.quasis[0].value.cooked];
209
220
  let index = 0;
210
221
  for (const expr of node.expressions) {
211
- const val = (0, _contextFree.GetValue)(Evaluate(expr));
222
+ const val = (0, _contextFree.GetValue)(yield* Evaluate(expr));
212
223
  chunks.push(String(val));
213
224
  chunks.push(node.quasis[index += 1].value.cooked);
214
225
  }
@@ -217,15 +228,16 @@ function cook(rootAst, codeSource, {
217
228
  case "TaggedTemplateExpression":
218
229
  {
219
230
  // https://tc39.es/ecma262/#sec-tagged-templates
220
- const tagRef = Evaluate(node.tag).Value;
231
+ const tagRef = (yield* Evaluate(node.tag)).Value;
221
232
  const tagFunc = (0, _contextFree.GetValue)(tagRef);
222
233
  (0, _sanitize.sanitize)(tagFunc);
223
- return EvaluateCall(tagFunc, tagRef, node.quasi, node.tag);
234
+ if (debug) yield;
235
+ return yield* EvaluateCall(tagFunc, tagRef, node.quasi, node.tag);
224
236
  }
225
237
  case "UnaryExpression":
226
238
  {
227
239
  // https://tc39.es/ecma262/#sec-unary-operators
228
- const ref = Evaluate(node.argument).Value;
240
+ const ref = (yield* Evaluate(node.argument)).Value;
229
241
  if (!expressionOnly && node.operator === "delete") {
230
242
  // Delete operator is supported only in function mode.
231
243
  if (!(ref instanceof _ExecutionContext.ReferenceRecord)) {
@@ -255,22 +267,26 @@ function cook(rootAst, codeSource, {
255
267
  // https://tc39.es/ecma262/#sec-assignment-operators
256
268
  if (node.operator === "=") {
257
269
  if (!(node.left.type === "ArrayPattern" || node.left.type === "ObjectPattern")) {
258
- const lref = Evaluate(node.left).Value;
259
- // Todo: IsAnonymousFunctionDefinition(lref)
260
- const rref = Evaluate(node.right);
261
- const rval = (0, _contextFree.GetValue)(rref);
270
+ const lref = (yield* Evaluate(node.left)).Value;
271
+ let rval;
272
+ if (IsAnonymousFunctionDefinition(node.right) && node.left.type === "Identifier") {
273
+ rval = NamedEvaluation(node.right, node.left.name);
274
+ } else {
275
+ const rref = yield* Evaluate(node.right);
276
+ rval = (0, _contextFree.GetValue)(rref);
277
+ }
262
278
  (0, _contextFree.PutValue)(lref, rval);
263
279
  return (0, _ExecutionContext.NormalCompletion)(rval);
264
280
  }
265
- const rref = Evaluate(node.right);
281
+ const rref = yield* Evaluate(node.right);
266
282
  const rval = (0, _contextFree.GetValue)(rref);
267
- DestructuringAssignmentEvaluation(node.left, rval);
283
+ yield* DestructuringAssignmentEvaluation(node.left, rval);
268
284
  return (0, _ExecutionContext.NormalCompletion)(rval);
269
285
  }
270
286
  // Operators other than `=`.
271
- const lref = Evaluate(node.left).Value;
287
+ const lref = (yield* Evaluate(node.left)).Value;
272
288
  const lval = (0, _contextFree.GetValue)(lref);
273
- const rref = Evaluate(node.right);
289
+ const rref = yield* Evaluate(node.right);
274
290
  const rval = (0, _contextFree.GetValue)(rref);
275
291
  const r = (0, _contextFree.ApplyStringOrNumericAssignment)(lval, node.operator, rval);
276
292
  (0, _contextFree.PutValue)(lref, r);
@@ -286,7 +302,7 @@ function cook(rootAst, codeSource, {
286
302
  const blockEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
287
303
  BlockDeclarationInstantiation(node.body, blockEnv);
288
304
  getRunningContext().LexicalEnvironment = blockEnv;
289
- const blockValue = EvaluateStatementList(node.body);
305
+ const blockValue = yield* EvaluateStatementList(node.body);
290
306
  getRunningContext().LexicalEnvironment = oldEnv;
291
307
  return blockValue;
292
308
  }
@@ -301,18 +317,18 @@ function cook(rootAst, codeSource, {
301
317
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
302
318
  case "DoWhileStatement":
303
319
  // https://tc39.es/ecma262/#sec-do-while-statement
304
- return EvaluateBreakableStatement(DoWhileLoopEvaluation(node));
320
+ return EvaluateBreakableStatement(yield* DoWhileLoopEvaluation(node));
305
321
  case "ExpressionStatement":
306
322
  case "TSAsExpression":
307
323
  // https://tc39.es/ecma262/#sec-expression-statement
308
- return Evaluate(node.expression);
324
+ return yield* Evaluate(node.expression);
309
325
  case "ForInStatement":
310
326
  case "ForOfStatement":
311
327
  // https://tc39.es/ecma262/#sec-for-in-and-for-of-statements
312
- return EvaluateBreakableStatement(ForInOfLoopEvaluation(node));
328
+ return EvaluateBreakableStatement(yield* ForInOfLoopEvaluation(node));
313
329
  case "ForStatement":
314
330
  // https://tc39.es/ecma262/#sec-for-statement
315
- return EvaluateBreakableStatement(ForLoopEvaluation(node));
331
+ return EvaluateBreakableStatement(yield* ForLoopEvaluation(node));
316
332
  case "FunctionDeclaration":
317
333
  // https://tc39.es/ecma262/#sec-function-definitions
318
334
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
@@ -322,41 +338,51 @@ function cook(rootAst, codeSource, {
322
338
  return (0, _ExecutionContext.NormalCompletion)(InstantiateOrdinaryFunctionExpression(node));
323
339
  case "IfStatement":
324
340
  // https://tc39.es/ecma262/#sec-if-statement
325
- return (0, _contextFree.GetValue)(Evaluate(node.test)) ? ((_hooks$beforeBranch = hooks.beforeBranch) !== null && _hooks$beforeBranch !== void 0 && _hooks$beforeBranch.call(hooks, node, "if"), (0, _contextFree.UpdateEmpty)(Evaluate(node.consequent), undefined)) : ((_hooks$beforeBranch2 = hooks.beforeBranch) !== null && _hooks$beforeBranch2 !== void 0 && _hooks$beforeBranch2.call(hooks, node, "else"), node.alternate) ? (0, _contextFree.UpdateEmpty)(Evaluate(node.alternate), undefined) : (0, _ExecutionContext.NormalCompletion)(undefined);
341
+ if ((0, _contextFree.GetValue)(yield* Evaluate(node.test))) {
342
+ var _hooks$beforeBranch;
343
+ (_hooks$beforeBranch = hooks.beforeBranch) === null || _hooks$beforeBranch === void 0 || _hooks$beforeBranch.call(hooks, node, "if");
344
+ return (0, _contextFree.UpdateEmpty)(yield* Evaluate(node.consequent), undefined);
345
+ }
346
+ (_hooks$beforeBranch2 = hooks.beforeBranch) === null || _hooks$beforeBranch2 === void 0 || _hooks$beforeBranch2.call(hooks, node, "else");
347
+ if (node.alternate) {
348
+ return (0, _contextFree.UpdateEmpty)(yield* Evaluate(node.alternate), undefined);
349
+ }
350
+ return (0, _ExecutionContext.NormalCompletion)(undefined);
326
351
  case "ReturnStatement":
327
352
  {
328
353
  // https://tc39.es/ecma262/#sec-return-statement
329
354
  let v;
330
355
  if (node.argument) {
331
- const exprRef = Evaluate(node.argument);
356
+ const exprRef = yield* Evaluate(node.argument);
332
357
  v = (0, _contextFree.GetValue)(exprRef);
333
358
  }
359
+ currentNode = node;
334
360
  return new _ExecutionContext.CompletionRecord("return", v);
335
361
  }
336
362
  case "ThrowStatement":
337
363
  // https://tc39.es/ecma262/#sec-throw-statement
338
- throw (0, _contextFree.GetValue)(Evaluate(node.argument));
364
+ throw (0, _contextFree.GetValue)(yield* Evaluate(node.argument));
339
365
  case "UpdateExpression":
340
366
  {
341
367
  // https://tc39.es/ecma262/#sec-update-expressions
342
- const lhs = Evaluate(node.argument).Value;
368
+ const lhs = (yield* Evaluate(node.argument)).Value;
343
369
  const oldValue = Number((0, _contextFree.GetValue)(lhs));
344
370
  const newValue = node.operator === "++" ? oldValue + 1 : oldValue - 1;
345
371
  (0, _contextFree.PutValue)(lhs, newValue);
346
372
  return (0, _ExecutionContext.NormalCompletion)(node.prefix ? newValue : oldValue);
347
373
  }
348
374
  case "SwitchCase":
349
- return EvaluateStatementList(node.consequent);
375
+ return yield* EvaluateStatementList(node.consequent);
350
376
  case "SwitchStatement":
351
377
  {
352
378
  // https://tc39.es/ecma262/#sec-switch-statement
353
- const exprRef = Evaluate(node.discriminant);
379
+ const exprRef = yield* Evaluate(node.discriminant);
354
380
  const switchValue = (0, _contextFree.GetValue)(exprRef);
355
381
  const oldEnv = getRunningContext().LexicalEnvironment;
356
382
  const blockEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
357
383
  BlockDeclarationInstantiation(node.cases, blockEnv);
358
384
  getRunningContext().LexicalEnvironment = blockEnv;
359
- const R = CaseBlockEvaluation(node.cases, switchValue);
385
+ const R = yield* CaseBlockEvaluation(node.cases, switchValue);
360
386
  getRunningContext().LexicalEnvironment = oldEnv;
361
387
  return EvaluateBreakableStatement(R);
362
388
  }
@@ -365,18 +391,19 @@ function cook(rootAst, codeSource, {
365
391
  // https://tc39.es/ecma262/#sec-try-statement
366
392
  let R;
367
393
  try {
368
- R = Evaluate(node.block);
394
+ R = yield* Evaluate(node.block);
369
395
  } catch (error) {
370
396
  if (node.handler) {
371
397
  var _hooks$beforeEvaluate2;
398
+ currentNode = node.handler;
372
399
  (_hooks$beforeEvaluate2 = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate2 === void 0 || _hooks$beforeEvaluate2.call(hooks, node.handler);
373
- R = CatchClauseEvaluation(node.handler, error);
400
+ R = yield* CatchClauseEvaluation(node.handler, error);
374
401
  } else {
375
402
  throw error;
376
403
  }
377
404
  } finally {
378
405
  if (node.finalizer) {
379
- const F = Evaluate(node.finalizer);
406
+ const F = yield* Evaluate(node.finalizer);
380
407
  if (F.Type !== "normal") {
381
408
  R = F;
382
409
  }
@@ -389,6 +416,7 @@ function cook(rootAst, codeSource, {
389
416
  // https://tc39.es/ecma262/#sec-declarations-and-the-variable-statement
390
417
  let result;
391
418
  for (const declarator of node.declarations) {
419
+ currentNode = declarator;
392
420
  if (!declarator.init) {
393
421
  // Assert: a declarator without init is always an identifier.
394
422
  if (node.kind === "var") {
@@ -398,23 +426,31 @@ function cook(rootAst, codeSource, {
398
426
  result = (0, _contextFree.InitializeReferencedBinding)(lhs, undefined);
399
427
  }
400
428
  } else if (declarator.id.type === "Identifier") {
429
+ currentNode = declarator.init;
430
+ if (debug) yield;
401
431
  const bindingId = declarator.id.name;
402
432
  const lhs = ResolveBinding(bindingId);
403
- // Todo: IsAnonymousFunctionDefinition(Initializer)
404
- const rhs = Evaluate(declarator.init);
405
- const value = (0, _contextFree.GetValue)(rhs);
433
+ let value;
434
+ if (IsAnonymousFunctionDefinition(declarator.init)) {
435
+ value = NamedEvaluation(declarator.init, bindingId);
436
+ } else {
437
+ const rhs = yield* Evaluate(declarator.init);
438
+ value = (0, _contextFree.GetValue)(rhs);
439
+ }
406
440
  result = node.kind === "var" ? (0, _contextFree.PutValue)(lhs, value) : (0, _contextFree.InitializeReferencedBinding)(lhs, value);
407
441
  } else {
408
- const rhs = Evaluate(declarator.init);
442
+ currentNode = declarator.init;
443
+ if (debug) yield;
444
+ const rhs = yield* Evaluate(declarator.init);
409
445
  const rval = (0, _contextFree.GetValue)(rhs);
410
- result = BindingInitialization(declarator.id, rval, node.kind === "var" ? undefined : getRunningContext().LexicalEnvironment);
446
+ result = yield* BindingInitialization(declarator.id, rval, node.kind === "var" ? undefined : getRunningContext().LexicalEnvironment);
411
447
  }
412
448
  }
413
449
  return result;
414
450
  }
415
451
  case "WhileStatement":
416
452
  // https://tc39.es/ecma262/#sec-while-statement
417
- return EvaluateBreakableStatement(WhileLoopEvaluation(node));
453
+ return EvaluateBreakableStatement(yield* WhileLoopEvaluation(node));
418
454
  }
419
455
  }
420
456
  // eslint-disable-next-line no-console
@@ -436,9 +472,9 @@ function cook(rootAst, codeSource, {
436
472
 
437
473
  // Try statements.
438
474
  // https://tc39.es/ecma262/#sec-runtime-semantics-catchclauseevaluation
439
- function CatchClauseEvaluation(node, thrownValue) {
475
+ function* CatchClauseEvaluation(node, thrownValue) {
440
476
  if (!node.param) {
441
- return Evaluate(node.body);
477
+ return yield* Evaluate(node.body);
442
478
  }
443
479
  const oldEnv = getRunningContext().LexicalEnvironment;
444
480
  const catchEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
@@ -446,8 +482,8 @@ function cook(rootAst, codeSource, {
446
482
  catchEnv.CreateMutableBinding(argName, false);
447
483
  }
448
484
  getRunningContext().LexicalEnvironment = catchEnv;
449
- BindingInitialization(node.param, thrownValue, catchEnv);
450
- const B = Evaluate(node.body);
485
+ yield* BindingInitialization(node.param, thrownValue, catchEnv);
486
+ const B = yield* Evaluate(node.body);
451
487
  getRunningContext().LexicalEnvironment = oldEnv;
452
488
  return B;
453
489
  }
@@ -460,7 +496,7 @@ function cook(rootAst, codeSource, {
460
496
 
461
497
  // Switch statements.
462
498
  // https://tc39.es/ecma262/#sec-runtime-semantics-caseblockevaluation
463
- function CaseBlockEvaluation(cases, input) {
499
+ function* CaseBlockEvaluation(cases, input) {
464
500
  let V;
465
501
  const defaultCaseIndex = cases.findIndex(switchCase => !switchCase.test);
466
502
  const hasDefaultCase = defaultCaseIndex >= 0;
@@ -468,10 +504,10 @@ function cook(rootAst, codeSource, {
468
504
  let found = false;
469
505
  for (const C of A) {
470
506
  if (!found) {
471
- found = CaseClauseIsSelected(C, input);
507
+ found = yield* CaseClauseIsSelected(C, input);
472
508
  }
473
509
  if (found) {
474
- const R = Evaluate(C);
510
+ const R = yield* Evaluate(C);
475
511
  if (R.Value !== _ExecutionContext.Empty) {
476
512
  V = R.Value;
477
513
  }
@@ -488,10 +524,10 @@ function cook(rootAst, codeSource, {
488
524
  if (!found) {
489
525
  for (const C of B) {
490
526
  if (!foundInB) {
491
- foundInB = CaseClauseIsSelected(C, input);
527
+ foundInB = yield* CaseClauseIsSelected(C, input);
492
528
  }
493
529
  if (foundInB) {
494
- const R = Evaluate(C);
530
+ const R = yield* Evaluate(C);
495
531
  if (R.Value !== _ExecutionContext.Empty) {
496
532
  V = R.Value;
497
533
  }
@@ -504,7 +540,7 @@ function cook(rootAst, codeSource, {
504
540
  if (foundInB) {
505
541
  return (0, _ExecutionContext.NormalCompletion)(V);
506
542
  }
507
- const R = Evaluate(cases[defaultCaseIndex]);
543
+ const R = yield* Evaluate(cases[defaultCaseIndex]);
508
544
  if (R.Value !== _ExecutionContext.Empty) {
509
545
  V = R.Value;
510
546
  }
@@ -514,7 +550,7 @@ function cook(rootAst, codeSource, {
514
550
 
515
551
  // NOTE: The following is another complete iteration of the second CaseClauses.
516
552
  for (const C of B) {
517
- const R = Evaluate(C);
553
+ const R = yield* Evaluate(C);
518
554
  if (R.Value !== _ExecutionContext.Empty) {
519
555
  V = R.Value;
520
556
  }
@@ -526,22 +562,22 @@ function cook(rootAst, codeSource, {
526
562
  }
527
563
 
528
564
  // https://tc39.es/ecma262/#sec-runtime-semantics-caseclauseisselected
529
- function CaseClauseIsSelected(C, input) {
530
- const clauseSelector = (0, _contextFree.GetValue)(Evaluate(C.test));
565
+ function* CaseClauseIsSelected(C, input) {
566
+ const clauseSelector = (0, _contextFree.GetValue)(yield* Evaluate(C.test));
531
567
  return input === clauseSelector;
532
568
  }
533
569
 
534
570
  // While statements.
535
571
  // https://tc39.es/ecma262/#sec-runtime-semantics-whileloopevaluation
536
- function WhileLoopEvaluation(node) {
572
+ function* WhileLoopEvaluation(node) {
537
573
  let V;
538
574
  // eslint-disable-next-line no-constant-condition
539
575
  while (true) {
540
- const exprValue = (0, _contextFree.GetValue)(Evaluate(node.test));
576
+ const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test));
541
577
  if (!exprValue) {
542
578
  return (0, _ExecutionContext.NormalCompletion)(V);
543
579
  }
544
- const stmtResult = Evaluate(node.body);
580
+ const stmtResult = yield* Evaluate(node.body);
545
581
  if (!(0, _contextFree.LoopContinues)(stmtResult)) {
546
582
  return (0, _contextFree.UpdateEmpty)(stmtResult, V);
547
583
  }
@@ -553,18 +589,18 @@ function cook(rootAst, codeSource, {
553
589
 
554
590
  // Do-while Statements.
555
591
  // https://tc39.es/ecma262/#sec-runtime-semantics-dowhileloopevaluation
556
- function DoWhileLoopEvaluation(node) {
592
+ function* DoWhileLoopEvaluation(node) {
557
593
  let V;
558
594
  // eslint-disable-next-line no-constant-condition
559
595
  while (true) {
560
- const stmtResult = Evaluate(node.body);
596
+ const stmtResult = yield* Evaluate(node.body);
561
597
  if (!(0, _contextFree.LoopContinues)(stmtResult)) {
562
598
  return (0, _contextFree.UpdateEmpty)(stmtResult, V);
563
599
  }
564
600
  if (stmtResult.Value !== _ExecutionContext.Empty) {
565
601
  V = stmtResult.Value;
566
602
  }
567
- const exprValue = (0, _contextFree.GetValue)(Evaluate(node.test));
603
+ const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test));
568
604
  if (!exprValue) {
569
605
  return (0, _ExecutionContext.NormalCompletion)(V);
570
606
  }
@@ -573,22 +609,22 @@ function cook(rootAst, codeSource, {
573
609
 
574
610
  // For in/of statements.
575
611
  // https://tc39.es/ecma262/#sec-runtime-semantics-forinofloopevaluation
576
- function ForInOfLoopEvaluation(node) {
612
+ function* ForInOfLoopEvaluation(node) {
577
613
  const lhs = node.left;
578
614
  const isVariableDeclaration = lhs.type === "VariableDeclaration";
579
615
  const lhsKind = isVariableDeclaration ? lhs.kind === "var" ? "varBinding" : "lexicalBinding" : "assignment";
580
616
  const uninitializedBoundNames = lhsKind === "lexicalBinding" ? (0, _traverse.collectBoundNames)(lhs) : [];
581
617
  const iterationKind = node.type === "ForInStatement" ? "enumerate" : "iterate";
582
- const keyResult = ForInOfHeadEvaluation(uninitializedBoundNames, node.right, iterationKind);
618
+ const keyResult = yield* ForInOfHeadEvaluation(uninitializedBoundNames, node.right, iterationKind);
583
619
  if (keyResult.Type !== "normal") {
584
620
  // When enumerate, if the target is nil, a break completion will be returned.
585
621
  return keyResult;
586
622
  }
587
- return ForInOfBodyEvaluation(lhs, node.body, keyResult.Value, iterationKind, lhsKind);
623
+ return yield* ForInOfBodyEvaluation(lhs, node.body, keyResult.Value, iterationKind, lhsKind);
588
624
  }
589
625
 
590
626
  // https://tc39.es/ecma262/#sec-runtime-semantics-forinofheadevaluation
591
- function ForInOfHeadEvaluation(uninitializedBoundNames, expr, iterationKind) {
627
+ function* ForInOfHeadEvaluation(uninitializedBoundNames, expr, iterationKind) {
592
628
  const runningContext = getRunningContext();
593
629
  const oldEnv = runningContext.LexicalEnvironment;
594
630
  if (uninitializedBoundNames.length > 0) {
@@ -598,7 +634,7 @@ function cook(rootAst, codeSource, {
598
634
  }
599
635
  runningContext.LexicalEnvironment = newEnv;
600
636
  }
601
- const exprRef = Evaluate(expr);
637
+ const exprRef = yield* Evaluate(expr, undefined, true);
602
638
  runningContext.LexicalEnvironment = oldEnv;
603
639
  const exprValue = (0, _contextFree.GetValue)(exprRef);
604
640
  if (iterationKind === "enumerate") {
@@ -611,7 +647,7 @@ function cook(rootAst, codeSource, {
611
647
  const iterator = (0, _contextFree.CreateListIteratorRecord)(exprValue);
612
648
  return (0, _ExecutionContext.NormalCompletion)(iterator);
613
649
  }
614
- function ForInOfBodyEvaluation(node, stmt, iteratorRecord, iterationKind, lhsKind) {
650
+ function* ForInOfBodyEvaluation(node, stmt, iteratorRecord, iterationKind, lhsKind) {
615
651
  const lhs = lhsKind === "assignment" ? node : node.declarations[0].id;
616
652
  const oldEnv = getRunningContext().LexicalEnvironment;
617
653
  let V;
@@ -623,11 +659,13 @@ function cook(rootAst, codeSource, {
623
659
  const destructuring = lhs.type === "ObjectPattern" || lhs.type === "ArrayPattern";
624
660
  // eslint-disable-next-line no-constant-condition
625
661
  while (true) {
662
+ currentNode = lhs;
626
663
  const {
627
664
  done,
628
665
  value: nextValue
629
666
  } = iteratorRecord.next();
630
667
  if (done) {
668
+ if (debug) yield;
631
669
  return (0, _ExecutionContext.NormalCompletion)(V);
632
670
  }
633
671
  let lhsRef;
@@ -636,15 +674,19 @@ function cook(rootAst, codeSource, {
636
674
  iterationEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
637
675
  (0, _contextFree.ForDeclarationBindingInstantiation)(node, iterationEnv);
638
676
  getRunningContext().LexicalEnvironment = iterationEnv;
677
+ if (debug) yield;
639
678
  if (!destructuring) {
640
679
  const [lhsName] = (0, _traverse.collectBoundNames)(lhs);
641
680
  lhsRef = ResolveBinding(lhsName);
642
681
  }
643
- } else if (!destructuring) {
644
- lhsRef = Evaluate(lhs).Value;
682
+ } else {
683
+ if (debug) yield;
684
+ if (!destructuring) {
685
+ lhsRef = (yield* Evaluate(lhs)).Value;
686
+ }
645
687
  }
646
- destructuring ? lhsKind === "assignment" ? DestructuringAssignmentEvaluation(lhs, nextValue) : lhsKind === "varBinding" ? BindingInitialization(lhs, nextValue, undefined) : BindingInitialization(lhs, nextValue, iterationEnv) : lhsKind === "lexicalBinding" ? (0, _contextFree.InitializeReferencedBinding)(lhsRef, nextValue) : (0, _contextFree.PutValue)(lhsRef, nextValue);
647
- const result = Evaluate(stmt);
688
+ destructuring ? lhsKind === "assignment" ? yield* DestructuringAssignmentEvaluation(lhs, nextValue) : lhsKind === "varBinding" ? yield* BindingInitialization(lhs, nextValue, undefined) : yield* BindingInitialization(lhs, nextValue, iterationEnv) : lhsKind === "lexicalBinding" ? (0, _contextFree.InitializeReferencedBinding)(lhsRef, nextValue) : (0, _contextFree.PutValue)(lhsRef, nextValue);
689
+ const result = yield* Evaluate(stmt);
648
690
  getRunningContext().LexicalEnvironment = oldEnv;
649
691
  if (!(0, _contextFree.LoopContinues)(result)) {
650
692
  const status = (0, _contextFree.UpdateEmpty)(result, V);
@@ -673,13 +715,13 @@ function cook(rootAst, codeSource, {
673
715
 
674
716
  // For statements.
675
717
  // https://tc39.es/ecma262/#sec-runtime-semantics-forloopevaluation
676
- function ForLoopEvaluation(node) {
718
+ function* ForLoopEvaluation(node) {
677
719
  var _node$init;
678
720
  if (((_node$init = node.init) === null || _node$init === void 0 ? void 0 : _node$init.type) === "VariableDeclaration") {
679
721
  // `for (var … ; … ; … ) …`
680
722
  if (node.init.kind === "var") {
681
- Evaluate(node.init);
682
- return ForBodyEvaluation(node.test, node.update, node.body, []);
723
+ yield* Evaluate(node.init);
724
+ return yield* ForBodyEvaluation(node.test, node.update, node.body, []);
683
725
  }
684
726
  // `for (let/const … ; … ; … ) …`
685
727
  const oldEnv = getRunningContext().LexicalEnvironment;
@@ -694,34 +736,34 @@ function cook(rootAst, codeSource, {
694
736
  }
695
737
  }
696
738
  getRunningContext().LexicalEnvironment = loopEnv;
697
- Evaluate(node.init);
739
+ yield* Evaluate(node.init);
698
740
  const perIterationLets = isConst ? [] : Array.from(boundNames);
699
- const bodyResult = ForBodyEvaluation(node.test, node.update, node.body, perIterationLets);
741
+ const bodyResult = yield* ForBodyEvaluation(node.test, node.update, node.body, perIterationLets);
700
742
  getRunningContext().LexicalEnvironment = oldEnv;
701
743
  return bodyResult;
702
744
  }
703
745
  // `for ( … ; … ; … ) …`
704
746
  if (node.init) {
705
- const exprRef = Evaluate(node.init);
747
+ const exprRef = yield* Evaluate(node.init);
706
748
  (0, _contextFree.GetValue)(exprRef);
707
749
  }
708
- return ForBodyEvaluation(node.test, node.update, node.body, []);
750
+ return yield* ForBodyEvaluation(node.test, node.update, node.body, []);
709
751
  }
710
752
 
711
753
  // https://tc39.es/ecma262/#sec-forbodyevaluation
712
- function ForBodyEvaluation(test, increment, stmt, perIterationBindings) {
754
+ function* ForBodyEvaluation(test, increment, stmt, perIterationBindings) {
713
755
  CreatePerIterationEnvironment(perIterationBindings);
714
756
  let V;
715
757
  // eslint-disable-next-line no-constant-condition
716
758
  while (true) {
717
759
  if (test) {
718
- const testRef = Evaluate(test);
760
+ const testRef = yield* Evaluate(test, undefined, true);
719
761
  const testValue = (0, _contextFree.GetValue)(testRef);
720
762
  if (!testValue) {
721
763
  return (0, _ExecutionContext.NormalCompletion)(V);
722
764
  }
723
765
  }
724
- const result = Evaluate(stmt);
766
+ const result = yield* Evaluate(stmt);
725
767
  if (!(0, _contextFree.LoopContinues)(result)) {
726
768
  return (0, _contextFree.UpdateEmpty)(result, V);
727
769
  }
@@ -730,7 +772,7 @@ function cook(rootAst, codeSource, {
730
772
  }
731
773
  CreatePerIterationEnvironment(perIterationBindings);
732
774
  if (increment) {
733
- const incRef = Evaluate(increment);
775
+ const incRef = yield* Evaluate(increment, undefined, true);
734
776
  (0, _contextFree.GetValue)(incRef);
735
777
  }
736
778
  }
@@ -754,77 +796,80 @@ function cook(rootAst, codeSource, {
754
796
 
755
797
  // Destructuring assignments.
756
798
  // https://tc39.es/ecma262/#sec-runtime-semantics-destructuringassignmentevaluation
757
- function DestructuringAssignmentEvaluation(pattern, value) {
799
+ function* DestructuringAssignmentEvaluation(pattern, value) {
758
800
  if (pattern.type === "ObjectPattern") {
759
801
  (0, _contextFree.RequireObjectCoercible)(value);
760
802
  if (pattern.properties.length > 0) {
761
- PropertyDestructuringAssignmentEvaluation(pattern.properties, value);
803
+ yield* PropertyDestructuringAssignmentEvaluation(pattern.properties, value);
762
804
  }
763
805
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
764
806
  }
765
807
  const iteratorRecord = (0, _contextFree.CreateListIteratorRecord)(value);
766
- return IteratorDestructuringAssignmentEvaluation(pattern.elements, iteratorRecord);
808
+ return yield* IteratorDestructuringAssignmentEvaluation(pattern.elements, iteratorRecord);
767
809
  }
768
810
 
769
811
  // https://tc39.es/ecma262/#sec-runtime-semantics-propertydestructuringassignmentevaluation
770
- function PropertyDestructuringAssignmentEvaluation(properties, value) {
812
+ function* PropertyDestructuringAssignmentEvaluation(properties, value) {
771
813
  const excludedNames = new Set();
772
814
  for (const prop of properties) {
773
815
  if (prop.type === "Property") {
774
- const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : EvaluateComputedPropertyName(prop.key);
816
+ const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : yield* EvaluateComputedPropertyName(prop.key);
775
817
  const valueTarget = prop.value.type === "AssignmentPattern" ? prop.value.left : prop.value;
776
818
  if (valueTarget.type === "Identifier") {
777
819
  const lref = ResolveBinding(valueTarget.name);
778
820
  let v = (0, _contextFree.GetV)(value, propName);
779
821
  if (prop.value.type === "AssignmentPattern" && v === undefined) {
780
- // Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
781
- const defaultValue = Evaluate(prop.value.right);
782
- v = (0, _contextFree.GetValue)(defaultValue);
822
+ if (IsAnonymousFunctionDefinition(prop.value.right)) {
823
+ v = NamedEvaluation(prop.value.right, valueTarget.name);
824
+ } else {
825
+ const defaultValue = yield* Evaluate(prop.value.right);
826
+ v = (0, _contextFree.GetValue)(defaultValue);
827
+ }
783
828
  }
784
829
  (0, _contextFree.PutValue)(lref, v);
785
830
  excludedNames.add(propName);
786
831
  } else {
787
- KeyedDestructuringAssignmentEvaluation(prop.value, value, propName);
832
+ yield* KeyedDestructuringAssignmentEvaluation(prop.value, value, propName);
788
833
  excludedNames.add(propName);
789
834
  }
790
835
  } else {
791
- RestDestructuringAssignmentEvaluation(prop, value, excludedNames);
836
+ yield* RestDestructuringAssignmentEvaluation(prop, value, excludedNames);
792
837
  }
793
838
  }
794
839
  }
795
840
 
796
841
  // https://tc39.es/ecma262/#sec-runtime-semantics-keyeddestructuringassignmentevaluation
797
- function KeyedDestructuringAssignmentEvaluation(node, value, propertyName) {
842
+ function* KeyedDestructuringAssignmentEvaluation(node, value, propertyName) {
798
843
  const assignmentTarget = node.type === "AssignmentPattern" ? node.left : node;
799
844
  const isObjectOrArray = assignmentTarget.type === "ArrayPattern" || assignmentTarget.type === "ObjectPattern";
800
845
  let lref;
801
846
  if (!isObjectOrArray) {
802
- lref = Evaluate(assignmentTarget).Value;
847
+ lref = (yield* Evaluate(assignmentTarget)).Value;
803
848
  }
804
849
  const v = (0, _contextFree.GetV)(value, propertyName);
805
850
  let rhsValue;
806
851
  if (node.type === "AssignmentPattern" && v === undefined) {
807
- // Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
808
- const defaultValue = Evaluate(node.right);
852
+ // `assignmentTarget.type` is never "Identifier" here.
853
+ const defaultValue = yield* Evaluate(node.right);
809
854
  rhsValue = (0, _contextFree.GetValue)(defaultValue);
810
855
  } else {
811
856
  rhsValue = v;
812
857
  }
813
858
  if (isObjectOrArray) {
814
- return DestructuringAssignmentEvaluation(assignmentTarget, rhsValue);
859
+ return yield* DestructuringAssignmentEvaluation(assignmentTarget, rhsValue);
815
860
  }
816
861
  return (0, _contextFree.PutValue)(lref, rhsValue);
817
862
  }
818
863
 
819
864
  // https://tc39.es/ecma262/#sec-runtime-semantics-restdestructuringassignmentevaluation
820
- function RestDestructuringAssignmentEvaluation(restProperty, value, excludedNames) {
821
- const lref = Evaluate(restProperty.argument).Value;
865
+ function* RestDestructuringAssignmentEvaluation(restProperty, value, excludedNames) {
866
+ const lref = (yield* Evaluate(restProperty.argument)).Value;
822
867
  const restObj = (0, _contextFree.CopyDataProperties)({}, value, excludedNames);
823
868
  return (0, _contextFree.PutValue)(lref, restObj);
824
869
  }
825
870
 
826
871
  // https://tc39.es/ecma262/#sec-runtime-semantics-iteratordestructuringassignmentevaluation
827
- function IteratorDestructuringAssignmentEvaluation(elements, iteratorRecord) {
872
+ function* IteratorDestructuringAssignmentEvaluation(elements, iteratorRecord) {
828
873
  let status = (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
829
874
  for (const element of elements) {
830
875
  if (!element) {
@@ -836,7 +881,7 @@ function cook(rootAst, codeSource, {
836
881
  const isObjectOrArray = assignmentTarget.type === "ArrayPattern" || assignmentTarget.type === "ObjectPattern";
837
882
  let lref;
838
883
  if (!isObjectOrArray) {
839
- lref = Evaluate(assignmentTarget).Value;
884
+ lref = (yield* Evaluate(assignmentTarget)).Value;
840
885
  }
841
886
  let v;
842
887
  if (element.type !== "RestElement") {
@@ -846,9 +891,12 @@ function cook(rootAst, codeSource, {
846
891
  } = iteratorRecord.next();
847
892
  const value = done ? undefined : nextValue;
848
893
  if (element.type === "AssignmentPattern" && value === undefined) {
849
- // Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
850
- const defaultValue = Evaluate(element.right);
851
- v = (0, _contextFree.GetValue)(defaultValue);
894
+ if (IsAnonymousFunctionDefinition(element.right) && assignmentTarget.type === "Identifier") {
895
+ v = NamedEvaluation(element.right, assignmentTarget.name);
896
+ } else {
897
+ const defaultValue = yield* Evaluate(element.right);
898
+ v = (0, _contextFree.GetValue)(defaultValue);
899
+ }
852
900
  } else {
853
901
  v = value;
854
902
  }
@@ -870,7 +918,7 @@ function cook(rootAst, codeSource, {
870
918
  }
871
919
  }
872
920
  if (isObjectOrArray) {
873
- status = DestructuringAssignmentEvaluation(assignmentTarget, v);
921
+ status = yield* DestructuringAssignmentEvaluation(assignmentTarget, v);
874
922
  } else {
875
923
  status = (0, _contextFree.PutValue)(lref, v);
876
924
  }
@@ -880,8 +928,8 @@ function cook(rootAst, codeSource, {
880
928
 
881
929
  // Object expressions.
882
930
  // https://tc39.es/ecma262/#sec-evaluate-property-access-with-expression-key
883
- function EvaluatePropertyAccessWithExpressionKey(baseValue, expression, strict) {
884
- const propertyNameReference = Evaluate(expression);
931
+ function* EvaluatePropertyAccessWithExpressionKey(baseValue, expression, strict) {
932
+ const propertyNameReference = yield* Evaluate(expression);
885
933
  const propertyNameValue = (0, _contextFree.GetValue)(propertyNameReference);
886
934
  const propertyKey = (0, _contextFree.ToPropertyKey)(propertyNameValue);
887
935
  return new _ExecutionContext.ReferenceRecord(baseValue, propertyKey, strict);
@@ -919,28 +967,36 @@ function cook(rootAst, codeSource, {
919
967
 
920
968
  // Function declarations and expressions.
921
969
  // https://tc39.es/ecma262/#sec-evaluatecall
922
- function EvaluateCall(func, ref, args, callee) {
970
+ function* EvaluateCall(func, ref, args, callee) {
923
971
  let thisValue;
924
972
  if (ref instanceof _ExecutionContext.ReferenceRecord) {
925
973
  if ((0, _contextFree.IsPropertyReference)(ref)) {
926
974
  thisValue = ref.Base;
927
975
  }
928
976
  }
929
- const argList = ArgumentListEvaluation(args);
977
+ const argList = yield* ArgumentListEvaluation(args);
930
978
  if (typeof func !== "function") {
931
979
  const funcName = codeSource.substring(callee.start, callee.end);
932
980
  throw new TypeError(`${funcName} is not a function`);
933
981
  }
982
+ if (debug) {
983
+ const debuggerCall = func[_ExecutionContext.DebuggerCall];
984
+ if (debuggerCall) {
985
+ const result = yield* debuggerCall.apply(thisValue, argList);
986
+ (0, _sanitize.sanitize)(result);
987
+ return (0, _ExecutionContext.NormalCompletion)(result);
988
+ }
989
+ }
934
990
  const result = func.apply(thisValue, argList);
935
991
  (0, _sanitize.sanitize)(result);
936
992
  return (0, _ExecutionContext.NormalCompletion)(result);
937
993
  }
938
994
 
939
995
  // https://tc39.es/ecma262/#sec-evaluatenew
940
- function EvaluateNew(constructExpr, args) {
941
- const ref = Evaluate(constructExpr);
996
+ function* EvaluateNew(constructExpr, args) {
997
+ const ref = yield* Evaluate(constructExpr);
942
998
  const constructor = (0, _contextFree.GetValue)(ref);
943
- const argList = ArgumentListEvaluation(args);
999
+ const argList = yield* ArgumentListEvaluation(args);
944
1000
  if (typeof constructor !== "function" || constructor[_ExecutionContext.IsConstructor] === false) {
945
1001
  const constructorName = codeSource.substring(constructExpr.start, constructExpr.end);
946
1002
  throw new TypeError(`${constructorName} is not a constructor`);
@@ -953,32 +1009,39 @@ function cook(rootAst, codeSource, {
953
1009
  }
954
1010
 
955
1011
  // https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation
956
- function ArgumentListEvaluation(args) {
1012
+ function* ArgumentListEvaluation(args) {
957
1013
  const array = [];
958
1014
  if (Array.isArray(args)) {
959
1015
  for (const arg of args) {
960
1016
  if (arg.type === "SpreadElement") {
961
- const spreadValues = (0, _contextFree.GetValue)(Evaluate(arg.argument));
1017
+ const spreadValues = (0, _contextFree.GetValue)(yield* Evaluate(arg.argument));
962
1018
  array.push(...spreadValues);
963
1019
  } else {
964
- array.push((0, _contextFree.GetValue)(Evaluate(arg)));
1020
+ array.push((0, _contextFree.GetValue)(yield* Evaluate(arg)));
965
1021
  }
966
1022
  }
967
1023
  } else {
968
1024
  array.push(GetTemplateObject(args));
969
1025
  for (const expr of args.expressions) {
970
- array.push((0, _contextFree.GetValue)(Evaluate(expr)));
1026
+ array.push((0, _contextFree.GetValue)(yield* Evaluate(expr)));
971
1027
  }
972
1028
  }
973
1029
  return array;
974
1030
  }
975
1031
 
976
1032
  // https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
977
- function CallFunction(closure, args) {
978
- var _hooks$beforeCall;
1033
+ function* CallFunction(closure, args) {
1034
+ var _hooks$beforeCall, _currentNode;
979
1035
  (_hooks$beforeCall = hooks.beforeCall) === null || _hooks$beforeCall === void 0 || _hooks$beforeCall.call(hooks, closure[_ExecutionContext.SourceNode]);
980
1036
  PrepareForOrdinaryCall(closure);
981
- const result = OrdinaryCallEvaluateBody(closure, args);
1037
+ const result = yield* OrdinaryCallEvaluateBody(closure, args);
1038
+ if (((_currentNode = currentNode) === null || _currentNode === void 0 ? void 0 : _currentNode.type) !== "ReturnStatement") {
1039
+ currentNode = closure[_ExecutionContext.SourceNode];
1040
+ }
1041
+ if (debug) yield {
1042
+ type: "return",
1043
+ value: result.Type === "return" ? result.Value : undefined
1044
+ };
982
1045
  executionContextStack.pop();
983
1046
  if (result.Type === "return") {
984
1047
  return result.Value;
@@ -998,24 +1061,24 @@ function cook(rootAst, codeSource, {
998
1061
  }
999
1062
 
1000
1063
  // https://tc39.es/ecma262/#sec-ordinarycallevaluatebody
1001
- function OrdinaryCallEvaluateBody(F, args) {
1002
- return EvaluateFunctionBody(F[_ExecutionContext.ECMAScriptCode], F, args);
1064
+ function* OrdinaryCallEvaluateBody(F, args) {
1065
+ return yield* EvaluateFunctionBody(F[_ExecutionContext.ECMAScriptCode], F, args);
1003
1066
  }
1004
1067
 
1005
1068
  // https://tc39.es/ecma262/#sec-runtime-semantics-evaluatefunctionbody
1006
- function EvaluateFunctionBody(body, F, args) {
1007
- FunctionDeclarationInstantiation(F, args);
1069
+ function* EvaluateFunctionBody(body, F, args) {
1070
+ yield* FunctionDeclarationInstantiation(F, args);
1008
1071
  if (Array.isArray(body)) {
1009
- return EvaluateStatementList(body);
1072
+ return yield* EvaluateStatementList(body);
1010
1073
  }
1011
- return new _ExecutionContext.CompletionRecord("return", (0, _contextFree.GetValue)(Evaluate(body)));
1074
+ return new _ExecutionContext.CompletionRecord("return", (0, _contextFree.GetValue)(yield* Evaluate(body)));
1012
1075
  }
1013
1076
 
1014
1077
  // https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
1015
- function EvaluateStatementList(statements) {
1078
+ function* EvaluateStatementList(statements) {
1016
1079
  let result = (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
1017
1080
  for (const stmt of statements) {
1018
- const s = Evaluate(stmt);
1081
+ const s = yield* Evaluate(stmt);
1019
1082
  if (s.Type !== "normal") {
1020
1083
  return s;
1021
1084
  }
@@ -1024,8 +1087,28 @@ function cook(rootAst, codeSource, {
1024
1087
  return result;
1025
1088
  }
1026
1089
 
1090
+ // https://tc39.es/ecma262/#sec-isanonymousfunctiondefinition
1091
+ function IsAnonymousFunctionDefinition(node) {
1092
+ // No ParenthesizedExpression in ESTree.
1093
+ return node.type === "FunctionExpression" && !node.id || node.type === "ArrowFunctionExpression";
1094
+ }
1095
+
1096
+ // https://tc39.es/ecma262/#sec-runtime-semantics-namedevaluation
1097
+ function NamedEvaluation(node, name) {
1098
+ // No ParenthesizedExpression in ESTree.
1099
+ switch (node.type) {
1100
+ case "FunctionExpression":
1101
+ return InstantiateOrdinaryFunctionExpression(node, name);
1102
+ case "ArrowFunctionExpression":
1103
+ return InstantiateArrowFunctionExpression(node, name);
1104
+ // istanbul ignore next: should never happen
1105
+ default:
1106
+ throw new Error(`Unexpected node type for NamedEvaluation: ${node.type}`);
1107
+ }
1108
+ }
1109
+
1027
1110
  // https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
1028
- function FunctionDeclarationInstantiation(func, args) {
1111
+ function* FunctionDeclarationInstantiation(func, args) {
1029
1112
  const calleeContext = getRunningContext();
1030
1113
  const code = func[_ExecutionContext.ECMAScriptCode];
1031
1114
  const formals = func[_ExecutionContext.FormalParameters];
@@ -1060,7 +1143,7 @@ function cook(rootAst, codeSource, {
1060
1143
  env.CreateMutableBinding(paramName, false);
1061
1144
  }
1062
1145
  const iteratorRecord = (0, _contextFree.CreateListIteratorRecord)(args);
1063
- IteratorBindingInitialization(formals, iteratorRecord, env);
1146
+ yield* IteratorBindingInitialization(formals, iteratorRecord, env);
1064
1147
  let varEnv;
1065
1148
  if (!hasParameterExpressions) {
1066
1149
  // NOTE: Only a single Environment Record is needed for the parameters
@@ -1116,37 +1199,50 @@ function cook(rootAst, codeSource, {
1116
1199
 
1117
1200
  // https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
1118
1201
  function InstantiateFunctionObject(func, scope) {
1119
- return OrdinaryFunctionCreate(func, scope, true);
1202
+ const F = OrdinaryFunctionCreate(func, scope, true);
1203
+ if (func.id) {
1204
+ SetFunctionName(F, func.id.name);
1205
+ }
1206
+ return F;
1120
1207
  }
1121
1208
 
1122
1209
  // https://tc39.es/ecma262/#sec-runtime-semantics-instantiateordinaryfunctionexpression
1123
- function InstantiateOrdinaryFunctionExpression(functionExpression) {
1210
+ function InstantiateOrdinaryFunctionExpression(functionExpression, name) {
1124
1211
  const scope = getRunningContext().LexicalEnvironment;
1125
1212
  if (functionExpression.id) {
1126
1213
  const name = functionExpression.id.name;
1127
1214
  const funcEnv = new _ExecutionContext.DeclarativeEnvironment(scope);
1128
1215
  funcEnv.CreateImmutableBinding(name, false);
1129
1216
  const closure = OrdinaryFunctionCreate(functionExpression, funcEnv, true);
1217
+ SetFunctionName(closure, name);
1130
1218
  funcEnv.InitializeBinding(name, closure);
1131
1219
  return closure;
1132
1220
  } else {
1133
1221
  const closure = OrdinaryFunctionCreate(functionExpression, scope, true);
1222
+ SetFunctionName(closure, name ?? "");
1134
1223
  return closure;
1135
1224
  }
1136
1225
  }
1137
1226
 
1138
1227
  // https://tc39.es/ecma262/#sec-runtime-semantics-instantiatearrowfunctionexpression
1139
- function InstantiateArrowFunctionExpression(arrowFunction) {
1228
+ function InstantiateArrowFunctionExpression(arrowFunction, name) {
1140
1229
  const scope = getRunningContext().LexicalEnvironment;
1141
1230
  const closure = OrdinaryFunctionCreate(arrowFunction, scope, false);
1231
+ SetFunctionName(closure, name ?? "");
1142
1232
  return closure;
1143
1233
  }
1234
+ function SetFunctionName(F, name) {
1235
+ Object.defineProperty(F, "name", {
1236
+ value: name,
1237
+ configurable: true
1238
+ });
1239
+ }
1144
1240
 
1145
1241
  // https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
1146
1242
  function OrdinaryFunctionCreate(sourceNode, scope, isConstructor) {
1147
1243
  const F = function () {
1148
1244
  // eslint-disable-next-line prefer-rest-params
1149
- return CallFunction(F, arguments);
1245
+ return unwind(CallFunction(F, arguments));
1150
1246
  };
1151
1247
  Object.defineProperties(F, {
1152
1248
  [_ExecutionContext.SourceNode]: {
@@ -1165,39 +1261,47 @@ function cook(rootAst, codeSource, {
1165
1261
  value: isConstructor
1166
1262
  }
1167
1263
  });
1264
+ if (debug) {
1265
+ Object.defineProperty(F, _ExecutionContext.DebuggerCall, {
1266
+ value: function () {
1267
+ // eslint-disable-next-line prefer-rest-params
1268
+ return CallFunction(F, arguments);
1269
+ }
1270
+ });
1271
+ }
1168
1272
  return F;
1169
1273
  }
1170
1274
 
1171
1275
  // Patterns initialization.
1172
1276
  // https://tc39.es/ecma262/#sec-runtime-semantics-bindinginitialization
1173
- function BindingInitialization(node, value, environment) {
1277
+ function* BindingInitialization(node, value, environment) {
1174
1278
  switch (node.type) {
1175
1279
  case "Identifier":
1176
1280
  return InitializeBoundName(node.name, value, environment);
1177
1281
  case "ObjectPattern":
1178
1282
  (0, _contextFree.RequireObjectCoercible)(value);
1179
- return PropertyBindingInitialization(node.properties, value, environment);
1283
+ return yield* PropertyBindingInitialization(node.properties, value, environment);
1180
1284
  case "ArrayPattern":
1181
1285
  {
1182
1286
  const iteratorRecord = (0, _contextFree.CreateListIteratorRecord)(value);
1183
- return IteratorBindingInitialization(node.elements, iteratorRecord, environment);
1287
+ return yield* IteratorBindingInitialization(node.elements, iteratorRecord, environment);
1184
1288
  }
1185
1289
  }
1186
1290
  }
1187
1291
 
1188
1292
  // https://tc39.es/ecma262/#sec-destructuring-binding-patterns-runtime-semantics-propertybindinginitialization
1189
- function PropertyBindingInitialization(properties, value, environment) {
1293
+ function* PropertyBindingInitialization(properties, value, environment) {
1190
1294
  const excludedNames = new Set();
1191
1295
  for (const prop of properties) {
1192
1296
  if (prop.type === "RestElement") {
1193
1297
  return RestBindingInitialization(prop, value, environment, excludedNames);
1194
1298
  }
1195
1299
  if (!prop.computed && prop.key.type === "Identifier") {
1196
- KeyedBindingInitialization(prop.value, value, environment, prop.key.name);
1300
+ yield* KeyedBindingInitialization(prop.value, value, environment, prop.key.name);
1197
1301
  excludedNames.add(prop.key.name);
1198
1302
  } else {
1199
- const P = EvaluateComputedPropertyName(prop.key);
1200
- KeyedBindingInitialization(prop.value, value, environment, P);
1303
+ const P = yield* EvaluateComputedPropertyName(prop.key);
1304
+ yield* KeyedBindingInitialization(prop.value, value, environment, P);
1201
1305
  excludedNames.add(P);
1202
1306
  }
1203
1307
  }
@@ -1205,8 +1309,8 @@ function cook(rootAst, codeSource, {
1205
1309
  }
1206
1310
 
1207
1311
  // https://tc39.es/ecma262/#prod-ComputedPropertyName
1208
- function EvaluateComputedPropertyName(node) {
1209
- const propName = (0, _contextFree.GetValue)(Evaluate(node));
1312
+ function* EvaluateComputedPropertyName(node) {
1313
+ const propName = (0, _contextFree.GetValue)(yield* Evaluate(node));
1210
1314
  return (0, _contextFree.ToPropertyKey)(propName);
1211
1315
  }
1212
1316
 
@@ -1221,7 +1325,7 @@ function cook(rootAst, codeSource, {
1221
1325
  }
1222
1326
 
1223
1327
  // https://tc39.es/ecma262/#sec-runtime-semantics-iteratorbindinginitialization
1224
- function IteratorBindingInitialization(elements, iteratorRecord, environment) {
1328
+ function* IteratorBindingInitialization(elements, iteratorRecord, environment) {
1225
1329
  if (elements.length === 0) {
1226
1330
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
1227
1331
  }
@@ -1260,7 +1364,7 @@ function cook(rootAst, codeSource, {
1260
1364
  value
1261
1365
  } = iteratorRecord.next();
1262
1366
  if (done) {
1263
- result = BindingInitialization(node.argument, A, environment);
1367
+ result = yield* BindingInitialization(node.argument, A, environment);
1264
1368
  break;
1265
1369
  }
1266
1370
  A[n] = value;
@@ -1283,10 +1387,10 @@ function cook(rootAst, codeSource, {
1283
1387
  v = value;
1284
1388
  }
1285
1389
  if (node.type === "AssignmentPattern" && v === undefined) {
1286
- const defaultValue = Evaluate(node.right);
1390
+ const defaultValue = yield* Evaluate(node.right);
1287
1391
  v = (0, _contextFree.GetValue)(defaultValue);
1288
1392
  }
1289
- result = BindingInitialization(bindingElement, v, environment);
1393
+ result = yield* BindingInitialization(bindingElement, v, environment);
1290
1394
  break;
1291
1395
  }
1292
1396
  case "Identifier":
@@ -1302,9 +1406,12 @@ function cook(rootAst, codeSource, {
1302
1406
  v = value;
1303
1407
  }
1304
1408
  if (node.type === "AssignmentPattern" && v === undefined) {
1305
- // IsAnonymousFunctionDefinition(Initializer)
1306
- const defaultValue = Evaluate(node.right);
1307
- v = (0, _contextFree.GetValue)(defaultValue);
1409
+ if (IsAnonymousFunctionDefinition(node.right)) {
1410
+ v = NamedEvaluation(node.right, bindingId);
1411
+ } else {
1412
+ const defaultValue = yield* Evaluate(node.right);
1413
+ v = (0, _contextFree.GetValue)(defaultValue);
1414
+ }
1308
1415
  }
1309
1416
  result = environment ? (0, _contextFree.InitializeReferencedBinding)(lhs, v) : (0, _contextFree.PutValue)(lhs, v);
1310
1417
  break;
@@ -1316,16 +1423,19 @@ function cook(rootAst, codeSource, {
1316
1423
  }
1317
1424
 
1318
1425
  // https://tc39.es/ecma262/#sec-runtime-semantics-keyedbindinginitialization
1319
- function KeyedBindingInitialization(node, value, environment, propertyName) {
1426
+ function* KeyedBindingInitialization(node, value, environment, propertyName) {
1320
1427
  const isIdentifier = node.type === "Identifier" || node.type === "AssignmentPattern" && node.left.type === "Identifier";
1321
1428
  if (isIdentifier) {
1322
1429
  const bindingId = node.type === "Identifier" ? node.name : node.left.name;
1323
1430
  const lhs = ResolveBinding(bindingId, environment);
1324
1431
  let v = (0, _contextFree.GetV)(value, propertyName);
1325
1432
  if (node.type === "AssignmentPattern" && v === undefined) {
1326
- // If IsAnonymousFunctionDefinition(Initializer)
1327
- const defaultValue = Evaluate(node.right);
1328
- v = (0, _contextFree.GetValue)(defaultValue);
1433
+ if (IsAnonymousFunctionDefinition(node.right)) {
1434
+ v = NamedEvaluation(node.right, bindingId);
1435
+ } else {
1436
+ const defaultValue = yield* Evaluate(node.right);
1437
+ v = (0, _contextFree.GetValue)(defaultValue);
1438
+ }
1329
1439
  }
1330
1440
  if (!environment) {
1331
1441
  return (0, _contextFree.PutValue)(lhs, v);
@@ -1334,10 +1444,10 @@ function cook(rootAst, codeSource, {
1334
1444
  }
1335
1445
  let v = (0, _contextFree.GetV)(value, propertyName);
1336
1446
  if (node.type === "AssignmentPattern" && v === undefined) {
1337
- const defaultValue = Evaluate(node.right);
1447
+ const defaultValue = yield* Evaluate(node.right);
1338
1448
  v = (0, _contextFree.GetValue)(defaultValue);
1339
1449
  }
1340
- return BindingInitialization(node.type === "AssignmentPattern" ? node.left : node, v, environment);
1450
+ return yield* BindingInitialization(node.type === "AssignmentPattern" ? node.left : node, v, environment);
1341
1451
  }
1342
1452
 
1343
1453
  // https://tc39.es/ecma262/#sec-initializeboundname
@@ -1355,7 +1465,7 @@ function cook(rootAst, codeSource, {
1355
1465
  }
1356
1466
  }
1357
1467
  if (expressionOnly) {
1358
- return (0, _contextFree.GetValue)(Evaluate(rootAst));
1468
+ return (0, _contextFree.GetValue)(unwind(Evaluate(rootAst)));
1359
1469
  }
1360
1470
  (_hooks$beforeEvaluate3 = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate3 === void 0 || _hooks$beforeEvaluate3.call(hooks, rootAst);
1361
1471
  ThrowIfFunctionIsInvalid(rootAst);
@@ -1364,6 +1474,32 @@ function cook(rootAst, codeSource, {
1364
1474
  rootEnv.CreateImmutableBinding(fn, true);
1365
1475
  const fo = InstantiateFunctionObject(rootAst, rootEnv);
1366
1476
  rootEnv.InitializeBinding(fn, fo);
1477
+ if (debug) {
1478
+ Object.defineProperties(fo, {
1479
+ [_ExecutionContext.DebuggerScope]: {
1480
+ value: function () {
1481
+ return getRunningContext().LexicalEnvironment;
1482
+ }
1483
+ },
1484
+ [_ExecutionContext.DebuggerNode]: {
1485
+ value: function () {
1486
+ return currentNode;
1487
+ }
1488
+ }
1489
+ });
1490
+ }
1367
1491
  return fo;
1368
1492
  }
1493
+ function unwind(iterator) {
1494
+ // eslint-disable-next-line no-constant-condition
1495
+ while (true) {
1496
+ const {
1497
+ done,
1498
+ value
1499
+ } = iterator.next();
1500
+ if (done) {
1501
+ return value;
1502
+ }
1503
+ }
1504
+ }
1369
1505
  //# sourceMappingURL=cook.js.map