1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
---|
4 | var _assert = _interopRequireDefault(require("assert"));
|
---|
5 | var _emit = require("./emit");
|
---|
6 | var _util = require("util");
|
---|
7 | var _util2 = require("./util");
|
---|
8 | /**
|
---|
9 | * Copyright (c) 2014-present, Facebook, Inc.
|
---|
10 | *
|
---|
11 | * This source code is licensed under the MIT license found in the
|
---|
12 | * LICENSE file in the root directory of this source tree.
|
---|
13 | */
|
---|
14 |
|
---|
15 | function Entry() {
|
---|
16 | _assert["default"].ok(this instanceof Entry);
|
---|
17 | }
|
---|
18 | function FunctionEntry(returnLoc) {
|
---|
19 | Entry.call(this);
|
---|
20 | (0, _util2.getTypes)().assertLiteral(returnLoc);
|
---|
21 | this.returnLoc = returnLoc;
|
---|
22 | }
|
---|
23 | (0, _util.inherits)(FunctionEntry, Entry);
|
---|
24 | exports.FunctionEntry = FunctionEntry;
|
---|
25 | function LoopEntry(breakLoc, continueLoc, label) {
|
---|
26 | Entry.call(this);
|
---|
27 | var t = (0, _util2.getTypes)();
|
---|
28 | t.assertLiteral(breakLoc);
|
---|
29 | t.assertLiteral(continueLoc);
|
---|
30 | if (label) {
|
---|
31 | t.assertIdentifier(label);
|
---|
32 | } else {
|
---|
33 | label = null;
|
---|
34 | }
|
---|
35 | this.breakLoc = breakLoc;
|
---|
36 | this.continueLoc = continueLoc;
|
---|
37 | this.label = label;
|
---|
38 | }
|
---|
39 | (0, _util.inherits)(LoopEntry, Entry);
|
---|
40 | exports.LoopEntry = LoopEntry;
|
---|
41 | function SwitchEntry(breakLoc) {
|
---|
42 | Entry.call(this);
|
---|
43 | (0, _util2.getTypes)().assertLiteral(breakLoc);
|
---|
44 | this.breakLoc = breakLoc;
|
---|
45 | }
|
---|
46 | (0, _util.inherits)(SwitchEntry, Entry);
|
---|
47 | exports.SwitchEntry = SwitchEntry;
|
---|
48 | function TryEntry(firstLoc, catchEntry, finallyEntry) {
|
---|
49 | Entry.call(this);
|
---|
50 | var t = (0, _util2.getTypes)();
|
---|
51 | t.assertLiteral(firstLoc);
|
---|
52 | if (catchEntry) {
|
---|
53 | _assert["default"].ok(catchEntry instanceof CatchEntry);
|
---|
54 | } else {
|
---|
55 | catchEntry = null;
|
---|
56 | }
|
---|
57 | if (finallyEntry) {
|
---|
58 | _assert["default"].ok(finallyEntry instanceof FinallyEntry);
|
---|
59 | } else {
|
---|
60 | finallyEntry = null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // Have to have one or the other (or both).
|
---|
64 | _assert["default"].ok(catchEntry || finallyEntry);
|
---|
65 | this.firstLoc = firstLoc;
|
---|
66 | this.catchEntry = catchEntry;
|
---|
67 | this.finallyEntry = finallyEntry;
|
---|
68 | }
|
---|
69 | (0, _util.inherits)(TryEntry, Entry);
|
---|
70 | exports.TryEntry = TryEntry;
|
---|
71 | function CatchEntry(firstLoc, paramId) {
|
---|
72 | Entry.call(this);
|
---|
73 | var t = (0, _util2.getTypes)();
|
---|
74 | t.assertLiteral(firstLoc);
|
---|
75 | t.assertIdentifier(paramId);
|
---|
76 | this.firstLoc = firstLoc;
|
---|
77 | this.paramId = paramId;
|
---|
78 | }
|
---|
79 | (0, _util.inherits)(CatchEntry, Entry);
|
---|
80 | exports.CatchEntry = CatchEntry;
|
---|
81 | function FinallyEntry(firstLoc, afterLoc) {
|
---|
82 | Entry.call(this);
|
---|
83 | var t = (0, _util2.getTypes)();
|
---|
84 | t.assertLiteral(firstLoc);
|
---|
85 | t.assertLiteral(afterLoc);
|
---|
86 | this.firstLoc = firstLoc;
|
---|
87 | this.afterLoc = afterLoc;
|
---|
88 | }
|
---|
89 | (0, _util.inherits)(FinallyEntry, Entry);
|
---|
90 | exports.FinallyEntry = FinallyEntry;
|
---|
91 | function LabeledEntry(breakLoc, label) {
|
---|
92 | Entry.call(this);
|
---|
93 | var t = (0, _util2.getTypes)();
|
---|
94 | t.assertLiteral(breakLoc);
|
---|
95 | t.assertIdentifier(label);
|
---|
96 | this.breakLoc = breakLoc;
|
---|
97 | this.label = label;
|
---|
98 | }
|
---|
99 | (0, _util.inherits)(LabeledEntry, Entry);
|
---|
100 | exports.LabeledEntry = LabeledEntry;
|
---|
101 | function LeapManager(emitter) {
|
---|
102 | _assert["default"].ok(this instanceof LeapManager);
|
---|
103 | _assert["default"].ok(emitter instanceof _emit.Emitter);
|
---|
104 | this.emitter = emitter;
|
---|
105 | this.entryStack = [new FunctionEntry(emitter.finalLoc)];
|
---|
106 | }
|
---|
107 | var LMp = LeapManager.prototype;
|
---|
108 | exports.LeapManager = LeapManager;
|
---|
109 | LMp.withEntry = function (entry, callback) {
|
---|
110 | _assert["default"].ok(entry instanceof Entry);
|
---|
111 | this.entryStack.push(entry);
|
---|
112 | try {
|
---|
113 | callback.call(this.emitter);
|
---|
114 | } finally {
|
---|
115 | var popped = this.entryStack.pop();
|
---|
116 | _assert["default"].strictEqual(popped, entry);
|
---|
117 | }
|
---|
118 | };
|
---|
119 | LMp._findLeapLocation = function (property, label) {
|
---|
120 | for (var i = this.entryStack.length - 1; i >= 0; --i) {
|
---|
121 | var entry = this.entryStack[i];
|
---|
122 | var loc = entry[property];
|
---|
123 | if (loc) {
|
---|
124 | if (label) {
|
---|
125 | if (entry.label && entry.label.name === label.name) {
|
---|
126 | return loc;
|
---|
127 | }
|
---|
128 | } else if (entry instanceof LabeledEntry) {
|
---|
129 | // Ignore LabeledEntry entries unless we are actually breaking to
|
---|
130 | // a label.
|
---|
131 | } else {
|
---|
132 | return loc;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | return null;
|
---|
137 | };
|
---|
138 | LMp.getBreakLoc = function (label) {
|
---|
139 | return this._findLeapLocation("breakLoc", label);
|
---|
140 | };
|
---|
141 | LMp.getContinueLoc = function (label) {
|
---|
142 | return this._findLeapLocation("continueLoc", label);
|
---|
143 | }; |
---|