1 // Copyright 2011 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 // The functions used for testing backtraces. They are at the top to make the
30 // testing of source line/column easier.
33 // Get the Debug object exposed from the debug context global object.
37 var listener_delegate;
40 var begin_test_count = 0;
41 var end_test_count = 0;
45 // Debug event listener which delegates.
46 function listener(event, exec_state, event_data, data) {
48 if (event == Debug.DebugEvent.Break) {
50 listener_called = true;
51 listener_delegate(exec_state);
58 // Add the debug event listener.
59 Debug.setListener(listener);
62 // Initialize for a new test.
63 function BeginTest(name) {
65 listener_delegate = null;
66 listener_called = false;
72 // Check result of a test.
74 assertTrue(listener_called, "listerner not called for " + test_name);
75 assertNull(exception, test_name);
80 // Check that the scope chain contains the expected types of scopes.
81 function CheckScopeChain(scopes, exec_state) {
82 assertEquals(scopes.length, exec_state.frame().scopeCount());
83 for (var i = 0; i < scopes.length; i++) {
84 var scope = exec_state.frame().scope(i);
85 assertTrue(scope.isScope());
86 assertEquals(scopes[i], scope.scopeType());
88 // Check the global object when hitting the global scope.
89 if (scopes[i] == debug.ScopeType.Global) {
90 // Objects don't have same class (one is "global", other is "Object",
91 // so just check the properties directly.
92 assertPropertiesEqual(this, scope.scopeObject().value());
96 // Get the debug command processor.
97 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
99 // Send a scopes request and check the result.
101 var request_json = '{"seq":0,"type":"request","command":"scopes"}';
102 var response_json = dcp.processDebugJSONRequest(request_json);
103 var response = JSON.parse(response_json);
104 assertEquals(scopes.length, response.body.scopes.length);
105 for (var i = 0; i < scopes.length; i++) {
106 assertEquals(i, response.body.scopes[i].index);
107 assertEquals(scopes[i], response.body.scopes[i].type);
108 if (scopes[i] == debug.ScopeType.Local ||
109 scopes[i] == debug.ScopeType.Closure) {
110 assertTrue(response.body.scopes[i].object.ref < 0);
112 assertTrue(response.body.scopes[i].object.ref >= 0);
115 for (var j = 0; j < response.refs.length && !found; j++) {
116 found = response.refs[j].handle == response.body.scopes[i].object.ref;
118 assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
122 // Check that the content of the scope is as expected. For functions just check
123 // that there is a function.
124 function CheckScopeContent(content, number, exec_state) {
125 var scope = exec_state.frame().scope(number);
127 for (var p in content) {
128 var property_mirror = scope.scopeObject().property(p);
129 if (property_mirror.isUndefined()) {
130 print('property ' + p + ' not found in scope');
132 assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
133 if (typeof(content[p]) === 'function') {
134 assertTrue(property_mirror.value().isFunction());
136 assertEquals(content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
141 // 'arguments' and might be exposed in the local and closure scope. Just
143 var scope_size = scope.scopeObject().properties().length;
144 if (!scope.scopeObject().property('arguments').isUndefined()) {
147 // Skip property with empty name.
148 if (!scope.scopeObject().property('').isUndefined()) {
152 if (count != scope_size) {
153 print('Names found in scope:');
154 var names = scope.scopeObject().propertyNames();
155 for (var i = 0; i < names.length; i++) {
159 assertEquals(count, scope_size);
161 // Get the debug command processor.
162 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
164 // Send a scope request for information on a single scope and check the
166 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
167 request_json += scope.scopeIndex();
168 request_json += '}}';
169 var response_json = dcp.processDebugJSONRequest(request_json);
170 var response = JSON.parse(response_json);
171 assertEquals(scope.scopeType(), response.body.type);
172 assertEquals(number, response.body.index);
173 if (scope.scopeType() == debug.ScopeType.Local ||
174 scope.scopeType() == debug.ScopeType.Closure) {
175 assertTrue(response.body.object.ref < 0);
177 assertTrue(response.body.object.ref >= 0);
180 for (var i = 0; i < response.refs.length && !found; i++) {
181 found = response.refs[i].handle == response.body.object.ref;
183 assertTrue(found, "Scope object " + response.body.object.ref + " not found");
187 // Simple closure formed by returning an inner function referering to an outer
188 // block local variable and an outer function's parameter. Due to VM
189 // optimizations parts of the actual closure is missing from the debugger
191 BeginTest("Closure 1");
193 function closure_1(a) {
200 return a + x + y + z;
206 listener_delegate = function(exec_state) {
207 CheckScopeChain([debug.ScopeType.Local,
208 debug.ScopeType.Block,
209 debug.ScopeType.Closure,
210 debug.ScopeType.Global], exec_state);
211 CheckScopeContent({}, 0, exec_state);
212 CheckScopeContent({z:4}, 1, exec_state);
213 CheckScopeContent({a:1,x:2,y:3}, 2, exec_state);