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 // Also ignore synthetic variable from catch block.
151 if (!scope.scopeObject().property('.catch-var').isUndefined()) {
154 // Skip property with empty name.
155 if (!scope.scopeObject().property('').isUndefined()) {
158 // Also ignore synthetic variable from block scopes.
159 if (!scope.scopeObject().property('.block').isUndefined()) {
163 if (count != scope_size) {
164 print('Names found in scope:');
165 var names = scope.scopeObject().propertyNames();
166 for (var i = 0; i < names.length; i++) {
170 assertEquals(count, scope_size);
172 // Get the debug command processor.
173 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
175 // Send a scope request for information on a single scope and check the
177 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
178 request_json += scope.scopeIndex();
179 request_json += '}}';
180 var response_json = dcp.processDebugJSONRequest(request_json);
181 var response = JSON.parse(response_json);
182 assertEquals(scope.scopeType(), response.body.type);
183 assertEquals(number, response.body.index);
184 if (scope.scopeType() == debug.ScopeType.Local ||
185 scope.scopeType() == debug.ScopeType.Closure) {
186 assertTrue(response.body.object.ref < 0);
188 assertTrue(response.body.object.ref >= 0);
191 for (var i = 0; i < response.refs.length && !found; i++) {
192 found = response.refs[i].handle == response.body.object.ref;
194 assertTrue(found, "Scope object " + response.body.object.ref + " not found");
198 // Simple empty block scope in local scope.
199 BeginTest("Local block 1");
201 function local_block_1() {
207 listener_delegate = function(exec_state) {
208 CheckScopeChain([debug.ScopeType.Local,
209 debug.ScopeType.Global], exec_state);
210 CheckScopeContent({}, 0, exec_state);
216 // Simple empty block scope in local scope with a parameter.
217 BeginTest("Local 2");
219 function local_2(a) {
225 listener_delegate = function(exec_state) {
226 CheckScopeChain([debug.ScopeType.Local,
227 debug.ScopeType.Global], exec_state);
228 CheckScopeContent({a:1}, 0, exec_state);
234 // Local scope with a parameter and a local variable.
235 BeginTest("Local 3");
237 function local_3(a) {
242 listener_delegate = function(exec_state) {
243 CheckScopeChain([debug.ScopeType.Local,
244 debug.ScopeType.Global], exec_state);
245 CheckScopeContent({a:1,x:3}, 0, exec_state);
251 // Local scope with parameters and local variables.
252 BeginTest("Local 4");
254 function local_4(a, b) {
260 listener_delegate = function(exec_state) {
261 CheckScopeChain([debug.ScopeType.Local,
262 debug.ScopeType.Global], exec_state);
263 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
269 // Single variable in a block scope.
270 BeginTest("Local 5");
272 function local_5(a) {
279 listener_delegate = function(exec_state) {
280 CheckScopeChain([debug.ScopeType.Block,
281 debug.ScopeType.Local,
282 debug.ScopeType.Global], exec_state);
283 CheckScopeContent({x:5}, 0, exec_state);
284 CheckScopeContent({a:1}, 1, exec_state);
290 // Two variables in a block scope.
291 BeginTest("Local 6");
293 function local_6(a) {
301 listener_delegate = function(exec_state) {
302 CheckScopeChain([debug.ScopeType.Block,
303 debug.ScopeType.Local,
304 debug.ScopeType.Global], exec_state);
305 CheckScopeContent({x:6,y:7}, 0, exec_state);
306 CheckScopeContent({a:1}, 1, exec_state);
312 // Two variables in a block scope.
313 BeginTest("Local 7");
315 function local_7(a) {
324 listener_delegate = function(exec_state) {
325 CheckScopeChain([debug.ScopeType.Block,
326 debug.ScopeType.Local,
327 debug.ScopeType.Global], exec_state);
328 CheckScopeContent({x:8}, 0, exec_state);
329 CheckScopeContent({a:1}, 1, exec_state);
335 // Simple closure formed by returning an inner function referering to an outer
336 // block local variable and an outer function's parameter.
337 BeginTest("Closure 1");
339 function closure_1(a) {
346 return a + x + y + z;
352 listener_delegate = function(exec_state) {
353 CheckScopeChain([debug.ScopeType.Local,
354 debug.ScopeType.Block,
355 debug.ScopeType.Closure,
356 debug.ScopeType.Global], exec_state);
357 CheckScopeContent({}, 0, exec_state);
358 CheckScopeContent({a:1,x:2,y:3}, 2, exec_state);
364 // Simple for-in loop over the keys of an object.
365 BeginTest("For loop 1");
367 function for_loop_1() {
368 for (let x in {y:undefined}) {
373 listener_delegate = function(exec_state) {
374 CheckScopeChain([debug.ScopeType.Block,
375 debug.ScopeType.Local,
376 debug.ScopeType.Global], exec_state);
377 CheckScopeContent({x:'y'}, 0, exec_state);
378 // The function scope contains a temporary iteration variable.
379 CheckScopeContent({x:'y'}, 1, exec_state);
385 // For-in loop over the keys of an object with a block scoped let variable
386 // shadowing the iteration variable.
387 BeginTest("For loop 2");
389 function for_loop_2() {
390 for (let x in {y:undefined}) {
396 listener_delegate = function(exec_state) {
397 CheckScopeChain([debug.ScopeType.Block,
398 debug.ScopeType.Block,
399 debug.ScopeType.Local,
400 debug.ScopeType.Global], exec_state);
401 CheckScopeContent({x:3}, 0, exec_state);
402 CheckScopeContent({x:'y'}, 1, exec_state);
403 // The function scope contains a temporary iteration variable.
404 CheckScopeContent({x:'y'}, 2, exec_state);
411 BeginTest("For loop 3");
413 function for_loop_3() {
414 for (let x = 3; x < 4; ++x) {
419 listener_delegate = function(exec_state) {
420 CheckScopeChain([debug.ScopeType.Block,
421 debug.ScopeType.Local,
422 debug.ScopeType.Global], exec_state);
423 CheckScopeContent({x:3}, 0, exec_state);
424 CheckScopeContent({}, 1, exec_state);
430 // For loop with a block scoped let variable shadowing the iteration variable.
431 BeginTest("For loop 4");
433 function for_loop_4() {
434 for (let x = 3; x < 4; ++x) {
440 listener_delegate = function(exec_state) {
441 CheckScopeChain([debug.ScopeType.Block,
442 debug.ScopeType.Block,
443 debug.ScopeType.Local,
444 debug.ScopeType.Global], exec_state);
445 CheckScopeContent({x:5}, 0, exec_state);
446 CheckScopeContent({x:3}, 1, exec_state);
447 CheckScopeContent({}, 2, exec_state);
453 // For loop with two variable declarations.
454 BeginTest("For loop 5");
456 function for_loop_5() {
457 for (let x = 3, y = 5; x < 4; ++x) {
462 listener_delegate = function(exec_state) {
463 CheckScopeChain([debug.ScopeType.Block,
464 debug.ScopeType.Local,
465 debug.ScopeType.Global], exec_state);
466 CheckScopeContent({x:3,y:5}, 0, exec_state);
467 CheckScopeContent({}, 1, exec_state);