1 // Copyright 2010 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
29 // Get the Debug object exposed from the debug context global object.
33 function ParsedResponse(json) {
34 this.response_ = eval('(' + json + ')');
36 if (this.response_.refs) {
37 for (var i = 0; i < this.response_.refs.length; i++) {
38 this.refs_[this.response_.refs[i].handle] = this.response_.refs[i];
44 ParsedResponse.prototype.response = function() {
45 return this.response_;
49 ParsedResponse.prototype.body = function() {
50 return this.response_.body;
54 ParsedResponse.prototype.running = function() {
55 return this.response_.running;
59 ParsedResponse.prototype.lookup = function(handle) {
60 return this.refs_[handle];
64 listener_complete = false;
67 expected_return_value = 0;
68 debugger_source_position = 0;
70 // Listener which expects to do four steps to reach returning from the function.
71 function listener(event, exec_state, event_data, data) {
73 if (event == Debug.DebugEvent.Break)
76 if (break_count < 4) {
77 assertFalse(exec_state.frame(0).isAtReturn())
78 switch (break_count) {
80 // Collect the position of the debugger statement.
81 debugger_source_position = exec_state.frame(0).sourcePosition();
84 // Position now at the if statement.
85 assertEquals(debugger_source_position + 10,
86 exec_state.frame(0).sourcePosition());
89 // Position now at either of the returns.
90 if (expected_return_value == 1) {
91 assertEquals(debugger_source_position + 19,
92 exec_state.frame(0).sourcePosition());
94 assertEquals(debugger_source_position + 38,
95 exec_state.frame(0).sourcePosition());
101 exec_state.prepareStep(Debug.StepAction.StepIn, 1);
103 // Position at the end of the function.
104 assertEquals(debugger_source_position + 50,
105 exec_state.frame(0).sourcePosition());
107 // Just about to return from the function.
108 assertTrue(exec_state.frame(0).isAtReturn())
109 assertEquals(expected_return_value,
110 exec_state.frame(0).returnValue().value());
112 // Check the same using the JSON commands.
113 var dcp = exec_state.debugCommandProcessor(false);
114 var request = '{"seq":0,"type":"request","command":"backtrace"}';
115 var resp = dcp.processDebugJSONRequest(request);
116 response = new ParsedResponse(resp);
117 frames = response.body().frames;
118 assertTrue(frames[0].atReturn);
119 assertEquals(expected_return_value,
120 response.lookup(frames[0].returnValue.ref).value);
122 listener_complete = true;
130 // Add the debug event listener.
131 Debug.setListener(listener);
133 // Four steps from the debugger statement in this function will position us at
134 // the function return.
136 // 0123456789012345678901234567890123456789012345678901
138 function f(x) {debugger; if (x) { return 1; } else { return 2; } };
140 // Call f expecting different return values.
142 expected_return_value = 2;
143 listener_complete = false;
145 assertFalse(exception, "exception in listener")
146 assertTrue(listener_complete);
147 assertEquals(4, break_count);
150 expected_return_value = 1;
151 listener_complete = false;
153 assertFalse(exception, "exception in listener")
154 assertTrue(listener_complete);
155 assertEquals(4, break_count);
158 expected_return_value = 2;
159 listener_complete = false;
161 assertFalse(exception, "exception in listener")
162 assertTrue(listener_complete);
163 assertEquals(4, break_count);