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.
34 // Get the Debug object exposed from the debug context global object.
35 var Debug = debug.Debug;
38 var listener_delegate;
41 var begin_test_count = 0;
42 var end_test_count = 0;
46 // Debug event listener which delegates.
47 function listener(event, exec_state, event_data, data) {
49 if (event == Debug.DebugEvent.Break) {
51 listener_called = true;
52 listener_delegate(exec_state);
59 // Add the debug event listener.
60 Debug.setListener(listener);
63 // Initialize for a new test.
64 function BeginTest(name) {
66 listener_delegate = null;
67 listener_called = false;
73 // Check result of a test.
75 assertTrue(listener_called, "listerner not called for " + test_name);
76 assertNull(exception, test_name, exception);
80 var global_object = this;
82 // Check that the scope chain contains the expected types of scopes.
83 function CheckScopeChain(scopes, exec_state) {
84 assertEquals(scopes.length, exec_state.frame().scopeCount());
85 for (var i = 0; i < scopes.length; i++) {
86 var scope = exec_state.frame().scope(i);
87 assertTrue(scope.isScope());
88 assertEquals(scopes[i], scope.scopeType());
90 // Check the global object when hitting the global scope.
91 if (scopes[i] == debug.ScopeType.Global) {
92 // Objects don't have same class (one is "global", other is "Object",
93 // so just check the properties directly.
94 assertPropertiesEqual(global_object, scope.scopeObject().value());
98 // Get the debug command processor.
99 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
101 // Send a scopes request and check the result.
103 var request_json = '{"seq":0,"type":"request","command":"scopes"}';
104 var response_json = dcp.processDebugJSONRequest(request_json);
105 var response = JSON.parse(response_json);
106 assertEquals(scopes.length, response.body.scopes.length);
107 for (var i = 0; i < scopes.length; i++) {
108 assertEquals(i, response.body.scopes[i].index);
109 assertEquals(scopes[i], response.body.scopes[i].type);
110 if (scopes[i] == debug.ScopeType.Local ||
111 scopes[i] == debug.ScopeType.Script ||
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.Script,
202 debug.ScopeType.Global], exec_state);
203 CheckScopeContent({}, 0, exec_state);
209 // Simple empty block scope in local scope with a parameter.
210 BeginTest("Local 2");
212 function local_2(a) {
218 listener_delegate = function(exec_state) {
219 CheckScopeChain([debug.ScopeType.Local,
220 debug.ScopeType.Script,
221 debug.ScopeType.Global], exec_state);
222 CheckScopeContent({a:1}, 0, exec_state);
228 // Local scope with a parameter and a local variable.
229 BeginTest("Local 3");
231 function local_3(a) {
236 listener_delegate = function(exec_state) {
237 CheckScopeChain([debug.ScopeType.Local,
238 debug.ScopeType.Script,
239 debug.ScopeType.Global], exec_state);
240 CheckScopeContent({a:1,x:3}, 0, exec_state);
246 // Local scope with parameters and local variables.
247 BeginTest("Local 4");
249 function local_4(a, b) {
255 listener_delegate = function(exec_state) {
256 CheckScopeChain([debug.ScopeType.Local,
257 debug.ScopeType.Script,
258 debug.ScopeType.Global], exec_state);
259 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
265 // Single variable in a block scope.
266 BeginTest("Local 5");
268 function local_5(a) {
275 listener_delegate = function(exec_state) {
276 CheckScopeChain([debug.ScopeType.Block,
277 debug.ScopeType.Local,
278 debug.ScopeType.Script,
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.Script,
302 debug.ScopeType.Global], exec_state);
303 CheckScopeContent({x:6,y:7}, 0, exec_state);
304 CheckScopeContent({a:1}, 1, exec_state);
310 // Two variables in a block scope.
311 BeginTest("Local 7");
313 function local_7(a) {
322 listener_delegate = function(exec_state) {
323 CheckScopeChain([debug.ScopeType.Block,
324 debug.ScopeType.Local,
325 debug.ScopeType.Script,
326 debug.ScopeType.Global], exec_state);
327 CheckScopeContent({x:8}, 0, exec_state);
328 CheckScopeContent({a:1}, 1, exec_state);
334 // Simple closure formed by returning an inner function referering to an outer
335 // block local variable and an outer function's parameter.
336 BeginTest("Closure 1");
338 function closure_1(a) {
345 return a + x + y + z;
351 listener_delegate = function(exec_state) {
352 CheckScopeChain([debug.ScopeType.Local,
353 debug.ScopeType.Block,
354 debug.ScopeType.Closure,
355 debug.ScopeType.Script,
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.Script,
377 debug.ScopeType.Global], exec_state);
378 CheckScopeContent({x:'y'}, 0, exec_state);
379 // The function scope contains a temporary iteration variable, but it is
380 // hidden to the debugger.
381 CheckScopeContent({}, 1, exec_state);
387 // For-in loop over the keys of an object with a block scoped let variable
388 // shadowing the iteration variable.
389 BeginTest("For loop 2");
391 function for_loop_2() {
392 for (let x in {y:undefined}) {
398 listener_delegate = function(exec_state) {
399 CheckScopeChain([debug.ScopeType.Block,
400 debug.ScopeType.Block,
401 debug.ScopeType.Local,
402 debug.ScopeType.Script,
403 debug.ScopeType.Global], exec_state);
404 CheckScopeContent({x:3}, 0, exec_state);
405 CheckScopeContent({x:'y'}, 1, exec_state);
406 // The function scope contains a temporary iteration variable, hidden to the
408 CheckScopeContent({}, 2, exec_state);
415 BeginTest("For loop 3");
417 function for_loop_3() {
418 for (let x = 3; x < 4; ++x) {
423 listener_delegate = function(exec_state) {
424 CheckScopeChain([debug.ScopeType.Block,
425 debug.ScopeType.Block,
426 debug.ScopeType.Local,
427 debug.ScopeType.Script,
428 debug.ScopeType.Global], exec_state);
429 CheckScopeContent({x:3}, 0, exec_state);
430 CheckScopeContent({x:3}, 1, exec_state);
431 CheckScopeContent({}, 2, exec_state);
437 // For loop with a block scoped let variable shadowing the iteration variable.
438 BeginTest("For loop 4");
440 function for_loop_4() {
441 for (let x = 3; x < 4; ++x) {
447 listener_delegate = function(exec_state) {
448 CheckScopeChain([debug.ScopeType.Block,
449 debug.ScopeType.Block,
450 debug.ScopeType.Block,
451 debug.ScopeType.Local,
452 debug.ScopeType.Script,
453 debug.ScopeType.Global], exec_state);
454 CheckScopeContent({x:5}, 0, exec_state);
455 CheckScopeContent({x:3}, 1, exec_state);
456 CheckScopeContent({x:3}, 2, exec_state);
457 CheckScopeContent({}, 3, exec_state);
463 // For loop with two variable declarations.
464 BeginTest("For loop 5");
466 function for_loop_5() {
467 for (let x = 3, y = 5; x < 4; ++x) {
472 listener_delegate = function(exec_state) {
473 CheckScopeChain([debug.ScopeType.Block,
474 debug.ScopeType.Block,
475 debug.ScopeType.Local,
476 debug.ScopeType.Script,
477 debug.ScopeType.Global], exec_state);
478 CheckScopeContent({x:3,y:5}, 0, exec_state);
479 CheckScopeContent({x:3,y:5}, 1, exec_state);
480 CheckScopeContent({}, 2, exec_state);
486 // Uninitialized variables
487 BeginTest("Uninitialized 1");
489 function uninitialized_1() {
496 listener_delegate = function(exec_state) {
497 CheckScopeChain([debug.ScopeType.Block,
498 debug.ScopeType.Local,
499 debug.ScopeType.Script,
500 debug.ScopeType.Global], exec_state);
501 CheckScopeContent({}, 0, exec_state);