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