[6a3a178] | 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 |
|
---|
| 8 | import assert from "assert";
|
---|
| 9 | import { Emitter } from "./emit";
|
---|
| 10 | import { inherits } from "util";
|
---|
| 11 | import { getTypes } from "./util";
|
---|
| 12 |
|
---|
| 13 | function Entry() {
|
---|
| 14 | assert.ok(this instanceof Entry);
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | function FunctionEntry(returnLoc) {
|
---|
| 18 | Entry.call(this);
|
---|
| 19 | getTypes().assertLiteral(returnLoc);
|
---|
| 20 | this.returnLoc = returnLoc;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | inherits(FunctionEntry, Entry);
|
---|
| 24 | exports.FunctionEntry = FunctionEntry;
|
---|
| 25 |
|
---|
| 26 | function 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 |
|
---|
| 45 | inherits(LoopEntry, Entry);
|
---|
| 46 | exports.LoopEntry = LoopEntry;
|
---|
| 47 |
|
---|
| 48 | function SwitchEntry(breakLoc) {
|
---|
| 49 | Entry.call(this);
|
---|
| 50 | getTypes().assertLiteral(breakLoc);
|
---|
| 51 | this.breakLoc = breakLoc;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | inherits(SwitchEntry, Entry);
|
---|
| 55 | exports.SwitchEntry = SwitchEntry;
|
---|
| 56 |
|
---|
| 57 | function 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 |
|
---|
| 83 | inherits(TryEntry, Entry);
|
---|
| 84 | exports.TryEntry = TryEntry;
|
---|
| 85 |
|
---|
| 86 | function 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 |
|
---|
| 98 | inherits(CatchEntry, Entry);
|
---|
| 99 | exports.CatchEntry = CatchEntry;
|
---|
| 100 |
|
---|
| 101 | function 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 |
|
---|
| 110 | inherits(FinallyEntry, Entry);
|
---|
| 111 | exports.FinallyEntry = FinallyEntry;
|
---|
| 112 |
|
---|
| 113 | function 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 |
|
---|
| 125 | inherits(LabeledEntry, Entry);
|
---|
| 126 | exports.LabeledEntry = LabeledEntry;
|
---|
| 127 |
|
---|
| 128 | function 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 |
|
---|
| 137 | let LMp = LeapManager.prototype;
|
---|
| 138 | exports.LeapManager = LeapManager;
|
---|
| 139 |
|
---|
| 140 | LMp.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 |
|
---|
| 151 | LMp._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 |
|
---|
| 173 | LMp.getBreakLoc = function(label) {
|
---|
| 174 | return this._findLeapLocation("breakLoc", label);
|
---|
| 175 | };
|
---|
| 176 |
|
---|
| 177 | LMp.getContinueLoc = function(label) {
|
---|
| 178 | return this._findLeapLocation("continueLoc", label);
|
---|
| 179 | };
|
---|