1 | "use strict";
|
2 |
|
3 | var _assert = require("assert");
|
4 |
|
5 |
|
6 | var _assert2 = _interopRequireDefault(_assert);
|
7 |
|
8 | var _babelTypes = require("babel-types");
|
9 |
|
10 |
|
11 | var t = _interopRequireWildcard(_babelTypes);
|
12 |
|
13 | var _util = require("util");
|
14 |
|
15 |
|
16 | 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | function Entry() {
|
21 | _assert2.default.ok(this instanceof Entry);
|
22 | } |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function FunctionEntry(returnLoc) {
|
33 | Entry.call(this);
|
34 | t.assertLiteral(returnLoc);
|
35 | this.returnLoc = returnLoc;
|
36 | }
|
37 |
|
38 | (0, _util.inherits)(FunctionEntry, Entry);
|
39 | exports.FunctionEntry = FunctionEntry;
|
40 |
|
41 | function LoopEntry(breakLoc, continueLoc, label) {
|
42 | Entry.call(this);
|
43 |
|
44 | t.assertLiteral(breakLoc);
|
45 | t.assertLiteral(continueLoc);
|
46 |
|
47 | if (label) {
|
48 | t.assertIdentifier(label);
|
49 | } else {
|
50 | label = null;
|
51 | }
|
52 |
|
53 | this.breakLoc = breakLoc;
|
54 | this.continueLoc = continueLoc;
|
55 | this.label = label;
|
56 | }
|
57 |
|
58 | (0, _util.inherits)(LoopEntry, Entry);
|
59 | exports.LoopEntry = LoopEntry;
|
60 |
|
61 | function SwitchEntry(breakLoc) {
|
62 | Entry.call(this);
|
63 | t.assertLiteral(breakLoc);
|
64 | this.breakLoc = breakLoc;
|
65 | }
|
66 |
|
67 | (0, _util.inherits)(SwitchEntry, Entry);
|
68 | exports.SwitchEntry = SwitchEntry;
|
69 |
|
70 | function TryEntry(firstLoc, catchEntry, finallyEntry) {
|
71 | Entry.call(this);
|
72 |
|
73 | t.assertLiteral(firstLoc);
|
74 |
|
75 | if (catchEntry) {
|
76 | _assert2.default.ok(catchEntry instanceof CatchEntry);
|
77 | } else {
|
78 | catchEntry = null;
|
79 | }
|
80 |
|
81 | if (finallyEntry) {
|
82 | _assert2.default.ok(finallyEntry instanceof FinallyEntry);
|
83 | } else {
|
84 | finallyEntry = null;
|
85 | }
|
86 |
|
87 |
|
88 | _assert2.default.ok(catchEntry || finallyEntry);
|
89 |
|
90 | this.firstLoc = firstLoc;
|
91 | this.catchEntry = catchEntry;
|
92 | this.finallyEntry = finallyEntry;
|
93 | }
|
94 |
|
95 | (0, _util.inherits)(TryEntry, Entry);
|
96 | exports.TryEntry = TryEntry;
|
97 |
|
98 | function CatchEntry(firstLoc, paramId) {
|
99 | Entry.call(this);
|
100 |
|
101 | t.assertLiteral(firstLoc);
|
102 | t.assertIdentifier(paramId);
|
103 |
|
104 | this.firstLoc = firstLoc;
|
105 | this.paramId = paramId;
|
106 | }
|
107 |
|
108 | (0, _util.inherits)(CatchEntry, Entry);
|
109 | exports.CatchEntry = CatchEntry;
|
110 |
|
111 | function FinallyEntry(firstLoc, afterLoc) {
|
112 | Entry.call(this);
|
113 | t.assertLiteral(firstLoc);
|
114 | t.assertLiteral(afterLoc);
|
115 | this.firstLoc = firstLoc;
|
116 | this.afterLoc = afterLoc;
|
117 | }
|
118 |
|
119 | (0, _util.inherits)(FinallyEntry, Entry);
|
120 | exports.FinallyEntry = FinallyEntry;
|
121 |
|
122 | function LabeledEntry(breakLoc, label) {
|
123 | Entry.call(this);
|
124 |
|
125 | t.assertLiteral(breakLoc);
|
126 | t.assertIdentifier(label);
|
127 |
|
128 | this.breakLoc = breakLoc;
|
129 | this.label = label;
|
130 | }
|
131 |
|
132 | (0, _util.inherits)(LabeledEntry, Entry);
|
133 | exports.LabeledEntry = LabeledEntry;
|
134 |
|
135 | function LeapManager(emitter) {
|
136 | _assert2.default.ok(this instanceof LeapManager);
|
137 |
|
138 | var Emitter = require("./emit").Emitter;
|
139 | _assert2.default.ok(emitter instanceof Emitter);
|
140 |
|
141 | this.emitter = emitter;
|
142 | this.entryStack = [new FunctionEntry(emitter.finalLoc)];
|
143 | }
|
144 |
|
145 | var LMp = LeapManager.prototype;
|
146 | exports.LeapManager = LeapManager;
|
147 |
|
148 | LMp.withEntry = function (entry, callback) {
|
149 | _assert2.default.ok(entry instanceof Entry);
|
150 | this.entryStack.push(entry);
|
151 | try {
|
152 | callback.call(this.emitter);
|
153 | } finally {
|
154 | var popped = this.entryStack.pop();
|
155 | _assert2.default.strictEqual(popped, entry);
|
156 | }
|
157 | };
|
158 |
|
159 | LMp._findLeapLocation = function (property, label) {
|
160 | for (var i = this.entryStack.length - 1; i >= 0; --i) {
|
161 | var entry = this.entryStack[i];
|
162 | var loc = entry[property];
|
163 | if (loc) {
|
164 | if (label) {
|
165 | if (entry.label && entry.label.name === label.name) {
|
166 | return loc;
|
167 | }
|
168 | } else if (entry instanceof LabeledEntry) {
|
169 |
|
170 |
|
171 | } else {
|
172 | return loc;
|
173 | }
|
174 | }
|
175 | }
|
176 |
|
177 | return null;
|
178 | };
|
179 |
|
180 | LMp.getBreakLoc = function (label) {
|
181 | return this._findLeapLocation("breakLoc", label);
|
182 | };
|
183 |
|
184 | LMp.getContinueLoc = function (label) {
|
185 | return this._findLeapLocation("continueLoc", label);
|
186 | }; |
\ | No newline at end of file |