leap.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. import assert from "assert";
  8. import { Emitter } from "./emit";
  9. import { inherits } from "util";
  10. import { getTypes } from "./util";
  11. function Entry() {
  12. assert.ok(this instanceof Entry);
  13. }
  14. function FunctionEntry(returnLoc) {
  15. Entry.call(this);
  16. getTypes().assertLiteral(returnLoc);
  17. this.returnLoc = returnLoc;
  18. }
  19. inherits(FunctionEntry, Entry);
  20. exports.FunctionEntry = FunctionEntry;
  21. function LoopEntry(breakLoc, continueLoc, label) {
  22. Entry.call(this);
  23. const t = getTypes();
  24. t.assertLiteral(breakLoc);
  25. t.assertLiteral(continueLoc);
  26. if (label) {
  27. t.assertIdentifier(label);
  28. } else {
  29. label = null;
  30. }
  31. this.breakLoc = breakLoc;
  32. this.continueLoc = continueLoc;
  33. this.label = label;
  34. }
  35. inherits(LoopEntry, Entry);
  36. exports.LoopEntry = LoopEntry;
  37. function SwitchEntry(breakLoc) {
  38. Entry.call(this);
  39. getTypes().assertLiteral(breakLoc);
  40. this.breakLoc = breakLoc;
  41. }
  42. inherits(SwitchEntry, Entry);
  43. exports.SwitchEntry = SwitchEntry;
  44. function TryEntry(firstLoc, catchEntry, finallyEntry) {
  45. Entry.call(this);
  46. const t = getTypes();
  47. t.assertLiteral(firstLoc);
  48. if (catchEntry) {
  49. assert.ok(catchEntry instanceof CatchEntry);
  50. } else {
  51. catchEntry = null;
  52. }
  53. if (finallyEntry) {
  54. assert.ok(finallyEntry instanceof FinallyEntry);
  55. } else {
  56. finallyEntry = null;
  57. }
  58. // Have to have one or the other (or both).
  59. assert.ok(catchEntry || finallyEntry);
  60. this.firstLoc = firstLoc;
  61. this.catchEntry = catchEntry;
  62. this.finallyEntry = finallyEntry;
  63. }
  64. inherits(TryEntry, Entry);
  65. exports.TryEntry = TryEntry;
  66. function CatchEntry(firstLoc, paramId) {
  67. Entry.call(this);
  68. const t = getTypes();
  69. t.assertLiteral(firstLoc);
  70. t.assertIdentifier(paramId);
  71. this.firstLoc = firstLoc;
  72. this.paramId = paramId;
  73. }
  74. inherits(CatchEntry, Entry);
  75. exports.CatchEntry = CatchEntry;
  76. function FinallyEntry(firstLoc, afterLoc) {
  77. Entry.call(this);
  78. const t = getTypes();
  79. t.assertLiteral(firstLoc);
  80. t.assertLiteral(afterLoc);
  81. this.firstLoc = firstLoc;
  82. this.afterLoc = afterLoc;
  83. }
  84. inherits(FinallyEntry, Entry);
  85. exports.FinallyEntry = FinallyEntry;
  86. function LabeledEntry(breakLoc, label) {
  87. Entry.call(this);
  88. const t = getTypes();
  89. t.assertLiteral(breakLoc);
  90. t.assertIdentifier(label);
  91. this.breakLoc = breakLoc;
  92. this.label = label;
  93. }
  94. inherits(LabeledEntry, Entry);
  95. exports.LabeledEntry = LabeledEntry;
  96. function LeapManager(emitter) {
  97. assert.ok(this instanceof LeapManager);
  98. assert.ok(emitter instanceof Emitter);
  99. this.emitter = emitter;
  100. this.entryStack = [new FunctionEntry(emitter.finalLoc)];
  101. }
  102. let LMp = LeapManager.prototype;
  103. exports.LeapManager = LeapManager;
  104. LMp.withEntry = function(entry, callback) {
  105. assert.ok(entry instanceof Entry);
  106. this.entryStack.push(entry);
  107. try {
  108. callback.call(this.emitter);
  109. } finally {
  110. let popped = this.entryStack.pop();
  111. assert.strictEqual(popped, entry);
  112. }
  113. };
  114. LMp._findLeapLocation = function(property, label) {
  115. for (let i = this.entryStack.length - 1; i >= 0; --i) {
  116. let entry = this.entryStack[i];
  117. let loc = entry[property];
  118. if (loc) {
  119. if (label) {
  120. if (entry.label &&
  121. entry.label.name === label.name) {
  122. return loc;
  123. }
  124. } else if (entry instanceof LabeledEntry) {
  125. // Ignore LabeledEntry entries unless we are actually breaking to
  126. // a label.
  127. } else {
  128. return loc;
  129. }
  130. }
  131. }
  132. return null;
  133. };
  134. LMp.getBreakLoc = function(label) {
  135. return this._findLeapLocation("breakLoc", label);
  136. };
  137. LMp.getContinueLoc = function(label) {
  138. return this._findLeapLocation("continueLoc", label);
  139. };