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 --harmony-scoping
29 // The functions used for testing backtraces. They are at the top to make the
30 // testing of source line/column easier.
32 // TODO(ES6): properly activate extended mode
35 // Get the Debug object exposed from the debug context global object.
36 var Debug = debug.Debug;
39 var listener_delegate;
42 var begin_test_count = 0;
43 var end_test_count = 0;
47 // Debug event listener which delegates.
48 function listener(event, exec_state, event_data, data) {
50 if (event == Debug.DebugEvent.Break) {
52 listener_called = true;
53 listener_delegate(exec_state);
60 // Add the debug event listener.
61 Debug.setListener(listener);
64 // Initialize for a new test.
65 function BeginTest(name) {
67 listener_delegate = null;
68 listener_called = false;
74 // Check result of a test.
76 assertTrue(listener_called, "listerner not called for " + test_name);
77 assertNull(exception, test_name);
81 var global_object = this;
83 // Check that the scope chain contains the expected types of scopes.
84 function CheckScopeChain(scopes, exec_state) {
85 assertEquals(scopes.length, exec_state.frame().scopeCount());
86 for (var i = 0; i < scopes.length; i++) {
87 var scope = exec_state.frame().scope(i);
88 assertTrue(scope.isScope());
89 assertEquals(scopes[i], scope.scopeType());
91 // Check the global object when hitting the global scope.
92 if (scopes[i] == debug.ScopeType.Global) {
93 // Objects don't have same class (one is "global", other is "Object",
94 // so just check the properties directly.
95 assertPropertiesEqual(global_object, scope.scopeObject().value());
99 // Get the debug command processor.
100 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
102 // Send a scopes request and check the result.
104 var request_json = '{"seq":0,"type":"request","command":"scopes"}';
105 var response_json = dcp.processDebugJSONRequest(request_json);
106 var response = JSON.parse(response_json);
107 assertEquals(scopes.length, response.body.scopes.length);
108 for (var i = 0; i < scopes.length; i++) {
109 assertEquals(i, response.body.scopes[i].index);
110 assertEquals(scopes[i], response.body.scopes[i].type);
111 if (scopes[i] == debug.ScopeType.Local ||
112 scopes[i] == debug.ScopeType.Closure) {
113 assertTrue(response.body.scopes[i].object.ref < 0);
115 assertTrue(response.body.scopes[i].object.ref >= 0);
118 for (var j = 0; j < response.refs.length && !found; j++) {
119 found = response.refs[j].handle == response.body.scopes[i].object.ref;
121 assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
125 // Check that the content of the scope is as expected. For functions just check
126 // that there is a function.
127 function CheckScopeContent(content, number, exec_state) {
128 var scope = exec_state.frame().scope(number);
130 for (var p in content) {
131 var property_mirror = scope.scopeObject().property(p);
132 if (property_mirror.isUndefined()) {
133 print('property ' + p + ' not found in scope');
135 assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
136 if (typeof(content[p]) === 'function') {
137 assertTrue(property_mirror.value().isFunction());
139 assertEquals(content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
144 // 'arguments' and might be exposed in the local and closure scope. Just
146 var scope_size = scope.scopeObject().properties().length;
147 if (!scope.scopeObject().property('arguments').isUndefined()) {
150 // Skip property with empty name.
151 if (!scope.scopeObject().property('').isUndefined()) {
155 if (count != scope_size) {
156 print('Names found in scope:');
157 var names = scope.scopeObject().propertyNames();
158 for (var i = 0; i < names.length; i++) {
162 assertEquals(count, scope_size);
164 // Get the debug command processor.
165 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
167 // Send a scope request for information on a single scope and check the
169 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
170 request_json += scope.scopeIndex();
171 request_json += '}}';
172 var response_json = dcp.processDebugJSONRequest(request_json);
173 var response = JSON.parse(response_json);
174 assertEquals(scope.scopeType(), response.body.type);
175 assertEquals(number, response.body.index);
176 if (scope.scopeType() == debug.ScopeType.Local ||
177 scope.scopeType() == debug.ScopeType.Closure) {
178 assertTrue(response.body.object.ref < 0);
180 assertTrue(response.body.object.ref >= 0);
183 for (var i = 0; i < response.refs.length && !found; i++) {
184 found = response.refs[i].handle == response.body.object.ref;
186 assertTrue(found, "Scope object " + response.body.object.ref + " not found");
190 // Simple empty block scope in local scope.
191 BeginTest("Local block 1");
193 function local_block_1() {
199 listener_delegate = function(exec_state) {
200 CheckScopeChain([debug.ScopeType.Local,
201 debug.ScopeType.Global], exec_state);
202 CheckScopeContent({}, 0, exec_state);
208 // Simple empty block scope in local scope with a parameter.
209 BeginTest("Local 2");
211 function local_2(a) {
217 listener_delegate = function(exec_state) {
218 CheckScopeChain([debug.ScopeType.Local,
219 debug.ScopeType.Global], exec_state);
220 CheckScopeContent({a:1}, 0, exec_state);
226 // Local scope with a parameter and a local variable.
227 BeginTest("Local 3");
229 function local_3(a) {
234 listener_delegate = function(exec_state) {
235 CheckScopeChain([debug.ScopeType.Local,
236 debug.ScopeType.Global], exec_state);
237 CheckScopeContent({a:1,x:3}, 0, exec_state);
243 // Local scope with parameters and local variables.
244 BeginTest("Local 4");
246 function local_4(a, b) {
252 listener_delegate = function(exec_state) {
253 CheckScopeChain([debug.ScopeType.Local,
254 debug.ScopeType.Global], exec_state);
255 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
261 // Single variable in a block scope.
262 BeginTest("Local 5");
264 function local_5(a) {
271 listener_delegate = function(exec_state) {
272 CheckScopeChain([debug.ScopeType.Block,
273 debug.ScopeType.Local,
274 debug.ScopeType.Global], exec_state);
275 CheckScopeContent({x:5}, 0, exec_state);
276 CheckScopeContent({a:1}, 1, exec_state);
282 // Two variables in a block scope.
283 BeginTest("Local 6");
285 function local_6(a) {
293 listener_delegate = function(exec_state) {
294 CheckScopeChain([debug.ScopeType.Block,
295 debug.ScopeType.Local,
296 debug.ScopeType.Global], exec_state);
297 CheckScopeContent({x:6,y:7}, 0, exec_state);
298 CheckScopeContent({a:1}, 1, exec_state);
304 // Two variables in a block scope.
305 BeginTest("Local 7");
307 function local_7(a) {
316 listener_delegate = function(exec_state) {
317 CheckScopeChain([debug.ScopeType.Block,
318 debug.ScopeType.Local,
319 debug.ScopeType.Global], exec_state);
320 CheckScopeContent({x:8}, 0, exec_state);
321 CheckScopeContent({a:1}, 1, exec_state);
327 // Simple closure formed by returning an inner function referering to an outer
328 // block local variable and an outer function's parameter.
329 BeginTest("Closure 1");
331 function closure_1(a) {
338 return a + x + y + z;
344 listener_delegate = function(exec_state) {
345 CheckScopeChain([debug.ScopeType.Local,
346 debug.ScopeType.Block,
347 debug.ScopeType.Closure,
348 debug.ScopeType.Global], exec_state);
349 CheckScopeContent({}, 0, exec_state);
350 CheckScopeContent({a:1,x:2,y:3}, 2, exec_state);
356 // Simple for-in loop over the keys of an object.
357 BeginTest("For loop 1");
359 function for_loop_1() {
360 for (let x in {y:undefined}) {
365 listener_delegate = function(exec_state) {
366 CheckScopeChain([debug.ScopeType.Block,
367 debug.ScopeType.Local,
368 debug.ScopeType.Global], exec_state);
369 CheckScopeContent({x:'y'}, 0, exec_state);
370 // The function scope contains a temporary iteration variable, but it is
371 // hidden to the debugger.
372 CheckScopeContent({}, 1, exec_state);
378 // For-in loop over the keys of an object with a block scoped let variable
379 // shadowing the iteration variable.
380 BeginTest("For loop 2");
382 function for_loop_2() {
383 for (let x in {y:undefined}) {
389 listener_delegate = function(exec_state) {
390 CheckScopeChain([debug.ScopeType.Block,
391 debug.ScopeType.Block,
392 debug.ScopeType.Local,
393 debug.ScopeType.Global], exec_state);
394 CheckScopeContent({x:3}, 0, exec_state);
395 CheckScopeContent({x:'y'}, 1, exec_state);
396 // The function scope contains a temporary iteration variable, hidden to the
398 CheckScopeContent({}, 2, exec_state);
405 BeginTest("For loop 3");
407 function for_loop_3() {
408 for (let x = 3; x < 4; ++x) {
413 listener_delegate = function(exec_state) {
414 CheckScopeChain([debug.ScopeType.Block,
415 debug.ScopeType.Local,
416 debug.ScopeType.Global], exec_state);
417 CheckScopeContent({x:3}, 0, exec_state);
418 CheckScopeContent({}, 1, exec_state);
424 // For loop with a block scoped let variable shadowing the iteration variable.
425 BeginTest("For loop 4");
427 function for_loop_4() {
428 for (let x = 3; x < 4; ++x) {
434 listener_delegate = function(exec_state) {
435 CheckScopeChain([debug.ScopeType.Block,
436 debug.ScopeType.Block,
437 debug.ScopeType.Local,
438 debug.ScopeType.Global], exec_state);
439 CheckScopeContent({x:5}, 0, exec_state);
440 CheckScopeContent({x:3}, 1, exec_state);
441 CheckScopeContent({}, 2, exec_state);
447 // For loop with two variable declarations.
448 BeginTest("For loop 5");
450 function for_loop_5() {
451 for (let x = 3, y = 5; x < 4; ++x) {
456 listener_delegate = function(exec_state) {
457 CheckScopeChain([debug.ScopeType.Block,
458 debug.ScopeType.Local,
459 debug.ScopeType.Global], exec_state);
460 CheckScopeContent({x:3,y:5}, 0, exec_state);
461 CheckScopeContent({}, 1, exec_state);