leap.js 4.0 KB

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