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 --allow-natives-syntax 29 // Flags: --debug-eval-readonly-locals 30 // The functions used for testing backtraces. They are at the top to make the 31 // testing of source line/column easier. 32 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, exception); 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.Script || 113 scopes[i] == debug.ScopeType.Closure) { 114 assertTrue(response.body.scopes[i].object.ref < 0); 115 } else { 116 assertTrue(response.body.scopes[i].object.ref >= 0); 117 } 118 var found = false; 119 for (var j = 0; j < response.refs.length && !found; j++) { 120 found = response.refs[j].handle == response.body.scopes[i].object.ref; 121 } 122 assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found"); 123 } 124 } 125 126 // Check that the content of the scope is as expected. For functions just check 127 // that there is a function. 128 function CheckScopeContent(content, number, exec_state) { 129 var scope = exec_state.frame().scope(number); 130 var count = 0; 131 for (var p in content) { 132 var property_mirror = scope.scopeObject().property(p); 133 if (property_mirror.isUndefined()) { 134 print('property ' + p + ' not found in scope'); 135 } 136 assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope'); 137 if (typeof(content[p]) === 'function') { 138 assertTrue(property_mirror.value().isFunction()); 139 } else { 140 assertEquals(content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value'); 141 } 142 count++; 143 } 144 145 // 'arguments' and might be exposed in the local and closure scope. Just 146 // ignore this. 147 var scope_size = scope.scopeObject().properties().length; 148 if (!scope.scopeObject().property('arguments').isUndefined()) { 149 scope_size--; 150 } 151 // Skip property with empty name. 152 if (!scope.scopeObject().property('').isUndefined()) { 153 scope_size--; 154 } 155 156 if (count != scope_size) { 157 print('Names found in scope:'); 158 var names = scope.scopeObject().propertyNames(); 159 for (var i = 0; i < names.length; i++) { 160 print(names[i]); 161 } 162 } 163 assertEquals(count, scope_size); 164 165 // Get the debug command processor. 166 var dcp = exec_state.debugCommandProcessor("unspecified_running_state"); 167 168 // Send a scope request for information on a single scope and check the 169 // result. 170 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":'; 171 request_json += scope.scopeIndex(); 172 request_json += '}}'; 173 var response_json = dcp.processDebugJSONRequest(request_json); 174 var response = JSON.parse(response_json); 175 assertEquals(scope.scopeType(), response.body.type); 176 assertEquals(number, response.body.index); 177 if (scope.scopeType() == debug.ScopeType.Local || 178 scope.scopeType() == debug.ScopeType.Closure) { 179 assertTrue(response.body.object.ref < 0); 180 } else { 181 assertTrue(response.body.object.ref >= 0); 182 } 183 var found = false; 184 for (var i = 0; i < response.refs.length && !found; i++) { 185 found = response.refs[i].handle == response.body.object.ref; 186 } 187 assertTrue(found, "Scope object " + response.body.object.ref + " not found"); 188 } 189 190 191 function assertEqualsUnlessOptimized(expected, value, f) { 192 try { 193 assertEquals(expected, value); 194 } catch (e) { 195 assertOptimized(f); 196 } 197 } 198 199 // Simple empty block scope in local scope. 200 BeginTest("Local block 1"); 201 202 function local_block_1() { 203 { 204 debugger; 205 } 206 } 207 208 listener_delegate = function(exec_state) { 209 CheckScopeChain([debug.ScopeType.Local, 210 debug.ScopeType.Script, 211 debug.ScopeType.Global], exec_state); 212 CheckScopeContent({}, 0, exec_state); 213 }; 214 local_block_1(); 215 EndTest(); 216 217 218 // Simple empty block scope in local scope with a parameter. 219 BeginTest("Local 2"); 220 221 function local_2(a) { 222 { 223 debugger; 224 } 225 } 226 227 listener_delegate = function(exec_state) { 228 CheckScopeChain([debug.ScopeType.Local, 229 debug.ScopeType.Script, 230 debug.ScopeType.Global], exec_state); 231 CheckScopeContent({a:1}, 0, exec_state); 232 }; 233 local_2(1); 234 EndTest(); 235 236 237 // Local scope with a parameter and a local variable. 238 BeginTest("Local 3"); 239 240 function local_3(a) { 241 let x = 3; 242 debugger; 243 } 244 245 listener_delegate = function(exec_state) { 246 CheckScopeChain([debug.ScopeType.Local, 247 debug.ScopeType.Script, 248 debug.ScopeType.Global], exec_state); 249 CheckScopeContent({a:1,x:3}, 0, exec_state); 250 }; 251 local_3(1); 252 EndTest(); 253 254 255 // Local scope with parameters and local variables. 256 BeginTest("Local 4"); 257 258 function local_4(a, b) { 259 let x = 3; 260 let y = 4; 261 debugger; 262 } 263 264 listener_delegate = function(exec_state) { 265 CheckScopeChain([debug.ScopeType.Local, 266 debug.ScopeType.Script, 267 debug.ScopeType.Global], exec_state); 268 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state); 269 }; 270 local_4(1, 2); 271 EndTest(); 272 273 274 // Single variable in a block scope. 275 BeginTest("Local 5"); 276 277 function local_5(a) { 278 { 279 let x = 5; 280 debugger; 281 } 282 } 283 284 listener_delegate = function(exec_state) { 285 CheckScopeChain([debug.ScopeType.Block, 286 debug.ScopeType.Local, 287 debug.ScopeType.Script, 288 debug.ScopeType.Global], exec_state); 289 CheckScopeContent({x:5}, 0, exec_state); 290 CheckScopeContent({a:1}, 1, exec_state); 291 }; 292 local_5(1); 293 EndTest(); 294 295 296 // Two variables in a block scope. 297 BeginTest("Local 6"); 298 299 function local_6(a) { 300 { 301 let x = 6; 302 let y = 7; 303 debugger; 304 } 305 } 306 307 listener_delegate = function(exec_state) { 308 CheckScopeChain([debug.ScopeType.Block, 309 debug.ScopeType.Local, 310 debug.ScopeType.Script, 311 debug.ScopeType.Global], exec_state); 312 CheckScopeContent({x:6,y:7}, 0, exec_state); 313 CheckScopeContent({a:1}, 1, exec_state); 314 }; 315 local_6(1); 316 EndTest(); 317 318 319 // Two variables in a block scope. 320 BeginTest("Local 7"); 321 322 function local_7(a) { 323 { 324 { 325 let x = 8; 326 debugger; 327 } 328 } 329 } 330 331 listener_delegate = function(exec_state) { 332 CheckScopeChain([debug.ScopeType.Block, 333 debug.ScopeType.Local, 334 debug.ScopeType.Script, 335 debug.ScopeType.Global], exec_state); 336 CheckScopeContent({x:8}, 0, exec_state); 337 CheckScopeContent({a:1}, 1, exec_state); 338 }; 339 local_7(1); 340 EndTest(); 341 342 343 // Simple closure formed by returning an inner function referering to an outer 344 // block local variable and an outer function's parameter. 345 BeginTest("Closure 1"); 346 347 function closure_1(a) { 348 var x = 2; 349 let y = 3; 350 if (true) { 351 let z = 4; 352 function f() { 353 debugger; 354 return a + x + y + z; 355 }; 356 return f; 357 } 358 } 359 360 listener_delegate = function(exec_state) { 361 CheckScopeChain([debug.ScopeType.Local, 362 debug.ScopeType.Block, 363 debug.ScopeType.Closure, 364 debug.ScopeType.Script, 365 debug.ScopeType.Global], exec_state); 366 CheckScopeContent({}, 0, exec_state); 367 CheckScopeContent({a:1,x:2,y:3}, 2, exec_state); 368 }; 369 closure_1(1)(); 370 EndTest(); 371 372 373 // Simple for-in loop over the keys of an object. 374 BeginTest("For loop 1"); 375 376 function for_loop_1() { 377 for (let x in {y:undefined}) { 378 debugger; 379 } 380 } 381 382 listener_delegate = function(exec_state) { 383 CheckScopeChain([debug.ScopeType.Block, 384 debug.ScopeType.Block, 385 debug.ScopeType.Local, 386 debug.ScopeType.Script, 387 debug.ScopeType.Global], exec_state); 388 CheckScopeContent({x:'y'}, 0, exec_state); 389 // The function scope contains a temporary iteration variable, but it is 390 // hidden to the debugger. 391 // TODO(adamk): This variable is only used to provide a TDZ for the enumerable 392 // expression and should not be visible to the debugger. 393 CheckScopeContent({x:undefined}, 1, exec_state); 394 }; 395 for_loop_1(); 396 EndTest(); 397 398 399 // For-in loop over the keys of an object with a block scoped let variable 400 // shadowing the iteration variable. 401 BeginTest("For loop 2"); 402 403 function for_loop_2() { 404 for (let x in {y:undefined}) { 405 let x = 3; 406 debugger; 407 } 408 } 409 410 listener_delegate = function(exec_state) { 411 CheckScopeChain([debug.ScopeType.Block, 412 debug.ScopeType.Block, 413 debug.ScopeType.Block, 414 debug.ScopeType.Local, 415 debug.ScopeType.Script, 416 debug.ScopeType.Global], exec_state); 417 CheckScopeContent({x:3}, 0, exec_state); 418 CheckScopeContent({x:'y'}, 1, exec_state); 419 // The function scope contains a temporary iteration variable, hidden to the 420 // debugger. 421 // TODO(adamk): This variable is only used to provide a TDZ for the enumerable 422 // expression and should not be visible to the debugger. 423 CheckScopeContent({x:undefined}, 2, exec_state); 424 }; 425 for_loop_2(); 426 EndTest(); 427 428 429 // Simple for loop. 430 BeginTest("For loop 3"); 431 432 function for_loop_3() { 433 for (let x = 3; x < 4; ++x) { 434 debugger; 435 } 436 } 437 438 listener_delegate = function(exec_state) { 439 CheckScopeChain([debug.ScopeType.Block, 440 debug.ScopeType.Block, 441 debug.ScopeType.Local, 442 debug.ScopeType.Script, 443 debug.ScopeType.Global], exec_state); 444 CheckScopeContent({x:3}, 0, exec_state); 445 CheckScopeContent({x:3}, 1, exec_state); 446 CheckScopeContent({}, 2, exec_state); 447 }; 448 for_loop_3(); 449 EndTest(); 450 451 452 // For loop with a block scoped let variable shadowing the iteration variable. 453 BeginTest("For loop 4"); 454 455 function for_loop_4() { 456 for (let x = 3; x < 4; ++x) { 457 let x = 5; 458 debugger; 459 } 460 } 461 462 listener_delegate = function(exec_state) { 463 CheckScopeChain([debug.ScopeType.Block, 464 debug.ScopeType.Block, 465 debug.ScopeType.Block, 466 debug.ScopeType.Local, 467 debug.ScopeType.Script, 468 debug.ScopeType.Global], exec_state); 469 CheckScopeContent({x:5}, 0, exec_state); 470 CheckScopeContent({x:3}, 1, exec_state); 471 CheckScopeContent({x:3}, 2, exec_state); 472 CheckScopeContent({}, 3, exec_state); 473 }; 474 for_loop_4(); 475 EndTest(); 476 477 478 // For loop with two variable declarations. 479 BeginTest("For loop 5"); 480 481 function for_loop_5() { 482 for (let x = 3, y = 5; x < 4; ++x) { 483 debugger; 484 } 485 } 486 487 listener_delegate = function(exec_state) { 488 CheckScopeChain([debug.ScopeType.Block, 489 debug.ScopeType.Block, 490 debug.ScopeType.Local, 491 debug.ScopeType.Script, 492 debug.ScopeType.Global], exec_state); 493 CheckScopeContent({x:3,y:5}, 0, exec_state); 494 CheckScopeContent({x:3,y:5}, 1, exec_state); 495 CheckScopeContent({}, 2, exec_state); 496 }; 497 for_loop_5(); 498 EndTest(); 499 500 501 // Uninitialized variables 502 BeginTest("Uninitialized 1"); 503 504 function uninitialized_1() { 505 { 506 debugger; 507 let x = 1; 508 } 509 } 510 511 listener_delegate = function(exec_state) { 512 CheckScopeChain([debug.ScopeType.Block, 513 debug.ScopeType.Local, 514 debug.ScopeType.Script, 515 debug.ScopeType.Global], exec_state); 516 CheckScopeContent({x:undefined}, 0, exec_state); 517 }; 518 uninitialized_1(); 519 EndTest(); 520 521 522 // Block scopes shadowing 523 BeginTest("Block scopes shadowing 1"); 524 function shadowing_1() { 525 let i = 0; 526 { 527 let i = 5; 528 debugger; 529 } 530 assertEquals(0, i); 531 } 532 533 listener_delegate = function (exec_state) { 534 assertEqualsUnlessOptimized(5, exec_state.frame(0).evaluate("i").value()); 535 } 536 shadowing_1(); 537 EndTest(); 538 539 540 // Block scopes shadowing 541 BeginTest("Block scopes shadowing 2"); 542 function shadowing_2() { 543 let i = 0; 544 { 545 let j = 5; 546 debugger; 547 } 548 } 549 550 listener_delegate = function (exec_state) { 551 assertEqualsUnlessOptimized(0, exec_state.frame(0).evaluate("i").value()); 552 assertEqualsUnlessOptimized(5, exec_state.frame(0).evaluate("j").value()); 553 } 554 shadowing_2(); 555 EndTest(); 556