You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.1 KiB

4 years ago
  1. "use strict";
  2. var _assert = _interopRequireDefault(require("assert"));
  3. var _emit = require("./emit");
  4. var _util = require("util");
  5. var _util2 = require("./util");
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  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. } // Have to have one or the other (or both).
  60. _assert["default"].ok(catchEntry || finallyEntry);
  61. this.firstLoc = firstLoc;
  62. this.catchEntry = catchEntry;
  63. this.finallyEntry = finallyEntry;
  64. }
  65. (0, _util.inherits)(TryEntry, Entry);
  66. exports.TryEntry = TryEntry;
  67. function CatchEntry(firstLoc, paramId) {
  68. Entry.call(this);
  69. var t = (0, _util2.getTypes)();
  70. t.assertLiteral(firstLoc);
  71. t.assertIdentifier(paramId);
  72. this.firstLoc = firstLoc;
  73. this.paramId = paramId;
  74. }
  75. (0, _util.inherits)(CatchEntry, Entry);
  76. exports.CatchEntry = CatchEntry;
  77. function FinallyEntry(firstLoc, afterLoc) {
  78. Entry.call(this);
  79. var t = (0, _util2.getTypes)();
  80. t.assertLiteral(firstLoc);
  81. t.assertLiteral(afterLoc);
  82. this.firstLoc = firstLoc;
  83. this.afterLoc = afterLoc;
  84. }
  85. (0, _util.inherits)(FinallyEntry, Entry);
  86. exports.FinallyEntry = FinallyEntry;
  87. function LabeledEntry(breakLoc, label) {
  88. Entry.call(this);
  89. var t = (0, _util2.getTypes)();
  90. t.assertLiteral(breakLoc);
  91. t.assertIdentifier(label);
  92. this.breakLoc = breakLoc;
  93. this.label = label;
  94. }
  95. (0, _util.inherits)(LabeledEntry, Entry);
  96. exports.LabeledEntry = LabeledEntry;
  97. function LeapManager(emitter) {
  98. _assert["default"].ok(this instanceof LeapManager);
  99. _assert["default"].ok(emitter instanceof _emit.Emitter);
  100. this.emitter = emitter;
  101. this.entryStack = [new FunctionEntry(emitter.finalLoc)];
  102. }
  103. var LMp = LeapManager.prototype;
  104. exports.LeapManager = LeapManager;
  105. LMp.withEntry = function (entry, callback) {
  106. _assert["default"].ok(entry instanceof Entry);
  107. this.entryStack.push(entry);
  108. try {
  109. callback.call(this.emitter);
  110. } finally {
  111. var popped = this.entryStack.pop();
  112. _assert["default"].strictEqual(popped, entry);
  113. }
  114. };
  115. LMp._findLeapLocation = function (property, label) {
  116. for (var i = this.entryStack.length - 1; i >= 0; --i) {
  117. var entry = this.entryStack[i];
  118. var loc = entry[property];
  119. if (loc) {
  120. if (label) {
  121. if (entry.label && entry.label.name === label.name) {
  122. return loc;
  123. }
  124. } else if (entry instanceof LabeledEntry) {// Ignore LabeledEntry entries unless we are actually breaking to
  125. // a label.
  126. } else {
  127. return loc;
  128. }
  129. }
  130. }
  131. return null;
  132. };
  133. LMp.getBreakLoc = function (label) {
  134. return this._findLeapLocation("breakLoc", label);
  135. };
  136. LMp.getContinueLoc = function (label) {
  137. return this._findLeapLocation("continueLoc", label);
  138. };