source: trip-planner-front/node_modules/regenerator-transform/src/leap.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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