@next-core/cook 2.2.18 → 2.4.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,11 +11,20 @@ var _traverse = require("./traverse.js");
11
11
  /** For next-core internal usage only. */
12
12
  function cook(rootAst, codeSource, {
13
13
  rules,
14
+ debug,
15
+ externalSourceForDebug,
14
16
  globalVariables = {},
17
+ // Allow debugger to override Array constructor.
18
+ ArrayConstructor = Array,
15
19
  hooks = {}
16
20
  } = {}) {
17
21
  var _hooks$beforeEvaluate3;
18
22
  const expressionOnly = rootAst.type !== "FunctionDeclaration";
23
+ function doSanitize(cooked) {
24
+ if (!externalSourceForDebug) {
25
+ (0, _sanitize.sanitize)(cooked);
26
+ }
27
+ }
19
28
  const rootEnv = new _ExecutionContext.DeclarativeEnvironment(null);
20
29
  const rootContext = new _ExecutionContext.ExecutionContext();
21
30
  rootContext.VariableEnvironment = rootEnv;
@@ -46,23 +55,28 @@ function cook(rootAst, codeSource, {
46
55
  TemplateMap.set(templateLiteral, template);
47
56
  return template;
48
57
  }
49
- function Evaluate(node, optionalChainRef) {
50
- var _hooks$beforeEvaluate, _hooks$beforeBranch, _hooks$beforeBranch2;
58
+ let currentNode;
59
+ function* Evaluate(node, optionalChainRef, forceYield) {
60
+ var _hooks$beforeEvaluate, _hooks$beforeBranch2;
51
61
  (_hooks$beforeEvaluate = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate === void 0 || _hooks$beforeEvaluate.call(hooks, node);
62
+ currentNode = node;
63
+ if (debug && (forceYield || node.type.endsWith("Statement") && node.type !== "TryStatement" && node.type !== "BlockStatement" && node.type !== "ForStatement" && node.type !== "ForInStatement" && node.type !== "ForOfStatement")) {
64
+ yield;
65
+ }
52
66
  // Expressions:
53
67
  switch (node.type) {
54
68
  case "ArrayExpression":
55
69
  {
56
70
  // https://tc39.es/ecma262/#sec-array-initializer
57
- const array = [];
71
+ const array = new ArrayConstructor();
58
72
  for (const element of node.elements) {
59
73
  if (!element) {
60
74
  array.length += 1;
61
75
  } else if (element.type === "SpreadElement") {
62
- const spreadValues = (0, _contextFree.GetValue)(Evaluate(element.argument));
76
+ const spreadValues = (0, _contextFree.GetValue)(yield* Evaluate(element.argument));
63
77
  array.push(...spreadValues);
64
78
  } else {
65
- array.push((0, _contextFree.GetValue)(Evaluate(element)));
79
+ array.push((0, _contextFree.GetValue)(yield* Evaluate(element)));
66
80
  }
67
81
  }
68
82
  return (0, _ExecutionContext.NormalCompletion)(array);
@@ -76,9 +90,9 @@ function cook(rootAst, codeSource, {
76
90
  }
77
91
  case "BinaryExpression":
78
92
  {
79
- const leftRef = Evaluate(node.left);
93
+ const leftRef = yield* Evaluate(node.left);
80
94
  const leftValue = (0, _contextFree.GetValue)(leftRef);
81
- const rightRef = Evaluate(node.right).Value;
95
+ const rightRef = yield* Evaluate(node.right);
82
96
  const rightValue = (0, _contextFree.GetValue)(rightRef);
83
97
  if (expressionOnly && node.operator === "|>") {
84
98
  // Minimal pipeline operator is supported only in expression-only mode.
@@ -103,21 +117,22 @@ function cook(rootAst, codeSource, {
103
117
  case "CallExpression":
104
118
  {
105
119
  // https://tc39.es/ecma262/#sec-function-calls
106
- const ref = Evaluate(node.callee, optionalChainRef).Value;
120
+ const ref = (yield* Evaluate(node.callee, optionalChainRef)).Value;
107
121
  const func = (0, _contextFree.GetValue)(ref);
108
122
  if ((func === undefined || func === null) && (node.optional || optionalChainRef !== null && optionalChainRef !== void 0 && optionalChainRef.skipped)) {
109
123
  optionalChainRef.skipped = true;
110
124
  return (0, _ExecutionContext.NormalCompletion)(undefined);
111
125
  }
112
- (0, _sanitize.sanitize)(func);
113
- return EvaluateCall(func, ref, node.arguments, node.callee);
126
+ doSanitize(func);
127
+ if (debug) yield;
128
+ return yield* EvaluateCall(func, ref, node.arguments, node.callee);
114
129
  }
115
130
  case "ChainExpression":
116
131
  // https://tc39.es/ecma262/#sec-optional-chains
117
- return Evaluate(node.expression, {});
132
+ return yield* Evaluate(node.expression, {});
118
133
  case "ConditionalExpression":
119
134
  // 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)));
135
+ return (0, _ExecutionContext.NormalCompletion)((0, _contextFree.GetValue)(yield* Evaluate((0, _contextFree.GetValue)(yield* Evaluate(node.test)) ? node.consequent : node.alternate)));
121
136
  case "Identifier":
122
137
  // https://tc39.es/ecma262/#sec-identifiers
123
138
  return (0, _ExecutionContext.NormalCompletion)(ResolveBinding(node.name));
@@ -139,14 +154,14 @@ function cook(rootAst, codeSource, {
139
154
  case "LogicalExpression":
140
155
  {
141
156
  // https://tc39.es/ecma262/#sec-binary-logical-operators
142
- const leftValue = (0, _contextFree.GetValue)(Evaluate(node.left));
157
+ const leftValue = (0, _contextFree.GetValue)(yield* Evaluate(node.left));
143
158
  switch (node.operator) {
144
159
  case "&&":
145
- return (0, _ExecutionContext.NormalCompletion)(leftValue && (0, _contextFree.GetValue)(Evaluate(node.right)));
160
+ return (0, _ExecutionContext.NormalCompletion)(leftValue && (0, _contextFree.GetValue)(yield* Evaluate(node.right)));
146
161
  case "||":
147
- return (0, _ExecutionContext.NormalCompletion)(leftValue || (0, _contextFree.GetValue)(Evaluate(node.right)));
162
+ return (0, _ExecutionContext.NormalCompletion)(leftValue || (0, _contextFree.GetValue)(yield* Evaluate(node.right)));
148
163
  case "??":
149
- return (0, _ExecutionContext.NormalCompletion)(leftValue ?? (0, _contextFree.GetValue)(Evaluate(node.right)));
164
+ return (0, _ExecutionContext.NormalCompletion)(leftValue ?? (0, _contextFree.GetValue)(yield* Evaluate(node.right)));
150
165
  // istanbul ignore next
151
166
  default:
152
167
  throw new SyntaxError(
@@ -158,37 +173,41 @@ function cook(rootAst, codeSource, {
158
173
  case "MemberExpression":
159
174
  {
160
175
  // https://tc39.es/ecma262/#sec-property-accessors
161
- const baseReference = Evaluate(node.object, optionalChainRef).Value;
176
+ const baseReference = (yield* Evaluate(node.object, optionalChainRef)).Value;
162
177
  const baseValue = (0, _contextFree.GetValue)(baseReference);
163
178
  if ((baseValue === undefined || baseValue === null) && (node.optional || optionalChainRef !== null && optionalChainRef !== void 0 && optionalChainRef.skipped)) {
164
179
  optionalChainRef.skipped = true;
165
180
  return (0, _ExecutionContext.NormalCompletion)(undefined);
166
181
  }
167
- (0, _sanitize.sanitize)(baseValue);
168
- const result = node.computed ? EvaluatePropertyAccessWithExpressionKey(baseValue, node.property, true) : EvaluatePropertyAccessWithIdentifierKey(baseValue, node.property, true);
169
- (0, _sanitize.sanitize)(result);
182
+ doSanitize(baseValue);
183
+ const result = node.computed ? yield* EvaluatePropertyAccessWithExpressionKey(baseValue, node.property, true) : EvaluatePropertyAccessWithIdentifierKey(baseValue, node.property, true);
184
+ doSanitize(result);
170
185
  return (0, _ExecutionContext.NormalCompletion)(result);
171
186
  }
172
187
  case "NewExpression":
173
188
  // https://tc39.es/ecma262/#sec-new-operator
174
- return EvaluateNew(node.callee, node.arguments);
189
+ return yield* EvaluateNew(node.callee, node.arguments);
175
190
  case "ObjectExpression":
176
191
  {
177
192
  // https://tc39.es/ecma262/#sec-object-initializer
178
193
  const object = {};
179
194
  for (const prop of node.properties) {
180
195
  if (prop.type === "SpreadElement") {
181
- const fromValue = (0, _contextFree.GetValue)(Evaluate(prop.argument));
196
+ const fromValue = (0, _contextFree.GetValue)(yield* Evaluate(prop.argument));
182
197
  (0, _contextFree.CopyDataProperties)(object, fromValue, new Set());
183
198
  } else {
184
199
  if (prop.kind !== "init") {
185
200
  throw new SyntaxError("Unsupported object getter/setter");
186
201
  }
187
- const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : EvaluateComputedPropertyName(prop.key);
202
+ const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : yield* EvaluateComputedPropertyName(prop.key);
188
203
  if (propName === "__proto__") {
189
204
  throw new TypeError("Setting '__proto__' property is not allowed");
190
205
  }
191
- object[propName] = (0, _contextFree.GetValue)(Evaluate(prop.value));
206
+ const propValue = (0, _contextFree.GetValue)(yield* Evaluate(prop.value));
207
+ if (prop.method && typeof propValue === "function") {
208
+ SetFunctionName(propValue, propName);
209
+ }
210
+ object[propName] = propValue;
192
211
  }
193
212
  }
194
213
  return (0, _ExecutionContext.NormalCompletion)(object);
@@ -198,7 +217,7 @@ function cook(rootAst, codeSource, {
198
217
  // https://tc39.es/ecma262/#sec-comma-operator
199
218
  let result;
200
219
  for (const expr of node.expressions) {
201
- result = (0, _ExecutionContext.NormalCompletion)((0, _contextFree.GetValue)(Evaluate(expr)));
220
+ result = (0, _ExecutionContext.NormalCompletion)((0, _contextFree.GetValue)(yield* Evaluate(expr)));
202
221
  }
203
222
  return result;
204
223
  }
@@ -208,7 +227,7 @@ function cook(rootAst, codeSource, {
208
227
  const chunks = [node.quasis[0].value.cooked];
209
228
  let index = 0;
210
229
  for (const expr of node.expressions) {
211
- const val = (0, _contextFree.GetValue)(Evaluate(expr));
230
+ const val = (0, _contextFree.GetValue)(yield* Evaluate(expr));
212
231
  chunks.push(String(val));
213
232
  chunks.push(node.quasis[index += 1].value.cooked);
214
233
  }
@@ -217,15 +236,16 @@ function cook(rootAst, codeSource, {
217
236
  case "TaggedTemplateExpression":
218
237
  {
219
238
  // https://tc39.es/ecma262/#sec-tagged-templates
220
- const tagRef = Evaluate(node.tag).Value;
239
+ const tagRef = (yield* Evaluate(node.tag)).Value;
221
240
  const tagFunc = (0, _contextFree.GetValue)(tagRef);
222
- (0, _sanitize.sanitize)(tagFunc);
223
- return EvaluateCall(tagFunc, tagRef, node.quasi, node.tag);
241
+ doSanitize(tagFunc);
242
+ if (debug) yield;
243
+ return yield* EvaluateCall(tagFunc, tagRef, node.quasi, node.tag);
224
244
  }
225
245
  case "UnaryExpression":
226
246
  {
227
247
  // https://tc39.es/ecma262/#sec-unary-operators
228
- const ref = Evaluate(node.argument).Value;
248
+ const ref = (yield* Evaluate(node.argument)).Value;
229
249
  if (!expressionOnly && node.operator === "delete") {
230
250
  // Delete operator is supported only in function mode.
231
251
  if (!(ref instanceof _ExecutionContext.ReferenceRecord)) {
@@ -255,22 +275,26 @@ function cook(rootAst, codeSource, {
255
275
  // https://tc39.es/ecma262/#sec-assignment-operators
256
276
  if (node.operator === "=") {
257
277
  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);
278
+ const lref = (yield* Evaluate(node.left)).Value;
279
+ let rval;
280
+ if (IsAnonymousFunctionDefinition(node.right) && node.left.type === "Identifier") {
281
+ rval = NamedEvaluation(node.right, node.left.name);
282
+ } else {
283
+ const rref = yield* Evaluate(node.right);
284
+ rval = (0, _contextFree.GetValue)(rref);
285
+ }
262
286
  (0, _contextFree.PutValue)(lref, rval);
263
287
  return (0, _ExecutionContext.NormalCompletion)(rval);
264
288
  }
265
- const rref = Evaluate(node.right);
289
+ const rref = yield* Evaluate(node.right);
266
290
  const rval = (0, _contextFree.GetValue)(rref);
267
- DestructuringAssignmentEvaluation(node.left, rval);
291
+ yield* DestructuringAssignmentEvaluation(node.left, rval);
268
292
  return (0, _ExecutionContext.NormalCompletion)(rval);
269
293
  }
270
294
  // Operators other than `=`.
271
- const lref = Evaluate(node.left).Value;
295
+ const lref = (yield* Evaluate(node.left)).Value;
272
296
  const lval = (0, _contextFree.GetValue)(lref);
273
- const rref = Evaluate(node.right);
297
+ const rref = yield* Evaluate(node.right);
274
298
  const rval = (0, _contextFree.GetValue)(rref);
275
299
  const r = (0, _contextFree.ApplyStringOrNumericAssignment)(lval, node.operator, rval);
276
300
  (0, _contextFree.PutValue)(lref, r);
@@ -286,7 +310,7 @@ function cook(rootAst, codeSource, {
286
310
  const blockEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
287
311
  BlockDeclarationInstantiation(node.body, blockEnv);
288
312
  getRunningContext().LexicalEnvironment = blockEnv;
289
- const blockValue = EvaluateStatementList(node.body);
313
+ const blockValue = yield* EvaluateStatementList(node.body);
290
314
  getRunningContext().LexicalEnvironment = oldEnv;
291
315
  return blockValue;
292
316
  }
@@ -301,18 +325,18 @@ function cook(rootAst, codeSource, {
301
325
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
302
326
  case "DoWhileStatement":
303
327
  // https://tc39.es/ecma262/#sec-do-while-statement
304
- return EvaluateBreakableStatement(DoWhileLoopEvaluation(node));
328
+ return EvaluateBreakableStatement(yield* DoWhileLoopEvaluation(node));
305
329
  case "ExpressionStatement":
306
330
  case "TSAsExpression":
307
331
  // https://tc39.es/ecma262/#sec-expression-statement
308
- return Evaluate(node.expression);
332
+ return yield* Evaluate(node.expression);
309
333
  case "ForInStatement":
310
334
  case "ForOfStatement":
311
335
  // https://tc39.es/ecma262/#sec-for-in-and-for-of-statements
312
- return EvaluateBreakableStatement(ForInOfLoopEvaluation(node));
336
+ return EvaluateBreakableStatement(yield* ForInOfLoopEvaluation(node));
313
337
  case "ForStatement":
314
338
  // https://tc39.es/ecma262/#sec-for-statement
315
- return EvaluateBreakableStatement(ForLoopEvaluation(node));
339
+ return EvaluateBreakableStatement(yield* ForLoopEvaluation(node));
316
340
  case "FunctionDeclaration":
317
341
  // https://tc39.es/ecma262/#sec-function-definitions
318
342
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
@@ -322,41 +346,58 @@ function cook(rootAst, codeSource, {
322
346
  return (0, _ExecutionContext.NormalCompletion)(InstantiateOrdinaryFunctionExpression(node));
323
347
  case "IfStatement":
324
348
  // 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);
349
+ if ((0, _contextFree.GetValue)(yield* Evaluate(node.test))) {
350
+ var _hooks$beforeBranch;
351
+ (_hooks$beforeBranch = hooks.beforeBranch) === null || _hooks$beforeBranch === void 0 || _hooks$beforeBranch.call(hooks, node, "if");
352
+ return (0, _contextFree.UpdateEmpty)(yield* Evaluate(node.consequent), undefined);
353
+ }
354
+ (_hooks$beforeBranch2 = hooks.beforeBranch) === null || _hooks$beforeBranch2 === void 0 || _hooks$beforeBranch2.call(hooks, node, "else");
355
+ if (node.alternate) {
356
+ return (0, _contextFree.UpdateEmpty)(yield* Evaluate(node.alternate), undefined);
357
+ }
358
+ return (0, _ExecutionContext.NormalCompletion)(undefined);
326
359
  case "ReturnStatement":
327
360
  {
328
361
  // https://tc39.es/ecma262/#sec-return-statement
329
362
  let v;
330
363
  if (node.argument) {
331
- const exprRef = Evaluate(node.argument);
364
+ const exprRef = yield* Evaluate(node.argument);
332
365
  v = (0, _contextFree.GetValue)(exprRef);
333
366
  }
334
367
  return new _ExecutionContext.CompletionRecord("return", v);
335
368
  }
369
+ case "ThisExpression":
370
+ {
371
+ if (!externalSourceForDebug) {
372
+ break;
373
+ }
374
+ const envRec = GetThisEnvironment();
375
+ return (0, _ExecutionContext.NormalCompletion)(envRec.GetThisBinding());
376
+ }
336
377
  case "ThrowStatement":
337
378
  // https://tc39.es/ecma262/#sec-throw-statement
338
- throw (0, _contextFree.GetValue)(Evaluate(node.argument));
379
+ throw (0, _contextFree.GetValue)(yield* Evaluate(node.argument));
339
380
  case "UpdateExpression":
340
381
  {
341
382
  // https://tc39.es/ecma262/#sec-update-expressions
342
- const lhs = Evaluate(node.argument).Value;
383
+ const lhs = (yield* Evaluate(node.argument)).Value;
343
384
  const oldValue = Number((0, _contextFree.GetValue)(lhs));
344
385
  const newValue = node.operator === "++" ? oldValue + 1 : oldValue - 1;
345
386
  (0, _contextFree.PutValue)(lhs, newValue);
346
387
  return (0, _ExecutionContext.NormalCompletion)(node.prefix ? newValue : oldValue);
347
388
  }
348
389
  case "SwitchCase":
349
- return EvaluateStatementList(node.consequent);
390
+ return yield* EvaluateStatementList(node.consequent);
350
391
  case "SwitchStatement":
351
392
  {
352
393
  // https://tc39.es/ecma262/#sec-switch-statement
353
- const exprRef = Evaluate(node.discriminant);
394
+ const exprRef = yield* Evaluate(node.discriminant);
354
395
  const switchValue = (0, _contextFree.GetValue)(exprRef);
355
396
  const oldEnv = getRunningContext().LexicalEnvironment;
356
397
  const blockEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
357
398
  BlockDeclarationInstantiation(node.cases, blockEnv);
358
399
  getRunningContext().LexicalEnvironment = blockEnv;
359
- const R = CaseBlockEvaluation(node.cases, switchValue);
400
+ const R = yield* CaseBlockEvaluation(node.cases, switchValue);
360
401
  getRunningContext().LexicalEnvironment = oldEnv;
361
402
  return EvaluateBreakableStatement(R);
362
403
  }
@@ -365,18 +406,19 @@ function cook(rootAst, codeSource, {
365
406
  // https://tc39.es/ecma262/#sec-try-statement
366
407
  let R;
367
408
  try {
368
- R = Evaluate(node.block);
409
+ R = yield* Evaluate(node.block);
369
410
  } catch (error) {
370
411
  if (node.handler) {
371
412
  var _hooks$beforeEvaluate2;
413
+ currentNode = node.handler;
372
414
  (_hooks$beforeEvaluate2 = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate2 === void 0 || _hooks$beforeEvaluate2.call(hooks, node.handler);
373
- R = CatchClauseEvaluation(node.handler, error);
415
+ R = yield* CatchClauseEvaluation(node.handler, error);
374
416
  } else {
375
417
  throw error;
376
418
  }
377
419
  } finally {
378
420
  if (node.finalizer) {
379
- const F = Evaluate(node.finalizer);
421
+ const F = yield* Evaluate(node.finalizer);
380
422
  if (F.Type !== "normal") {
381
423
  R = F;
382
424
  }
@@ -389,6 +431,7 @@ function cook(rootAst, codeSource, {
389
431
  // https://tc39.es/ecma262/#sec-declarations-and-the-variable-statement
390
432
  let result;
391
433
  for (const declarator of node.declarations) {
434
+ currentNode = declarator;
392
435
  if (!declarator.init) {
393
436
  // Assert: a declarator without init is always an identifier.
394
437
  if (node.kind === "var") {
@@ -398,23 +441,31 @@ function cook(rootAst, codeSource, {
398
441
  result = (0, _contextFree.InitializeReferencedBinding)(lhs, undefined);
399
442
  }
400
443
  } else if (declarator.id.type === "Identifier") {
444
+ currentNode = declarator.init;
445
+ if (debug) yield;
401
446
  const bindingId = declarator.id.name;
402
447
  const lhs = ResolveBinding(bindingId);
403
- // Todo: IsAnonymousFunctionDefinition(Initializer)
404
- const rhs = Evaluate(declarator.init);
405
- const value = (0, _contextFree.GetValue)(rhs);
448
+ let value;
449
+ if (IsAnonymousFunctionDefinition(declarator.init)) {
450
+ value = NamedEvaluation(declarator.init, bindingId);
451
+ } else {
452
+ const rhs = yield* Evaluate(declarator.init);
453
+ value = (0, _contextFree.GetValue)(rhs);
454
+ }
406
455
  result = node.kind === "var" ? (0, _contextFree.PutValue)(lhs, value) : (0, _contextFree.InitializeReferencedBinding)(lhs, value);
407
456
  } else {
408
- const rhs = Evaluate(declarator.init);
457
+ currentNode = declarator.init;
458
+ if (debug) yield;
459
+ const rhs = yield* Evaluate(declarator.init);
409
460
  const rval = (0, _contextFree.GetValue)(rhs);
410
- result = BindingInitialization(declarator.id, rval, node.kind === "var" ? undefined : getRunningContext().LexicalEnvironment);
461
+ result = yield* BindingInitialization(declarator.id, rval, node.kind === "var" ? undefined : getRunningContext().LexicalEnvironment);
411
462
  }
412
463
  }
413
464
  return result;
414
465
  }
415
466
  case "WhileStatement":
416
467
  // https://tc39.es/ecma262/#sec-while-statement
417
- return EvaluateBreakableStatement(WhileLoopEvaluation(node));
468
+ return EvaluateBreakableStatement(yield* WhileLoopEvaluation(node));
418
469
  }
419
470
  }
420
471
  // eslint-disable-next-line no-console
@@ -436,9 +487,9 @@ function cook(rootAst, codeSource, {
436
487
 
437
488
  // Try statements.
438
489
  // https://tc39.es/ecma262/#sec-runtime-semantics-catchclauseevaluation
439
- function CatchClauseEvaluation(node, thrownValue) {
490
+ function* CatchClauseEvaluation(node, thrownValue) {
440
491
  if (!node.param) {
441
- return Evaluate(node.body);
492
+ return yield* Evaluate(node.body);
442
493
  }
443
494
  const oldEnv = getRunningContext().LexicalEnvironment;
444
495
  const catchEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
@@ -446,8 +497,8 @@ function cook(rootAst, codeSource, {
446
497
  catchEnv.CreateMutableBinding(argName, false);
447
498
  }
448
499
  getRunningContext().LexicalEnvironment = catchEnv;
449
- BindingInitialization(node.param, thrownValue, catchEnv);
450
- const B = Evaluate(node.body);
500
+ yield* BindingInitialization(node.param, thrownValue, catchEnv);
501
+ const B = yield* Evaluate(node.body);
451
502
  getRunningContext().LexicalEnvironment = oldEnv;
452
503
  return B;
453
504
  }
@@ -460,7 +511,7 @@ function cook(rootAst, codeSource, {
460
511
 
461
512
  // Switch statements.
462
513
  // https://tc39.es/ecma262/#sec-runtime-semantics-caseblockevaluation
463
- function CaseBlockEvaluation(cases, input) {
514
+ function* CaseBlockEvaluation(cases, input) {
464
515
  let V;
465
516
  const defaultCaseIndex = cases.findIndex(switchCase => !switchCase.test);
466
517
  const hasDefaultCase = defaultCaseIndex >= 0;
@@ -468,10 +519,10 @@ function cook(rootAst, codeSource, {
468
519
  let found = false;
469
520
  for (const C of A) {
470
521
  if (!found) {
471
- found = CaseClauseIsSelected(C, input);
522
+ found = yield* CaseClauseIsSelected(C, input);
472
523
  }
473
524
  if (found) {
474
- const R = Evaluate(C);
525
+ const R = yield* Evaluate(C);
475
526
  if (R.Value !== _ExecutionContext.Empty) {
476
527
  V = R.Value;
477
528
  }
@@ -488,10 +539,10 @@ function cook(rootAst, codeSource, {
488
539
  if (!found) {
489
540
  for (const C of B) {
490
541
  if (!foundInB) {
491
- foundInB = CaseClauseIsSelected(C, input);
542
+ foundInB = yield* CaseClauseIsSelected(C, input);
492
543
  }
493
544
  if (foundInB) {
494
- const R = Evaluate(C);
545
+ const R = yield* Evaluate(C);
495
546
  if (R.Value !== _ExecutionContext.Empty) {
496
547
  V = R.Value;
497
548
  }
@@ -504,7 +555,7 @@ function cook(rootAst, codeSource, {
504
555
  if (foundInB) {
505
556
  return (0, _ExecutionContext.NormalCompletion)(V);
506
557
  }
507
- const R = Evaluate(cases[defaultCaseIndex]);
558
+ const R = yield* Evaluate(cases[defaultCaseIndex]);
508
559
  if (R.Value !== _ExecutionContext.Empty) {
509
560
  V = R.Value;
510
561
  }
@@ -514,7 +565,7 @@ function cook(rootAst, codeSource, {
514
565
 
515
566
  // NOTE: The following is another complete iteration of the second CaseClauses.
516
567
  for (const C of B) {
517
- const R = Evaluate(C);
568
+ const R = yield* Evaluate(C);
518
569
  if (R.Value !== _ExecutionContext.Empty) {
519
570
  V = R.Value;
520
571
  }
@@ -526,22 +577,22 @@ function cook(rootAst, codeSource, {
526
577
  }
527
578
 
528
579
  // https://tc39.es/ecma262/#sec-runtime-semantics-caseclauseisselected
529
- function CaseClauseIsSelected(C, input) {
530
- const clauseSelector = (0, _contextFree.GetValue)(Evaluate(C.test));
580
+ function* CaseClauseIsSelected(C, input) {
581
+ const clauseSelector = (0, _contextFree.GetValue)(yield* Evaluate(C.test));
531
582
  return input === clauseSelector;
532
583
  }
533
584
 
534
585
  // While statements.
535
586
  // https://tc39.es/ecma262/#sec-runtime-semantics-whileloopevaluation
536
- function WhileLoopEvaluation(node) {
587
+ function* WhileLoopEvaluation(node) {
537
588
  let V;
538
589
  // eslint-disable-next-line no-constant-condition
539
590
  while (true) {
540
- const exprValue = (0, _contextFree.GetValue)(Evaluate(node.test));
591
+ const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test));
541
592
  if (!exprValue) {
542
593
  return (0, _ExecutionContext.NormalCompletion)(V);
543
594
  }
544
- const stmtResult = Evaluate(node.body);
595
+ const stmtResult = yield* Evaluate(node.body);
545
596
  if (!(0, _contextFree.LoopContinues)(stmtResult)) {
546
597
  return (0, _contextFree.UpdateEmpty)(stmtResult, V);
547
598
  }
@@ -553,18 +604,18 @@ function cook(rootAst, codeSource, {
553
604
 
554
605
  // Do-while Statements.
555
606
  // https://tc39.es/ecma262/#sec-runtime-semantics-dowhileloopevaluation
556
- function DoWhileLoopEvaluation(node) {
607
+ function* DoWhileLoopEvaluation(node) {
557
608
  let V;
558
609
  // eslint-disable-next-line no-constant-condition
559
610
  while (true) {
560
- const stmtResult = Evaluate(node.body);
611
+ const stmtResult = yield* Evaluate(node.body);
561
612
  if (!(0, _contextFree.LoopContinues)(stmtResult)) {
562
613
  return (0, _contextFree.UpdateEmpty)(stmtResult, V);
563
614
  }
564
615
  if (stmtResult.Value !== _ExecutionContext.Empty) {
565
616
  V = stmtResult.Value;
566
617
  }
567
- const exprValue = (0, _contextFree.GetValue)(Evaluate(node.test));
618
+ const exprValue = (0, _contextFree.GetValue)(yield* Evaluate(node.test));
568
619
  if (!exprValue) {
569
620
  return (0, _ExecutionContext.NormalCompletion)(V);
570
621
  }
@@ -573,22 +624,22 @@ function cook(rootAst, codeSource, {
573
624
 
574
625
  // For in/of statements.
575
626
  // https://tc39.es/ecma262/#sec-runtime-semantics-forinofloopevaluation
576
- function ForInOfLoopEvaluation(node) {
627
+ function* ForInOfLoopEvaluation(node) {
577
628
  const lhs = node.left;
578
629
  const isVariableDeclaration = lhs.type === "VariableDeclaration";
579
630
  const lhsKind = isVariableDeclaration ? lhs.kind === "var" ? "varBinding" : "lexicalBinding" : "assignment";
580
631
  const uninitializedBoundNames = lhsKind === "lexicalBinding" ? (0, _traverse.collectBoundNames)(lhs) : [];
581
632
  const iterationKind = node.type === "ForInStatement" ? "enumerate" : "iterate";
582
- const keyResult = ForInOfHeadEvaluation(uninitializedBoundNames, node.right, iterationKind);
633
+ const keyResult = yield* ForInOfHeadEvaluation(uninitializedBoundNames, node.right, iterationKind);
583
634
  if (keyResult.Type !== "normal") {
584
635
  // When enumerate, if the target is nil, a break completion will be returned.
585
636
  return keyResult;
586
637
  }
587
- return ForInOfBodyEvaluation(lhs, node.body, keyResult.Value, iterationKind, lhsKind);
638
+ return yield* ForInOfBodyEvaluation(lhs, node.body, keyResult.Value, iterationKind, lhsKind);
588
639
  }
589
640
 
590
641
  // https://tc39.es/ecma262/#sec-runtime-semantics-forinofheadevaluation
591
- function ForInOfHeadEvaluation(uninitializedBoundNames, expr, iterationKind) {
642
+ function* ForInOfHeadEvaluation(uninitializedBoundNames, expr, iterationKind) {
592
643
  const runningContext = getRunningContext();
593
644
  const oldEnv = runningContext.LexicalEnvironment;
594
645
  if (uninitializedBoundNames.length > 0) {
@@ -598,7 +649,7 @@ function cook(rootAst, codeSource, {
598
649
  }
599
650
  runningContext.LexicalEnvironment = newEnv;
600
651
  }
601
- const exprRef = Evaluate(expr);
652
+ const exprRef = yield* Evaluate(expr, undefined, true);
602
653
  runningContext.LexicalEnvironment = oldEnv;
603
654
  const exprValue = (0, _contextFree.GetValue)(exprRef);
604
655
  if (iterationKind === "enumerate") {
@@ -611,7 +662,7 @@ function cook(rootAst, codeSource, {
611
662
  const iterator = (0, _contextFree.CreateListIteratorRecord)(exprValue);
612
663
  return (0, _ExecutionContext.NormalCompletion)(iterator);
613
664
  }
614
- function ForInOfBodyEvaluation(node, stmt, iteratorRecord, iterationKind, lhsKind) {
665
+ function* ForInOfBodyEvaluation(node, stmt, iteratorRecord, iterationKind, lhsKind) {
615
666
  const lhs = lhsKind === "assignment" ? node : node.declarations[0].id;
616
667
  const oldEnv = getRunningContext().LexicalEnvironment;
617
668
  let V;
@@ -623,11 +674,13 @@ function cook(rootAst, codeSource, {
623
674
  const destructuring = lhs.type === "ObjectPattern" || lhs.type === "ArrayPattern";
624
675
  // eslint-disable-next-line no-constant-condition
625
676
  while (true) {
677
+ currentNode = lhs;
626
678
  const {
627
679
  done,
628
680
  value: nextValue
629
681
  } = iteratorRecord.next();
630
682
  if (done) {
683
+ if (debug) yield;
631
684
  return (0, _ExecutionContext.NormalCompletion)(V);
632
685
  }
633
686
  let lhsRef;
@@ -636,15 +689,19 @@ function cook(rootAst, codeSource, {
636
689
  iterationEnv = new _ExecutionContext.DeclarativeEnvironment(oldEnv);
637
690
  (0, _contextFree.ForDeclarationBindingInstantiation)(node, iterationEnv);
638
691
  getRunningContext().LexicalEnvironment = iterationEnv;
692
+ if (debug) yield;
639
693
  if (!destructuring) {
640
694
  const [lhsName] = (0, _traverse.collectBoundNames)(lhs);
641
695
  lhsRef = ResolveBinding(lhsName);
642
696
  }
643
- } else if (!destructuring) {
644
- lhsRef = Evaluate(lhs).Value;
697
+ } else {
698
+ if (debug) yield;
699
+ if (!destructuring) {
700
+ lhsRef = (yield* Evaluate(lhs)).Value;
701
+ }
645
702
  }
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);
703
+ 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);
704
+ const result = yield* Evaluate(stmt);
648
705
  getRunningContext().LexicalEnvironment = oldEnv;
649
706
  if (!(0, _contextFree.LoopContinues)(result)) {
650
707
  const status = (0, _contextFree.UpdateEmpty)(result, V);
@@ -673,13 +730,13 @@ function cook(rootAst, codeSource, {
673
730
 
674
731
  // For statements.
675
732
  // https://tc39.es/ecma262/#sec-runtime-semantics-forloopevaluation
676
- function ForLoopEvaluation(node) {
733
+ function* ForLoopEvaluation(node) {
677
734
  var _node$init;
678
735
  if (((_node$init = node.init) === null || _node$init === void 0 ? void 0 : _node$init.type) === "VariableDeclaration") {
679
736
  // `for (var … ; … ; … ) …`
680
737
  if (node.init.kind === "var") {
681
- Evaluate(node.init);
682
- return ForBodyEvaluation(node.test, node.update, node.body, []);
738
+ yield* Evaluate(node.init);
739
+ return yield* ForBodyEvaluation(node.test, node.update, node.body, []);
683
740
  }
684
741
  // `for (let/const … ; … ; … ) …`
685
742
  const oldEnv = getRunningContext().LexicalEnvironment;
@@ -694,34 +751,34 @@ function cook(rootAst, codeSource, {
694
751
  }
695
752
  }
696
753
  getRunningContext().LexicalEnvironment = loopEnv;
697
- Evaluate(node.init);
754
+ yield* Evaluate(node.init);
698
755
  const perIterationLets = isConst ? [] : Array.from(boundNames);
699
- const bodyResult = ForBodyEvaluation(node.test, node.update, node.body, perIterationLets);
756
+ const bodyResult = yield* ForBodyEvaluation(node.test, node.update, node.body, perIterationLets);
700
757
  getRunningContext().LexicalEnvironment = oldEnv;
701
758
  return bodyResult;
702
759
  }
703
760
  // `for ( … ; … ; … ) …`
704
761
  if (node.init) {
705
- const exprRef = Evaluate(node.init);
762
+ const exprRef = yield* Evaluate(node.init);
706
763
  (0, _contextFree.GetValue)(exprRef);
707
764
  }
708
- return ForBodyEvaluation(node.test, node.update, node.body, []);
765
+ return yield* ForBodyEvaluation(node.test, node.update, node.body, []);
709
766
  }
710
767
 
711
768
  // https://tc39.es/ecma262/#sec-forbodyevaluation
712
- function ForBodyEvaluation(test, increment, stmt, perIterationBindings) {
769
+ function* ForBodyEvaluation(test, increment, stmt, perIterationBindings) {
713
770
  CreatePerIterationEnvironment(perIterationBindings);
714
771
  let V;
715
772
  // eslint-disable-next-line no-constant-condition
716
773
  while (true) {
717
774
  if (test) {
718
- const testRef = Evaluate(test);
775
+ const testRef = yield* Evaluate(test, undefined, true);
719
776
  const testValue = (0, _contextFree.GetValue)(testRef);
720
777
  if (!testValue) {
721
778
  return (0, _ExecutionContext.NormalCompletion)(V);
722
779
  }
723
780
  }
724
- const result = Evaluate(stmt);
781
+ const result = yield* Evaluate(stmt);
725
782
  if (!(0, _contextFree.LoopContinues)(result)) {
726
783
  return (0, _contextFree.UpdateEmpty)(result, V);
727
784
  }
@@ -730,7 +787,7 @@ function cook(rootAst, codeSource, {
730
787
  }
731
788
  CreatePerIterationEnvironment(perIterationBindings);
732
789
  if (increment) {
733
- const incRef = Evaluate(increment);
790
+ const incRef = yield* Evaluate(increment, undefined, true);
734
791
  (0, _contextFree.GetValue)(incRef);
735
792
  }
736
793
  }
@@ -754,77 +811,80 @@ function cook(rootAst, codeSource, {
754
811
 
755
812
  // Destructuring assignments.
756
813
  // https://tc39.es/ecma262/#sec-runtime-semantics-destructuringassignmentevaluation
757
- function DestructuringAssignmentEvaluation(pattern, value) {
814
+ function* DestructuringAssignmentEvaluation(pattern, value) {
758
815
  if (pattern.type === "ObjectPattern") {
759
816
  (0, _contextFree.RequireObjectCoercible)(value);
760
817
  if (pattern.properties.length > 0) {
761
- PropertyDestructuringAssignmentEvaluation(pattern.properties, value);
818
+ yield* PropertyDestructuringAssignmentEvaluation(pattern.properties, value);
762
819
  }
763
820
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
764
821
  }
765
822
  const iteratorRecord = (0, _contextFree.CreateListIteratorRecord)(value);
766
- return IteratorDestructuringAssignmentEvaluation(pattern.elements, iteratorRecord);
823
+ return yield* IteratorDestructuringAssignmentEvaluation(pattern.elements, iteratorRecord);
767
824
  }
768
825
 
769
826
  // https://tc39.es/ecma262/#sec-runtime-semantics-propertydestructuringassignmentevaluation
770
- function PropertyDestructuringAssignmentEvaluation(properties, value) {
827
+ function* PropertyDestructuringAssignmentEvaluation(properties, value) {
771
828
  const excludedNames = new Set();
772
829
  for (const prop of properties) {
773
830
  if (prop.type === "Property") {
774
- const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : EvaluateComputedPropertyName(prop.key);
831
+ const propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : yield* EvaluateComputedPropertyName(prop.key);
775
832
  const valueTarget = prop.value.type === "AssignmentPattern" ? prop.value.left : prop.value;
776
833
  if (valueTarget.type === "Identifier") {
777
834
  const lref = ResolveBinding(valueTarget.name);
778
835
  let v = (0, _contextFree.GetV)(value, propName);
779
836
  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);
837
+ if (IsAnonymousFunctionDefinition(prop.value.right)) {
838
+ v = NamedEvaluation(prop.value.right, valueTarget.name);
839
+ } else {
840
+ const defaultValue = yield* Evaluate(prop.value.right);
841
+ v = (0, _contextFree.GetValue)(defaultValue);
842
+ }
783
843
  }
784
844
  (0, _contextFree.PutValue)(lref, v);
785
845
  excludedNames.add(propName);
786
846
  } else {
787
- KeyedDestructuringAssignmentEvaluation(prop.value, value, propName);
847
+ yield* KeyedDestructuringAssignmentEvaluation(prop.value, value, propName);
788
848
  excludedNames.add(propName);
789
849
  }
790
850
  } else {
791
- RestDestructuringAssignmentEvaluation(prop, value, excludedNames);
851
+ yield* RestDestructuringAssignmentEvaluation(prop, value, excludedNames);
792
852
  }
793
853
  }
794
854
  }
795
855
 
796
856
  // https://tc39.es/ecma262/#sec-runtime-semantics-keyeddestructuringassignmentevaluation
797
- function KeyedDestructuringAssignmentEvaluation(node, value, propertyName) {
857
+ function* KeyedDestructuringAssignmentEvaluation(node, value, propertyName) {
798
858
  const assignmentTarget = node.type === "AssignmentPattern" ? node.left : node;
799
859
  const isObjectOrArray = assignmentTarget.type === "ArrayPattern" || assignmentTarget.type === "ObjectPattern";
800
860
  let lref;
801
861
  if (!isObjectOrArray) {
802
- lref = Evaluate(assignmentTarget).Value;
862
+ lref = (yield* Evaluate(assignmentTarget)).Value;
803
863
  }
804
864
  const v = (0, _contextFree.GetV)(value, propertyName);
805
865
  let rhsValue;
806
866
  if (node.type === "AssignmentPattern" && v === undefined) {
807
- // Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
808
- const defaultValue = Evaluate(node.right);
867
+ // `assignmentTarget.type` is never "Identifier" here.
868
+ const defaultValue = yield* Evaluate(node.right);
809
869
  rhsValue = (0, _contextFree.GetValue)(defaultValue);
810
870
  } else {
811
871
  rhsValue = v;
812
872
  }
813
873
  if (isObjectOrArray) {
814
- return DestructuringAssignmentEvaluation(assignmentTarget, rhsValue);
874
+ return yield* DestructuringAssignmentEvaluation(assignmentTarget, rhsValue);
815
875
  }
816
876
  return (0, _contextFree.PutValue)(lref, rhsValue);
817
877
  }
818
878
 
819
879
  // https://tc39.es/ecma262/#sec-runtime-semantics-restdestructuringassignmentevaluation
820
- function RestDestructuringAssignmentEvaluation(restProperty, value, excludedNames) {
821
- const lref = Evaluate(restProperty.argument).Value;
880
+ function* RestDestructuringAssignmentEvaluation(restProperty, value, excludedNames) {
881
+ const lref = (yield* Evaluate(restProperty.argument)).Value;
822
882
  const restObj = (0, _contextFree.CopyDataProperties)({}, value, excludedNames);
823
883
  return (0, _contextFree.PutValue)(lref, restObj);
824
884
  }
825
885
 
826
886
  // https://tc39.es/ecma262/#sec-runtime-semantics-iteratordestructuringassignmentevaluation
827
- function IteratorDestructuringAssignmentEvaluation(elements, iteratorRecord) {
887
+ function* IteratorDestructuringAssignmentEvaluation(elements, iteratorRecord) {
828
888
  let status = (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
829
889
  for (const element of elements) {
830
890
  if (!element) {
@@ -836,7 +896,7 @@ function cook(rootAst, codeSource, {
836
896
  const isObjectOrArray = assignmentTarget.type === "ArrayPattern" || assignmentTarget.type === "ObjectPattern";
837
897
  let lref;
838
898
  if (!isObjectOrArray) {
839
- lref = Evaluate(assignmentTarget).Value;
899
+ lref = (yield* Evaluate(assignmentTarget)).Value;
840
900
  }
841
901
  let v;
842
902
  if (element.type !== "RestElement") {
@@ -846,15 +906,18 @@ function cook(rootAst, codeSource, {
846
906
  } = iteratorRecord.next();
847
907
  const value = done ? undefined : nextValue;
848
908
  if (element.type === "AssignmentPattern" && value === undefined) {
849
- // Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
850
- const defaultValue = Evaluate(element.right);
851
- v = (0, _contextFree.GetValue)(defaultValue);
909
+ if (IsAnonymousFunctionDefinition(element.right) && assignmentTarget.type === "Identifier") {
910
+ v = NamedEvaluation(element.right, assignmentTarget.name);
911
+ } else {
912
+ const defaultValue = yield* Evaluate(element.right);
913
+ v = (0, _contextFree.GetValue)(defaultValue);
914
+ }
852
915
  } else {
853
916
  v = value;
854
917
  }
855
918
  } else {
856
919
  // RestElement
857
- v = [];
920
+ v = new ArrayConstructor();
858
921
  let n = 0;
859
922
  // eslint-disable-next-line no-constant-condition
860
923
  while (true) {
@@ -870,7 +933,7 @@ function cook(rootAst, codeSource, {
870
933
  }
871
934
  }
872
935
  if (isObjectOrArray) {
873
- status = DestructuringAssignmentEvaluation(assignmentTarget, v);
936
+ status = yield* DestructuringAssignmentEvaluation(assignmentTarget, v);
874
937
  } else {
875
938
  status = (0, _contextFree.PutValue)(lref, v);
876
939
  }
@@ -880,8 +943,8 @@ function cook(rootAst, codeSource, {
880
943
 
881
944
  // Object expressions.
882
945
  // https://tc39.es/ecma262/#sec-evaluate-property-access-with-expression-key
883
- function EvaluatePropertyAccessWithExpressionKey(baseValue, expression, strict) {
884
- const propertyNameReference = Evaluate(expression);
946
+ function* EvaluatePropertyAccessWithExpressionKey(baseValue, expression, strict) {
947
+ const propertyNameReference = yield* Evaluate(expression);
885
948
  const propertyNameValue = (0, _contextFree.GetValue)(propertyNameReference);
886
949
  const propertyKey = (0, _contextFree.ToPropertyKey)(propertyNameValue);
887
950
  return new _ExecutionContext.ReferenceRecord(baseValue, propertyKey, strict);
@@ -919,33 +982,41 @@ function cook(rootAst, codeSource, {
919
982
 
920
983
  // Function declarations and expressions.
921
984
  // https://tc39.es/ecma262/#sec-evaluatecall
922
- function EvaluateCall(func, ref, args, callee) {
985
+ function* EvaluateCall(func, ref, args, callee) {
923
986
  let thisValue;
924
987
  if (ref instanceof _ExecutionContext.ReferenceRecord) {
925
988
  if ((0, _contextFree.IsPropertyReference)(ref)) {
926
989
  thisValue = ref.Base;
927
990
  }
928
991
  }
929
- const argList = ArgumentListEvaluation(args);
992
+ const argList = yield* ArgumentListEvaluation(args);
930
993
  if (typeof func !== "function") {
931
994
  const funcName = codeSource.substring(callee.start, callee.end);
932
995
  throw new TypeError(`${funcName} is not a function`);
933
996
  }
997
+ if (debug || externalSourceForDebug) {
998
+ const debuggerCall = func[_ExecutionContext.DebuggerCall];
999
+ if (debuggerCall) {
1000
+ const result = yield* debuggerCall.apply(thisValue, argList);
1001
+ doSanitize(result);
1002
+ return (0, _ExecutionContext.NormalCompletion)(result);
1003
+ }
1004
+ }
934
1005
  const result = func.apply(thisValue, argList);
935
- (0, _sanitize.sanitize)(result);
1006
+ doSanitize(result);
936
1007
  return (0, _ExecutionContext.NormalCompletion)(result);
937
1008
  }
938
1009
 
939
1010
  // https://tc39.es/ecma262/#sec-evaluatenew
940
- function EvaluateNew(constructExpr, args) {
941
- const ref = Evaluate(constructExpr);
1011
+ function* EvaluateNew(constructExpr, args) {
1012
+ const ref = yield* Evaluate(constructExpr);
942
1013
  const constructor = (0, _contextFree.GetValue)(ref);
943
- const argList = ArgumentListEvaluation(args);
1014
+ const argList = yield* ArgumentListEvaluation(args);
944
1015
  if (typeof constructor !== "function" || constructor[_ExecutionContext.IsConstructor] === false) {
945
1016
  const constructorName = codeSource.substring(constructExpr.start, constructExpr.end);
946
1017
  throw new TypeError(`${constructorName} is not a constructor`);
947
1018
  }
948
- if (!(0, _sanitize.isAllowedConstructor)(constructor)) {
1019
+ if (!externalSourceForDebug && !(0, _sanitize.isAllowedConstructor)(constructor) && constructor !== ArrayConstructor) {
949
1020
  const constructorName = codeSource.substring(constructExpr.start, constructExpr.end);
950
1021
  throw new TypeError(`${constructorName} is not an allowed constructor`);
951
1022
  }
@@ -953,32 +1024,43 @@ function cook(rootAst, codeSource, {
953
1024
  }
954
1025
 
955
1026
  // https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation
956
- function ArgumentListEvaluation(args) {
1027
+ function* ArgumentListEvaluation(args) {
957
1028
  const array = [];
958
1029
  if (Array.isArray(args)) {
959
1030
  for (const arg of args) {
960
1031
  if (arg.type === "SpreadElement") {
961
- const spreadValues = (0, _contextFree.GetValue)(Evaluate(arg.argument));
1032
+ const spreadValues = (0, _contextFree.GetValue)(yield* Evaluate(arg.argument));
962
1033
  array.push(...spreadValues);
963
1034
  } else {
964
- array.push((0, _contextFree.GetValue)(Evaluate(arg)));
1035
+ array.push((0, _contextFree.GetValue)(yield* Evaluate(arg)));
965
1036
  }
966
1037
  }
967
1038
  } else {
968
1039
  array.push(GetTemplateObject(args));
969
1040
  for (const expr of args.expressions) {
970
- array.push((0, _contextFree.GetValue)(Evaluate(expr)));
1041
+ array.push((0, _contextFree.GetValue)(yield* Evaluate(expr)));
971
1042
  }
972
1043
  }
973
1044
  return array;
974
1045
  }
975
1046
 
976
1047
  // https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
977
- function CallFunction(closure, args) {
1048
+ function* CallFunction(closure, thisArgument, args) {
978
1049
  var _hooks$beforeCall;
979
1050
  (_hooks$beforeCall = hooks.beforeCall) === null || _hooks$beforeCall === void 0 || _hooks$beforeCall.call(hooks, closure[_ExecutionContext.SourceNode]);
980
- PrepareForOrdinaryCall(closure);
981
- const result = OrdinaryCallEvaluateBody(closure, args);
1051
+ const calleeContext = PrepareForOrdinaryCall(closure);
1052
+ OrdinaryCallBindThis(closure, calleeContext, thisArgument);
1053
+ const result = yield* OrdinaryCallEvaluateBody(closure, args);
1054
+ if (debug) {
1055
+ currentNode = {
1056
+ ...closure[_ExecutionContext.SourceNode],
1057
+ [_ExecutionContext.DebuggerReturn]: true
1058
+ };
1059
+ yield {
1060
+ type: "return",
1061
+ value: result.Type === "return" ? result.Value : undefined
1062
+ };
1063
+ }
982
1064
  executionContextStack.pop();
983
1065
  if (result.Type === "return") {
984
1066
  return result.Value;
@@ -990,32 +1072,39 @@ function cook(rootAst, codeSource, {
990
1072
  function PrepareForOrdinaryCall(F) {
991
1073
  const calleeContext = new _ExecutionContext.ExecutionContext();
992
1074
  calleeContext.Function = F;
993
- const localEnv = new _ExecutionContext.FunctionEnvironment(F[_ExecutionContext.Environment]);
1075
+ const localEnv = new _ExecutionContext.FunctionEnvironment(F);
994
1076
  calleeContext.VariableEnvironment = localEnv;
995
1077
  calleeContext.LexicalEnvironment = localEnv;
996
1078
  executionContextStack.push(calleeContext);
997
1079
  return calleeContext;
998
1080
  }
1081
+ function OrdinaryCallBindThis(F, calleeContext, thisArgument) {
1082
+ if (F[_ExecutionContext.ThisMode] === _ExecutionContext.Mode.LEXICAL) {
1083
+ return;
1084
+ }
1085
+ const localEnv = calleeContext.LexicalEnvironment;
1086
+ localEnv === null || localEnv === void 0 || localEnv.BindThisValue(thisArgument);
1087
+ }
999
1088
 
1000
1089
  // https://tc39.es/ecma262/#sec-ordinarycallevaluatebody
1001
- function OrdinaryCallEvaluateBody(F, args) {
1002
- return EvaluateFunctionBody(F[_ExecutionContext.ECMAScriptCode], F, args);
1090
+ function* OrdinaryCallEvaluateBody(F, args) {
1091
+ return yield* EvaluateFunctionBody(F[_ExecutionContext.ECMAScriptCode], F, args);
1003
1092
  }
1004
1093
 
1005
1094
  // https://tc39.es/ecma262/#sec-runtime-semantics-evaluatefunctionbody
1006
- function EvaluateFunctionBody(body, F, args) {
1007
- FunctionDeclarationInstantiation(F, args);
1095
+ function* EvaluateFunctionBody(body, F, args) {
1096
+ yield* FunctionDeclarationInstantiation(F, args);
1008
1097
  if (Array.isArray(body)) {
1009
- return EvaluateStatementList(body);
1098
+ return yield* EvaluateStatementList(body);
1010
1099
  }
1011
- return new _ExecutionContext.CompletionRecord("return", (0, _contextFree.GetValue)(Evaluate(body)));
1100
+ return new _ExecutionContext.CompletionRecord("return", (0, _contextFree.GetValue)(yield* Evaluate(body)));
1012
1101
  }
1013
1102
 
1014
1103
  // https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
1015
- function EvaluateStatementList(statements) {
1104
+ function* EvaluateStatementList(statements) {
1016
1105
  let result = (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
1017
1106
  for (const stmt of statements) {
1018
- const s = Evaluate(stmt);
1107
+ const s = yield* Evaluate(stmt);
1019
1108
  if (s.Type !== "normal") {
1020
1109
  return s;
1021
1110
  }
@@ -1023,9 +1112,39 @@ function cook(rootAst, codeSource, {
1023
1112
  }
1024
1113
  return result;
1025
1114
  }
1115
+ function GetThisEnvironment() {
1116
+ let env = getRunningContext().LexicalEnvironment;
1117
+ while (env) {
1118
+ if (env.HasThisBinding()) {
1119
+ return env;
1120
+ }
1121
+ env = env.OuterEnv;
1122
+ }
1123
+ throw new Error("Accessing global this is forbidden");
1124
+ }
1125
+
1126
+ // https://tc39.es/ecma262/#sec-isanonymousfunctiondefinition
1127
+ function IsAnonymousFunctionDefinition(node) {
1128
+ // No ParenthesizedExpression in ESTree.
1129
+ return node.type === "FunctionExpression" && !node.id || node.type === "ArrowFunctionExpression";
1130
+ }
1131
+
1132
+ // https://tc39.es/ecma262/#sec-runtime-semantics-namedevaluation
1133
+ function NamedEvaluation(node, name) {
1134
+ // No ParenthesizedExpression in ESTree.
1135
+ switch (node.type) {
1136
+ case "FunctionExpression":
1137
+ return InstantiateOrdinaryFunctionExpression(node, name);
1138
+ case "ArrowFunctionExpression":
1139
+ return InstantiateArrowFunctionExpression(node, name);
1140
+ // istanbul ignore next: should never happen
1141
+ default:
1142
+ throw new Error(`Unexpected node type for NamedEvaluation: ${node.type}`);
1143
+ }
1144
+ }
1026
1145
 
1027
1146
  // https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
1028
- function FunctionDeclarationInstantiation(func, args) {
1147
+ function* FunctionDeclarationInstantiation(func, args) {
1029
1148
  const calleeContext = getRunningContext();
1030
1149
  const code = func[_ExecutionContext.ECMAScriptCode];
1031
1150
  const formals = func[_ExecutionContext.FormalParameters];
@@ -1054,20 +1173,44 @@ function cook(rootAst, codeSource, {
1054
1173
  throw new SyntaxError("Var declaration is not recommended, use `let` or `const` instead");
1055
1174
  }
1056
1175
  }
1176
+
1177
+ // let argumentsObjectNeeded = true;
1178
+ // if (func[ThisMode] === Mode.LEXICAL) {
1179
+ // // NOTE: Arrow functions never have an arguments object.
1180
+ // argumentsObjectNeeded = false;
1181
+ // } else if (parameterNames.includes("arguments")) {
1182
+ // argumentsObjectNeeded = false;
1183
+ // } else if (!hasParameterExpressions && (
1184
+ // varNames.includes("arguments") ||
1185
+ // collectBoundNames(collectScopedDeclarations(code, { var: false })).includes("arguments")
1186
+ // )) {
1187
+ // argumentsObjectNeeded = false;
1188
+ // }
1189
+ // NOTE: In strict mode, no parameter/function/var/lexical names can be "arguments".
1190
+ const argumentsObjectNeeded = !!externalSourceForDebug && func[_ExecutionContext.ThisMode] !== _ExecutionContext.Mode.LEXICAL;
1057
1191
  const env = calleeContext.LexicalEnvironment;
1058
1192
  for (const paramName of parameterNames) {
1059
1193
  // In strict mode, it's guaranteed no duplicate params exist.
1060
1194
  env.CreateMutableBinding(paramName, false);
1061
1195
  }
1196
+ let parameterBindings = parameterNames;
1197
+ if (argumentsObjectNeeded) {
1198
+ const ao = CreateUnmappedArgumentsObject(args);
1199
+ env.CreateImmutableBinding("arguments", false);
1200
+ env.InitializeBinding("arguments", ao);
1201
+ parameterBindings = parameterNames.concat("arguments");
1202
+ }
1062
1203
  const iteratorRecord = (0, _contextFree.CreateListIteratorRecord)(args);
1063
- IteratorBindingInitialization(formals, iteratorRecord, env);
1204
+ yield* IteratorBindingInitialization(formals, iteratorRecord, env);
1064
1205
  let varEnv;
1065
1206
  if (!hasParameterExpressions) {
1066
1207
  // NOTE: Only a single Environment Record is needed for the parameters
1067
1208
  // and top-level vars.
1068
1209
  // `varNames` are unique.
1210
+ const instantiatedVarNames = [...parameterBindings];
1069
1211
  for (const n of varNames) {
1070
- if (!parameterNames.includes(n)) {
1212
+ if (!instantiatedVarNames.includes(n)) {
1213
+ instantiatedVarNames.push(n);
1071
1214
  env.CreateMutableBinding(n, false);
1072
1215
  env.InitializeBinding(n, undefined);
1073
1216
  }
@@ -1080,15 +1223,19 @@ function cook(rootAst, codeSource, {
1080
1223
  varEnv = new _ExecutionContext.DeclarativeEnvironment(env);
1081
1224
  calleeContext.VariableEnvironment = varEnv;
1082
1225
  // `varNames` are unique.
1226
+ const instantiatedVarNames = [];
1083
1227
  for (const n of varNames) {
1084
- varEnv.CreateMutableBinding(n, false);
1085
- let initialValue;
1086
- if (parameterNames.includes(n) && !functionNames.includes(n)) {
1087
- initialValue = env.GetBindingValue(n, false);
1228
+ if (!instantiatedVarNames.includes(n)) {
1229
+ instantiatedVarNames.push(n);
1230
+ varEnv.CreateMutableBinding(n, false);
1231
+ let initialValue;
1232
+ if (parameterBindings.includes(n) && !functionNames.includes(n)) {
1233
+ initialValue = env.GetBindingValue(n, false);
1234
+ }
1235
+ varEnv.InitializeBinding(n, initialValue);
1236
+ // NOTE: A var with the same name as a formal parameter initially has
1237
+ // the same value as the corresponding initialized parameter.
1088
1238
  }
1089
- varEnv.InitializeBinding(n, initialValue);
1090
- // NOTE: A var with the same name as a formal parameter initially has
1091
- // the same value as the corresponding initialized parameter.
1092
1239
  }
1093
1240
  }
1094
1241
  const lexEnv = varEnv;
@@ -1113,40 +1260,78 @@ function cook(rootAst, codeSource, {
1113
1260
  varEnv.SetMutableBinding(fn, fo, false);
1114
1261
  }
1115
1262
  }
1263
+ function CreateUnmappedArgumentsObject(args) {
1264
+ const argList = [...args];
1265
+ const argumentObject = {};
1266
+ Object.defineProperty(argumentObject, "length", {
1267
+ value: argList.length,
1268
+ writable: true,
1269
+ configurable: true
1270
+ });
1271
+ for (let index = 0; index < argList.length; index++) {
1272
+ argumentObject[String(index)] = argList[index];
1273
+ }
1274
+ Object.defineProperty(argumentObject, Symbol.iterator, {
1275
+ value: Array.prototype.values,
1276
+ writable: true,
1277
+ configurable: true
1278
+ });
1279
+ const ThrowTypeError = () => {
1280
+ throw new TypeError("'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them");
1281
+ };
1282
+ Object.defineProperty(argumentObject, "callee", {
1283
+ get: ThrowTypeError,
1284
+ set: ThrowTypeError
1285
+ });
1286
+ return argumentObject;
1287
+ }
1116
1288
 
1117
1289
  // https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
1118
1290
  function InstantiateFunctionObject(func, scope) {
1119
- return OrdinaryFunctionCreate(func, scope, true);
1291
+ const F = OrdinaryFunctionCreate(func, scope, true, false);
1292
+ if (func.id) {
1293
+ SetFunctionName(F, func.id.name);
1294
+ }
1295
+ return F;
1120
1296
  }
1121
1297
 
1122
1298
  // https://tc39.es/ecma262/#sec-runtime-semantics-instantiateordinaryfunctionexpression
1123
- function InstantiateOrdinaryFunctionExpression(functionExpression) {
1299
+ function InstantiateOrdinaryFunctionExpression(functionExpression, name) {
1124
1300
  const scope = getRunningContext().LexicalEnvironment;
1125
1301
  if (functionExpression.id) {
1126
1302
  const name = functionExpression.id.name;
1127
1303
  const funcEnv = new _ExecutionContext.DeclarativeEnvironment(scope);
1128
1304
  funcEnv.CreateImmutableBinding(name, false);
1129
- const closure = OrdinaryFunctionCreate(functionExpression, funcEnv, true);
1305
+ const closure = OrdinaryFunctionCreate(functionExpression, funcEnv, true, false);
1306
+ SetFunctionName(closure, name);
1130
1307
  funcEnv.InitializeBinding(name, closure);
1131
1308
  return closure;
1132
1309
  } else {
1133
- const closure = OrdinaryFunctionCreate(functionExpression, scope, true);
1310
+ const closure = OrdinaryFunctionCreate(functionExpression, scope, true, false);
1311
+ SetFunctionName(closure, name ?? "");
1134
1312
  return closure;
1135
1313
  }
1136
1314
  }
1137
1315
 
1138
1316
  // https://tc39.es/ecma262/#sec-runtime-semantics-instantiatearrowfunctionexpression
1139
- function InstantiateArrowFunctionExpression(arrowFunction) {
1317
+ function InstantiateArrowFunctionExpression(arrowFunction, name) {
1140
1318
  const scope = getRunningContext().LexicalEnvironment;
1141
- const closure = OrdinaryFunctionCreate(arrowFunction, scope, false);
1319
+ const closure = OrdinaryFunctionCreate(arrowFunction, scope, false, true);
1320
+ SetFunctionName(closure, name ?? "");
1142
1321
  return closure;
1143
1322
  }
1323
+ function SetFunctionName(F, name) {
1324
+ Object.defineProperty(F, "name", {
1325
+ value: name,
1326
+ configurable: true
1327
+ });
1328
+ }
1144
1329
 
1145
1330
  // https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
1146
- function OrdinaryFunctionCreate(sourceNode, scope, isConstructor) {
1331
+ function OrdinaryFunctionCreate(sourceNode, scope, isConstructor, lexicalThis) {
1147
1332
  const F = function () {
1148
1333
  // eslint-disable-next-line prefer-rest-params
1149
- return CallFunction(F, arguments);
1334
+ return unwind(CallFunction(F, this, arguments));
1150
1335
  };
1151
1336
  Object.defineProperties(F, {
1152
1337
  [_ExecutionContext.SourceNode]: {
@@ -1163,41 +1348,52 @@ function cook(rootAst, codeSource, {
1163
1348
  },
1164
1349
  [_ExecutionContext.IsConstructor]: {
1165
1350
  value: isConstructor
1351
+ },
1352
+ [_ExecutionContext.ThisMode]: {
1353
+ value: lexicalThis ? _ExecutionContext.Mode.LEXICAL : _ExecutionContext.Mode.STRICT
1166
1354
  }
1167
1355
  });
1356
+ if (debug || externalSourceForDebug) {
1357
+ Object.defineProperty(F, _ExecutionContext.DebuggerCall, {
1358
+ value: function () {
1359
+ // eslint-disable-next-line prefer-rest-params
1360
+ return CallFunction(F, this, arguments);
1361
+ }
1362
+ });
1363
+ }
1168
1364
  return F;
1169
1365
  }
1170
1366
 
1171
1367
  // Patterns initialization.
1172
1368
  // https://tc39.es/ecma262/#sec-runtime-semantics-bindinginitialization
1173
- function BindingInitialization(node, value, environment) {
1369
+ function* BindingInitialization(node, value, environment) {
1174
1370
  switch (node.type) {
1175
1371
  case "Identifier":
1176
1372
  return InitializeBoundName(node.name, value, environment);
1177
1373
  case "ObjectPattern":
1178
1374
  (0, _contextFree.RequireObjectCoercible)(value);
1179
- return PropertyBindingInitialization(node.properties, value, environment);
1375
+ return yield* PropertyBindingInitialization(node.properties, value, environment);
1180
1376
  case "ArrayPattern":
1181
1377
  {
1182
1378
  const iteratorRecord = (0, _contextFree.CreateListIteratorRecord)(value);
1183
- return IteratorBindingInitialization(node.elements, iteratorRecord, environment);
1379
+ return yield* IteratorBindingInitialization(node.elements, iteratorRecord, environment);
1184
1380
  }
1185
1381
  }
1186
1382
  }
1187
1383
 
1188
1384
  // https://tc39.es/ecma262/#sec-destructuring-binding-patterns-runtime-semantics-propertybindinginitialization
1189
- function PropertyBindingInitialization(properties, value, environment) {
1385
+ function* PropertyBindingInitialization(properties, value, environment) {
1190
1386
  const excludedNames = new Set();
1191
1387
  for (const prop of properties) {
1192
1388
  if (prop.type === "RestElement") {
1193
1389
  return RestBindingInitialization(prop, value, environment, excludedNames);
1194
1390
  }
1195
1391
  if (!prop.computed && prop.key.type === "Identifier") {
1196
- KeyedBindingInitialization(prop.value, value, environment, prop.key.name);
1392
+ yield* KeyedBindingInitialization(prop.value, value, environment, prop.key.name);
1197
1393
  excludedNames.add(prop.key.name);
1198
1394
  } else {
1199
- const P = EvaluateComputedPropertyName(prop.key);
1200
- KeyedBindingInitialization(prop.value, value, environment, P);
1395
+ const P = yield* EvaluateComputedPropertyName(prop.key);
1396
+ yield* KeyedBindingInitialization(prop.value, value, environment, P);
1201
1397
  excludedNames.add(P);
1202
1398
  }
1203
1399
  }
@@ -1205,8 +1401,8 @@ function cook(rootAst, codeSource, {
1205
1401
  }
1206
1402
 
1207
1403
  // https://tc39.es/ecma262/#prod-ComputedPropertyName
1208
- function EvaluateComputedPropertyName(node) {
1209
- const propName = (0, _contextFree.GetValue)(Evaluate(node));
1404
+ function* EvaluateComputedPropertyName(node) {
1405
+ const propName = (0, _contextFree.GetValue)(yield* Evaluate(node));
1210
1406
  return (0, _contextFree.ToPropertyKey)(propName);
1211
1407
  }
1212
1408
 
@@ -1221,7 +1417,7 @@ function cook(rootAst, codeSource, {
1221
1417
  }
1222
1418
 
1223
1419
  // https://tc39.es/ecma262/#sec-runtime-semantics-iteratorbindinginitialization
1224
- function IteratorBindingInitialization(elements, iteratorRecord, environment) {
1420
+ function* IteratorBindingInitialization(elements, iteratorRecord, environment) {
1225
1421
  if (elements.length === 0) {
1226
1422
  return (0, _ExecutionContext.NormalCompletion)(_ExecutionContext.Empty);
1227
1423
  }
@@ -1235,7 +1431,7 @@ function cook(rootAst, codeSource, {
1235
1431
  // Rest element.
1236
1432
  if (node.argument.type === "Identifier") {
1237
1433
  const lhs = ResolveBinding(node.argument.name, environment);
1238
- const A = [];
1434
+ const A = new ArrayConstructor();
1239
1435
  let n = 0;
1240
1436
  // eslint-disable-next-line no-constant-condition
1241
1437
  while (true) {
@@ -1251,7 +1447,7 @@ function cook(rootAst, codeSource, {
1251
1447
  n++;
1252
1448
  }
1253
1449
  } else {
1254
- const A = [];
1450
+ const A = new ArrayConstructor();
1255
1451
  let n = 0;
1256
1452
  // eslint-disable-next-line no-constant-condition
1257
1453
  while (true) {
@@ -1260,7 +1456,7 @@ function cook(rootAst, codeSource, {
1260
1456
  value
1261
1457
  } = iteratorRecord.next();
1262
1458
  if (done) {
1263
- result = BindingInitialization(node.argument, A, environment);
1459
+ result = yield* BindingInitialization(node.argument, A, environment);
1264
1460
  break;
1265
1461
  }
1266
1462
  A[n] = value;
@@ -1283,10 +1479,10 @@ function cook(rootAst, codeSource, {
1283
1479
  v = value;
1284
1480
  }
1285
1481
  if (node.type === "AssignmentPattern" && v === undefined) {
1286
- const defaultValue = Evaluate(node.right);
1482
+ const defaultValue = yield* Evaluate(node.right);
1287
1483
  v = (0, _contextFree.GetValue)(defaultValue);
1288
1484
  }
1289
- result = BindingInitialization(bindingElement, v, environment);
1485
+ result = yield* BindingInitialization(bindingElement, v, environment);
1290
1486
  break;
1291
1487
  }
1292
1488
  case "Identifier":
@@ -1302,9 +1498,12 @@ function cook(rootAst, codeSource, {
1302
1498
  v = value;
1303
1499
  }
1304
1500
  if (node.type === "AssignmentPattern" && v === undefined) {
1305
- // IsAnonymousFunctionDefinition(Initializer)
1306
- const defaultValue = Evaluate(node.right);
1307
- v = (0, _contextFree.GetValue)(defaultValue);
1501
+ if (IsAnonymousFunctionDefinition(node.right)) {
1502
+ v = NamedEvaluation(node.right, bindingId);
1503
+ } else {
1504
+ const defaultValue = yield* Evaluate(node.right);
1505
+ v = (0, _contextFree.GetValue)(defaultValue);
1506
+ }
1308
1507
  }
1309
1508
  result = environment ? (0, _contextFree.InitializeReferencedBinding)(lhs, v) : (0, _contextFree.PutValue)(lhs, v);
1310
1509
  break;
@@ -1316,16 +1515,19 @@ function cook(rootAst, codeSource, {
1316
1515
  }
1317
1516
 
1318
1517
  // https://tc39.es/ecma262/#sec-runtime-semantics-keyedbindinginitialization
1319
- function KeyedBindingInitialization(node, value, environment, propertyName) {
1518
+ function* KeyedBindingInitialization(node, value, environment, propertyName) {
1320
1519
  const isIdentifier = node.type === "Identifier" || node.type === "AssignmentPattern" && node.left.type === "Identifier";
1321
1520
  if (isIdentifier) {
1322
1521
  const bindingId = node.type === "Identifier" ? node.name : node.left.name;
1323
1522
  const lhs = ResolveBinding(bindingId, environment);
1324
1523
  let v = (0, _contextFree.GetV)(value, propertyName);
1325
1524
  if (node.type === "AssignmentPattern" && v === undefined) {
1326
- // If IsAnonymousFunctionDefinition(Initializer)
1327
- const defaultValue = Evaluate(node.right);
1328
- v = (0, _contextFree.GetValue)(defaultValue);
1525
+ if (IsAnonymousFunctionDefinition(node.right)) {
1526
+ v = NamedEvaluation(node.right, bindingId);
1527
+ } else {
1528
+ const defaultValue = yield* Evaluate(node.right);
1529
+ v = (0, _contextFree.GetValue)(defaultValue);
1530
+ }
1329
1531
  }
1330
1532
  if (!environment) {
1331
1533
  return (0, _contextFree.PutValue)(lhs, v);
@@ -1334,10 +1536,10 @@ function cook(rootAst, codeSource, {
1334
1536
  }
1335
1537
  let v = (0, _contextFree.GetV)(value, propertyName);
1336
1538
  if (node.type === "AssignmentPattern" && v === undefined) {
1337
- const defaultValue = Evaluate(node.right);
1539
+ const defaultValue = yield* Evaluate(node.right);
1338
1540
  v = (0, _contextFree.GetValue)(defaultValue);
1339
1541
  }
1340
- return BindingInitialization(node.type === "AssignmentPattern" ? node.left : node, v, environment);
1542
+ return yield* BindingInitialization(node.type === "AssignmentPattern" ? node.left : node, v, environment);
1341
1543
  }
1342
1544
 
1343
1545
  // https://tc39.es/ecma262/#sec-initializeboundname
@@ -1355,7 +1557,7 @@ function cook(rootAst, codeSource, {
1355
1557
  }
1356
1558
  }
1357
1559
  if (expressionOnly) {
1358
- return (0, _contextFree.GetValue)(Evaluate(rootAst));
1560
+ return (0, _contextFree.GetValue)(unwind(Evaluate(rootAst)));
1359
1561
  }
1360
1562
  (_hooks$beforeEvaluate3 = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate3 === void 0 || _hooks$beforeEvaluate3.call(hooks, rootAst);
1361
1563
  ThrowIfFunctionIsInvalid(rootAst);
@@ -1364,6 +1566,32 @@ function cook(rootAst, codeSource, {
1364
1566
  rootEnv.CreateImmutableBinding(fn, true);
1365
1567
  const fo = InstantiateFunctionObject(rootAst, rootEnv);
1366
1568
  rootEnv.InitializeBinding(fn, fo);
1569
+ if (debug) {
1570
+ Object.defineProperties(fo, {
1571
+ [_ExecutionContext.DebuggerScope]: {
1572
+ value: function () {
1573
+ return getRunningContext().LexicalEnvironment;
1574
+ }
1575
+ },
1576
+ [_ExecutionContext.DebuggerNode]: {
1577
+ value: function () {
1578
+ return currentNode;
1579
+ }
1580
+ }
1581
+ });
1582
+ }
1367
1583
  return fo;
1368
1584
  }
1585
+ function unwind(iterator) {
1586
+ // eslint-disable-next-line no-constant-condition
1587
+ while (true) {
1588
+ const {
1589
+ done,
1590
+ value
1591
+ } = iterator.next();
1592
+ if (done) {
1593
+ return value;
1594
+ }
1595
+ }
1596
+ }
1369
1597
  //# sourceMappingURL=cook.js.map