blob: 8180377e6d399a8d48d9fc2ecdf10b9ab69028eb [file] [log] [blame]
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001// 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
4// met:
5//
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.
15//
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.
27
Ben Murdoch3ef787d2012-04-12 10:51:47 +010028// Flags: --expose-debug-as debug --harmony-scoping
Ben Murdoch69a99ed2011-11-30 16:03:39 +000029// The functions used for testing backtraces. They are at the top to make the
30// testing of source line/column easier.
31
Ben Murdoch3ef787d2012-04-12 10:51:47 +010032"use strict";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000033
34// Get the Debug object exposed from the debug context global object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010035var Debug = debug.Debug;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000036
37var test_name;
38var listener_delegate;
39var listener_called;
40var exception;
41var begin_test_count = 0;
42var end_test_count = 0;
43var break_count = 0;
44
45
46// Debug event listener which delegates.
47function listener(event, exec_state, event_data, data) {
48 try {
49 if (event == Debug.DebugEvent.Break) {
50 break_count++;
51 listener_called = true;
52 listener_delegate(exec_state);
53 }
54 } catch (e) {
55 exception = e;
56 }
57}
58
59// Add the debug event listener.
60Debug.setListener(listener);
61
62
63// Initialize for a new test.
64function BeginTest(name) {
65 test_name = name;
66 listener_delegate = null;
67 listener_called = false;
68 exception = null;
69 begin_test_count++;
70}
71
72
73// Check result of a test.
74function EndTest() {
75 assertTrue(listener_called, "listerner not called for " + test_name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040076 assertNull(exception, test_name, exception);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000077 end_test_count++;
78}
79
Ben Murdoch3ef787d2012-04-12 10:51:47 +010080var global_object = this;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000081
82// Check that the scope chain contains the expected types of scopes.
83function 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());
89
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.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010094 assertPropertiesEqual(global_object, scope.scopeObject().value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +000095 }
96 }
97
98 // Get the debug command processor.
99 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
100
101 // Send a scopes request and check the result.
102 var json;
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 ||
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400111 scopes[i] == debug.ScopeType.Script ||
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000112 scopes[i] == debug.ScopeType.Closure) {
113 assertTrue(response.body.scopes[i].object.ref < 0);
114 } else {
115 assertTrue(response.body.scopes[i].object.ref >= 0);
116 }
117 var found = false;
118 for (var j = 0; j < response.refs.length && !found; j++) {
119 found = response.refs[j].handle == response.body.scopes[i].object.ref;
120 }
121 assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
122 }
123}
124
125// Check that the content of the scope is as expected. For functions just check
126// that there is a function.
127function CheckScopeContent(content, number, exec_state) {
128 var scope = exec_state.frame().scope(number);
129 var count = 0;
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');
134 }
135 assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
136 if (typeof(content[p]) === 'function') {
137 assertTrue(property_mirror.value().isFunction());
138 } else {
139 assertEquals(content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
140 }
141 count++;
142 }
143
144 // 'arguments' and might be exposed in the local and closure scope. Just
145 // ignore this.
146 var scope_size = scope.scopeObject().properties().length;
147 if (!scope.scopeObject().property('arguments').isUndefined()) {
148 scope_size--;
149 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000150 // Skip property with empty name.
151 if (!scope.scopeObject().property('').isUndefined()) {
152 scope_size--;
153 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000154
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++) {
159 print(names[i]);
160 }
161 }
162 assertEquals(count, scope_size);
163
164 // Get the debug command processor.
165 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
166
167 // Send a scope request for information on a single scope and check the
168 // result.
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);
179 } else {
180 assertTrue(response.body.object.ref >= 0);
181 }
182 var found = false;
183 for (var i = 0; i < response.refs.length && !found; i++) {
184 found = response.refs[i].handle == response.body.object.ref;
185 }
186 assertTrue(found, "Scope object " + response.body.object.ref + " not found");
187}
188
189
190// Simple empty block scope in local scope.
191BeginTest("Local block 1");
192
193function local_block_1() {
194 {
195 debugger;
196 }
197}
198
199listener_delegate = function(exec_state) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000200 CheckScopeChain([debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400201 debug.ScopeType.Script,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000202 debug.ScopeType.Global], exec_state);
203 CheckScopeContent({}, 0, exec_state);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000204};
205local_block_1();
206EndTest();
207
208
Ben Murdoch589d6972011-11-30 16:04:58 +0000209// Simple empty block scope in local scope with a parameter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000210BeginTest("Local 2");
211
212function local_2(a) {
213 {
214 debugger;
215 }
216}
217
218listener_delegate = function(exec_state) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000219 CheckScopeChain([debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400220 debug.ScopeType.Script,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000221 debug.ScopeType.Global], exec_state);
Ben Murdoch589d6972011-11-30 16:04:58 +0000222 CheckScopeContent({a:1}, 0, exec_state);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000223};
224local_2(1);
225EndTest();
226
227
228// Local scope with a parameter and a local variable.
229BeginTest("Local 3");
230
231function local_3(a) {
232 let x = 3;
233 debugger;
234}
235
236listener_delegate = function(exec_state) {
237 CheckScopeChain([debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400238 debug.ScopeType.Script,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000239 debug.ScopeType.Global], exec_state);
240 CheckScopeContent({a:1,x:3}, 0, exec_state);
241};
242local_3(1);
243EndTest();
244
245
246// Local scope with parameters and local variables.
247BeginTest("Local 4");
248
249function local_4(a, b) {
250 let x = 3;
251 let y = 4;
252 debugger;
253}
254
255listener_delegate = function(exec_state) {
256 CheckScopeChain([debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400257 debug.ScopeType.Script,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000258 debug.ScopeType.Global], exec_state);
259 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
260};
261local_4(1, 2);
262EndTest();
263
264
Ben Murdoch589d6972011-11-30 16:04:58 +0000265// Single variable in a block scope.
266BeginTest("Local 5");
267
268function local_5(a) {
269 {
270 let x = 5;
271 debugger;
272 }
273}
274
275listener_delegate = function(exec_state) {
276 CheckScopeChain([debug.ScopeType.Block,
277 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400278 debug.ScopeType.Script,
Ben Murdoch589d6972011-11-30 16:04:58 +0000279 debug.ScopeType.Global], exec_state);
280 CheckScopeContent({x:5}, 0, exec_state);
281 CheckScopeContent({a:1}, 1, exec_state);
282};
283local_5(1);
284EndTest();
285
286
287// Two variables in a block scope.
288BeginTest("Local 6");
289
290function local_6(a) {
291 {
292 let x = 6;
293 let y = 7;
294 debugger;
295 }
296}
297
298listener_delegate = function(exec_state) {
299 CheckScopeChain([debug.ScopeType.Block,
300 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400301 debug.ScopeType.Script,
Ben Murdoch589d6972011-11-30 16:04:58 +0000302 debug.ScopeType.Global], exec_state);
303 CheckScopeContent({x:6,y:7}, 0, exec_state);
304 CheckScopeContent({a:1}, 1, exec_state);
305};
306local_6(1);
307EndTest();
308
309
310// Two variables in a block scope.
311BeginTest("Local 7");
312
313function local_7(a) {
314 {
315 {
316 let x = 8;
317 debugger;
318 }
319 }
320}
321
322listener_delegate = function(exec_state) {
323 CheckScopeChain([debug.ScopeType.Block,
324 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400325 debug.ScopeType.Script,
Ben Murdoch589d6972011-11-30 16:04:58 +0000326 debug.ScopeType.Global], exec_state);
327 CheckScopeContent({x:8}, 0, exec_state);
328 CheckScopeContent({a:1}, 1, exec_state);
329};
330local_7(1);
331EndTest();
332
333
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000334// Simple closure formed by returning an inner function referering to an outer
335// block local variable and an outer function's parameter.
336BeginTest("Closure 1");
337
338function closure_1(a) {
339 var x = 2;
340 let y = 3;
341 if (true) {
342 let z = 4;
343 function f() {
344 debugger;
345 return a + x + y + z;
346 };
347 return f;
348 }
349}
350
351listener_delegate = function(exec_state) {
352 CheckScopeChain([debug.ScopeType.Local,
353 debug.ScopeType.Block,
354 debug.ScopeType.Closure,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400355 debug.ScopeType.Script,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000356 debug.ScopeType.Global], exec_state);
357 CheckScopeContent({}, 0, exec_state);
358 CheckScopeContent({a:1,x:2,y:3}, 2, exec_state);
359};
360closure_1(1)();
361EndTest();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100362
363
364// Simple for-in loop over the keys of an object.
365BeginTest("For loop 1");
366
367function for_loop_1() {
368 for (let x in {y:undefined}) {
369 debugger;
370 }
371}
372
373listener_delegate = function(exec_state) {
374 CheckScopeChain([debug.ScopeType.Block,
375 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400376 debug.ScopeType.Script,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100377 debug.ScopeType.Global], exec_state);
378 CheckScopeContent({x:'y'}, 0, exec_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000379 // The function scope contains a temporary iteration variable, but it is
380 // hidden to the debugger.
381 CheckScopeContent({}, 1, exec_state);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100382};
383for_loop_1();
384EndTest();
385
386
387// For-in loop over the keys of an object with a block scoped let variable
388// shadowing the iteration variable.
389BeginTest("For loop 2");
390
391function for_loop_2() {
392 for (let x in {y:undefined}) {
393 let x = 3;
394 debugger;
395 }
396}
397
398listener_delegate = function(exec_state) {
399 CheckScopeChain([debug.ScopeType.Block,
400 debug.ScopeType.Block,
401 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400402 debug.ScopeType.Script,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100403 debug.ScopeType.Global], exec_state);
404 CheckScopeContent({x:3}, 0, exec_state);
405 CheckScopeContent({x:'y'}, 1, exec_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000406 // The function scope contains a temporary iteration variable, hidden to the
407 // debugger.
408 CheckScopeContent({}, 2, exec_state);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100409};
410for_loop_2();
411EndTest();
412
413
414// Simple for loop.
415BeginTest("For loop 3");
416
417function for_loop_3() {
418 for (let x = 3; x < 4; ++x) {
419 debugger;
420 }
421}
422
423listener_delegate = function(exec_state) {
424 CheckScopeChain([debug.ScopeType.Block,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 debug.ScopeType.Block,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100426 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400427 debug.ScopeType.Script,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100428 debug.ScopeType.Global], exec_state);
429 CheckScopeContent({x:3}, 0, exec_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 CheckScopeContent({x:3}, 1, exec_state);
431 CheckScopeContent({}, 2, exec_state);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100432};
433for_loop_3();
434EndTest();
435
436
437// For loop with a block scoped let variable shadowing the iteration variable.
438BeginTest("For loop 4");
439
440function for_loop_4() {
441 for (let x = 3; x < 4; ++x) {
442 let x = 5;
443 debugger;
444 }
445}
446
447listener_delegate = function(exec_state) {
448 CheckScopeChain([debug.ScopeType.Block,
449 debug.ScopeType.Block,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 debug.ScopeType.Block,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100451 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400452 debug.ScopeType.Script,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100453 debug.ScopeType.Global], exec_state);
454 CheckScopeContent({x:5}, 0, exec_state);
455 CheckScopeContent({x:3}, 1, exec_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000456 CheckScopeContent({x:3}, 2, exec_state);
457 CheckScopeContent({}, 3, exec_state);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100458};
459for_loop_4();
460EndTest();
461
462
463// For loop with two variable declarations.
464BeginTest("For loop 5");
465
466function for_loop_5() {
467 for (let x = 3, y = 5; x < 4; ++x) {
468 debugger;
469 }
470}
471
472listener_delegate = function(exec_state) {
473 CheckScopeChain([debug.ScopeType.Block,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000474 debug.ScopeType.Block,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100475 debug.ScopeType.Local,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400476 debug.ScopeType.Script,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100477 debug.ScopeType.Global], exec_state);
478 CheckScopeContent({x:3,y:5}, 0, exec_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000479 CheckScopeContent({x:3,y:5}, 1, exec_state);
480 CheckScopeContent({}, 2, exec_state);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100481};
482for_loop_5();
483EndTest();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400484
485
486// Uninitialized variables
487BeginTest("Uninitialized 1");
488
489function uninitialized_1() {
490 {
491 debugger;
492 let x = 1;
493 }
494}
495
496listener_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);
502};
503uninitialized_1();
504EndTest();