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.
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 // Also ignore synthetic variable from catch block.
148 if (!scope.scopeObject().property('.catch-var').isUndefined()) {
151 // Skip property with empty name.
152 if (!scope.scopeObject().property('').isUndefined()) {
155 // Also ignore synthetic variable from block scopes.
156 if (!scope.scopeObject().property('.block').isUndefined()) {
160 if (count != scope_size) {
161 print('Names found in scope:');
162 var names = scope.scopeObject().propertyNames();
163 for (var i = 0; i < names.length; i++) {
167 assertEquals(count, scope_size);
169 // Get the debug command processor.
170 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
172 // Send a scope request for information on a single scope and check the
174 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
175 request_json += scope.scopeIndex();
176 request_json += '}}';
177 var response_json = dcp.processDebugJSONRequest(request_json);
178 var response = JSON.parse(response_json);
179 assertEquals(scope.scopeType(), response.body.type);
180 assertEquals(number, response.body.index);
181 if (scope.scopeType() == debug.ScopeType.Local ||
182 scope.scopeType() == debug.ScopeType.Closure) {
183 assertTrue(response.body.object.ref < 0);
185 assertTrue(response.body.object.ref >= 0);
188 for (var i = 0; i < response.refs.length && !found; i++) {
189 found = response.refs[i].handle == response.body.object.ref;
191 assertTrue(found, "Scope object " + response.body.object.ref + " not found");
195 // Simple empty block scope in local scope.
196 BeginTest("Local block 1");
198 function local_block_1() {
204 listener_delegate = function(exec_state) {
205 CheckScopeChain([debug.ScopeType.Local,
206 debug.ScopeType.Global], exec_state);
207 CheckScopeContent({}, 0, exec_state);
213 // Simple empty block scope in local scope with a parameter.
214 BeginTest("Local 2");
216 function local_2(a) {
222 listener_delegate = function(exec_state) {
223 CheckScopeChain([debug.ScopeType.Local,
224 debug.ScopeType.Global], exec_state);
225 CheckScopeContent({a:1}, 0, exec_state);
231 // Local scope with a parameter and a local variable.
232 BeginTest("Local 3");
234 function local_3(a) {
239 listener_delegate = function(exec_state) {
240 CheckScopeChain([debug.ScopeType.Local,
241 debug.ScopeType.Global], exec_state);
242 CheckScopeContent({a:1,x:3}, 0, exec_state);
248 // Local scope with parameters and local variables.
249 BeginTest("Local 4");
251 function local_4(a, b) {
257 listener_delegate = function(exec_state) {
258 CheckScopeChain([debug.ScopeType.Local,
259 debug.ScopeType.Global], exec_state);
260 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
266 // Single variable in a block scope.
267 BeginTest("Local 5");
269 function local_5(a) {
276 listener_delegate = function(exec_state) {
277 CheckScopeChain([debug.ScopeType.Block,
278 debug.ScopeType.Local,
279 debug.ScopeType.Global], exec_state);
280 CheckScopeContent({x:5}, 0, exec_state);
281 CheckScopeContent({a:1}, 1, exec_state);
287 // Two variables in a block scope.
288 BeginTest("Local 6");
290 function local_6(a) {
298 listener_delegate = function(exec_state) {
299 CheckScopeChain([debug.ScopeType.Block,
300 debug.ScopeType.Local,
301 debug.ScopeType.Global], exec_state);
302 CheckScopeContent({x:6,y:7}, 0, exec_state);
303 CheckScopeContent({a:1}, 1, exec_state);
309 // Two variables in a block scope.
310 BeginTest("Local 7");
312 function local_7(a) {
321 listener_delegate = function(exec_state) {
322 CheckScopeChain([debug.ScopeType.Block,
323 debug.ScopeType.Local,
324 debug.ScopeType.Global], exec_state);
325 CheckScopeContent({x:8}, 0, exec_state);
326 CheckScopeContent({a:1}, 1, exec_state);
332 // Single empty with block.
333 BeginTest("With block 1");
335 function with_block_1() {
341 listener_delegate = function(exec_state) {
342 CheckScopeChain([debug.ScopeType.With,
343 debug.ScopeType.Local,
344 debug.ScopeType.Global], exec_state);
345 CheckScopeContent({}, 0, exec_state);
346 CheckScopeContent({}, 1, exec_state);
352 // Nested empty with blocks.
353 BeginTest("With block 2");
355 function with_block_2() {
363 listener_delegate = function(exec_state) {
364 CheckScopeChain([debug.ScopeType.With,
365 debug.ScopeType.With,
366 debug.ScopeType.Local,
367 debug.ScopeType.Global], exec_state);
368 CheckScopeContent({}, 0, exec_state);
369 CheckScopeContent({}, 1, exec_state);
370 CheckScopeContent({}, 2, exec_state);
376 // With block using an in-place object literal.
377 BeginTest("With block 3");
379 function with_block_3() {
385 listener_delegate = function(exec_state) {
386 CheckScopeChain([debug.ScopeType.With,
387 debug.ScopeType.Local,
388 debug.ScopeType.Global], exec_state);
389 CheckScopeContent({a:1,b:2}, 0, exec_state);
395 // Nested with blocks using in-place object literals.
396 BeginTest("With block 4");
398 function with_block_4() {
406 listener_delegate = function(exec_state) {
407 CheckScopeChain([debug.ScopeType.With,
408 debug.ScopeType.With,
409 debug.ScopeType.Local,
410 debug.ScopeType.Global], exec_state);
411 CheckScopeContent({a:2,b:1}, 0, exec_state);
412 CheckScopeContent({a:1,b:2}, 1, exec_state);
418 // With block and a block local variable.
419 BeginTest("With block 5");
421 function with_block_5() {
428 listener_delegate = function(exec_state) {
429 CheckScopeChain([debug.ScopeType.Block,
430 debug.ScopeType.With,
431 debug.ScopeType.Local,
432 debug.ScopeType.Global], exec_state);
433 CheckScopeContent({a:2}, 0, exec_state);
434 CheckScopeContent({a:1}, 1, exec_state);
440 // Simple closure formed by returning an inner function referering to an outer
441 // block local variable and an outer function's parameter.
442 BeginTest("Closure 1");
444 function closure_1(a) {
451 return a + x + y + z;
457 listener_delegate = function(exec_state) {
458 CheckScopeChain([debug.ScopeType.Local,
459 debug.ScopeType.Block,
460 debug.ScopeType.Closure,
461 debug.ScopeType.Global], exec_state);
462 CheckScopeContent({}, 0, exec_state);
463 CheckScopeContent({a:1,x:2,y:3}, 2, exec_state);
469 // Simple for-in loop over the keys of an object.
470 BeginTest("For loop 1");
472 function for_loop_1() {
473 for (let x in {y:undefined}) {
478 listener_delegate = function(exec_state) {
479 CheckScopeChain([debug.ScopeType.Block,
480 debug.ScopeType.Local,
481 debug.ScopeType.Global], exec_state);
482 CheckScopeContent({x:'y'}, 0, exec_state);
483 // The function scope contains a temporary iteration variable.
484 CheckScopeContent({x:'y'}, 1, exec_state);
490 // For-in loop over the keys of an object with a block scoped let variable
491 // shadowing the iteration variable.
492 BeginTest("For loop 2");
494 function for_loop_2() {
495 for (let x in {y:undefined}) {
501 listener_delegate = function(exec_state) {
502 CheckScopeChain([debug.ScopeType.Block,
503 debug.ScopeType.Block,
504 debug.ScopeType.Local,
505 debug.ScopeType.Global], exec_state);
506 CheckScopeContent({x:3}, 0, exec_state);
507 CheckScopeContent({x:'y'}, 1, exec_state);
508 // The function scope contains a temporary iteration variable.
509 CheckScopeContent({x:'y'}, 2, exec_state);
516 BeginTest("For loop 3");
518 function for_loop_3() {
519 for (let x = 3; x < 4; ++x) {
524 listener_delegate = function(exec_state) {
525 CheckScopeChain([debug.ScopeType.Block,
526 debug.ScopeType.Local,
527 debug.ScopeType.Global], exec_state);
528 CheckScopeContent({x:3}, 0, exec_state);
529 CheckScopeContent({}, 1, exec_state);
535 // For loop with a block scoped let variable shadowing the iteration variable.
536 BeginTest("For loop 4");
538 function for_loop_4() {
539 for (let x = 3; x < 4; ++x) {
545 listener_delegate = function(exec_state) {
546 CheckScopeChain([debug.ScopeType.Block,
547 debug.ScopeType.Block,
548 debug.ScopeType.Local,
549 debug.ScopeType.Global], exec_state);
550 CheckScopeContent({x:5}, 0, exec_state);
551 CheckScopeContent({x:3}, 1, exec_state);
552 CheckScopeContent({}, 2, exec_state);
558 // For loop with two variable declarations.
559 BeginTest("For loop 5");
561 function for_loop_5() {
562 for (let x = 3, y = 5; x < 4; ++x) {
567 listener_delegate = function(exec_state) {
568 CheckScopeChain([debug.ScopeType.Block,
569 debug.ScopeType.Local,
570 debug.ScopeType.Global], exec_state);
571 CheckScopeContent({x:3,y:5}, 0, exec_state);
572 CheckScopeContent({}, 1, exec_state);