leap.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Copyright (c) 2014, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  7. * additional grant of patent rights can be found in the PATENTS file in
  8. * the same directory.
  9. */
  10. import assert from "assert";
  11. import * as t from "babel-types";
  12. import { inherits } from "util";
  13. function Entry() {
  14. assert.ok(this instanceof Entry);
  15. }
  16. function FunctionEntry(returnLoc) {
  17. Entry.call(this);
  18. t.assertLiteral(returnLoc);
  19. this.returnLoc = returnLoc;
  20. }
  21. inherits(FunctionEntry, Entry);
  22. exports.FunctionEntry = FunctionEntry;
  23. function LoopEntry(breakLoc, continueLoc, label) {
  24. Entry.call(this);
  25. t.assertLiteral(breakLoc);
  26. t.assertLiteral(continueLoc);
  27. if (label) {
  28. t.assertIdentifier(label);
  29. } else {
  30. label = null;
  31. }
  32. this.breakLoc = breakLoc;
  33. this.continueLoc = continueLoc;
  34. this.label = label;
  35. }
  36. inherits(LoopEntry, Entry);
  37. exports.LoopEntry = LoopEntry;
  38. function SwitchEntry(breakLoc) {
  39. Entry.call(this);
  40. t.assertLiteral(breakLoc);
  41. this.breakLoc = breakLoc;
  42. }
  43. inherits(SwitchEntry, Entry);
  44. exports.SwitchEntry = SwitchEntry;
  45. function TryEntry(firstLoc, catchEntry, finallyEntry) {
  46. Entry.call(this);
  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. t.assertLiteral(firstLoc);
  69. t.assertIdentifier(paramId);
  70. this.firstLoc = firstLoc;
  71. this.paramId = paramId;
  72. }
  73. inherits(CatchEntry, Entry);
  74. exports.CatchEntry = CatchEntry;
  75. function FinallyEntry(firstLoc, afterLoc) {
  76. Entry.call(this);
  77. t.assertLiteral(firstLoc);
  78. t.assertLiteral(afterLoc);
  79. this.firstLoc = firstLoc;
  80. this.afterLoc = afterLoc;
  81. }
  82. inherits(FinallyEntry, Entry);
  83. exports.FinallyEntry = FinallyEntry;
  84. function LabeledEntry(breakLoc, label) {
  85. Entry.call(this);
  86. t.assertLiteral(breakLoc);
  87. t.assertIdentifier(label);
  88. this.breakLoc = breakLoc;
  89. this.label = label;
  90. }
  91. inherits(LabeledEntry, Entry);
  92. exports.LabeledEntry = LabeledEntry;
  93. function LeapManager(emitter) {
  94. assert.ok(this instanceof LeapManager);
  95. let Emitter = require("./emit").Emitter;
  96. assert.ok(emitter instanceof Emitter);
  97. this.emitter = emitter;
  98. this.entryStack = [new FunctionEntry(emitter.finalLoc)];
  99. }
  100. let LMp = LeapManager.prototype;
  101. exports.LeapManager = LeapManager;
  102. LMp.withEntry = function(entry, callback) {
  103. assert.ok(entry instanceof Entry);
  104. this.entryStack.push(entry);
  105. try {
  106. callback.call(this.emitter);
  107. } finally {
  108. let popped = this.entryStack.pop();
  109. assert.strictEqual(popped, entry);
  110. }
  111. };
  112. LMp._findLeapLocation = function(property, label) {
  113. for (let i = this.entryStack.length - 1; i >= 0; --i) {
  114. let entry = this.entryStack[i];
  115. let loc = entry[property];
  116. if (loc) {
  117. if (label) {
  118. if (entry.label &&
  119. entry.label.name === label.name) {
  120. return loc;
  121. }
  122. } else if (entry instanceof LabeledEntry) {
  123. // Ignore LabeledEntry entries unless we are actually breaking to
  124. // a label.
  125. } else {
  126. return loc;
  127. }
  128. }
  129. }
  130. return null;
  131. };
  132. LMp.getBreakLoc = function(label) {
  133. return this._findLeapLocation("breakLoc", label);
  134. };
  135. LMp.getContinueLoc = function(label) {
  136. return this._findLeapLocation("continueLoc", label);
  137. };