leap.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. "use strict";
  2. var _assert = require("assert");
  3. var _assert2 = _interopRequireDefault(_assert);
  4. var _babelTypes = require("babel-types");
  5. var t = _interopRequireWildcard(_babelTypes);
  6. var _util = require("util");
  7. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function Entry() {
  10. _assert2.default.ok(this instanceof Entry);
  11. } /**
  12. * Copyright (c) 2014, Facebook, Inc.
  13. * All rights reserved.
  14. *
  15. * This source code is licensed under the BSD-style license found in the
  16. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  17. * additional grant of patent rights can be found in the PATENTS file in
  18. * the same directory.
  19. */
  20. function FunctionEntry(returnLoc) {
  21. Entry.call(this);
  22. t.assertLiteral(returnLoc);
  23. this.returnLoc = returnLoc;
  24. }
  25. (0, _util.inherits)(FunctionEntry, Entry);
  26. exports.FunctionEntry = FunctionEntry;
  27. function LoopEntry(breakLoc, continueLoc, label) {
  28. Entry.call(this);
  29. t.assertLiteral(breakLoc);
  30. t.assertLiteral(continueLoc);
  31. if (label) {
  32. t.assertIdentifier(label);
  33. } else {
  34. label = null;
  35. }
  36. this.breakLoc = breakLoc;
  37. this.continueLoc = continueLoc;
  38. this.label = label;
  39. }
  40. (0, _util.inherits)(LoopEntry, Entry);
  41. exports.LoopEntry = LoopEntry;
  42. function SwitchEntry(breakLoc) {
  43. Entry.call(this);
  44. t.assertLiteral(breakLoc);
  45. this.breakLoc = breakLoc;
  46. }
  47. (0, _util.inherits)(SwitchEntry, Entry);
  48. exports.SwitchEntry = SwitchEntry;
  49. function TryEntry(firstLoc, catchEntry, finallyEntry) {
  50. Entry.call(this);
  51. t.assertLiteral(firstLoc);
  52. if (catchEntry) {
  53. _assert2.default.ok(catchEntry instanceof CatchEntry);
  54. } else {
  55. catchEntry = null;
  56. }
  57. if (finallyEntry) {
  58. _assert2.default.ok(finallyEntry instanceof FinallyEntry);
  59. } else {
  60. finallyEntry = null;
  61. }
  62. // Have to have one or the other (or both).
  63. _assert2.default.ok(catchEntry || finallyEntry);
  64. this.firstLoc = firstLoc;
  65. this.catchEntry = catchEntry;
  66. this.finallyEntry = finallyEntry;
  67. }
  68. (0, _util.inherits)(TryEntry, Entry);
  69. exports.TryEntry = TryEntry;
  70. function CatchEntry(firstLoc, paramId) {
  71. Entry.call(this);
  72. t.assertLiteral(firstLoc);
  73. t.assertIdentifier(paramId);
  74. this.firstLoc = firstLoc;
  75. this.paramId = paramId;
  76. }
  77. (0, _util.inherits)(CatchEntry, Entry);
  78. exports.CatchEntry = CatchEntry;
  79. function FinallyEntry(firstLoc, afterLoc) {
  80. Entry.call(this);
  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. 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. _assert2.default.ok(this instanceof LeapManager);
  99. var Emitter = require("./emit").Emitter;
  100. _assert2.default.ok(emitter instanceof 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. _assert2.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. _assert2.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. };