@next-core/cook 1.6.77 → 1.6.79
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/AnalysisContext.js +2 -11
- package/dist/cjs/AnalysisContext.js.map +1 -1
- package/dist/cjs/ExecutionContext.js +11 -37
- package/dist/cjs/ExecutionContext.js.map +1 -1
- package/dist/cjs/context-free.js +30 -71
- package/dist/cjs/context-free.js.map +1 -1
- package/dist/cjs/cook.js +147 -386
- package/dist/cjs/cook.js.map +1 -1
- package/dist/cjs/hasOwnProperty.js +0 -1
- package/dist/cjs/hasOwnProperty.js.map +1 -1
- package/dist/cjs/index.js +0 -14
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/interfaces.js.map +1 -1
- package/dist/cjs/lint.js +0 -21
- package/dist/cjs/lint.js.map +1 -1
- package/dist/cjs/parse.js +0 -8
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/precook.js +6 -74
- package/dist/cjs/precook.js.map +1 -1
- package/dist/cjs/precookFunction.js +0 -3
- package/dist/cjs/precookFunction.js.map +1 -1
- package/dist/cjs/preevaluate.js +2 -6
- package/dist/cjs/preevaluate.js.map +1 -1
- package/dist/cjs/sanitize.js +9 -13
- package/dist/cjs/sanitize.js.map +1 -1
- package/dist/cjs/traverse.js +0 -32
- package/dist/cjs/traverse.js.map +1 -1
- package/dist/esm/AnalysisContext.js +2 -8
- package/dist/esm/AnalysisContext.js.map +1 -1
- package/dist/esm/ExecutionContext.js +11 -30
- package/dist/esm/ExecutionContext.js.map +1 -1
- package/dist/esm/context-free.js +32 -53
- package/dist/esm/context-free.js.map +1 -1
- package/dist/esm/cook.js +145 -411
- package/dist/esm/cook.js.map +1 -1
- package/dist/esm/hasOwnProperty.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/interfaces.js.map +1 -1
- package/dist/esm/lint.js +0 -19
- package/dist/esm/lint.js.map +1 -1
- package/dist/esm/parse.js +0 -6
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/precook.js +4 -82
- package/dist/esm/precook.js.map +1 -1
- package/dist/esm/precookFunction.js +4 -5
- package/dist/esm/precookFunction.js.map +1 -1
- package/dist/esm/preevaluate.js.map +1 -1
- package/dist/esm/sanitize.js +9 -9
- package/dist/esm/sanitize.js.map +1 -1
- package/dist/esm/traverse.js +0 -29
- package/dist/esm/traverse.js.map +1 -1
- package/dist/types/interfaces.d.ts +2 -0
- package/dist/types/lint.d.ts +3 -4
- package/dist/types/parse.d.ts +3 -3
- package/package.json +6 -6
package/dist/esm/cook.js
CHANGED
|
@@ -2,11 +2,9 @@ import { ApplyStringOrNumericAssignment, CreateListIteratorRecord, ApplyStringOr
|
|
|
2
2
|
import { CompletionRecord, DeclarativeEnvironment, ECMAScriptCode, Empty, Environment, ExecutionContext, FormalParameters, FunctionEnvironment, IsConstructor, NormalCompletion, ReferenceRecord, SourceNode } from "./ExecutionContext";
|
|
3
3
|
import { sanitize, isAllowedConstructor } from "./sanitize";
|
|
4
4
|
import { collectBoundNames, collectScopedDeclarations, containsExpression } from "./traverse";
|
|
5
|
-
|
|
6
5
|
/** For next-core internal usage only. */
|
|
7
6
|
export function cook(rootAst, codeSource) {
|
|
8
7
|
var _hooks$beforeEvaluate3;
|
|
9
|
-
|
|
10
8
|
var {
|
|
11
9
|
rules,
|
|
12
10
|
globalVariables = {},
|
|
@@ -18,21 +16,18 @@ export function cook(rootAst, codeSource) {
|
|
|
18
16
|
rootContext.VariableEnvironment = rootEnv;
|
|
19
17
|
rootContext.LexicalEnvironment = rootEnv;
|
|
20
18
|
var executionContextStack = [rootContext];
|
|
21
|
-
|
|
22
19
|
for (var [key, value] of Object.entries(globalVariables)) {
|
|
23
20
|
rootEnv.CreateImmutableBinding(key, true);
|
|
24
21
|
rootEnv.InitializeBinding(key, value);
|
|
25
22
|
}
|
|
23
|
+
var TemplateMap = new WeakMap();
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
// https://tc39.es/ecma262/#sec-gettemplateobject
|
|
29
26
|
function GetTemplateObject(templateLiteral) {
|
|
30
27
|
var memo = TemplateMap.get(templateLiteral);
|
|
31
|
-
|
|
32
28
|
if (memo) {
|
|
33
29
|
return memo;
|
|
34
30
|
}
|
|
35
|
-
|
|
36
31
|
var rawObj = templateLiteral.quasis.map(quasi => quasi.value.raw);
|
|
37
32
|
var template = templateLiteral.quasis.map(quasi => quasi.value.cooked);
|
|
38
33
|
Object.freeze(rawObj);
|
|
@@ -46,18 +41,15 @@ export function cook(rootAst, codeSource) {
|
|
|
46
41
|
TemplateMap.set(templateLiteral, template);
|
|
47
42
|
return template;
|
|
48
43
|
}
|
|
49
|
-
|
|
50
44
|
function Evaluate(node, optionalChainRef) {
|
|
51
45
|
var _hooks$beforeEvaluate, _hooks$beforeBranch, _hooks$beforeBranch2;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
(_hooks$beforeEvaluate = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate === void 0 ? void 0 : _hooks$beforeEvaluate.call(hooks, node);
|
|
47
|
+
// Expressions:
|
|
55
48
|
switch (node.type) {
|
|
56
49
|
case "ArrayExpression":
|
|
57
50
|
{
|
|
58
51
|
// https://tc39.es/ecma262/#sec-array-initializer
|
|
59
52
|
var array = [];
|
|
60
|
-
|
|
61
53
|
for (var element of node.elements) {
|
|
62
54
|
if (!element) {
|
|
63
55
|
array.length += 1;
|
|
@@ -68,10 +60,8 @@ export function cook(rootAst, codeSource) {
|
|
|
68
60
|
array.push(GetValue(Evaluate(element)));
|
|
69
61
|
}
|
|
70
62
|
}
|
|
71
|
-
|
|
72
63
|
return NormalCompletion(array);
|
|
73
64
|
}
|
|
74
|
-
|
|
75
65
|
case "ArrowFunctionExpression":
|
|
76
66
|
{
|
|
77
67
|
// https://tc39.es/ecma262/#sec-arrow-function-definitions
|
|
@@ -79,14 +69,12 @@ export function cook(rootAst, codeSource) {
|
|
|
79
69
|
var closure = InstantiateArrowFunctionExpression(node);
|
|
80
70
|
return NormalCompletion(closure);
|
|
81
71
|
}
|
|
82
|
-
|
|
83
72
|
case "BinaryExpression":
|
|
84
73
|
{
|
|
85
74
|
var leftRef = Evaluate(node.left);
|
|
86
75
|
var leftValue = GetValue(leftRef);
|
|
87
76
|
var rightRef = Evaluate(node.right).Value;
|
|
88
77
|
var rightValue = GetValue(rightRef);
|
|
89
|
-
|
|
90
78
|
if (expressionOnly && node.operator === "|>") {
|
|
91
79
|
// Minimal pipeline operator is supported only in expression-only mode.
|
|
92
80
|
// See https://tc39.es/proposal-pipeline-operator
|
|
@@ -95,50 +83,39 @@ export function cook(rootAst, codeSource) {
|
|
|
95
83
|
var funcName = codeSource.substring(node.right.start, node.right.end);
|
|
96
84
|
throw new TypeError("".concat(funcName, " is not a function"));
|
|
97
85
|
}
|
|
98
|
-
|
|
99
86
|
var thisValue;
|
|
100
|
-
|
|
101
87
|
if (rightRef instanceof ReferenceRecord) {
|
|
102
88
|
if (IsPropertyReference(rightRef)) {
|
|
103
89
|
thisValue = rightRef.Base;
|
|
104
90
|
}
|
|
105
91
|
}
|
|
106
|
-
|
|
107
92
|
return NormalCompletion(rightValue.call(thisValue, leftValue));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
93
|
+
}
|
|
94
|
+
// https://tc39.es/ecma262/#sec-additive-operators
|
|
111
95
|
var result = ApplyStringOrNumericBinaryOperator(leftValue, node.operator, rightValue);
|
|
112
96
|
return NormalCompletion(result);
|
|
113
97
|
}
|
|
114
|
-
|
|
115
98
|
case "CallExpression":
|
|
116
99
|
{
|
|
117
100
|
// https://tc39.es/ecma262/#sec-function-calls
|
|
118
101
|
var ref = Evaluate(node.callee, optionalChainRef).Value;
|
|
119
102
|
var func = GetValue(ref);
|
|
120
|
-
|
|
121
103
|
if ((func === undefined || func === null) && (node.optional || optionalChainRef !== null && optionalChainRef !== void 0 && optionalChainRef.skipped)) {
|
|
122
104
|
optionalChainRef.skipped = true;
|
|
123
105
|
return NormalCompletion(undefined);
|
|
124
106
|
}
|
|
125
|
-
|
|
126
107
|
sanitize(func);
|
|
127
108
|
return EvaluateCall(func, ref, node.arguments, node.callee);
|
|
128
109
|
}
|
|
129
|
-
|
|
130
110
|
case "ChainExpression":
|
|
131
111
|
// https://tc39.es/ecma262/#sec-optional-chains
|
|
132
112
|
return Evaluate(node.expression, {});
|
|
133
|
-
|
|
134
113
|
case "ConditionalExpression":
|
|
135
114
|
// https://tc39.es/ecma262/#sec-conditional-operator
|
|
136
115
|
return NormalCompletion(GetValue(Evaluate(GetValue(Evaluate(node.test)) ? node.consequent : node.alternate)));
|
|
137
|
-
|
|
138
116
|
case "Identifier":
|
|
139
117
|
// https://tc39.es/ecma262/#sec-identifiers
|
|
140
118
|
return NormalCompletion(ResolveBinding(node.name));
|
|
141
|
-
|
|
142
119
|
case "Literal":
|
|
143
120
|
{
|
|
144
121
|
// https://tc39.es/ecma262/#sec-primary-expression-literals
|
|
@@ -147,67 +124,52 @@ export function cook(rootAst, codeSource) {
|
|
|
147
124
|
// Invalid regular expression fails silently in @babel/parser.
|
|
148
125
|
throw new SyntaxError("Invalid regular expression: ".concat(node.raw));
|
|
149
126
|
}
|
|
150
|
-
|
|
151
127
|
if (node.regex.flags.includes("u")) {
|
|
152
128
|
// Currently unicode flag is not fully supported across major browsers.
|
|
153
129
|
throw new SyntaxError("Unsupported unicode flag in regular expression: ".concat(node.raw));
|
|
154
130
|
}
|
|
155
131
|
}
|
|
156
|
-
|
|
157
132
|
return NormalCompletion(node.value);
|
|
158
133
|
}
|
|
159
|
-
|
|
160
134
|
case "LogicalExpression":
|
|
161
135
|
{
|
|
162
136
|
// https://tc39.es/ecma262/#sec-binary-logical-operators
|
|
163
137
|
var _leftValue = GetValue(Evaluate(node.left));
|
|
164
|
-
|
|
165
138
|
switch (node.operator) {
|
|
166
139
|
case "&&":
|
|
167
140
|
return NormalCompletion(_leftValue && GetValue(Evaluate(node.right)));
|
|
168
|
-
|
|
169
141
|
case "||":
|
|
170
142
|
return NormalCompletion(_leftValue || GetValue(Evaluate(node.right)));
|
|
171
|
-
|
|
172
143
|
case "??":
|
|
173
144
|
return NormalCompletion(_leftValue !== null && _leftValue !== void 0 ? _leftValue : GetValue(Evaluate(node.right)));
|
|
174
145
|
// istanbul ignore next
|
|
175
|
-
|
|
176
146
|
default:
|
|
177
147
|
throw new SyntaxError( // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
178
148
|
// @ts-ignore never reach here.
|
|
179
149
|
"Unsupported logical operator '".concat(node.operator, "'"));
|
|
180
150
|
}
|
|
181
151
|
}
|
|
182
|
-
|
|
183
152
|
case "MemberExpression":
|
|
184
153
|
{
|
|
185
154
|
// https://tc39.es/ecma262/#sec-property-accessors
|
|
186
155
|
var baseReference = Evaluate(node.object, optionalChainRef).Value;
|
|
187
156
|
var baseValue = GetValue(baseReference);
|
|
188
|
-
|
|
189
157
|
if ((baseValue === undefined || baseValue === null) && (node.optional || optionalChainRef !== null && optionalChainRef !== void 0 && optionalChainRef.skipped)) {
|
|
190
158
|
optionalChainRef.skipped = true;
|
|
191
159
|
return NormalCompletion(undefined);
|
|
192
160
|
}
|
|
193
|
-
|
|
194
161
|
sanitize(baseValue);
|
|
195
|
-
|
|
196
162
|
var _result = node.computed ? EvaluatePropertyAccessWithExpressionKey(baseValue, node.property, true) : EvaluatePropertyAccessWithIdentifierKey(baseValue, node.property, true);
|
|
197
|
-
|
|
198
163
|
sanitize(_result);
|
|
199
164
|
return NormalCompletion(_result);
|
|
200
165
|
}
|
|
201
|
-
|
|
202
166
|
case "NewExpression":
|
|
203
167
|
// https://tc39.es/ecma262/#sec-new-operator
|
|
204
168
|
return EvaluateNew(node.callee, node.arguments);
|
|
205
|
-
|
|
206
169
|
case "ObjectExpression":
|
|
207
170
|
{
|
|
208
171
|
// https://tc39.es/ecma262/#sec-object-initializer
|
|
209
172
|
var object = {};
|
|
210
|
-
|
|
211
173
|
for (var prop of node.properties) {
|
|
212
174
|
if (prop.type === "SpreadElement") {
|
|
213
175
|
var fromValue = GetValue(Evaluate(prop.argument));
|
|
@@ -216,47 +178,36 @@ export function cook(rootAst, codeSource) {
|
|
|
216
178
|
if (prop.kind !== "init") {
|
|
217
179
|
throw new SyntaxError("Unsupported object getter/setter");
|
|
218
180
|
}
|
|
219
|
-
|
|
220
181
|
var propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : EvaluateComputedPropertyName(prop.key);
|
|
221
|
-
|
|
222
182
|
if (propName === "__proto__") {
|
|
223
183
|
throw new TypeError("Setting '__proto__' property is not allowed");
|
|
224
184
|
}
|
|
225
|
-
|
|
226
185
|
object[propName] = GetValue(Evaluate(prop.value));
|
|
227
186
|
}
|
|
228
187
|
}
|
|
229
|
-
|
|
230
188
|
return NormalCompletion(object);
|
|
231
189
|
}
|
|
232
|
-
|
|
233
190
|
case "SequenceExpression":
|
|
234
191
|
{
|
|
235
192
|
// https://tc39.es/ecma262/#sec-comma-operator
|
|
236
193
|
var _result2;
|
|
237
|
-
|
|
238
194
|
for (var expr of node.expressions) {
|
|
239
195
|
_result2 = NormalCompletion(GetValue(Evaluate(expr)));
|
|
240
196
|
}
|
|
241
|
-
|
|
242
197
|
return _result2;
|
|
243
198
|
}
|
|
244
|
-
|
|
245
199
|
case "TemplateLiteral":
|
|
246
200
|
{
|
|
247
201
|
// https://tc39.es/ecma262/#sec-template-literals
|
|
248
202
|
var chunks = [node.quasis[0].value.cooked];
|
|
249
203
|
var index = 0;
|
|
250
|
-
|
|
251
204
|
for (var _expr of node.expressions) {
|
|
252
205
|
var val = GetValue(Evaluate(_expr));
|
|
253
206
|
chunks.push(String(val));
|
|
254
207
|
chunks.push(node.quasis[index += 1].value.cooked);
|
|
255
208
|
}
|
|
256
|
-
|
|
257
209
|
return NormalCompletion(chunks.join(""));
|
|
258
210
|
}
|
|
259
|
-
|
|
260
211
|
case "TaggedTemplateExpression":
|
|
261
212
|
{
|
|
262
213
|
// https://tc39.es/ecma262/#sec-tagged-templates
|
|
@@ -265,38 +216,32 @@ export function cook(rootAst, codeSource) {
|
|
|
265
216
|
sanitize(tagFunc);
|
|
266
217
|
return EvaluateCall(tagFunc, tagRef, node.quasi, node.tag);
|
|
267
218
|
}
|
|
268
|
-
|
|
269
219
|
case "UnaryExpression":
|
|
270
220
|
{
|
|
271
221
|
// https://tc39.es/ecma262/#sec-unary-operators
|
|
272
222
|
var _ref = Evaluate(node.argument).Value;
|
|
273
|
-
|
|
274
223
|
if (!expressionOnly && node.operator === "delete") {
|
|
275
224
|
// Delete operator is supported only in function mode.
|
|
276
225
|
if (!(_ref instanceof ReferenceRecord)) {
|
|
277
226
|
return NormalCompletion(true);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
|
|
227
|
+
}
|
|
228
|
+
// istanbul ignore else
|
|
281
229
|
if (IsPropertyReference(_ref)) {
|
|
282
230
|
var deleteStatus = delete _ref.Base[_ref.ReferenceName];
|
|
283
231
|
return NormalCompletion(deleteStatus);
|
|
284
|
-
}
|
|
285
|
-
|
|
232
|
+
}
|
|
233
|
+
// Should never reach here in strict mode.
|
|
286
234
|
}
|
|
287
235
|
|
|
288
236
|
if (node.operator === "typeof") {
|
|
289
237
|
if (_ref instanceof ReferenceRecord && _ref.Base === "unresolvable") {
|
|
290
238
|
return NormalCompletion("undefined");
|
|
291
239
|
}
|
|
292
|
-
|
|
293
240
|
return NormalCompletion(typeof GetValue(_ref));
|
|
294
241
|
}
|
|
295
|
-
|
|
296
242
|
return NormalCompletion(ApplyUnaryOperator(GetValue(_ref), node.operator));
|
|
297
243
|
}
|
|
298
244
|
}
|
|
299
|
-
|
|
300
245
|
if (!expressionOnly) {
|
|
301
246
|
// Statements and assignments:
|
|
302
247
|
switch (node.type) {
|
|
@@ -305,25 +250,19 @@ export function cook(rootAst, codeSource) {
|
|
|
305
250
|
// https://tc39.es/ecma262/#sec-assignment-operators
|
|
306
251
|
if (node.operator === "=") {
|
|
307
252
|
if (!(node.left.type === "ArrayPattern" || node.left.type === "ObjectPattern")) {
|
|
308
|
-
var _lref = Evaluate(node.left).Value;
|
|
309
|
-
|
|
253
|
+
var _lref = Evaluate(node.left).Value;
|
|
254
|
+
// Todo: IsAnonymousFunctionDefinition(lref)
|
|
310
255
|
var _rref2 = Evaluate(node.right);
|
|
311
|
-
|
|
312
256
|
var _rval2 = GetValue(_rref2);
|
|
313
|
-
|
|
314
257
|
PutValue(_lref, _rval2);
|
|
315
258
|
return NormalCompletion(_rval2);
|
|
316
259
|
}
|
|
317
|
-
|
|
318
260
|
var _rref = Evaluate(node.right);
|
|
319
|
-
|
|
320
261
|
var _rval = GetValue(_rref);
|
|
321
|
-
|
|
322
262
|
DestructuringAssignmentEvaluation(node.left, _rval);
|
|
323
263
|
return NormalCompletion(_rval);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
264
|
+
}
|
|
265
|
+
// Operators other than `=`.
|
|
327
266
|
var lref = Evaluate(node.left).Value;
|
|
328
267
|
var lval = GetValue(lref);
|
|
329
268
|
var rref = Evaluate(node.right);
|
|
@@ -332,14 +271,12 @@ export function cook(rootAst, codeSource) {
|
|
|
332
271
|
PutValue(lref, r);
|
|
333
272
|
return NormalCompletion(r);
|
|
334
273
|
}
|
|
335
|
-
|
|
336
274
|
case "BlockStatement":
|
|
337
275
|
{
|
|
338
276
|
// https://tc39.es/ecma262/#sec-block
|
|
339
277
|
if (!node.body.length) {
|
|
340
278
|
return NormalCompletion(Empty);
|
|
341
279
|
}
|
|
342
|
-
|
|
343
280
|
var oldEnv = getRunningContext().LexicalEnvironment;
|
|
344
281
|
var blockEnv = new DeclarativeEnvironment(oldEnv);
|
|
345
282
|
BlockDeclarationInstantiation(node.body, blockEnv);
|
|
@@ -348,67 +285,52 @@ export function cook(rootAst, codeSource) {
|
|
|
348
285
|
getRunningContext().LexicalEnvironment = oldEnv;
|
|
349
286
|
return blockValue;
|
|
350
287
|
}
|
|
351
|
-
|
|
352
288
|
case "BreakStatement":
|
|
353
289
|
// https://tc39.es/ecma262/#sec-break-statement
|
|
354
290
|
return new CompletionRecord("break", Empty);
|
|
355
|
-
|
|
356
291
|
case "ContinueStatement":
|
|
357
292
|
// https://tc39.es/ecma262/#sec-continue-statement
|
|
358
293
|
return new CompletionRecord("continue", Empty);
|
|
359
|
-
|
|
360
294
|
case "EmptyStatement":
|
|
361
295
|
// https://tc39.es/ecma262/#sec-empty-statement
|
|
362
296
|
return NormalCompletion(Empty);
|
|
363
|
-
|
|
364
297
|
case "DoWhileStatement":
|
|
365
298
|
// https://tc39.es/ecma262/#sec-do-while-statement
|
|
366
299
|
return EvaluateBreakableStatement(DoWhileLoopEvaluation(node));
|
|
367
|
-
|
|
368
300
|
case "ExpressionStatement":
|
|
369
301
|
case "TSAsExpression":
|
|
370
302
|
// https://tc39.es/ecma262/#sec-expression-statement
|
|
371
303
|
return Evaluate(node.expression);
|
|
372
|
-
|
|
373
304
|
case "ForInStatement":
|
|
374
305
|
case "ForOfStatement":
|
|
375
306
|
// https://tc39.es/ecma262/#sec-for-in-and-for-of-statements
|
|
376
307
|
return EvaluateBreakableStatement(ForInOfLoopEvaluation(node));
|
|
377
|
-
|
|
378
308
|
case "ForStatement":
|
|
379
309
|
// https://tc39.es/ecma262/#sec-for-statement
|
|
380
310
|
return EvaluateBreakableStatement(ForLoopEvaluation(node));
|
|
381
|
-
|
|
382
311
|
case "FunctionDeclaration":
|
|
383
312
|
// https://tc39.es/ecma262/#sec-function-definitions
|
|
384
313
|
return NormalCompletion(Empty);
|
|
385
|
-
|
|
386
314
|
case "FunctionExpression":
|
|
387
315
|
// https://tc39.es/ecma262/#sec-function-defining-expressions
|
|
388
316
|
ThrowIfFunctionIsInvalid(node);
|
|
389
317
|
return NormalCompletion(InstantiateOrdinaryFunctionExpression(node));
|
|
390
|
-
|
|
391
318
|
case "IfStatement":
|
|
392
319
|
// https://tc39.es/ecma262/#sec-if-statement
|
|
393
320
|
return GetValue(Evaluate(node.test)) ? ((_hooks$beforeBranch = hooks.beforeBranch) !== null && _hooks$beforeBranch !== void 0 && _hooks$beforeBranch.call(hooks, node, "if"), UpdateEmpty(Evaluate(node.consequent), undefined)) : ((_hooks$beforeBranch2 = hooks.beforeBranch) !== null && _hooks$beforeBranch2 !== void 0 && _hooks$beforeBranch2.call(hooks, node, "else"), node.alternate) ? UpdateEmpty(Evaluate(node.alternate), undefined) : NormalCompletion(undefined);
|
|
394
|
-
|
|
395
321
|
case "ReturnStatement":
|
|
396
322
|
{
|
|
397
323
|
// https://tc39.es/ecma262/#sec-return-statement
|
|
398
324
|
var v;
|
|
399
|
-
|
|
400
325
|
if (node.argument) {
|
|
401
326
|
var exprRef = Evaluate(node.argument);
|
|
402
327
|
v = GetValue(exprRef);
|
|
403
328
|
}
|
|
404
|
-
|
|
405
329
|
return new CompletionRecord("return", v);
|
|
406
330
|
}
|
|
407
|
-
|
|
408
331
|
case "ThrowStatement":
|
|
409
332
|
// https://tc39.es/ecma262/#sec-throw-statement
|
|
410
333
|
throw GetValue(Evaluate(node.argument));
|
|
411
|
-
|
|
412
334
|
case "UpdateExpression":
|
|
413
335
|
{
|
|
414
336
|
// https://tc39.es/ecma262/#sec-update-expressions
|
|
@@ -418,38 +340,30 @@ export function cook(rootAst, codeSource) {
|
|
|
418
340
|
PutValue(lhs, newValue);
|
|
419
341
|
return NormalCompletion(node.prefix ? newValue : oldValue);
|
|
420
342
|
}
|
|
421
|
-
|
|
422
343
|
case "SwitchCase":
|
|
423
344
|
return EvaluateStatementList(node.consequent);
|
|
424
|
-
|
|
425
345
|
case "SwitchStatement":
|
|
426
346
|
{
|
|
427
347
|
// https://tc39.es/ecma262/#sec-switch-statement
|
|
428
348
|
var _exprRef = Evaluate(node.discriminant);
|
|
429
|
-
|
|
430
349
|
var switchValue = GetValue(_exprRef);
|
|
431
350
|
var _oldEnv = getRunningContext().LexicalEnvironment;
|
|
432
|
-
|
|
433
351
|
var _blockEnv = new DeclarativeEnvironment(_oldEnv);
|
|
434
|
-
|
|
435
352
|
BlockDeclarationInstantiation(node.cases, _blockEnv);
|
|
436
353
|
getRunningContext().LexicalEnvironment = _blockEnv;
|
|
437
354
|
var R = CaseBlockEvaluation(node.cases, switchValue);
|
|
438
355
|
getRunningContext().LexicalEnvironment = _oldEnv;
|
|
439
356
|
return EvaluateBreakableStatement(R);
|
|
440
357
|
}
|
|
441
|
-
|
|
442
358
|
case "TryStatement":
|
|
443
359
|
{
|
|
444
360
|
// https://tc39.es/ecma262/#sec-try-statement
|
|
445
361
|
var _R;
|
|
446
|
-
|
|
447
362
|
try {
|
|
448
363
|
_R = Evaluate(node.block);
|
|
449
364
|
} catch (error) {
|
|
450
365
|
if (node.handler) {
|
|
451
366
|
var _hooks$beforeEvaluate2;
|
|
452
|
-
|
|
453
367
|
(_hooks$beforeEvaluate2 = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate2 === void 0 ? void 0 : _hooks$beforeEvaluate2.call(hooks, node.handler);
|
|
454
368
|
_R = CatchClauseEvaluation(node.handler, error);
|
|
455
369
|
} else {
|
|
@@ -458,21 +372,17 @@ export function cook(rootAst, codeSource) {
|
|
|
458
372
|
} finally {
|
|
459
373
|
if (node.finalizer) {
|
|
460
374
|
var F = Evaluate(node.finalizer);
|
|
461
|
-
|
|
462
375
|
if (F.Type !== "normal") {
|
|
463
376
|
_R = F;
|
|
464
377
|
}
|
|
465
378
|
}
|
|
466
379
|
}
|
|
467
|
-
|
|
468
380
|
return _R;
|
|
469
381
|
}
|
|
470
|
-
|
|
471
382
|
case "VariableDeclaration":
|
|
472
383
|
{
|
|
473
384
|
// https://tc39.es/ecma262/#sec-declarations-and-the-variable-statement
|
|
474
385
|
var _result3;
|
|
475
|
-
|
|
476
386
|
for (var declarator of node.declarations) {
|
|
477
387
|
if (!declarator.init) {
|
|
478
388
|
// Assert: a declarator without init is always an identifier.
|
|
@@ -480,219 +390,181 @@ export function cook(rootAst, codeSource) {
|
|
|
480
390
|
_result3 = NormalCompletion(Empty);
|
|
481
391
|
} else {
|
|
482
392
|
var _lhs = ResolveBinding(declarator.id.name);
|
|
483
|
-
|
|
484
393
|
_result3 = InitializeReferencedBinding(_lhs, undefined);
|
|
485
394
|
}
|
|
486
395
|
} else if (declarator.id.type === "Identifier") {
|
|
487
396
|
var bindingId = declarator.id.name;
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
397
|
+
var _lhs2 = ResolveBinding(bindingId);
|
|
398
|
+
// Todo: IsAnonymousFunctionDefinition(Initializer)
|
|
492
399
|
var rhs = Evaluate(declarator.init);
|
|
493
|
-
|
|
494
400
|
var _value = GetValue(rhs);
|
|
495
|
-
|
|
496
401
|
_result3 = node.kind === "var" ? PutValue(_lhs2, _value) : InitializeReferencedBinding(_lhs2, _value);
|
|
497
402
|
} else {
|
|
498
403
|
var _rhs = Evaluate(declarator.init);
|
|
499
|
-
|
|
500
404
|
var _rval3 = GetValue(_rhs);
|
|
501
|
-
|
|
502
405
|
_result3 = BindingInitialization(declarator.id, _rval3, node.kind === "var" ? undefined : getRunningContext().LexicalEnvironment);
|
|
503
406
|
}
|
|
504
407
|
}
|
|
505
|
-
|
|
506
408
|
return _result3;
|
|
507
409
|
}
|
|
508
|
-
|
|
509
410
|
case "WhileStatement":
|
|
510
411
|
// https://tc39.es/ecma262/#sec-while-statement
|
|
511
412
|
return EvaluateBreakableStatement(WhileLoopEvaluation(node));
|
|
512
413
|
}
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
|
|
414
|
+
}
|
|
415
|
+
// eslint-disable-next-line no-console
|
|
516
416
|
throw new SyntaxError("Unsupported node type `".concat(node.type, "`"));
|
|
517
|
-
}
|
|
518
|
-
|
|
417
|
+
}
|
|
519
418
|
|
|
419
|
+
// https://tc39.es/ecma262/#sec-execution-contexts
|
|
520
420
|
function getRunningContext() {
|
|
521
421
|
return executionContextStack[executionContextStack.length - 1];
|
|
522
|
-
}
|
|
523
|
-
|
|
422
|
+
}
|
|
524
423
|
|
|
424
|
+
// https://tc39.es/ecma262/#sec-resolvebinding
|
|
525
425
|
function ResolveBinding(name, env) {
|
|
526
426
|
if (!env) {
|
|
527
427
|
env = getRunningContext().LexicalEnvironment;
|
|
528
428
|
}
|
|
529
|
-
|
|
530
429
|
return GetIdentifierReference(env, name, true);
|
|
531
|
-
}
|
|
532
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-catchclauseevaluation
|
|
533
|
-
|
|
430
|
+
}
|
|
534
431
|
|
|
432
|
+
// Try statements.
|
|
433
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-catchclauseevaluation
|
|
535
434
|
function CatchClauseEvaluation(node, thrownValue) {
|
|
536
435
|
var oldEnv = getRunningContext().LexicalEnvironment;
|
|
537
436
|
var catchEnv = new DeclarativeEnvironment(oldEnv);
|
|
538
|
-
|
|
539
437
|
for (var argName of collectBoundNames(node.param)) {
|
|
540
438
|
catchEnv.CreateMutableBinding(argName, false);
|
|
541
439
|
}
|
|
542
|
-
|
|
543
440
|
getRunningContext().LexicalEnvironment = catchEnv;
|
|
544
441
|
BindingInitialization(node.param, thrownValue, catchEnv);
|
|
545
442
|
var B = Evaluate(node.body);
|
|
546
443
|
getRunningContext().LexicalEnvironment = oldEnv;
|
|
547
444
|
return B;
|
|
548
|
-
}
|
|
549
|
-
// https://tc39.es/ecma262/#prod-BreakableStatement
|
|
550
|
-
|
|
445
|
+
}
|
|
551
446
|
|
|
447
|
+
// Iteration statements and switch statements.
|
|
448
|
+
// https://tc39.es/ecma262/#prod-BreakableStatement
|
|
552
449
|
function EvaluateBreakableStatement(stmtResult) {
|
|
553
450
|
return stmtResult.Type === "break" ? stmtResult.Value === Empty ? NormalCompletion(undefined) : NormalCompletion(stmtResult.Value) : stmtResult;
|
|
554
|
-
}
|
|
555
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-caseblockevaluation
|
|
556
|
-
|
|
451
|
+
}
|
|
557
452
|
|
|
453
|
+
// Switch statements.
|
|
454
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-caseblockevaluation
|
|
558
455
|
function CaseBlockEvaluation(cases, input) {
|
|
559
456
|
var V;
|
|
560
457
|
var defaultCaseIndex = cases.findIndex(switchCase => !switchCase.test);
|
|
561
458
|
var hasDefaultCase = defaultCaseIndex >= 0;
|
|
562
459
|
var A = hasDefaultCase ? cases.slice(0, defaultCaseIndex) : cases;
|
|
563
460
|
var found = false;
|
|
564
|
-
|
|
565
461
|
for (var C of A) {
|
|
566
462
|
if (!found) {
|
|
567
463
|
found = CaseClauseIsSelected(C, input);
|
|
568
464
|
}
|
|
569
|
-
|
|
570
465
|
if (found) {
|
|
571
466
|
var _R2 = Evaluate(C);
|
|
572
|
-
|
|
573
467
|
if (_R2.Value !== Empty) {
|
|
574
468
|
V = _R2.Value;
|
|
575
469
|
}
|
|
576
|
-
|
|
577
470
|
if (_R2.Type !== "normal") {
|
|
578
471
|
return UpdateEmpty(_R2, V);
|
|
579
472
|
}
|
|
580
473
|
}
|
|
581
474
|
}
|
|
582
|
-
|
|
583
475
|
if (!hasDefaultCase) {
|
|
584
476
|
return NormalCompletion(V);
|
|
585
477
|
}
|
|
586
|
-
|
|
587
478
|
var foundInB = false;
|
|
588
479
|
var B = cases.slice(defaultCaseIndex + 1);
|
|
589
|
-
|
|
590
480
|
if (!found) {
|
|
591
481
|
for (var _C of B) {
|
|
592
482
|
if (!foundInB) {
|
|
593
483
|
foundInB = CaseClauseIsSelected(_C, input);
|
|
594
484
|
}
|
|
595
|
-
|
|
596
485
|
if (foundInB) {
|
|
597
486
|
var _R3 = Evaluate(_C);
|
|
598
|
-
|
|
599
487
|
if (_R3.Value !== Empty) {
|
|
600
488
|
V = _R3.Value;
|
|
601
489
|
}
|
|
602
|
-
|
|
603
490
|
if (_R3.Type !== "normal") {
|
|
604
491
|
return UpdateEmpty(_R3, V);
|
|
605
492
|
}
|
|
606
493
|
}
|
|
607
494
|
}
|
|
608
495
|
}
|
|
609
|
-
|
|
610
496
|
if (foundInB) {
|
|
611
497
|
return NormalCompletion(V);
|
|
612
498
|
}
|
|
613
|
-
|
|
614
499
|
var R = Evaluate(cases[defaultCaseIndex]);
|
|
615
|
-
|
|
616
500
|
if (R.Value !== Empty) {
|
|
617
501
|
V = R.Value;
|
|
618
502
|
}
|
|
619
|
-
|
|
620
503
|
if (R.Type !== "normal") {
|
|
621
504
|
return UpdateEmpty(R, V);
|
|
622
|
-
}
|
|
623
|
-
|
|
505
|
+
}
|
|
624
506
|
|
|
507
|
+
// NOTE: The following is another complete iteration of the second CaseClauses.
|
|
625
508
|
for (var _C2 of B) {
|
|
626
509
|
var _R4 = Evaluate(_C2);
|
|
627
|
-
|
|
628
510
|
if (_R4.Value !== Empty) {
|
|
629
511
|
V = _R4.Value;
|
|
630
512
|
}
|
|
631
|
-
|
|
632
513
|
if (_R4.Type !== "normal") {
|
|
633
514
|
return UpdateEmpty(_R4, V);
|
|
634
515
|
}
|
|
635
516
|
}
|
|
636
|
-
|
|
637
517
|
return NormalCompletion(V);
|
|
638
|
-
}
|
|
639
|
-
|
|
518
|
+
}
|
|
640
519
|
|
|
520
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-caseclauseisselected
|
|
641
521
|
function CaseClauseIsSelected(C, input) {
|
|
642
522
|
var clauseSelector = GetValue(Evaluate(C.test));
|
|
643
523
|
return input === clauseSelector;
|
|
644
|
-
}
|
|
645
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-whileloopevaluation
|
|
646
|
-
|
|
524
|
+
}
|
|
647
525
|
|
|
526
|
+
// While statements.
|
|
527
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-whileloopevaluation
|
|
648
528
|
function WhileLoopEvaluation(node) {
|
|
649
|
-
var V;
|
|
650
|
-
|
|
529
|
+
var V;
|
|
530
|
+
// eslint-disable-next-line no-constant-condition
|
|
651
531
|
while (true) {
|
|
652
532
|
var exprValue = GetValue(Evaluate(node.test));
|
|
653
|
-
|
|
654
533
|
if (!exprValue) {
|
|
655
534
|
return NormalCompletion(V);
|
|
656
535
|
}
|
|
657
|
-
|
|
658
536
|
var stmtResult = Evaluate(node.body);
|
|
659
|
-
|
|
660
537
|
if (!LoopContinues(stmtResult)) {
|
|
661
538
|
return UpdateEmpty(stmtResult, V);
|
|
662
539
|
}
|
|
663
|
-
|
|
664
540
|
if (stmtResult.Value !== Empty) {
|
|
665
541
|
V = stmtResult.Value;
|
|
666
542
|
}
|
|
667
543
|
}
|
|
668
|
-
}
|
|
669
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-dowhileloopevaluation
|
|
670
|
-
|
|
544
|
+
}
|
|
671
545
|
|
|
546
|
+
// Do-while Statements.
|
|
547
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-dowhileloopevaluation
|
|
672
548
|
function DoWhileLoopEvaluation(node) {
|
|
673
|
-
var V;
|
|
674
|
-
|
|
549
|
+
var V;
|
|
550
|
+
// eslint-disable-next-line no-constant-condition
|
|
675
551
|
while (true) {
|
|
676
552
|
var stmtResult = Evaluate(node.body);
|
|
677
|
-
|
|
678
553
|
if (!LoopContinues(stmtResult)) {
|
|
679
554
|
return UpdateEmpty(stmtResult, V);
|
|
680
555
|
}
|
|
681
|
-
|
|
682
556
|
if (stmtResult.Value !== Empty) {
|
|
683
557
|
V = stmtResult.Value;
|
|
684
558
|
}
|
|
685
|
-
|
|
686
559
|
var exprValue = GetValue(Evaluate(node.test));
|
|
687
|
-
|
|
688
560
|
if (!exprValue) {
|
|
689
561
|
return NormalCompletion(V);
|
|
690
562
|
}
|
|
691
563
|
}
|
|
692
|
-
}
|
|
693
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-forinofloopevaluation
|
|
694
|
-
|
|
564
|
+
}
|
|
695
565
|
|
|
566
|
+
// For in/of statements.
|
|
567
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-forinofloopevaluation
|
|
696
568
|
function ForInOfLoopEvaluation(node) {
|
|
697
569
|
var lhs = node.left;
|
|
698
570
|
var isVariableDeclaration = lhs.type === "VariableDeclaration";
|
|
@@ -700,77 +572,62 @@ export function cook(rootAst, codeSource) {
|
|
|
700
572
|
var uninitializedBoundNames = lhsKind === "lexicalBinding" ? collectBoundNames(lhs) : [];
|
|
701
573
|
var iterationKind = node.type === "ForInStatement" ? "enumerate" : "iterate";
|
|
702
574
|
var keyResult = ForInOfHeadEvaluation(uninitializedBoundNames, node.right, iterationKind);
|
|
703
|
-
|
|
704
575
|
if (keyResult.Type !== "normal") {
|
|
705
576
|
// When enumerate, if the target is nil, a break completion will be returned.
|
|
706
577
|
return keyResult;
|
|
707
578
|
}
|
|
708
|
-
|
|
709
579
|
return ForInOfBodyEvaluation(lhs, node.body, keyResult.Value, iterationKind, lhsKind);
|
|
710
|
-
}
|
|
711
|
-
|
|
580
|
+
}
|
|
712
581
|
|
|
582
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-forinofheadevaluation
|
|
713
583
|
function ForInOfHeadEvaluation(uninitializedBoundNames, expr, iterationKind) {
|
|
714
584
|
var runningContext = getRunningContext();
|
|
715
585
|
var oldEnv = runningContext.LexicalEnvironment;
|
|
716
|
-
|
|
717
586
|
if (uninitializedBoundNames.length > 0) {
|
|
718
587
|
var newEnv = new DeclarativeEnvironment(oldEnv);
|
|
719
|
-
|
|
720
588
|
for (var name of uninitializedBoundNames) {
|
|
721
589
|
newEnv.CreateMutableBinding(name, false);
|
|
722
590
|
}
|
|
723
|
-
|
|
724
591
|
runningContext.LexicalEnvironment = newEnv;
|
|
725
592
|
}
|
|
726
|
-
|
|
727
593
|
var exprRef = Evaluate(expr);
|
|
728
594
|
runningContext.LexicalEnvironment = oldEnv;
|
|
729
595
|
var exprValue = GetValue(exprRef);
|
|
730
|
-
|
|
731
596
|
if (iterationKind === "enumerate") {
|
|
732
597
|
if (exprValue === null || exprValue === undefined) {
|
|
733
598
|
return new CompletionRecord("break", Empty);
|
|
734
599
|
}
|
|
735
|
-
|
|
736
600
|
var _iterator = EnumerateObjectProperties(exprValue);
|
|
737
|
-
|
|
738
601
|
return NormalCompletion(_iterator);
|
|
739
602
|
}
|
|
740
|
-
|
|
741
603
|
var iterator = CreateListIteratorRecord(exprValue);
|
|
742
604
|
return NormalCompletion(iterator);
|
|
743
605
|
}
|
|
744
|
-
|
|
745
606
|
function ForInOfBodyEvaluation(node, stmt, iteratorRecord, iterationKind, lhsKind) {
|
|
746
607
|
var lhs = lhsKind === "assignment" ? node : node.declarations[0].id;
|
|
747
608
|
var oldEnv = getRunningContext().LexicalEnvironment;
|
|
748
|
-
var V;
|
|
609
|
+
var V;
|
|
610
|
+
// When `destructuring` is false,
|
|
749
611
|
// For `node` whose `kind` is assignment:
|
|
750
612
|
// `lhs` is an `Identifier` or a `MemberExpression`,
|
|
751
613
|
// Otherwise:
|
|
752
614
|
// `lhs` is an `Identifier`.
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
615
|
+
var destructuring = lhs.type === "ObjectPattern" || lhs.type === "ArrayPattern";
|
|
616
|
+
// eslint-disable-next-line no-constant-condition
|
|
756
617
|
while (true) {
|
|
757
618
|
var {
|
|
758
619
|
done,
|
|
759
620
|
value: nextValue
|
|
760
621
|
} = iteratorRecord.next();
|
|
761
|
-
|
|
762
622
|
if (done) {
|
|
763
623
|
return NormalCompletion(V);
|
|
764
624
|
}
|
|
765
|
-
|
|
766
625
|
var lhsRef = void 0;
|
|
767
626
|
var iterationEnv = void 0;
|
|
768
|
-
|
|
769
627
|
if (lhsKind === "lexicalBinding") {
|
|
770
628
|
iterationEnv = new DeclarativeEnvironment(oldEnv);
|
|
771
629
|
ForDeclarationBindingInstantiation(node, iterationEnv);
|
|
772
630
|
getRunningContext().LexicalEnvironment = iterationEnv;
|
|
773
|
-
|
|
774
631
|
if (!destructuring) {
|
|
775
632
|
var [lhsName] = collectBoundNames(lhs);
|
|
776
633
|
lhsRef = ResolveBinding(lhsName);
|
|
@@ -778,58 +635,49 @@ export function cook(rootAst, codeSource) {
|
|
|
778
635
|
} else if (!destructuring) {
|
|
779
636
|
lhsRef = Evaluate(lhs).Value;
|
|
780
637
|
}
|
|
781
|
-
|
|
782
638
|
destructuring ? lhsKind === "assignment" ? DestructuringAssignmentEvaluation(lhs, nextValue) : lhsKind === "varBinding" ? BindingInitialization(lhs, nextValue, undefined) : BindingInitialization(lhs, nextValue, iterationEnv) : lhsKind === "lexicalBinding" ? InitializeReferencedBinding(lhsRef, nextValue) : PutValue(lhsRef, nextValue);
|
|
783
639
|
var result = Evaluate(stmt);
|
|
784
640
|
getRunningContext().LexicalEnvironment = oldEnv;
|
|
785
|
-
|
|
786
641
|
if (!LoopContinues(result)) {
|
|
787
642
|
var status = UpdateEmpty(result, V);
|
|
788
|
-
|
|
789
643
|
if (!(iterationKind === "enumerate" || iteratorRecord.return === undefined)) {
|
|
790
644
|
// Perform *IteratorClose*
|
|
791
645
|
// https://tc39.es/ecma262/#sec-iteratorclose
|
|
792
646
|
var innerResult = iteratorRecord.return();
|
|
793
|
-
|
|
794
647
|
if (!innerResult || !["object", "function"].includes(typeof innerResult)) {
|
|
795
648
|
throw new TypeError("Iterator result is not an object");
|
|
796
649
|
}
|
|
797
650
|
}
|
|
798
|
-
|
|
799
651
|
return status;
|
|
800
652
|
}
|
|
801
|
-
|
|
802
653
|
if (result.Value !== Empty) {
|
|
803
654
|
V = result.Value;
|
|
804
655
|
}
|
|
805
656
|
}
|
|
806
|
-
}
|
|
807
|
-
|
|
657
|
+
}
|
|
808
658
|
|
|
659
|
+
// https://tc39.es/ecma262/#sec-enumerate-object-properties
|
|
809
660
|
function* EnumerateObjectProperties(value) {
|
|
810
661
|
for (var _key in value) {
|
|
811
662
|
yield _key;
|
|
812
663
|
}
|
|
813
|
-
}
|
|
814
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-forloopevaluation
|
|
815
|
-
|
|
664
|
+
}
|
|
816
665
|
|
|
666
|
+
// For statements.
|
|
667
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-forloopevaluation
|
|
817
668
|
function ForLoopEvaluation(node) {
|
|
818
669
|
var _node$init;
|
|
819
|
-
|
|
820
670
|
if (((_node$init = node.init) === null || _node$init === void 0 ? void 0 : _node$init.type) === "VariableDeclaration") {
|
|
821
671
|
// `for (var … ; … ; … ) …`
|
|
822
672
|
if (node.init.kind === "var") {
|
|
823
673
|
Evaluate(node.init);
|
|
824
674
|
return ForBodyEvaluation(node.test, node.update, node.body, []);
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
|
|
675
|
+
}
|
|
676
|
+
// `for (let/const … ; … ; … ) …`
|
|
828
677
|
var oldEnv = getRunningContext().LexicalEnvironment;
|
|
829
678
|
var loopEnv = new DeclarativeEnvironment(oldEnv);
|
|
830
679
|
var isConst = node.init.kind === "const";
|
|
831
680
|
var boundNames = collectBoundNames(node.init);
|
|
832
|
-
|
|
833
681
|
for (var dn of boundNames) {
|
|
834
682
|
if (isConst) {
|
|
835
683
|
loopEnv.CreateImmutableBinding(dn, true);
|
|
@@ -837,113 +685,94 @@ export function cook(rootAst, codeSource) {
|
|
|
837
685
|
loopEnv.CreateMutableBinding(dn, false);
|
|
838
686
|
}
|
|
839
687
|
}
|
|
840
|
-
|
|
841
688
|
getRunningContext().LexicalEnvironment = loopEnv;
|
|
842
689
|
Evaluate(node.init);
|
|
843
690
|
var perIterationLets = isConst ? [] : Array.from(boundNames);
|
|
844
691
|
var bodyResult = ForBodyEvaluation(node.test, node.update, node.body, perIterationLets);
|
|
845
692
|
getRunningContext().LexicalEnvironment = oldEnv;
|
|
846
693
|
return bodyResult;
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
|
|
694
|
+
}
|
|
695
|
+
// `for ( … ; … ; … ) …`
|
|
850
696
|
if (node.init) {
|
|
851
697
|
var exprRef = Evaluate(node.init);
|
|
852
698
|
GetValue(exprRef);
|
|
853
699
|
}
|
|
854
|
-
|
|
855
700
|
return ForBodyEvaluation(node.test, node.update, node.body, []);
|
|
856
|
-
}
|
|
857
|
-
|
|
701
|
+
}
|
|
858
702
|
|
|
703
|
+
// https://tc39.es/ecma262/#sec-forbodyevaluation
|
|
859
704
|
function ForBodyEvaluation(test, increment, stmt, perIterationBindings) {
|
|
860
705
|
CreatePerIterationEnvironment(perIterationBindings);
|
|
861
|
-
var V;
|
|
862
|
-
|
|
706
|
+
var V;
|
|
707
|
+
// eslint-disable-next-line no-constant-condition
|
|
863
708
|
while (true) {
|
|
864
709
|
if (test) {
|
|
865
710
|
var testRef = Evaluate(test);
|
|
866
711
|
var testValue = GetValue(testRef);
|
|
867
|
-
|
|
868
712
|
if (!testValue) {
|
|
869
713
|
return NormalCompletion(V);
|
|
870
714
|
}
|
|
871
715
|
}
|
|
872
|
-
|
|
873
716
|
var result = Evaluate(stmt);
|
|
874
|
-
|
|
875
717
|
if (!LoopContinues(result)) {
|
|
876
718
|
return UpdateEmpty(result, V);
|
|
877
719
|
}
|
|
878
|
-
|
|
879
720
|
if (result.Value) {
|
|
880
721
|
V = result.Value;
|
|
881
722
|
}
|
|
882
|
-
|
|
883
723
|
CreatePerIterationEnvironment(perIterationBindings);
|
|
884
|
-
|
|
885
724
|
if (increment) {
|
|
886
725
|
var incRef = Evaluate(increment);
|
|
887
726
|
GetValue(incRef);
|
|
888
727
|
}
|
|
889
728
|
}
|
|
890
|
-
}
|
|
891
|
-
|
|
729
|
+
}
|
|
892
730
|
|
|
731
|
+
// https://tc39.es/ecma262/#sec-createperiterationenvironment
|
|
893
732
|
function CreatePerIterationEnvironment(perIterationBindings) {
|
|
894
733
|
if (perIterationBindings.length === 0) {
|
|
895
734
|
return;
|
|
896
735
|
}
|
|
897
|
-
|
|
898
736
|
var lastIterationEnv = getRunningContext().LexicalEnvironment;
|
|
899
737
|
var outer = lastIterationEnv.OuterEnv;
|
|
900
738
|
var thisIterationEnv = new DeclarativeEnvironment(outer);
|
|
901
|
-
|
|
902
739
|
for (var bn of perIterationBindings) {
|
|
903
740
|
thisIterationEnv.CreateMutableBinding(bn, false);
|
|
904
741
|
var lastValue = lastIterationEnv.GetBindingValue(bn, false);
|
|
905
742
|
thisIterationEnv.InitializeBinding(bn, lastValue);
|
|
906
743
|
}
|
|
907
|
-
|
|
908
744
|
getRunningContext().LexicalEnvironment = thisIterationEnv;
|
|
909
|
-
}
|
|
910
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-destructuringassignmentevaluation
|
|
911
|
-
|
|
745
|
+
}
|
|
912
746
|
|
|
747
|
+
// Destructuring assignments.
|
|
748
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-destructuringassignmentevaluation
|
|
913
749
|
function DestructuringAssignmentEvaluation(pattern, value) {
|
|
914
750
|
if (pattern.type === "ObjectPattern") {
|
|
915
751
|
RequireObjectCoercible(value);
|
|
916
|
-
|
|
917
752
|
if (pattern.properties.length > 0) {
|
|
918
753
|
PropertyDestructuringAssignmentEvaluation(pattern.properties, value);
|
|
919
754
|
}
|
|
920
|
-
|
|
921
755
|
return NormalCompletion(Empty);
|
|
922
756
|
}
|
|
923
|
-
|
|
924
757
|
var iteratorRecord = CreateListIteratorRecord(value);
|
|
925
758
|
return IteratorDestructuringAssignmentEvaluation(pattern.elements, iteratorRecord);
|
|
926
|
-
}
|
|
927
|
-
|
|
759
|
+
}
|
|
928
760
|
|
|
761
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-propertydestructuringassignmentevaluation
|
|
929
762
|
function PropertyDestructuringAssignmentEvaluation(properties, value) {
|
|
930
763
|
var excludedNames = new Set();
|
|
931
|
-
|
|
932
764
|
for (var prop of properties) {
|
|
933
765
|
if (prop.type === "Property") {
|
|
934
766
|
var propName = !prop.computed && prop.key.type === "Identifier" ? prop.key.name : EvaluateComputedPropertyName(prop.key);
|
|
935
767
|
var valueTarget = prop.value.type === "AssignmentPattern" ? prop.value.left : prop.value;
|
|
936
|
-
|
|
937
768
|
if (valueTarget.type === "Identifier") {
|
|
938
769
|
var lref = ResolveBinding(valueTarget.name);
|
|
939
770
|
var v = GetV(value, propName);
|
|
940
|
-
|
|
941
771
|
if (prop.value.type === "AssignmentPattern" && v === undefined) {
|
|
942
772
|
// Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
|
|
943
773
|
var defaultValue = Evaluate(prop.value.right);
|
|
944
774
|
v = GetValue(defaultValue);
|
|
945
775
|
}
|
|
946
|
-
|
|
947
776
|
PutValue(lref, v);
|
|
948
777
|
excludedNames.add(propName);
|
|
949
778
|
} else {
|
|
@@ -954,21 +783,18 @@ export function cook(rootAst, codeSource) {
|
|
|
954
783
|
RestDestructuringAssignmentEvaluation(prop, value, excludedNames);
|
|
955
784
|
}
|
|
956
785
|
}
|
|
957
|
-
}
|
|
958
|
-
|
|
786
|
+
}
|
|
959
787
|
|
|
788
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-keyeddestructuringassignmentevaluation
|
|
960
789
|
function KeyedDestructuringAssignmentEvaluation(node, value, propertyName) {
|
|
961
790
|
var assignmentTarget = node.type === "AssignmentPattern" ? node.left : node;
|
|
962
791
|
var isObjectOrArray = assignmentTarget.type === "ArrayPattern" || assignmentTarget.type === "ObjectPattern";
|
|
963
792
|
var lref;
|
|
964
|
-
|
|
965
793
|
if (!isObjectOrArray) {
|
|
966
794
|
lref = Evaluate(assignmentTarget).Value;
|
|
967
795
|
}
|
|
968
|
-
|
|
969
796
|
var v = GetV(value, propertyName);
|
|
970
797
|
var rhsValue;
|
|
971
|
-
|
|
972
798
|
if (node.type === "AssignmentPattern" && v === undefined) {
|
|
973
799
|
// Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
|
|
974
800
|
var defaultValue = Evaluate(node.right);
|
|
@@ -976,50 +802,41 @@ export function cook(rootAst, codeSource) {
|
|
|
976
802
|
} else {
|
|
977
803
|
rhsValue = v;
|
|
978
804
|
}
|
|
979
|
-
|
|
980
805
|
if (isObjectOrArray) {
|
|
981
806
|
return DestructuringAssignmentEvaluation(assignmentTarget, rhsValue);
|
|
982
807
|
}
|
|
983
|
-
|
|
984
808
|
return PutValue(lref, rhsValue);
|
|
985
|
-
}
|
|
986
|
-
|
|
809
|
+
}
|
|
987
810
|
|
|
811
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-restdestructuringassignmentevaluation
|
|
988
812
|
function RestDestructuringAssignmentEvaluation(restProperty, value, excludedNames) {
|
|
989
813
|
var lref = Evaluate(restProperty.argument).Value;
|
|
990
814
|
var restObj = CopyDataProperties({}, value, excludedNames);
|
|
991
815
|
return PutValue(lref, restObj);
|
|
992
|
-
}
|
|
993
|
-
|
|
816
|
+
}
|
|
994
817
|
|
|
818
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-iteratordestructuringassignmentevaluation
|
|
995
819
|
function IteratorDestructuringAssignmentEvaluation(elements, iteratorRecord) {
|
|
996
820
|
var status = NormalCompletion(Empty);
|
|
997
|
-
|
|
998
821
|
for (var element of elements) {
|
|
999
822
|
if (!element) {
|
|
1000
823
|
iteratorRecord.next();
|
|
1001
824
|
status = NormalCompletion(Empty);
|
|
1002
825
|
continue;
|
|
1003
826
|
}
|
|
1004
|
-
|
|
1005
827
|
var assignmentTarget = element.type === "RestElement" ? element.argument : element.type === "AssignmentPattern" ? element.left : element;
|
|
1006
828
|
var isObjectOrArray = assignmentTarget.type === "ArrayPattern" || assignmentTarget.type === "ObjectPattern";
|
|
1007
829
|
var lref = void 0;
|
|
1008
|
-
|
|
1009
830
|
if (!isObjectOrArray) {
|
|
1010
831
|
lref = Evaluate(assignmentTarget).Value;
|
|
1011
832
|
}
|
|
1012
|
-
|
|
1013
833
|
var v = void 0;
|
|
1014
|
-
|
|
1015
834
|
if (element.type !== "RestElement") {
|
|
1016
835
|
var {
|
|
1017
836
|
done,
|
|
1018
837
|
value: nextValue
|
|
1019
838
|
} = iteratorRecord.next();
|
|
1020
|
-
|
|
1021
839
|
var _value2 = done ? undefined : nextValue;
|
|
1022
|
-
|
|
1023
840
|
if (element.type === "AssignmentPattern" && _value2 === undefined) {
|
|
1024
841
|
// Todo(steve): check IsAnonymousFunctionDefinition(Initializer)
|
|
1025
842
|
var defaultValue = Evaluate(element.right);
|
|
@@ -1030,59 +847,53 @@ export function cook(rootAst, codeSource) {
|
|
|
1030
847
|
} else {
|
|
1031
848
|
// RestElement
|
|
1032
849
|
v = [];
|
|
1033
|
-
var n = 0;
|
|
1034
|
-
|
|
850
|
+
var n = 0;
|
|
851
|
+
// eslint-disable-next-line no-constant-condition
|
|
1035
852
|
while (true) {
|
|
1036
853
|
var {
|
|
1037
854
|
done: _done,
|
|
1038
855
|
value: _nextValue
|
|
1039
856
|
} = iteratorRecord.next();
|
|
1040
|
-
|
|
1041
857
|
if (_done) {
|
|
1042
858
|
break;
|
|
1043
859
|
}
|
|
1044
|
-
|
|
1045
860
|
v[n] = _nextValue;
|
|
1046
861
|
n++;
|
|
1047
862
|
}
|
|
1048
863
|
}
|
|
1049
|
-
|
|
1050
864
|
if (isObjectOrArray) {
|
|
1051
865
|
status = DestructuringAssignmentEvaluation(assignmentTarget, v);
|
|
1052
866
|
} else {
|
|
1053
867
|
status = PutValue(lref, v);
|
|
1054
868
|
}
|
|
1055
869
|
}
|
|
1056
|
-
|
|
1057
870
|
return status;
|
|
1058
|
-
}
|
|
1059
|
-
// https://tc39.es/ecma262/#sec-evaluate-property-access-with-expression-key
|
|
1060
|
-
|
|
871
|
+
}
|
|
1061
872
|
|
|
873
|
+
// Object expressions.
|
|
874
|
+
// https://tc39.es/ecma262/#sec-evaluate-property-access-with-expression-key
|
|
1062
875
|
function EvaluatePropertyAccessWithExpressionKey(baseValue, expression, strict) {
|
|
1063
876
|
var propertyNameReference = Evaluate(expression);
|
|
1064
877
|
var propertyNameValue = GetValue(propertyNameReference);
|
|
1065
878
|
var propertyKey = ToPropertyKey(propertyNameValue);
|
|
1066
879
|
return new ReferenceRecord(baseValue, propertyKey, strict);
|
|
1067
|
-
}
|
|
1068
|
-
|
|
880
|
+
}
|
|
1069
881
|
|
|
882
|
+
// https://tc39.es/ecma262/#sec-evaluate-property-access-with-identifier-key
|
|
1070
883
|
function EvaluatePropertyAccessWithIdentifierKey(baseValue, identifier, strict) {
|
|
1071
884
|
var propertyNameString = identifier.name;
|
|
1072
885
|
return new ReferenceRecord(baseValue, propertyNameString, strict);
|
|
1073
|
-
}
|
|
1074
|
-
// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
|
|
1075
|
-
|
|
886
|
+
}
|
|
1076
887
|
|
|
888
|
+
// Block statements.
|
|
889
|
+
// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
|
|
1077
890
|
function BlockDeclarationInstantiation(code, env) {
|
|
1078
891
|
var declarations = collectScopedDeclarations(code, {
|
|
1079
892
|
var: false,
|
|
1080
893
|
topLevel: false
|
|
1081
894
|
});
|
|
1082
|
-
|
|
1083
895
|
for (var d of declarations) {
|
|
1084
896
|
var IsConstantDeclaration = d.type === "VariableDeclaration" && d.kind === "const";
|
|
1085
|
-
|
|
1086
897
|
for (var dn of collectBoundNames(d)) {
|
|
1087
898
|
if (IsConstantDeclaration) {
|
|
1088
899
|
env.CreateImmutableBinding(dn, true);
|
|
@@ -1090,64 +901,52 @@ export function cook(rootAst, codeSource) {
|
|
|
1090
901
|
env.CreateMutableBinding(dn, false);
|
|
1091
902
|
}
|
|
1092
903
|
}
|
|
1093
|
-
|
|
1094
904
|
if (d.type === "FunctionDeclaration") {
|
|
1095
905
|
var [_fn] = collectBoundNames(d);
|
|
1096
|
-
|
|
1097
906
|
var _fo = InstantiateFunctionObject(d, env);
|
|
1098
|
-
|
|
1099
907
|
env.InitializeBinding(_fn, _fo);
|
|
1100
908
|
}
|
|
1101
909
|
}
|
|
1102
|
-
}
|
|
1103
|
-
// https://tc39.es/ecma262/#sec-evaluatecall
|
|
1104
|
-
|
|
910
|
+
}
|
|
1105
911
|
|
|
912
|
+
// Function declarations and expressions.
|
|
913
|
+
// https://tc39.es/ecma262/#sec-evaluatecall
|
|
1106
914
|
function EvaluateCall(func, ref, args, callee) {
|
|
1107
915
|
var thisValue;
|
|
1108
|
-
|
|
1109
916
|
if (ref instanceof ReferenceRecord) {
|
|
1110
917
|
if (IsPropertyReference(ref)) {
|
|
1111
918
|
thisValue = ref.Base;
|
|
1112
919
|
}
|
|
1113
920
|
}
|
|
1114
|
-
|
|
1115
921
|
var argList = ArgumentListEvaluation(args);
|
|
1116
|
-
|
|
1117
922
|
if (typeof func !== "function") {
|
|
1118
923
|
var funcName = codeSource.substring(callee.start, callee.end);
|
|
1119
924
|
throw new TypeError("".concat(funcName, " is not a function"));
|
|
1120
925
|
}
|
|
1121
|
-
|
|
1122
926
|
var result = func.apply(thisValue, argList);
|
|
1123
927
|
sanitize(result);
|
|
1124
928
|
return NormalCompletion(result);
|
|
1125
|
-
}
|
|
1126
|
-
|
|
929
|
+
}
|
|
1127
930
|
|
|
931
|
+
// https://tc39.es/ecma262/#sec-evaluatenew
|
|
1128
932
|
function EvaluateNew(constructExpr, args) {
|
|
1129
933
|
var ref = Evaluate(constructExpr);
|
|
1130
934
|
var constructor = GetValue(ref);
|
|
1131
935
|
var argList = ArgumentListEvaluation(args);
|
|
1132
|
-
|
|
1133
936
|
if (typeof constructor !== "function" || constructor[IsConstructor] === false) {
|
|
1134
937
|
var constructorName = codeSource.substring(constructExpr.start, constructExpr.end);
|
|
1135
938
|
throw new TypeError("".concat(constructorName, " is not a constructor"));
|
|
1136
939
|
}
|
|
1137
|
-
|
|
1138
940
|
if (!isAllowedConstructor(constructor)) {
|
|
1139
941
|
var _constructorName = codeSource.substring(constructExpr.start, constructExpr.end);
|
|
1140
|
-
|
|
1141
942
|
throw new TypeError("".concat(_constructorName, " is not an allowed constructor"));
|
|
1142
943
|
}
|
|
1143
|
-
|
|
1144
944
|
return NormalCompletion(new constructor(...argList));
|
|
1145
|
-
}
|
|
1146
|
-
|
|
945
|
+
}
|
|
1147
946
|
|
|
947
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation
|
|
1148
948
|
function ArgumentListEvaluation(args) {
|
|
1149
949
|
var array = [];
|
|
1150
|
-
|
|
1151
950
|
if (Array.isArray(args)) {
|
|
1152
951
|
for (var arg of args) {
|
|
1153
952
|
if (arg.type === "SpreadElement") {
|
|
@@ -1159,32 +958,27 @@ export function cook(rootAst, codeSource) {
|
|
|
1159
958
|
}
|
|
1160
959
|
} else {
|
|
1161
960
|
array.push(GetTemplateObject(args));
|
|
1162
|
-
|
|
1163
961
|
for (var expr of args.expressions) {
|
|
1164
962
|
array.push(GetValue(Evaluate(expr)));
|
|
1165
963
|
}
|
|
1166
964
|
}
|
|
1167
|
-
|
|
1168
965
|
return array;
|
|
1169
|
-
}
|
|
1170
|
-
|
|
966
|
+
}
|
|
1171
967
|
|
|
968
|
+
// https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
|
|
1172
969
|
function CallFunction(closure, args) {
|
|
1173
970
|
var _hooks$beforeCall;
|
|
1174
|
-
|
|
1175
971
|
(_hooks$beforeCall = hooks.beforeCall) === null || _hooks$beforeCall === void 0 ? void 0 : _hooks$beforeCall.call(hooks, closure[SourceNode]);
|
|
1176
972
|
PrepareForOrdinaryCall(closure);
|
|
1177
973
|
var result = OrdinaryCallEvaluateBody(closure, args);
|
|
1178
974
|
executionContextStack.pop();
|
|
1179
|
-
|
|
1180
975
|
if (result.Type === "return") {
|
|
1181
976
|
return result.Value;
|
|
1182
977
|
}
|
|
1183
|
-
|
|
1184
978
|
return undefined;
|
|
1185
|
-
}
|
|
1186
|
-
|
|
979
|
+
}
|
|
1187
980
|
|
|
981
|
+
// https://tc39.es/ecma262/#sec-prepareforordinarycall
|
|
1188
982
|
function PrepareForOrdinaryCall(F) {
|
|
1189
983
|
var calleeContext = new ExecutionContext();
|
|
1190
984
|
calleeContext.Function = F;
|
|
@@ -1193,42 +987,36 @@ export function cook(rootAst, codeSource) {
|
|
|
1193
987
|
calleeContext.LexicalEnvironment = localEnv;
|
|
1194
988
|
executionContextStack.push(calleeContext);
|
|
1195
989
|
return calleeContext;
|
|
1196
|
-
}
|
|
1197
|
-
|
|
990
|
+
}
|
|
1198
991
|
|
|
992
|
+
// https://tc39.es/ecma262/#sec-ordinarycallevaluatebody
|
|
1199
993
|
function OrdinaryCallEvaluateBody(F, args) {
|
|
1200
994
|
return EvaluateFunctionBody(F[ECMAScriptCode], F, args);
|
|
1201
|
-
}
|
|
1202
|
-
|
|
995
|
+
}
|
|
1203
996
|
|
|
997
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-evaluatefunctionbody
|
|
1204
998
|
function EvaluateFunctionBody(body, F, args) {
|
|
1205
999
|
FunctionDeclarationInstantiation(F, args);
|
|
1206
|
-
|
|
1207
1000
|
if (Array.isArray(body)) {
|
|
1208
1001
|
return EvaluateStatementList(body);
|
|
1209
1002
|
}
|
|
1210
|
-
|
|
1211
1003
|
return new CompletionRecord("return", GetValue(Evaluate(body)));
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1004
|
+
}
|
|
1214
1005
|
|
|
1006
|
+
// https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
|
|
1215
1007
|
function EvaluateStatementList(statements) {
|
|
1216
1008
|
var result = NormalCompletion(Empty);
|
|
1217
|
-
|
|
1218
1009
|
for (var stmt of statements) {
|
|
1219
1010
|
var s = Evaluate(stmt);
|
|
1220
|
-
|
|
1221
1011
|
if (s.Type !== "normal") {
|
|
1222
1012
|
return s;
|
|
1223
1013
|
}
|
|
1224
|
-
|
|
1225
1014
|
result = UpdateEmpty(result, s.Value);
|
|
1226
1015
|
}
|
|
1227
|
-
|
|
1228
1016
|
return result;
|
|
1229
|
-
}
|
|
1230
|
-
|
|
1017
|
+
}
|
|
1231
1018
|
|
|
1019
|
+
// https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
|
|
1232
1020
|
function FunctionDeclarationInstantiation(func, args) {
|
|
1233
1021
|
var calleeContext = getRunningContext();
|
|
1234
1022
|
var code = func[ECMAScriptCode];
|
|
@@ -1239,19 +1027,17 @@ export function cook(rootAst, codeSource) {
|
|
|
1239
1027
|
var: true,
|
|
1240
1028
|
topLevel: true
|
|
1241
1029
|
});
|
|
1242
|
-
var varNames = collectBoundNames(varDeclarations);
|
|
1243
|
-
// `functionsToInitialize` ≈ `functionNames`
|
|
1030
|
+
var varNames = collectBoundNames(varDeclarations);
|
|
1244
1031
|
|
|
1032
|
+
// `functionNames` ∈ `varNames`
|
|
1033
|
+
// `functionsToInitialize` ≈ `functionNames`
|
|
1245
1034
|
var functionNames = [];
|
|
1246
1035
|
var functionsToInitialize = [];
|
|
1247
|
-
|
|
1248
1036
|
for (var i = varDeclarations.length - 1; i >= 0; i--) {
|
|
1249
1037
|
var d = varDeclarations[i];
|
|
1250
|
-
|
|
1251
1038
|
if (d.type === "FunctionDeclaration") {
|
|
1252
1039
|
ThrowIfFunctionIsInvalid(d);
|
|
1253
1040
|
var [_fn2] = collectBoundNames(d);
|
|
1254
|
-
|
|
1255
1041
|
if (!functionNames.includes(_fn2)) {
|
|
1256
1042
|
functionNames.unshift(_fn2);
|
|
1257
1043
|
functionsToInitialize.unshift(d);
|
|
@@ -1260,18 +1046,14 @@ export function cook(rootAst, codeSource) {
|
|
|
1260
1046
|
throw new SyntaxError("Var declaration is not recommended, use `let` or `const` instead");
|
|
1261
1047
|
}
|
|
1262
1048
|
}
|
|
1263
|
-
|
|
1264
1049
|
var env = calleeContext.LexicalEnvironment;
|
|
1265
|
-
|
|
1266
1050
|
for (var paramName of parameterNames) {
|
|
1267
1051
|
// In strict mode, it's guaranteed no duplicate params exist.
|
|
1268
1052
|
env.CreateMutableBinding(paramName, false);
|
|
1269
1053
|
}
|
|
1270
|
-
|
|
1271
1054
|
var iteratorRecord = CreateListIteratorRecord(args);
|
|
1272
1055
|
IteratorBindingInitialization(formals, iteratorRecord, env);
|
|
1273
1056
|
var varEnv;
|
|
1274
|
-
|
|
1275
1057
|
if (!hasParameterExpressions) {
|
|
1276
1058
|
// NOTE: Only a single Environment Record is needed for the parameters
|
|
1277
1059
|
// and top-level vars.
|
|
@@ -1282,24 +1064,22 @@ export function cook(rootAst, codeSource) {
|
|
|
1282
1064
|
env.InitializeBinding(n, undefined);
|
|
1283
1065
|
}
|
|
1284
1066
|
}
|
|
1285
|
-
|
|
1286
1067
|
varEnv = env;
|
|
1287
1068
|
} else {
|
|
1288
1069
|
// NOTE: A separate Environment Record is needed to ensure that closures
|
|
1289
1070
|
// created by expressions in the formal parameter list do not have
|
|
1290
1071
|
// visibility of declarations in the function body.
|
|
1291
1072
|
varEnv = new DeclarativeEnvironment(env);
|
|
1292
|
-
calleeContext.VariableEnvironment = varEnv;
|
|
1293
|
-
|
|
1073
|
+
calleeContext.VariableEnvironment = varEnv;
|
|
1074
|
+
// `varNames` are unique.
|
|
1294
1075
|
for (var _n of varNames) {
|
|
1295
1076
|
varEnv.CreateMutableBinding(_n, false);
|
|
1296
1077
|
var initialValue = void 0;
|
|
1297
|
-
|
|
1298
1078
|
if (parameterNames.includes(_n) && !functionNames.includes(_n)) {
|
|
1299
1079
|
initialValue = env.GetBindingValue(_n, false);
|
|
1300
1080
|
}
|
|
1301
|
-
|
|
1302
|
-
|
|
1081
|
+
varEnv.InitializeBinding(_n, initialValue);
|
|
1082
|
+
// NOTE: A var with the same name as a formal parameter initially has
|
|
1303
1083
|
// the same value as the corresponding initialized parameter.
|
|
1304
1084
|
}
|
|
1305
1085
|
}
|
|
@@ -1310,7 +1090,6 @@ export function cook(rootAst, codeSource) {
|
|
|
1310
1090
|
var: false,
|
|
1311
1091
|
topLevel: true
|
|
1312
1092
|
});
|
|
1313
|
-
|
|
1314
1093
|
for (var _d of lexDeclarations) {
|
|
1315
1094
|
for (var dn of collectBoundNames(_d)) {
|
|
1316
1095
|
// Only lexical VariableDeclaration here in top-level.
|
|
@@ -1321,25 +1100,21 @@ export function cook(rootAst, codeSource) {
|
|
|
1321
1100
|
}
|
|
1322
1101
|
}
|
|
1323
1102
|
}
|
|
1324
|
-
|
|
1325
1103
|
for (var f of functionsToInitialize) {
|
|
1326
1104
|
var [_fn3] = collectBoundNames(f);
|
|
1327
|
-
|
|
1328
1105
|
var _fo2 = InstantiateFunctionObject(f, lexEnv);
|
|
1329
|
-
|
|
1330
1106
|
varEnv.SetMutableBinding(_fn3, _fo2, false);
|
|
1331
1107
|
}
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1108
|
+
}
|
|
1334
1109
|
|
|
1110
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
|
|
1335
1111
|
function InstantiateFunctionObject(func, scope) {
|
|
1336
1112
|
return OrdinaryFunctionCreate(func, scope, true);
|
|
1337
|
-
}
|
|
1338
|
-
|
|
1113
|
+
}
|
|
1339
1114
|
|
|
1115
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-instantiateordinaryfunctionexpression
|
|
1340
1116
|
function InstantiateOrdinaryFunctionExpression(functionExpression) {
|
|
1341
1117
|
var scope = getRunningContext().LexicalEnvironment;
|
|
1342
|
-
|
|
1343
1118
|
if (functionExpression.id) {
|
|
1344
1119
|
var name = functionExpression.id.name;
|
|
1345
1120
|
var funcEnv = new DeclarativeEnvironment(scope);
|
|
@@ -1349,25 +1124,23 @@ export function cook(rootAst, codeSource) {
|
|
|
1349
1124
|
return closure;
|
|
1350
1125
|
} else {
|
|
1351
1126
|
var _closure = OrdinaryFunctionCreate(functionExpression, scope, true);
|
|
1352
|
-
|
|
1353
1127
|
return _closure;
|
|
1354
1128
|
}
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1129
|
+
}
|
|
1357
1130
|
|
|
1131
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatearrowfunctionexpression
|
|
1358
1132
|
function InstantiateArrowFunctionExpression(arrowFunction) {
|
|
1359
1133
|
var scope = getRunningContext().LexicalEnvironment;
|
|
1360
1134
|
var closure = OrdinaryFunctionCreate(arrowFunction, scope, false);
|
|
1361
1135
|
return closure;
|
|
1362
|
-
}
|
|
1363
|
-
|
|
1136
|
+
}
|
|
1364
1137
|
|
|
1138
|
+
// https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
|
|
1365
1139
|
function OrdinaryFunctionCreate(sourceNode, scope, isConstructor) {
|
|
1366
1140
|
var F = function () {
|
|
1367
1141
|
// eslint-disable-next-line prefer-rest-params
|
|
1368
1142
|
return CallFunction(F, arguments);
|
|
1369
1143
|
};
|
|
1370
|
-
|
|
1371
1144
|
Object.defineProperties(F, {
|
|
1372
1145
|
[SourceNode]: {
|
|
1373
1146
|
value: sourceNode
|
|
@@ -1386,36 +1159,32 @@ export function cook(rootAst, codeSource) {
|
|
|
1386
1159
|
}
|
|
1387
1160
|
});
|
|
1388
1161
|
return F;
|
|
1389
|
-
}
|
|
1390
|
-
// https://tc39.es/ecma262/#sec-runtime-semantics-bindinginitialization
|
|
1391
|
-
|
|
1162
|
+
}
|
|
1392
1163
|
|
|
1164
|
+
// Patterns initialization.
|
|
1165
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-bindinginitialization
|
|
1393
1166
|
function BindingInitialization(node, value, environment) {
|
|
1394
1167
|
switch (node.type) {
|
|
1395
1168
|
case "Identifier":
|
|
1396
1169
|
return InitializeBoundName(node.name, value, environment);
|
|
1397
|
-
|
|
1398
1170
|
case "ObjectPattern":
|
|
1399
1171
|
RequireObjectCoercible(value);
|
|
1400
1172
|
return PropertyBindingInitialization(node.properties, value, environment);
|
|
1401
|
-
|
|
1402
1173
|
case "ArrayPattern":
|
|
1403
1174
|
{
|
|
1404
1175
|
var iteratorRecord = CreateListIteratorRecord(value);
|
|
1405
1176
|
return IteratorBindingInitialization(node.elements, iteratorRecord, environment);
|
|
1406
1177
|
}
|
|
1407
1178
|
}
|
|
1408
|
-
}
|
|
1409
|
-
|
|
1179
|
+
}
|
|
1410
1180
|
|
|
1181
|
+
// https://tc39.es/ecma262/#sec-destructuring-binding-patterns-runtime-semantics-propertybindinginitialization
|
|
1411
1182
|
function PropertyBindingInitialization(properties, value, environment) {
|
|
1412
1183
|
var excludedNames = new Set();
|
|
1413
|
-
|
|
1414
1184
|
for (var prop of properties) {
|
|
1415
1185
|
if (prop.type === "RestElement") {
|
|
1416
1186
|
return RestBindingInitialization(prop, value, environment, excludedNames);
|
|
1417
1187
|
}
|
|
1418
|
-
|
|
1419
1188
|
if (!prop.computed && prop.key.type === "Identifier") {
|
|
1420
1189
|
KeyedBindingInitialization(prop.value, value, environment, prop.key.name);
|
|
1421
1190
|
excludedNames.add(prop.key.name);
|
|
@@ -1425,36 +1194,31 @@ export function cook(rootAst, codeSource) {
|
|
|
1425
1194
|
excludedNames.add(P);
|
|
1426
1195
|
}
|
|
1427
1196
|
}
|
|
1428
|
-
|
|
1429
1197
|
return NormalCompletion(Empty);
|
|
1430
|
-
}
|
|
1431
|
-
|
|
1198
|
+
}
|
|
1432
1199
|
|
|
1200
|
+
// https://tc39.es/ecma262/#prod-ComputedPropertyName
|
|
1433
1201
|
function EvaluateComputedPropertyName(node) {
|
|
1434
1202
|
var propName = GetValue(Evaluate(node));
|
|
1435
1203
|
return ToPropertyKey(propName);
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1204
|
+
}
|
|
1438
1205
|
|
|
1206
|
+
// https://tc39.es/ecma262/#sec-destructuring-binding-patterns-runtime-semantics-restbindinginitialization
|
|
1439
1207
|
function RestBindingInitialization(restProperty, value, environment, excludedNames) {
|
|
1440
1208
|
var lhs = ResolveBinding(restProperty.argument.name, environment);
|
|
1441
1209
|
var restObj = CopyDataProperties({}, value, excludedNames);
|
|
1442
|
-
|
|
1443
1210
|
if (!environment) {
|
|
1444
1211
|
return PutValue(lhs, restObj);
|
|
1445
1212
|
}
|
|
1446
|
-
|
|
1447
1213
|
return InitializeReferencedBinding(lhs, restObj);
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1214
|
+
}
|
|
1450
1215
|
|
|
1216
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-iteratorbindinginitialization
|
|
1451
1217
|
function IteratorBindingInitialization(elements, iteratorRecord, environment) {
|
|
1452
1218
|
if (elements.length === 0) {
|
|
1453
1219
|
return NormalCompletion(Empty);
|
|
1454
1220
|
}
|
|
1455
|
-
|
|
1456
1221
|
var result;
|
|
1457
|
-
|
|
1458
1222
|
for (var _node of elements) {
|
|
1459
1223
|
if (!_node) {
|
|
1460
1224
|
// Elision element.
|
|
@@ -1465,37 +1229,33 @@ export function cook(rootAst, codeSource) {
|
|
|
1465
1229
|
if (_node.argument.type === "Identifier") {
|
|
1466
1230
|
var lhs = ResolveBinding(_node.argument.name, environment);
|
|
1467
1231
|
var A = [];
|
|
1468
|
-
var n = 0;
|
|
1469
|
-
|
|
1232
|
+
var n = 0;
|
|
1233
|
+
// eslint-disable-next-line no-constant-condition
|
|
1470
1234
|
while (true) {
|
|
1471
1235
|
var {
|
|
1472
1236
|
done,
|
|
1473
1237
|
value: _value3
|
|
1474
1238
|
} = iteratorRecord.next();
|
|
1475
|
-
|
|
1476
1239
|
if (done) {
|
|
1477
1240
|
result = environment ? InitializeReferencedBinding(lhs, A) : PutValue(lhs, A);
|
|
1478
1241
|
break;
|
|
1479
1242
|
}
|
|
1480
|
-
|
|
1481
1243
|
A[n] = _value3;
|
|
1482
1244
|
n++;
|
|
1483
1245
|
}
|
|
1484
1246
|
} else {
|
|
1485
1247
|
var _A = [];
|
|
1486
|
-
var _n2 = 0;
|
|
1487
|
-
|
|
1248
|
+
var _n2 = 0;
|
|
1249
|
+
// eslint-disable-next-line no-constant-condition
|
|
1488
1250
|
while (true) {
|
|
1489
1251
|
var {
|
|
1490
1252
|
done: _done2,
|
|
1491
1253
|
value: _value4
|
|
1492
1254
|
} = iteratorRecord.next();
|
|
1493
|
-
|
|
1494
1255
|
if (_done2) {
|
|
1495
1256
|
result = BindingInitialization(_node.argument, _A, environment);
|
|
1496
1257
|
break;
|
|
1497
1258
|
}
|
|
1498
|
-
|
|
1499
1259
|
_A[_n2] = _value4;
|
|
1500
1260
|
_n2++;
|
|
1501
1261
|
}
|
|
@@ -1503,7 +1263,6 @@ export function cook(rootAst, codeSource) {
|
|
|
1503
1263
|
} else {
|
|
1504
1264
|
// Normal element.
|
|
1505
1265
|
var bindingElement = _node.type === "AssignmentPattern" ? _node.left : _node;
|
|
1506
|
-
|
|
1507
1266
|
switch (bindingElement.type) {
|
|
1508
1267
|
case "ObjectPattern":
|
|
1509
1268
|
case "ArrayPattern":
|
|
@@ -1513,113 +1272,88 @@ export function cook(rootAst, codeSource) {
|
|
|
1513
1272
|
done: _done3,
|
|
1514
1273
|
value: _value5
|
|
1515
1274
|
} = iteratorRecord.next();
|
|
1516
|
-
|
|
1517
1275
|
if (!_done3) {
|
|
1518
1276
|
v = _value5;
|
|
1519
1277
|
}
|
|
1520
|
-
|
|
1521
1278
|
if (_node.type === "AssignmentPattern" && v === undefined) {
|
|
1522
1279
|
var defaultValue = Evaluate(_node.right);
|
|
1523
1280
|
v = GetValue(defaultValue);
|
|
1524
1281
|
}
|
|
1525
|
-
|
|
1526
1282
|
result = BindingInitialization(bindingElement, v, environment);
|
|
1527
1283
|
break;
|
|
1528
1284
|
}
|
|
1529
|
-
|
|
1530
1285
|
case "Identifier":
|
|
1531
1286
|
{
|
|
1532
1287
|
var bindingId = bindingElement.name;
|
|
1533
|
-
|
|
1534
1288
|
var _lhs3 = ResolveBinding(bindingId, environment);
|
|
1535
|
-
|
|
1536
1289
|
var _v = void 0;
|
|
1537
|
-
|
|
1538
1290
|
var {
|
|
1539
1291
|
done: _done4,
|
|
1540
1292
|
value: _value6
|
|
1541
1293
|
} = iteratorRecord.next();
|
|
1542
|
-
|
|
1543
1294
|
if (!_done4) {
|
|
1544
1295
|
_v = _value6;
|
|
1545
1296
|
}
|
|
1546
|
-
|
|
1547
1297
|
if (_node.type === "AssignmentPattern" && _v === undefined) {
|
|
1548
1298
|
// IsAnonymousFunctionDefinition(Initializer)
|
|
1549
1299
|
var _defaultValue = Evaluate(_node.right);
|
|
1550
|
-
|
|
1551
1300
|
_v = GetValue(_defaultValue);
|
|
1552
1301
|
}
|
|
1553
|
-
|
|
1554
1302
|
result = environment ? InitializeReferencedBinding(_lhs3, _v) : PutValue(_lhs3, _v);
|
|
1555
1303
|
break;
|
|
1556
1304
|
}
|
|
1557
1305
|
}
|
|
1558
1306
|
}
|
|
1559
1307
|
}
|
|
1560
|
-
|
|
1561
1308
|
return result;
|
|
1562
|
-
}
|
|
1563
|
-
|
|
1309
|
+
}
|
|
1564
1310
|
|
|
1311
|
+
// https://tc39.es/ecma262/#sec-runtime-semantics-keyedbindinginitialization
|
|
1565
1312
|
function KeyedBindingInitialization(node, value, environment, propertyName) {
|
|
1566
1313
|
var isIdentifier = node.type === "Identifier" || node.type === "AssignmentPattern" && node.left.type === "Identifier";
|
|
1567
|
-
|
|
1568
1314
|
if (isIdentifier) {
|
|
1569
1315
|
var bindingId = node.type === "Identifier" ? node.name : node.left.name;
|
|
1570
1316
|
var lhs = ResolveBinding(bindingId, environment);
|
|
1571
|
-
|
|
1572
1317
|
var _v2 = GetV(value, propertyName);
|
|
1573
|
-
|
|
1574
1318
|
if (node.type === "AssignmentPattern" && _v2 === undefined) {
|
|
1575
1319
|
// If IsAnonymousFunctionDefinition(Initializer)
|
|
1576
1320
|
var defaultValue = Evaluate(node.right);
|
|
1577
1321
|
_v2 = GetValue(defaultValue);
|
|
1578
1322
|
}
|
|
1579
|
-
|
|
1580
1323
|
if (!environment) {
|
|
1581
1324
|
return PutValue(lhs, _v2);
|
|
1582
1325
|
}
|
|
1583
|
-
|
|
1584
1326
|
return InitializeReferencedBinding(lhs, _v2);
|
|
1585
1327
|
}
|
|
1586
|
-
|
|
1587
1328
|
var v = GetV(value, propertyName);
|
|
1588
|
-
|
|
1589
1329
|
if (node.type === "AssignmentPattern" && v === undefined) {
|
|
1590
1330
|
var _defaultValue2 = Evaluate(node.right);
|
|
1591
|
-
|
|
1592
1331
|
v = GetValue(_defaultValue2);
|
|
1593
1332
|
}
|
|
1594
|
-
|
|
1595
1333
|
return BindingInitialization(node.type === "AssignmentPattern" ? node.left : node, v, environment);
|
|
1596
|
-
}
|
|
1597
|
-
|
|
1334
|
+
}
|
|
1598
1335
|
|
|
1336
|
+
// https://tc39.es/ecma262/#sec-initializeboundname
|
|
1599
1337
|
function InitializeBoundName(name, value, environment) {
|
|
1600
1338
|
// Assert: environment is always present.
|
|
1601
1339
|
environment.InitializeBinding(name, value);
|
|
1602
1340
|
return NormalCompletion(Empty);
|
|
1603
1341
|
}
|
|
1604
|
-
|
|
1605
1342
|
function ThrowIfFunctionIsInvalid(func) {
|
|
1606
1343
|
if (func.async || func.generator) {
|
|
1607
1344
|
throw new SyntaxError("".concat(func.async ? "Async" : "Generator", " function is not allowed"));
|
|
1608
1345
|
}
|
|
1609
|
-
|
|
1610
1346
|
if (expressionOnly && !func.expression) {
|
|
1611
1347
|
throw new SyntaxError("Only an `Expression` is allowed in `ArrowFunctionExpression`'s body");
|
|
1612
1348
|
}
|
|
1613
1349
|
}
|
|
1614
|
-
|
|
1615
1350
|
if (expressionOnly) {
|
|
1616
1351
|
return GetValue(Evaluate(rootAst));
|
|
1617
1352
|
}
|
|
1618
|
-
|
|
1619
1353
|
(_hooks$beforeEvaluate3 = hooks.beforeEvaluate) === null || _hooks$beforeEvaluate3 === void 0 ? void 0 : _hooks$beforeEvaluate3.call(hooks, rootAst);
|
|
1620
1354
|
ThrowIfFunctionIsInvalid(rootAst);
|
|
1621
|
-
var [fn] = collectBoundNames(rootAst);
|
|
1622
|
-
|
|
1355
|
+
var [fn] = collectBoundNames(rootAst);
|
|
1356
|
+
// Create an immutable binding for the root function.
|
|
1623
1357
|
rootEnv.CreateImmutableBinding(fn, true);
|
|
1624
1358
|
var fo = InstantiateFunctionObject(rootAst, rootEnv);
|
|
1625
1359
|
rootEnv.InitializeBinding(fn, fo);
|