Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / harmony / debug-blockscopes.js
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
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
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.
31
32 // TODO(ES6): properly activate extended mode
33 "use strict";
34
35 // Get the Debug object exposed from the debug context global object.
36 var Debug = debug.Debug;
37
38 var test_name;
39 var listener_delegate;
40 var listener_called;
41 var exception;
42 var begin_test_count = 0;
43 var end_test_count = 0;
44 var break_count = 0;
45
46
47 // Debug event listener which delegates.
48 function listener(event, exec_state, event_data, data) {
49   try {
50     if (event == Debug.DebugEvent.Break) {
51       break_count++;
52       listener_called = true;
53       listener_delegate(exec_state);
54     }
55   } catch (e) {
56     exception = e;
57   }
58 }
59
60 // Add the debug event listener.
61 Debug.setListener(listener);
62
63
64 // Initialize for a new test.
65 function BeginTest(name) {
66   test_name = name;
67   listener_delegate = null;
68   listener_called = false;
69   exception = null;
70   begin_test_count++;
71 }
72
73
74 // Check result of a test.
75 function EndTest() {
76   assertTrue(listener_called, "listerner not called for " + test_name);
77   assertNull(exception, test_name);
78   end_test_count++;
79 }
80
81 var global_object = this;
82
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());
90
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());
96     }
97   }
98
99   // Get the debug command processor.
100   var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
101
102   // Send a scopes request and check the result.
103   var json;
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);
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.
127 function 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   }
150   // Skip property with empty name.
151   if (!scope.scopeObject().property('').isUndefined()) {
152     scope_size--;
153   }
154
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.
191 BeginTest("Local block 1");
192
193 function local_block_1() {
194   {
195     debugger;
196   }
197 }
198
199 listener_delegate = function(exec_state) {
200   CheckScopeChain([debug.ScopeType.Local,
201                    debug.ScopeType.Global], exec_state);
202   CheckScopeContent({}, 0, exec_state);
203 };
204 local_block_1();
205 EndTest();
206
207
208 // Simple empty block scope in local scope with a parameter.
209 BeginTest("Local 2");
210
211 function local_2(a) {
212   {
213     debugger;
214   }
215 }
216
217 listener_delegate = function(exec_state) {
218   CheckScopeChain([debug.ScopeType.Local,
219                    debug.ScopeType.Global], exec_state);
220   CheckScopeContent({a:1}, 0, exec_state);
221 };
222 local_2(1);
223 EndTest();
224
225
226 // Local scope with a parameter and a local variable.
227 BeginTest("Local 3");
228
229 function local_3(a) {
230   let x = 3;
231   debugger;
232 }
233
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);
238 };
239 local_3(1);
240 EndTest();
241
242
243 // Local scope with parameters and local variables.
244 BeginTest("Local 4");
245
246 function local_4(a, b) {
247   let x = 3;
248   let y = 4;
249   debugger;
250 }
251
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);
256 };
257 local_4(1, 2);
258 EndTest();
259
260
261 // Single variable in a block scope.
262 BeginTest("Local 5");
263
264 function local_5(a) {
265   {
266     let x = 5;
267     debugger;
268   }
269 }
270
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);
277 };
278 local_5(1);
279 EndTest();
280
281
282 // Two variables in a block scope.
283 BeginTest("Local 6");
284
285 function local_6(a) {
286   {
287     let x = 6;
288     let y = 7;
289     debugger;
290   }
291 }
292
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);
299 };
300 local_6(1);
301 EndTest();
302
303
304 // Two variables in a block scope.
305 BeginTest("Local 7");
306
307 function local_7(a) {
308   {
309     {
310       let x = 8;
311       debugger;
312     }
313   }
314 }
315
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);
322 };
323 local_7(1);
324 EndTest();
325
326
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");
330
331 function closure_1(a) {
332   var x = 2;
333   let y = 3;
334   if (true) {
335     let z = 4;
336     function f() {
337       debugger;
338       return a + x + y + z;
339     };
340     return f;
341   }
342 }
343
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);
351 };
352 closure_1(1)();
353 EndTest();
354
355
356 // Simple for-in loop over the keys of an object.
357 BeginTest("For loop 1");
358
359 function for_loop_1() {
360   for (let x in {y:undefined}) {
361     debugger;
362   }
363 }
364
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);
373 };
374 for_loop_1();
375 EndTest();
376
377
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");
381
382 function for_loop_2() {
383   for (let x in {y:undefined}) {
384     let x = 3;
385     debugger;
386   }
387 }
388
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
397   // debugger.
398   CheckScopeContent({}, 2, exec_state);
399 };
400 for_loop_2();
401 EndTest();
402
403
404 // Simple for loop.
405 BeginTest("For loop 3");
406
407 function for_loop_3() {
408   for (let x = 3; x < 4; ++x) {
409     debugger;
410   }
411 }
412
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);
419 };
420 for_loop_3();
421 EndTest();
422
423
424 // For loop with a block scoped let variable shadowing the iteration variable.
425 BeginTest("For loop 4");
426
427 function for_loop_4() {
428   for (let x = 3; x < 4; ++x) {
429     let x = 5;
430     debugger;
431   }
432 }
433
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);
442 };
443 for_loop_4();
444 EndTest();
445
446
447 // For loop with two variable declarations.
448 BeginTest("For loop 5");
449
450 function for_loop_5() {
451   for (let x = 3, y = 5; x < 4; ++x) {
452     debugger;
453   }
454 }
455
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);
462 };
463 for_loop_5();
464 EndTest();