PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/v8/test/mjsunit/debug-stepin-positions.js

https://github.com/dshaw/node
JavaScript | 225 lines | 149 code | 36 blank | 40 comment | 9 complexity | 1962db6e639d9966079d55228ab1fe97 MD5 | raw file
  1. // Copyright 2008 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. // Flags: --expose-debug-as debug --nocrankshaft
  28. // Get the Debug object exposed from the debug context global object.
  29. Debug = debug.Debug
  30. function DebuggerStatement() {
  31. debugger; /*pause*/
  32. }
  33. function TestCase(fun, frame_number) {
  34. var exception = false;
  35. var codeSnippet = undefined;
  36. var resultPositions = undefined;
  37. function listener(event, exec_state, event_data, data) {
  38. try {
  39. if (event == Debug.DebugEvent.Break ||
  40. event == Debug.DebugEvent.Exception) {
  41. Debug.setListener(null);
  42. assertHasLineMark(/pause/, exec_state.frame(0));
  43. assertHasLineMark(/positions/, exec_state.frame(frame_number));
  44. var frame = exec_state.frame(frame_number);
  45. codeSnippet = frame.sourceLineText();
  46. resultPositions = frame.stepInPositions();
  47. }
  48. } catch (e) {
  49. exception = e
  50. }
  51. function assertHasLineMark(mark, frame) {
  52. var line = frame.sourceLineText();
  53. if (!mark.exec(frame.sourceLineText())) {
  54. throw new Error("Line " + line + " should contain mark " + mark);
  55. }
  56. }
  57. }
  58. Debug.setListener(listener);
  59. fun();
  60. Debug.setListener(null);
  61. assertTrue(!exception, exception);
  62. var expectedPositions = {};
  63. var markPattern = new RegExp("/\\*#\\*/", "g");
  64. var matchResult;
  65. while ( (matchResult = markPattern.exec(codeSnippet)) ) {
  66. expectedPositions[matchResult.index] = true;
  67. }
  68. print(codeSnippet);
  69. var decoratedResult = codeSnippet;
  70. function replaceStringRange(s, pos, substitute) {
  71. return s.substring(0, pos) + substitute +
  72. s.substring(pos + substitute.length);
  73. }
  74. var markLength = 5;
  75. var unexpectedPositionFound = false;
  76. for (var i = 0; i < resultPositions.length; i++) {
  77. var col = resultPositions[i].position.column - markLength;
  78. if (expectedPositions[col]) {
  79. delete expectedPositions[col];
  80. decoratedResult = replaceStringRange(decoratedResult, col, "*YES*");
  81. } else {
  82. decoratedResult = replaceStringRange(decoratedResult, col, "!BAD!");
  83. unexpectedPositionFound = true;
  84. }
  85. }
  86. print(decoratedResult);
  87. for (var n in expectedPositions) {
  88. assertTrue(false, "Some positions are not reported: " + decoratedResult);
  89. break;
  90. }
  91. assertFalse(unexpectedPositionFound, "Found unexpected position: " +
  92. decoratedResult);
  93. }
  94. function TestCaseWithDebugger(fun) {
  95. TestCase(fun, 1);
  96. }
  97. function TestCaseWithBreakpoint(fun, line_number, frame_number) {
  98. var breakpointId = Debug.setBreakPoint(fun, line_number);
  99. TestCase(fun, frame_number);
  100. Debug.clearBreakPoint(breakpointId);
  101. }
  102. function TestCaseWithException(fun, frame_number) {
  103. Debug.setBreakOnException();
  104. TestCase(fun, frame_number);
  105. Debug.clearBreakOnException();
  106. }
  107. // Test cases.
  108. // Step in position, when the function call that we are standing at is already
  109. // being executed.
  110. var fun = function() {
  111. function g(p) {
  112. throw String(p); /*pause*/
  113. }
  114. try {
  115. var res = [ g(1), /*#*/g(2) ]; /*positions*/
  116. } catch (e) {
  117. }
  118. };
  119. TestCaseWithBreakpoint(fun, 2, 1);
  120. TestCaseWithException(fun, 1);
  121. // Step in position, when the function call that we are standing at is raising
  122. // an exception.
  123. var fun = function() {
  124. var o = {
  125. g: function(p) {
  126. throw p;
  127. }
  128. };
  129. try {
  130. var res = [ /*#*/f(1), /*#*/g(2) ]; /*pause, positions*/
  131. } catch (e) {
  132. }
  133. };
  134. TestCaseWithException(fun, 0);
  135. // Step-in position, when already paused almost on the first call site.
  136. var fun = function() {
  137. function g(p) {
  138. throw p;
  139. }
  140. try {
  141. var res = [ /*#*/g(Math.rand), /*#*/g(2) ]; /*pause, positions*/
  142. } catch (e) {
  143. }
  144. };
  145. TestCaseWithBreakpoint(fun, 5, 0);
  146. // Step-in position, when already paused on the first call site.
  147. var fun = function() {
  148. function g() {
  149. throw "Debug";
  150. }
  151. try {
  152. var res = [ /*#*/g(), /*#*/g() ]; /*pause, positions*/
  153. } catch (e) {
  154. }
  155. };
  156. TestCaseWithBreakpoint(fun, 5, 0);
  157. // Method calls.
  158. var fun = function() {
  159. var data = {
  160. a: function() {}
  161. };
  162. var res = [ DebuggerStatement(), data./*#*/a(), data[/*#*/String("a")]/*#*/(), data["a"]/*#*/(), data.a, data["a"] ]; /*positions*/
  163. };
  164. TestCaseWithDebugger(fun);
  165. // Function call on a value.
  166. var fun = function() {
  167. function g(p) {
  168. return g;
  169. }
  170. var res = [ DebuggerStatement(), /*#*/g(2), /*#*/g(2)/*#*/(3), /*#*/g(0)/*#*/(0)/*#*/(g) ]; /*positions*/
  171. };
  172. TestCaseWithDebugger(fun);
  173. // Local function call, closure function call,
  174. // local function construction call.
  175. var fun = (function(p) {
  176. return function() {
  177. function f(a, b) {
  178. }
  179. var res = /*#*/f(DebuggerStatement(), /*#*/p(/*#*/new f())); /*positions*/
  180. };
  181. })(Object);
  182. TestCaseWithDebugger(fun);
  183. // Global function, global object construction, calls before pause point.
  184. var fun = (function(p) {
  185. return function() {
  186. var res = [ Math.abs(new Object()), DebuggerStatement(), Math./*#*/abs(4), /*#*/new Object()./*#*/toString() ]; /*positions*/
  187. };
  188. })(Object);
  189. TestCaseWithDebugger(fun);