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
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.
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.
28 // Flags: --expose-debug-as debug --nocrankshaft
29 // Get the Debug object exposed from the debug context global object.
32 function DebuggerStatement() {
36 function TestCase(fun, frame_number) {
37 var exception = false;
38 var codeSnippet = undefined;
39 var resultPositions = undefined;
42 function listener(event, exec_state, event_data, data) {
44 if (event == Debug.DebugEvent.Break ||
45 event == Debug.DebugEvent.Exception) {
46 if (step++ > 0) return;
47 assertHasLineMark(/pause/, exec_state.frame(0));
48 assertHasLineMark(/positions/, exec_state.frame(frame_number));
49 var frame = exec_state.frame(frame_number);
50 codeSnippet = frame.sourceLineText();
51 resultPositions = frame.stepInPositions();
57 function assertHasLineMark(mark, frame) {
58 var line = frame.sourceLineText();
59 if (!mark.exec(frame.sourceLineText())) {
60 throw new Error("Line " + line + " should contain mark " + mark);
65 Debug.setListener(listener);
69 Debug.setListener(null);
71 assertTrue(!exception, exception);
73 var expectedPositions = {};
74 var markPattern = new RegExp("/\\*#\\*/", "g");
77 while ( (matchResult = markPattern.exec(codeSnippet)) ) {
78 expectedPositions[matchResult.index] = true;
83 var decoratedResult = codeSnippet;
85 function replaceStringRange(s, pos, substitute) {
86 return s.substring(0, pos) + substitute +
87 s.substring(pos + substitute.length);
91 var unexpectedPositionFound = false;
93 for (var i = 0; i < resultPositions.length; i++) {
94 var col = resultPositions[i].position.column - markLength;
95 if (expectedPositions[col]) {
96 delete expectedPositions[col];
97 decoratedResult = replaceStringRange(decoratedResult, col, "*YES*");
99 decoratedResult = replaceStringRange(decoratedResult, col, "!BAD!");
100 unexpectedPositionFound = true;
104 print(decoratedResult);
106 for (var n in expectedPositions) {
107 assertTrue(false, "Some positions are not reported: " + decoratedResult);
110 assertFalse(unexpectedPositionFound, "Found unexpected position: " +
114 function TestCaseWithDebugger(fun) {
118 function TestCaseWithBreakpoint(fun, line_number, frame_number) {
119 var breakpointId = Debug.setBreakPoint(fun, line_number);
120 TestCase(fun, frame_number);
121 Debug.clearBreakPoint(breakpointId);
124 function TestCaseWithException(fun, frame_number) {
125 Debug.setBreakOnException();
126 TestCase(fun, frame_number);
127 Debug.clearBreakOnException();
133 // Step in position, when the function call that we are standing at is already
135 var fun = function() {
137 throw String(p); /*pause*/
140 var res = [ g(1), /*#*/g(2) ]; /*positions*/
144 TestCaseWithBreakpoint(fun, 2, 1);
145 TestCaseWithException(fun, 1);
148 // Step in position, when the function call that we are standing at is raising
150 var fun = function() {
157 var res = [ /*#*/f(1), /*#*/g(2) ]; /*pause, positions*/
161 TestCaseWithException(fun, 0);
164 // Step-in position, when already paused almost on the first call site.
165 var fun = function() {
170 var res = [ /*#*/g(Math.rand), /*#*/g(2) ]; /*pause, positions*/
174 TestCaseWithBreakpoint(fun, 5, 0);
176 // Step-in position, when already paused on the first call site.
177 var fun = function() {
182 var res = [ /*#*/g(), /*#*/g() ]; /*pause, positions*/
186 TestCaseWithBreakpoint(fun, 5, 0);
190 var fun = function() {
194 var res = [ DebuggerStatement(), data./*#*/a(), data[/*#*/String("a")]/*#*/(), data["a"]/*#*/(), data.a, data["a"] ]; /*positions*/
196 TestCaseWithDebugger(fun);
198 // Function call on a value.
199 var fun = function() {
203 var res = [ DebuggerStatement(), /*#*/g(2), /*#*/g(2)/*#*/(3), /*#*/g(0)/*#*/(0)/*#*/(g) ]; /*positions*/
205 TestCaseWithDebugger(fun);
207 // Local function call, closure function call,
208 // local function construction call.
209 var fun = (function(p) {
213 var res = /*#*/f(DebuggerStatement(), /*#*/p(/*#*/new f())); /*positions*/
216 TestCaseWithDebugger(fun);
218 // Global function, global object construction, calls before pause point.
219 var fun = (function(p) {
221 var res = [ Math.abs(new Object()), DebuggerStatement(), Math./*#*/abs(4), /*#*/new Object()./*#*/toString() ]; /*positions*/
224 TestCaseWithDebugger(fun);