1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.havocBinding = havocBinding;
|
7 | exports.mightBecomeAnObject = mightBecomeAnObject;
|
8 | exports.Reference = exports.LexicalEnvironment = exports.GlobalEnvironmentRecord = exports.FunctionEnvironmentRecord = exports.ObjectEnvironmentRecord = exports.DeclarativeEnvironmentRecord = exports.EnvironmentRecord = void 0;
|
9 |
|
10 | var t = _interopRequireWildcard(require("@babel/types"));
|
11 |
|
12 | var _completions = require("./completions.js");
|
13 |
|
14 | var _errors = require("./errors.js");
|
15 |
|
16 | var _options = require("./options.js");
|
17 |
|
18 | var _realm = require("./realm.js");
|
19 |
|
20 | var _index = require("./values/index.js");
|
21 |
|
22 | var _generator = _interopRequireDefault(require("@babel/generator"));
|
23 |
|
24 | var _parse = _interopRequireDefault(require("./utils/parse.js"));
|
25 |
|
26 | var _invariant = _interopRequireDefault(require("./invariant.js"));
|
27 |
|
28 | var _traverseFast = _interopRequireDefault(require("./utils/traverse-fast.js"));
|
29 |
|
30 | var _index2 = require("./methods/index.js");
|
31 |
|
32 | var _singletons = require("./singletons.js");
|
33 |
|
34 | var _index3 = require("./domains/index.js");
|
35 |
|
36 | var _PrimitiveValue = _interopRequireDefault(require("./values/PrimitiveValue.js"));
|
37 |
|
38 | var _generator2 = require("./utils/generator.js");
|
39 |
|
40 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
41 |
|
42 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
43 |
|
44 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
45 |
|
46 | const sourceMap = require("source-map");
|
47 |
|
48 | function deriveGetBinding(realm, binding) {
|
49 | let types = _index3.TypesDomain.topVal;
|
50 | let values = _index3.ValuesDomain.topVal;
|
51 | (0, _invariant.default)(realm.generator !== undefined);
|
52 | return realm.generator.deriveAbstract(types, values, [], (0, _generator2.createOperationDescriptor)("GET_BINDING", {
|
53 | binding
|
54 | }));
|
55 | }
|
56 |
|
57 | function havocBinding(binding) {
|
58 | let realm = binding.environment.realm;
|
59 | let value = binding.value;
|
60 |
|
61 | if (!binding.hasLeaked) {
|
62 | realm.recordModifiedBinding(binding).hasLeaked = true;
|
63 |
|
64 | if (value !== undefined) {
|
65 | let realmGenerator = realm.generator;
|
66 | if (realmGenerator !== undefined && value !== realm.intrinsics.undefined) realmGenerator.emitBindingAssignment(binding, value);
|
67 |
|
68 | if (binding.mutable === true) {
|
69 |
|
70 |
|
71 | binding.value = undefined;
|
72 | }
|
73 | }
|
74 | }
|
75 | }
|
76 |
|
77 |
|
78 | class EnvironmentRecord {
|
79 | constructor(realm) {
|
80 | (0, _invariant.default)(realm, "expected realm");
|
81 | this.realm = realm;
|
82 | this.isReadOnly = false;
|
83 | this.id = EnvironmentRecord.nextId++;
|
84 | }
|
85 |
|
86 | HasBinding(N) {
|
87 | (0, _invariant.default)(false, "abstract method; please override");
|
88 | }
|
89 |
|
90 | CreateMutableBinding(N, D, isGlobal) {
|
91 | (0, _invariant.default)(false, "abstract method; please override");
|
92 | }
|
93 |
|
94 | CreateImmutableBinding(N, S, isGlobal, skipRecord) {
|
95 | (0, _invariant.default)(false, "abstract method; please override");
|
96 | }
|
97 |
|
98 | InitializeBinding(N, V, skipRecord) {
|
99 | (0, _invariant.default)(false, "abstract method; please override");
|
100 | }
|
101 |
|
102 | SetMutableBinding(N, V, S) {
|
103 | (0, _invariant.default)(false, "abstract method; please override");
|
104 | }
|
105 |
|
106 | GetBindingValue(N, S) {
|
107 | (0, _invariant.default)(false, "abstract method; please override");
|
108 | }
|
109 |
|
110 | DeleteBinding(N) {
|
111 | (0, _invariant.default)(false, "abstract method; please override");
|
112 | }
|
113 |
|
114 | HasThisBinding() {
|
115 | (0, _invariant.default)(false, "abstract method; please override");
|
116 | }
|
117 |
|
118 | GetThisBinding() {
|
119 | (0, _invariant.default)(false, "abstract method; please override");
|
120 | }
|
121 |
|
122 | HasSuperBinding() {
|
123 | (0, _invariant.default)(false, "abstract method; please override");
|
124 | }
|
125 |
|
126 | WithBaseObject() {
|
127 | (0, _invariant.default)(false, "abstract method; please override");
|
128 | }
|
129 |
|
130 | BindThisValue(V) {
|
131 | (0, _invariant.default)(false, "abstract method; please override");
|
132 | }
|
133 |
|
134 | }
|
135 |
|
136 | exports.EnvironmentRecord = EnvironmentRecord;
|
137 |
|
138 | _defineProperty(EnvironmentRecord, "nextId", 0);
|
139 |
|
140 |
|
141 | class DeclarativeEnvironmentRecord extends EnvironmentRecord {
|
142 | constructor(realm) {
|
143 | super(realm);
|
144 | this.bindings = Object.create(null);
|
145 | this.frozen = false;
|
146 | }
|
147 |
|
148 |
|
149 | HasBinding(N) {
|
150 |
|
151 | let envRec = this;
|
152 |
|
153 | if (envRec.bindings[N]) return true;
|
154 |
|
155 | return false;
|
156 | }
|
157 |
|
158 |
|
159 | CreateMutableBinding(N, D, isGlobal = false) {
|
160 | (0, _invariant.default)(!this.frozen);
|
161 | let realm = this.realm;
|
162 |
|
163 | let envRec = this;
|
164 |
|
165 | (0, _invariant.default)(!envRec.bindings[N], `shouldn't have the binding ${N}`);
|
166 |
|
167 | this.bindings[N] = realm.recordModifiedBinding({
|
168 | initialized: false,
|
169 | mutable: true,
|
170 | deletable: D,
|
171 | environment: envRec,
|
172 | name: N,
|
173 | isGlobal: isGlobal,
|
174 | mightHaveBeenCaptured: false,
|
175 | hasLeaked: false
|
176 | });
|
177 |
|
178 | return realm.intrinsics.undefined;
|
179 | }
|
180 |
|
181 |
|
182 | CreateImmutableBinding(N, S, isGlobal = false, skipRecord = false) {
|
183 | (0, _invariant.default)(!this.frozen);
|
184 | let realm = this.realm;
|
185 |
|
186 | let envRec = this;
|
187 |
|
188 | (0, _invariant.default)(!envRec.bindings[N], `shouldn't have the binding ${N}`);
|
189 |
|
190 | let binding = {
|
191 | initialized: false,
|
192 | strict: S,
|
193 | deletable: false,
|
194 | environment: envRec,
|
195 | name: N,
|
196 | isGlobal: isGlobal,
|
197 | mightHaveBeenCaptured: false,
|
198 | hasLeaked: false
|
199 | };
|
200 | this.bindings[N] = skipRecord ? binding : realm.recordModifiedBinding(binding);
|
201 |
|
202 | return realm.intrinsics.undefined;
|
203 | }
|
204 |
|
205 |
|
206 | InitializeBinding(N, V, skipRecord = false) {
|
207 |
|
208 | let envRec = this;
|
209 | let binding = envRec.bindings[N];
|
210 |
|
211 | (0, _invariant.default)(binding && binding.initialized !== true, `shouldn't have the binding ${N}`);
|
212 |
|
213 | if (!skipRecord) this.realm.recordModifiedBinding(binding, V).value = V;else binding.value = V;
|
214 |
|
215 | binding.initialized = true;
|
216 |
|
217 | return this.realm.intrinsics.empty;
|
218 | }
|
219 |
|
220 |
|
221 | SetMutableBinding(N, V, _S) {
|
222 | let S = _S;
|
223 |
|
224 | let realm = this.realm;
|
225 |
|
226 | let envRec = this;
|
227 | let binding = envRec.bindings[N];
|
228 |
|
229 | if (!binding) {
|
230 |
|
231 | if (S) {
|
232 | throw realm.createErrorThrowCompletion(realm.intrinsics.ReferenceError, `${N} not found`);
|
233 | }
|
234 |
|
235 |
|
236 | envRec.CreateMutableBinding(N, true);
|
237 |
|
238 | envRec.InitializeBinding(N, V);
|
239 |
|
240 | return this.realm.intrinsics.empty;
|
241 | }
|
242 |
|
243 |
|
244 | if (binding.strict === true) S = true;
|
245 |
|
246 | if (binding.initialized !== true) {
|
247 | throw realm.createErrorThrowCompletion(realm.intrinsics.ReferenceError, `${N} has not yet been initialized`);
|
248 | } else if (binding.mutable) {
|
249 |
|
250 | if (binding.hasLeaked) {
|
251 | _singletons.Havoc.value(realm, V);
|
252 |
|
253 | (0, _invariant.default)(realm.generator);
|
254 | realm.generator.emitBindingAssignment(binding, V);
|
255 | } else {
|
256 | realm.recordModifiedBinding(binding, V).value = V;
|
257 | }
|
258 | } else {
|
259 |
|
260 |
|
261 |
|
262 | if (S) {
|
263 | throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError, "attempt to change immutable binding");
|
264 | }
|
265 | }
|
266 |
|
267 |
|
268 | return this.realm.intrinsics.empty;
|
269 | }
|
270 |
|
271 |
|
272 | GetBindingValue(N, S) {
|
273 | let realm = this.realm;
|
274 |
|
275 | let envRec = this;
|
276 | let binding = envRec.bindings[N];
|
277 |
|
278 | (0, _invariant.default)(binding, "expected binding");
|
279 |
|
280 | if (!binding.initialized) {
|
281 | throw realm.createErrorThrowCompletion(realm.intrinsics.ReferenceError);
|
282 | }
|
283 |
|
284 |
|
285 | if (binding.hasLeaked && binding.mutable) {
|
286 | return deriveGetBinding(realm, binding);
|
287 | }
|
288 |
|
289 | (0, _invariant.default)(binding.value);
|
290 | return binding.value;
|
291 | }
|
292 |
|
293 |
|
294 | DeleteBinding(N) {
|
295 | (0, _invariant.default)(!this.frozen);
|
296 |
|
297 | let envRec = this;
|
298 |
|
299 | (0, _invariant.default)(envRec.bindings[N], "expected binding to exist");
|
300 |
|
301 | if (!envRec.bindings[N].deletable) return false;
|
302 |
|
303 | this.realm.recordModifiedBinding(envRec.bindings[N]).value = undefined;
|
304 | delete envRec.bindings[N];
|
305 |
|
306 | return true;
|
307 | }
|
308 |
|
309 |
|
310 | HasThisBinding() {
|
311 |
|
312 | return false;
|
313 | }
|
314 |
|
315 |
|
316 | HasSuperBinding() {
|
317 |
|
318 | return false;
|
319 | }
|
320 |
|
321 |
|
322 | WithBaseObject() {
|
323 |
|
324 | return this.realm.intrinsics.undefined;
|
325 | }
|
326 |
|
327 | }
|
328 |
|
329 |
|
330 | exports.DeclarativeEnvironmentRecord = DeclarativeEnvironmentRecord;
|
331 |
|
332 | class ObjectEnvironmentRecord extends EnvironmentRecord {
|
333 | constructor(realm, obj) {
|
334 | super(realm);
|
335 | this.object = obj;
|
336 | }
|
337 |
|
338 |
|
339 | HasBinding(N) {
|
340 | let realm = this.realm;
|
341 |
|
342 | let envRec = this;
|
343 |
|
344 | let bindings = this.object;
|
345 |
|
346 | let foundBinding = (0, _index2.HasProperty)(realm, bindings, N);
|
347 |
|
348 | if (!foundBinding) return false;
|
349 |
|
350 | if (!envRec.withEnvironment) return true;
|
351 |
|
352 | let unscopables = (0, _index2.Get)(realm, bindings, realm.intrinsics.SymbolUnscopables);
|
353 |
|
354 | if (unscopables instanceof _index.ObjectValue || unscopables instanceof _index.AbstractObjectValue) {
|
355 |
|
356 | let blocked = _singletons.To.ToBooleanPartial(realm, (0, _index2.Get)(realm, unscopables, N));
|
357 |
|
358 |
|
359 | if (blocked) return false;
|
360 | }
|
361 |
|
362 | unscopables.throwIfNotConcrete();
|
363 |
|
364 | return true;
|
365 | }
|
366 |
|
367 |
|
368 | CreateMutableBinding(N, D) {
|
369 | let realm = this.realm;
|
370 |
|
371 | let envRec = this;
|
372 |
|
373 | let bindings = envRec.object;
|
374 |
|
375 | let configValue = D ? true : false;
|
376 |
|
377 | return new _index.BooleanValue(realm, _singletons.Properties.DefinePropertyOrThrow(realm, bindings, N, {
|
378 | value: realm.intrinsics.undefined,
|
379 | writable: true,
|
380 | enumerable: true,
|
381 | configurable: configValue
|
382 | }));
|
383 | }
|
384 |
|
385 |
|
386 | CreateImmutableBinding(N, S) {
|
387 |
|
388 | (0, _invariant.default)(false);
|
389 | }
|
390 |
|
391 |
|
392 | InitializeBinding(N, V) {
|
393 |
|
394 | let envRec = this;
|
395 |
|
396 |
|
397 |
|
398 | return envRec.SetMutableBinding(N, V, false);
|
399 | }
|
400 |
|
401 |
|
402 | SetMutableBinding(N, V, S) {
|
403 | let realm = this.realm;
|
404 |
|
405 | let envRec = this;
|
406 |
|
407 | let bindings = envRec.object;
|
408 |
|
409 | return new _index.BooleanValue(realm, _singletons.Properties.Set(realm, bindings, N, V, S));
|
410 | }
|
411 |
|
412 |
|
413 | GetBindingValue(N, S) {
|
414 | let realm = this.realm;
|
415 |
|
416 | let envRec = this;
|
417 |
|
418 | let bindings = envRec.object;
|
419 |
|
420 | let value = (0, _index2.HasProperty)(realm, bindings, N);
|
421 |
|
422 | if (!value) {
|
423 |
|
424 | if (!S) {
|
425 | return realm.intrinsics.undefined;
|
426 | } else {
|
427 | throw realm.createErrorThrowCompletion(realm.intrinsics.ReferenceError);
|
428 | }
|
429 | }
|
430 |
|
431 |
|
432 | return (0, _index2.Get)(realm, bindings, N);
|
433 | }
|
434 |
|
435 |
|
436 | DeleteBinding(N) {
|
437 |
|
438 | let envRec = this;
|
439 |
|
440 | let bindings = envRec.object;
|
441 |
|
442 | return bindings.$Delete(N);
|
443 | }
|
444 |
|
445 |
|
446 | HasThisBinding() {
|
447 |
|
448 | return false;
|
449 | }
|
450 |
|
451 |
|
452 | HasSuperBinding() {
|
453 |
|
454 | return false;
|
455 | }
|
456 |
|
457 |
|
458 | WithBaseObject() {
|
459 |
|
460 | let envRec = this;
|
461 |
|
462 | if (envRec.withEnvironment) return envRec.object;
|
463 |
|
464 | return this.realm.intrinsics.undefined;
|
465 | }
|
466 |
|
467 | }
|
468 |
|
469 |
|
470 | exports.ObjectEnvironmentRecord = ObjectEnvironmentRecord;
|
471 |
|
472 | class FunctionEnvironmentRecord extends DeclarativeEnvironmentRecord {
|
473 |
|
474 | BindThisValue(V) {
|
475 | let realm = this.realm;
|
476 |
|
477 | let envRec = this;
|
478 |
|
479 | (0, _invariant.default)(envRec.$ThisBindingStatus !== "lexical", "this binding status shouldn't be lexical");
|
480 |
|
481 | if (envRec.$ThisBindingStatus === "initialized") {
|
482 | throw realm.createErrorThrowCompletion(realm.intrinsics.ReferenceError);
|
483 | }
|
484 |
|
485 |
|
486 | envRec.$ThisValue = V;
|
487 |
|
488 | envRec.$ThisBindingStatus = "initialized";
|
489 |
|
490 | return V;
|
491 | }
|
492 |
|
493 |
|
494 | HasThisBinding() {
|
495 |
|
496 | let envRec = this;
|
497 |
|
498 | return envRec.$ThisBindingStatus === "lexical" ? false : true;
|
499 | }
|
500 |
|
501 |
|
502 | HasSuperBinding() {
|
503 |
|
504 | let envRec = this;
|
505 |
|
506 | if (envRec.$ThisBindingStatus === "lexical") return false;
|
507 |
|
508 | if (envRec.$HomeObject === undefined) {
|
509 | return false;
|
510 | } else {
|
511 | return true;
|
512 | }
|
513 | }
|
514 |
|
515 |
|
516 | GetThisBinding() {
|
517 | let realm = this.realm;
|
518 |
|
519 | let envRec = this;
|
520 |
|
521 | (0, _invariant.default)(envRec.$ThisBindingStatus !== "lexical", "this binding status shouldn't be lexical");
|
522 |
|
523 | if (envRec.$ThisBindingStatus === "uninitialized") {
|
524 | throw realm.createErrorThrowCompletion(realm.intrinsics.ReferenceError);
|
525 | }
|
526 |
|
527 |
|
528 | return envRec.$ThisValue;
|
529 | }
|
530 |
|
531 |
|
532 | GetSuperBase() {
|
533 |
|
534 | let envRec = this;
|
535 |
|
536 | let home = envRec.$HomeObject;
|
537 |
|
538 | if (home === undefined) return this.realm.intrinsics.undefined;
|
539 |
|
540 | (0, _invariant.default)(home instanceof _index.ObjectValue, "expected object value");
|
541 |
|
542 | return home.$GetPrototypeOf();
|
543 | }
|
544 |
|
545 | }
|
546 |
|
547 |
|
548 | exports.FunctionEnvironmentRecord = FunctionEnvironmentRecord;
|
549 |
|
550 | class GlobalEnvironmentRecord extends EnvironmentRecord {
|
551 |
|
552 | HasBinding(N) {
|
553 |
|
554 | let envRec = this;
|
555 |
|
556 | let DclRec = envRec.$DeclarativeRecord;
|
557 |
|
558 | if (DclRec.HasBinding(N)) return true;
|
559 |
|
560 | let ObjRec = envRec.$ObjectRecord;
|
561 |
|
562 | return ObjRec.HasBinding(N);
|
563 | }
|
564 |
|
565 |
|
566 | CreateMutableBinding(N, D) {
|
567 | let realm = this.realm;
|
568 |
|
569 | let envRec = this;
|
570 |
|
571 | let DclRec = envRec.$DeclarativeRecord;
|
572 |
|
573 | if (DclRec.HasBinding(N)) {
|
574 | throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
|
575 | }
|
576 |
|
577 |
|
578 | return DclRec.CreateMutableBinding(N, D, true);
|
579 | }
|
580 |
|
581 |
|
582 | CreateImmutableBinding(N, S) {
|
583 | let realm = this.realm;
|
584 |
|
585 | let envRec = this;
|
586 |
|
587 | let DclRec = envRec.$DeclarativeRecord;
|
588 |
|
589 | if (DclRec.HasBinding(N)) {
|
590 | throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
|
591 | }
|
592 |
|
593 |
|
594 | return DclRec.CreateImmutableBinding(N, S, true);
|
595 | }
|
596 |
|
597 |
|
598 | InitializeBinding(N, V) {
|
599 |
|
600 | let envRec = this;
|
601 |
|
602 | let DclRec = envRec.$DeclarativeRecord;
|
603 |
|
604 | if (DclRec.HasBinding(N)) {
|
605 |
|
606 | return DclRec.InitializeBinding(N, V);
|
607 | }
|
608 |
|
609 |
|
610 |
|
611 | let ObjRec = envRec.$ObjectRecord;
|
612 |
|
613 | return ObjRec.InitializeBinding(N, V);
|
614 | }
|
615 |
|
616 |
|
617 | SetMutableBinding(N, V, S) {
|
618 |
|
619 | let envRec = this;
|
620 |
|
621 | let DclRec = envRec.$DeclarativeRecord;
|
622 |
|
623 | if (DclRec.HasBinding(N)) {
|
624 |
|
625 | return DclRec.SetMutableBinding(N, V, S);
|
626 | }
|
627 |
|
628 |
|
629 | let ObjRec = envRec.$ObjectRecord;
|
630 |
|
631 | return ObjRec.SetMutableBinding(N, V, S);
|
632 | }
|
633 |
|
634 |
|
635 | GetBindingValue(N, S) {
|
636 |
|
637 | let envRec = this;
|
638 |
|
639 | let DclRec = envRec.$DeclarativeRecord;
|
640 |
|
641 | if (DclRec.HasBinding(N)) {
|
642 |
|
643 | return DclRec.GetBindingValue(N, S);
|
644 | }
|
645 |
|
646 |
|
647 | let ObjRec = envRec.$ObjectRecord;
|
648 |
|
649 | return ObjRec.GetBindingValue(N, S);
|
650 | }
|
651 |
|
652 |
|
653 | DeleteBinding(N) {
|
654 | let realm = this.realm;
|
655 |
|
656 | let envRec = this;
|
657 |
|
658 | let DclRec = envRec.$DeclarativeRecord;
|
659 |
|
660 | if (DclRec.HasBinding(N)) {
|
661 |
|
662 | return DclRec.DeleteBinding(N);
|
663 | }
|
664 |
|
665 |
|
666 | let ObjRec = envRec.$ObjectRecord;
|
667 |
|
668 | let globalObject = ObjRec.object;
|
669 |
|
670 | let existingProp = (0, _index2.HasOwnProperty)(realm, globalObject, N);
|
671 |
|
672 | if (existingProp) {
|
673 |
|
674 | let status = ObjRec.DeleteBinding(N);
|
675 |
|
676 | if (status) {
|
677 |
|
678 | let varNames = envRec.$VarNames;
|
679 |
|
680 | if (varNames.indexOf(N) >= 0) {
|
681 | varNames.splice(varNames.indexOf(N), 1);
|
682 | }
|
683 | }
|
684 |
|
685 |
|
686 | return status;
|
687 | }
|
688 |
|
689 |
|
690 | return true;
|
691 | }
|
692 |
|
693 |
|
694 | HasThisBinding() {
|
695 |
|
696 | return true;
|
697 | }
|
698 |
|
699 |
|
700 | HasSuperBinding() {
|
701 |
|
702 | return true;
|
703 | }
|
704 |
|
705 |
|
706 | WithBaseObject() {
|
707 |
|
708 | return this.realm.intrinsics.undefined;
|
709 | }
|
710 |
|
711 |
|
712 | GetThisBinding() {
|
713 |
|
714 | let envRec = this;
|
715 | (0, _invariant.default)(envRec.$GlobalThisValue);
|
716 |
|
717 | return envRec.$GlobalThisValue;
|
718 | }
|
719 |
|
720 |
|
721 | HasVarDeclaration(N) {
|
722 |
|
723 | let envRec = this;
|
724 |
|
725 | let varDeclaredNames = envRec.$VarNames;
|
726 |
|
727 | if (varDeclaredNames.indexOf(N) >= 0) return true;
|
728 |
|
729 | return false;
|
730 | }
|
731 |
|
732 |
|
733 | HasLexicalDeclaration(N) {
|
734 |
|
735 | let envRec = this;
|
736 |
|
737 | let DclRec = envRec.$DeclarativeRecord;
|
738 |
|
739 | return DclRec.HasBinding(N);
|
740 | }
|
741 |
|
742 |
|
743 | HasRestrictedGlobalProperty(N) {
|
744 |
|
745 | let envRec = this;
|
746 |
|
747 | let ObjRec = envRec.$ObjectRecord;
|
748 |
|
749 | let globalObject = ObjRec.object;
|
750 |
|
751 | let existingProp = globalObject.$GetOwnProperty(N);
|
752 |
|
753 | if (!existingProp) return false;
|
754 |
|
755 | _singletons.Properties.ThrowIfMightHaveBeenDeleted(existingProp.value);
|
756 |
|
757 |
|
758 | if (existingProp.configurable) return false;
|
759 |
|
760 | return true;
|
761 | }
|
762 |
|
763 |
|
764 | CanDeclareGlobalVar(N) {
|
765 | let realm = this.realm;
|
766 |
|
767 | let envRec = this;
|
768 |
|
769 | let ObjRec = envRec.$ObjectRecord;
|
770 |
|
771 | let globalObject = ObjRec.object;
|
772 |
|
773 | let hasProperty = (0, _index2.HasOwnProperty)(realm, globalObject, N);
|
774 |
|
775 | if (hasProperty) return true;
|
776 |
|
777 | return (0, _index2.IsExtensible)(realm, globalObject);
|
778 | }
|
779 |
|
780 |
|
781 | CanDeclareGlobalFunction(N) {
|
782 | let realm = this.realm;
|
783 |
|
784 | let envRec = this;
|
785 |
|
786 | let ObjRec = envRec.$ObjectRecord;
|
787 |
|
788 | let globalObject = ObjRec.object;
|
789 |
|
790 | let existingProp = globalObject.$GetOwnProperty(N);
|
791 |
|
792 | if (!existingProp) return (0, _index2.IsExtensible)(realm, globalObject);
|
793 |
|
794 | _singletons.Properties.ThrowIfMightHaveBeenDeleted(existingProp.value);
|
795 |
|
796 |
|
797 | if (existingProp.configurable) return true;
|
798 |
|
799 | if ((0, _index2.IsDataDescriptor)(realm, existingProp) && existingProp.writable && existingProp.enumerable) {
|
800 | return true;
|
801 | }
|
802 |
|
803 |
|
804 | return false;
|
805 | }
|
806 |
|
807 |
|
808 | CreateGlobalVarBinding(N, D) {
|
809 | let realm = this.realm;
|
810 |
|
811 | let envRec = this;
|
812 |
|
813 | let ObjRec = envRec.$ObjectRecord;
|
814 |
|
815 | let globalObject = ObjRec.object;
|
816 |
|
817 | let hasProperty = (0, _index2.HasOwnProperty)(realm, globalObject, N);
|
818 |
|
819 | let extensible = (0, _index2.IsExtensible)(realm, globalObject);
|
820 |
|
821 | if (!hasProperty && extensible) {
|
822 |
|
823 | ObjRec.CreateMutableBinding(N, D);
|
824 |
|
825 | ObjRec.InitializeBinding(N, this.realm.intrinsics.undefined);
|
826 | }
|
827 |
|
828 |
|
829 | let varDeclaredNames = envRec.$VarNames;
|
830 |
|
831 | if (varDeclaredNames.indexOf(N) < 0) {
|
832 |
|
833 | varDeclaredNames.push(N);
|
834 | }
|
835 |
|
836 | }
|
837 |
|
838 |
|
839 | CreateGlobalFunctionBinding(N, V, D) {
|
840 |
|
841 | let envRec = this;
|
842 |
|
843 | let ObjRec = envRec.$ObjectRecord;
|
844 |
|
845 | let globalObject = ObjRec.object;
|
846 |
|
847 | let existingProp = globalObject.$GetOwnProperty(N);
|
848 |
|
849 | let desc;
|
850 |
|
851 | if (!existingProp || existingProp.configurable) {
|
852 |
|
853 | desc = {
|
854 | value: V,
|
855 | writable: true,
|
856 | enumerable: true,
|
857 | configurable: D
|
858 | };
|
859 | } else {
|
860 |
|
861 | _singletons.Properties.ThrowIfMightHaveBeenDeleted(existingProp.value);
|
862 |
|
863 |
|
864 | desc = {
|
865 | value: V
|
866 | };
|
867 | }
|
868 |
|
869 |
|
870 | _singletons.Properties.DefinePropertyOrThrow(this.realm, globalObject, N, desc);
|
871 |
|
872 |
|
873 |
|
874 | _singletons.Properties.Set(this.realm, globalObject, N, V, false);
|
875 |
|
876 |
|
877 | let varDeclaredNames = envRec.$VarNames;
|
878 |
|
879 | if (varDeclaredNames.indexOf(N) < 0) {
|
880 |
|
881 | varDeclaredNames.push(N);
|
882 | }
|
883 |
|
884 | }
|
885 |
|
886 | }
|
887 |
|
888 |
|
889 | exports.GlobalEnvironmentRecord = GlobalEnvironmentRecord;
|
890 | let uid = 0;
|
891 |
|
892 | class LexicalEnvironment {
|
893 | constructor(realm) {
|
894 | (0, _invariant.default)(realm, "expected realm");
|
895 | this.realm = realm;
|
896 | this.destroyed = false;
|
897 | this._uid = uid++;
|
898 | }
|
899 |
|
900 |
|
901 | destroy() {
|
902 | this.destroyed = true;
|
903 |
|
904 |
|
905 | if (this.environmentRecord instanceof DeclarativeEnvironmentRecord) {
|
906 | this.environmentRecord.frozen = true;
|
907 | }
|
908 | }
|
909 |
|
910 | assignToGlobal(globalAst, rvalue) {
|
911 | let globalValue = this.evaluate(globalAst, false);
|
912 |
|
913 | _singletons.Properties.PutValue(this.realm, globalValue, rvalue);
|
914 | }
|
915 |
|
916 | partiallyEvaluateCompletionDeref(ast, strictCode, metadata) {
|
917 | let [result, partial_ast, partial_io] = this.partiallyEvaluateCompletion(ast, strictCode, metadata);
|
918 |
|
919 | if (result instanceof Reference) {
|
920 | result = _singletons.Environment.GetValue(this.realm, result);
|
921 | }
|
922 |
|
923 | return [result, partial_ast, partial_io];
|
924 | }
|
925 |
|
926 | partiallyEvaluateCompletion(ast, strictCode, metadata) {
|
927 | try {
|
928 | return this.partiallyEvaluate(ast, strictCode, metadata);
|
929 | } catch (err) {
|
930 | if (err instanceof _completions.Completion) return [err, ast, []];
|
931 | if (err instanceof Error)
|
932 | throw err;
|
933 |
|
934 | throw new _errors.FatalError(err);
|
935 | }
|
936 | }
|
937 |
|
938 | evaluateCompletionDeref(ast, strictCode, metadata) {
|
939 | let result = this.evaluateCompletion(ast, strictCode, metadata);
|
940 | if (result instanceof Reference) result = _singletons.Environment.GetValue(this.realm, result);
|
941 | return result;
|
942 | }
|
943 |
|
944 | evaluateCompletion(ast, strictCode, metadata) {
|
945 | try {
|
946 | return this.evaluate(ast, strictCode, metadata);
|
947 | } catch (err) {
|
948 | if (err instanceof _completions.AbruptCompletion) return err;
|
949 | if (err instanceof Error)
|
950 | throw err;
|
951 |
|
952 | throw new _errors.FatalError(err);
|
953 | }
|
954 | }
|
955 |
|
956 | evaluateAbstractCompletion(ast, strictCode, metadata) {
|
957 | try {
|
958 | return this.evaluateAbstract(ast, strictCode, metadata);
|
959 | } catch (err) {
|
960 | if (err instanceof _completions.Completion) return err;
|
961 | if (err instanceof Error)
|
962 | throw err;
|
963 |
|
964 | if (err instanceof Object) throw new _errors.FatalError(err.constructor.name + ": " + err);
|
965 | throw new _errors.FatalError(err);
|
966 | }
|
967 | }
|
968 |
|
969 | concatenateAndParse(sources, sourceType = "script") {
|
970 | let asts = [];
|
971 | let code = {};
|
972 | let directives = [];
|
973 |
|
974 | for (let source of sources) {
|
975 | try {
|
976 | let node = this.realm.statistics.parsing.measure(() => (0, _parse.default)(this.realm, source.fileContents, source.filePath, sourceType));
|
977 | let sourceMapContents = source.sourceMapContents;
|
978 |
|
979 | if (sourceMapContents && sourceMapContents.length > 0) {
|
980 | this.realm.statistics.fixupSourceLocations.measure(() => this.fixup_source_locations(node, sourceMapContents));
|
981 | }
|
982 |
|
983 | this.realm.statistics.fixupFilenames.measure(() => this.fixup_filenames(node));
|
984 | asts = asts.concat(node.program.body);
|
985 | code[source.filePath] = source.fileContents;
|
986 | directives = directives.concat(node.program.directives);
|
987 | } catch (e) {
|
988 | if (e instanceof _completions.ThrowCompletion) {
|
989 | let error = e.value;
|
990 |
|
991 | if (error instanceof _index.ObjectValue) {
|
992 | let message = error._SafeGetDataPropertyValue("message");
|
993 |
|
994 | if (message instanceof _index.StringValue) {
|
995 | message.value = `Syntax error: ${message.value}`;
|
996 | e.location.source = source.filePath;
|
997 |
|
998 |
|
999 | e.location.start = {
|
1000 | line: e.location.line,
|
1001 | column: e.location.column
|
1002 | };
|
1003 | e.location.end = {
|
1004 | line: e.location.line,
|
1005 | column: e.location.column
|
1006 | };
|
1007 | let diagnostic = new _errors.CompilerDiagnostic(message.value, e.location, "PP1004", "FatalError");
|
1008 | this.realm.handleError(diagnostic);
|
1009 | throw new _errors.FatalError(message.value);
|
1010 | }
|
1011 | }
|
1012 | }
|
1013 |
|
1014 | throw e;
|
1015 | }
|
1016 | }
|
1017 |
|
1018 | return [t.file(t.program(asts, directives)), code];
|
1019 | }
|
1020 |
|
1021 | executeSources(sources, sourceType = "script", onParse = undefined) {
|
1022 | let context = new _realm.ExecutionContext();
|
1023 | context.lexicalEnvironment = this;
|
1024 | context.variableEnvironment = this;
|
1025 | context.realm = this.realm;
|
1026 | this.realm.pushContext(context);
|
1027 | let res, code;
|
1028 |
|
1029 | try {
|
1030 | let ast;
|
1031 | [ast, code] = this.concatenateAndParse(sources, sourceType);
|
1032 | if (onParse) onParse(ast);
|
1033 | res = this.realm.statistics.evaluation.measure(() => this.evaluateCompletion(ast, false));
|
1034 | } finally {
|
1035 | this.realm.popContext(context);
|
1036 | this.realm.onDestroyScope(context.lexicalEnvironment);
|
1037 | if (!this.destroyed) this.realm.onDestroyScope(this);
|
1038 | (0, _invariant.default)(this.realm.activeLexicalEnvironments.size === 0, `expected 0 active lexical environments, got ${this.realm.activeLexicalEnvironments.size}`);
|
1039 | }
|
1040 |
|
1041 | if (res instanceof _completions.AbruptCompletion) return [res, code];
|
1042 | return [_singletons.Environment.GetValue(this.realm, res), code];
|
1043 | }
|
1044 |
|
1045 | executePartialEvaluator(sources, options = _options.defaultOptions, sourceType = "script") {
|
1046 | let [ast, code] = this.concatenateAndParse(sources, sourceType);
|
1047 | let context = new _realm.ExecutionContext();
|
1048 | context.lexicalEnvironment = this;
|
1049 | context.variableEnvironment = this;
|
1050 | context.realm = this.realm;
|
1051 | this.realm.pushContext(context);
|
1052 | let partialAST;
|
1053 |
|
1054 | try {
|
1055 | [, partialAST] = this.partiallyEvaluateCompletionDeref(ast, false);
|
1056 | } finally {
|
1057 | this.realm.popContext(context);
|
1058 | this.realm.onDestroyScope(context.lexicalEnvironment);
|
1059 | if (!this.destroyed) this.realm.onDestroyScope(this);
|
1060 | (0, _invariant.default)(this.realm.activeLexicalEnvironments.size === 0, `expected 0 active lexical environments, got ${this.realm.activeLexicalEnvironments.size}`);
|
1061 | }
|
1062 |
|
1063 | (0, _invariant.default)(partialAST.type === "File");
|
1064 | let fileAst = partialAST;
|
1065 | let prog = t.program(fileAst.program.body, ast.program.directives);
|
1066 | this.fixup_filenames(prog);
|
1067 |
|
1068 | return (0, _generator.default)(prog, {
|
1069 | sourceMaps: options.sourceMaps
|
1070 | }, code);
|
1071 | }
|
1072 |
|
1073 | execute(code, filename, map = "", sourceType = "script", onParse = undefined) {
|
1074 | let context = new _realm.ExecutionContext();
|
1075 | context.lexicalEnvironment = this;
|
1076 | context.variableEnvironment = this;
|
1077 | context.realm = this.realm;
|
1078 | this.realm.pushContext(context);
|
1079 | let ast, res;
|
1080 |
|
1081 | try {
|
1082 | try {
|
1083 | ast = (0, _parse.default)(this.realm, code, filename, sourceType);
|
1084 | } catch (e) {
|
1085 | if (e instanceof _completions.ThrowCompletion) return e;
|
1086 | throw e;
|
1087 | }
|
1088 |
|
1089 | if (onParse) onParse(ast);
|
1090 | if (map.length > 0) this.fixup_source_locations(ast, map);
|
1091 | this.fixup_filenames(ast);
|
1092 | res = this.evaluateCompletion(ast, false);
|
1093 | } finally {
|
1094 | this.realm.popContext(context);
|
1095 |
|
1096 | if (context.lexicalEnvironment !== this) this.realm.onDestroyScope(context.lexicalEnvironment);
|
1097 | (0, _invariant.default)(this.realm.activeLexicalEnvironments.size === 1, `expected 1 active lexical environment, got ${this.realm.activeLexicalEnvironments.size}`);
|
1098 | }
|
1099 |
|
1100 | if (res instanceof _completions.AbruptCompletion) return res;
|
1101 | return _singletons.Environment.GetValue(this.realm, res);
|
1102 | }
|
1103 |
|
1104 | fixup_source_locations(ast, map) {
|
1105 | const smc = new sourceMap.SourceMapConsumer(map);
|
1106 | (0, _traverseFast.default)(ast, node => {
|
1107 | let loc = node.loc;
|
1108 | if (!loc) return false;
|
1109 | fixup(loc, loc.start);
|
1110 | fixup(loc, loc.end);
|
1111 | fixup_comments(node.leadingComments);
|
1112 | fixup_comments(node.innerComments);
|
1113 | fixup_comments(node.trailingComments);
|
1114 | return false;
|
1115 |
|
1116 | function fixup(new_loc, new_pos) {
|
1117 | let old_pos = smc.originalPositionFor({
|
1118 | line: new_pos.line,
|
1119 | column: new_pos.column
|
1120 | });
|
1121 | if (old_pos.source === null) return;
|
1122 | new_pos.line = old_pos.line;
|
1123 | new_pos.column = old_pos.column;
|
1124 | new_loc.source = old_pos.source;
|
1125 | }
|
1126 |
|
1127 | function fixup_comments(comments) {
|
1128 | if (!comments) return;
|
1129 |
|
1130 | for (let c of comments) {
|
1131 | let cloc = c.loc;
|
1132 | if (!cloc) continue;
|
1133 | fixup(cloc, cloc.start);
|
1134 | fixup(cloc, cloc.end);
|
1135 | }
|
1136 | }
|
1137 | });
|
1138 | }
|
1139 |
|
1140 | fixup_filenames(ast) {
|
1141 | (0, _traverseFast.default)(ast, node => {
|
1142 | let loc = node.loc;
|
1143 |
|
1144 | if (!loc || !loc.source) {
|
1145 | node.leadingComments = null;
|
1146 | node.innerComments = null;
|
1147 | node.trailingComments = null;
|
1148 | node.loc = null;
|
1149 | } else {
|
1150 | let filename = loc.source;
|
1151 | loc.filename = filename;
|
1152 | fixup_comments(node.leadingComments, filename);
|
1153 | fixup_comments(node.innerComments, filename);
|
1154 | fixup_comments(node.trailingComments, filename);
|
1155 | }
|
1156 |
|
1157 | return false;
|
1158 |
|
1159 | function fixup_comments(comments, filename) {
|
1160 | if (!comments) return;
|
1161 |
|
1162 | for (let c of comments) {
|
1163 | if (c.loc) {
|
1164 | c.loc.filename = filename;
|
1165 | c.loc.source = filename;
|
1166 | }
|
1167 | }
|
1168 | }
|
1169 | });
|
1170 | }
|
1171 |
|
1172 | evaluate(ast, strictCode, metadata) {
|
1173 | if (this.realm.debuggerInstance) {
|
1174 | this.realm.debuggerInstance.checkForActions(ast);
|
1175 | }
|
1176 |
|
1177 | let res = this.evaluateAbstract(ast, strictCode, metadata);
|
1178 | (0, _invariant.default)(res instanceof _index.Value || res instanceof Reference, ast.type);
|
1179 | return res;
|
1180 | }
|
1181 |
|
1182 | evaluateAbstract(ast, strictCode, metadata) {
|
1183 | this.realm.currentLocation = ast.loc;
|
1184 | this.realm.testTimeout();
|
1185 | let evaluator = this.realm.evaluators[ast.type];
|
1186 |
|
1187 | if (evaluator) {
|
1188 | this.realm.statistics.evaluatedNodes++;
|
1189 | let result = evaluator(ast, strictCode, this, this.realm, metadata);
|
1190 | return result;
|
1191 | }
|
1192 |
|
1193 | throw new TypeError(`Unsupported node type ${ast.type}`);
|
1194 | }
|
1195 |
|
1196 | evaluateDeref(ast, strictCode, metadata) {
|
1197 | let result = this.evaluate(ast, strictCode, metadata);
|
1198 | if (result instanceof Reference) result = _singletons.Environment.GetValue(this.realm, result);
|
1199 | return result;
|
1200 | }
|
1201 |
|
1202 | partiallyEvaluate(ast, strictCode, metadata) {
|
1203 | let partialEvaluator = this.realm.partialEvaluators[ast.type];
|
1204 |
|
1205 | if (partialEvaluator) {
|
1206 | return partialEvaluator(ast, strictCode, this, this.realm, metadata);
|
1207 | }
|
1208 |
|
1209 | let err = new TypeError(`Unsupported node type ${ast.type}`);
|
1210 | throw err;
|
1211 | }
|
1212 |
|
1213 | }
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 | exports.LexicalEnvironment = LexicalEnvironment;
|
1221 |
|
1222 | function mightBecomeAnObject(base) {
|
1223 | let type = base.getType();
|
1224 |
|
1225 |
|
1226 | return type === _index.Value || type === _PrimitiveValue.default || type === _index.BooleanValue || type === _index.StringValue || type === _index.SymbolValue || type === _index.NumberValue || type === _index.IntegralValue;
|
1227 | }
|
1228 |
|
1229 | class Reference {
|
1230 | constructor(base, refName, strict, thisValue) {
|
1231 | (0, _invariant.default)(base instanceof _index.AbstractObjectValue || base === undefined || base instanceof _index.ObjectValue || base instanceof EnvironmentRecord || mightBecomeAnObject(base));
|
1232 | this.base = base;
|
1233 | this.referencedName = refName;
|
1234 | (0, _invariant.default)(!(refName instanceof _index.AbstractValue) || !(refName.mightNotBeString() && refName.mightNotBeNumber() && !refName.isSimpleObject()) || refName.$Realm.isInPureScope());
|
1235 | this.strict = strict;
|
1236 | this.thisValue = thisValue;
|
1237 | (0, _invariant.default)(thisValue === undefined || !(base instanceof EnvironmentRecord));
|
1238 | }
|
1239 |
|
1240 | }
|
1241 |
|
1242 | exports.Reference = Reference;
|
1243 |
|
\ | No newline at end of file |