UNPKG

4.99 kBJavaScriptView Raw
1/*istanbul ignore next*/"use strict";
2
3var /*istanbul ignore next*/_assert = require("assert");
4
5/*istanbul ignore next*/
6var _assert2 = _interopRequireDefault(_assert);
7
8var /*istanbul ignore next*/_babelTypes = require("babel-types");
9
10/*istanbul ignore next*/
11var t = _interopRequireWildcard(_babelTypes);
12
13var /*istanbul ignore next*/_util = require("util");
14
15/*istanbul ignore next*/
16function _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
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function Entry() {
21 /*istanbul ignore next*/_assert2.default.ok(this instanceof Entry);
22} /**
23 * Copyright (c) 2014, Facebook, Inc.
24 * All rights reserved.
25 *
26 * This source code is licensed under the BSD-style license found in the
27 * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
28 * additional grant of patent rights can be found in the PATENTS file in
29 * the same directory.
30 */
31
32function FunctionEntry(returnLoc) {
33 Entry.call(this);
34 t.assertLiteral(returnLoc);
35 this.returnLoc = returnLoc;
36}
37
38/*istanbul ignore next*/(0, _util.inherits)(FunctionEntry, Entry);
39exports.FunctionEntry = FunctionEntry;
40
41function 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/*istanbul ignore next*/(0, _util.inherits)(LoopEntry, Entry);
59exports.LoopEntry = LoopEntry;
60
61function SwitchEntry(breakLoc) {
62 Entry.call(this);
63 t.assertLiteral(breakLoc);
64 this.breakLoc = breakLoc;
65}
66
67/*istanbul ignore next*/(0, _util.inherits)(SwitchEntry, Entry);
68exports.SwitchEntry = SwitchEntry;
69
70function TryEntry(firstLoc, catchEntry, finallyEntry) {
71 Entry.call(this);
72
73 t.assertLiteral(firstLoc);
74
75 if (catchEntry) {
76 /*istanbul ignore next*/_assert2.default.ok(catchEntry instanceof CatchEntry);
77 } else {
78 catchEntry = null;
79 }
80
81 if (finallyEntry) {
82 /*istanbul ignore next*/_assert2.default.ok(finallyEntry instanceof FinallyEntry);
83 } else {
84 finallyEntry = null;
85 }
86
87 // Have to have one or the other (or both).
88 /*istanbul ignore next*/_assert2.default.ok(catchEntry || finallyEntry);
89
90 this.firstLoc = firstLoc;
91 this.catchEntry = catchEntry;
92 this.finallyEntry = finallyEntry;
93}
94
95/*istanbul ignore next*/(0, _util.inherits)(TryEntry, Entry);
96exports.TryEntry = TryEntry;
97
98function 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/*istanbul ignore next*/(0, _util.inherits)(CatchEntry, Entry);
109exports.CatchEntry = CatchEntry;
110
111function 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/*istanbul ignore next*/(0, _util.inherits)(FinallyEntry, Entry);
120exports.FinallyEntry = FinallyEntry;
121
122function 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/*istanbul ignore next*/(0, _util.inherits)(LabeledEntry, Entry);
133exports.LabeledEntry = LabeledEntry;
134
135function LeapManager(emitter) {
136 /*istanbul ignore next*/_assert2.default.ok(this instanceof LeapManager);
137
138 var Emitter = require("./emit").Emitter;
139 /*istanbul ignore next*/_assert2.default.ok(emitter instanceof Emitter);
140
141 this.emitter = emitter;
142 this.entryStack = [new FunctionEntry(emitter.finalLoc)];
143}
144
145var LMp = LeapManager.prototype;
146exports.LeapManager = LeapManager;
147
148LMp.withEntry = function (entry, callback) {
149 /*istanbul ignore next*/_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 /*istanbul ignore next*/_assert2.default.strictEqual(popped, entry);
156 }
157};
158
159LMp._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 // Ignore LabeledEntry entries unless we are actually breaking to
170 // a label.
171 } else {
172 return loc;
173 }
174 }
175 }
176
177 return null;
178};
179
180LMp.getBreakLoc = function (label) {
181 return this._findLeapLocation("breakLoc", label);
182};
183
184LMp.getContinueLoc = function (label) {
185 return this._findLeapLocation("continueLoc", label);
186};
\No newline at end of file