debug-scopes.js 35.4 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Flags: --expose-debug-as debug --allow-natives-syntax --noanalyze-environment-liveness
29 30 31 32
// The functions used for testing backtraces. They are at the top to make the
// testing of source line/column easier.

// Get the Debug object exposed from the debug context global object.
33
var Debug = debug.Debug;
34

35
var test_name;
36 37 38 39 40 41 42 43 44 45 46 47 48 49
var listener_delegate;
var listener_called;
var exception;
var begin_test_count = 0;
var end_test_count = 0;
var break_count = 0;


// Debug event listener which delegates.
function listener(event, exec_state, event_data, data) {
  try {
    if (event == Debug.DebugEvent.Break) {
      break_count++;
      listener_called = true;
50
      listener_delegate(exec_state);
51 52 53 54 55 56 57 58 59 60
    }
  } catch (e) {
    exception = e;
  }
}

// Add the debug event listener.
Debug.setListener(listener);


61
// Initialize for a new test.
62 63 64 65 66 67 68 69 70 71 72 73
function BeginTest(name) {
  test_name = name;
  listener_delegate = null;
  listener_called = false;
  exception = null;
  begin_test_count++;
}


// Check result of a test.
function EndTest() {
  assertTrue(listener_called, "listerner not called for " + test_name);
74
  assertNull(exception, test_name + " / " + exception);
75 76 77 78
  end_test_count++;
}


79
// Check that two scope are the same.
80
function assertScopeMirrorEquals(scope1, scope2) {
81 82 83
  assertEquals(scope1.scopeType(), scope2.scopeType());
  assertEquals(scope1.frameIndex(), scope2.frameIndex());
  assertEquals(scope1.scopeIndex(), scope2.scopeIndex());
84 85 86 87 88 89 90 91 92 93 94 95 96
  assertPropertiesEqual(scope1.scopeObject().value(), scope2.scopeObject().value());
}

function CheckFastAllScopes(scopes, exec_state)
{
  var fast_all_scopes = exec_state.frame().allScopes(true);
  var length = fast_all_scopes.length;
  assertTrue(scopes.length >= length);
  for (var i = 0; i < scopes.length && i < length; i++) {
    var scope = fast_all_scopes[length - i - 1];
    assertTrue(scope.isScope());
    assertEquals(scopes[scopes.length - i - 1], scope.scopeType());
  }
97 98 99
}


100 101
// Check that the scope chain contains the expected types of scopes.
function CheckScopeChain(scopes, exec_state) {
102
  var all_scopes = exec_state.frame().allScopes();
103
  assertEquals(scopes.length, exec_state.frame().scopeCount());
104
  assertEquals(scopes.length, all_scopes.length, "FrameMirror.allScopes length");
105 106 107 108
  for (var i = 0; i < scopes.length; i++) {
    var scope = exec_state.frame().scope(i);
    assertTrue(scope.isScope());
    assertEquals(scopes[i], scope.scopeType());
109
    assertScopeMirrorEquals(all_scopes[i], scope);
110

111 112
    // Check the global object when hitting the global scope.
    if (scopes[i] == debug.ScopeType.Global) {
113 114 115
      // Objects don't have same class (one is "global", other is "Object",
      // so just check the properties directly.
      assertPropertiesEqual(this, scope.scopeObject().value());
116 117
    }
  }
118
  CheckFastAllScopes(scopes, exec_state);
119 120 121
}


122 123 124 125 126 127 128
// Check that the scope chain contains the expected names of scopes.
function CheckScopeChainNames(names, exec_state) {
  var all_scopes = exec_state.frame().allScopes();
  assertEquals(names.length, all_scopes.length, "FrameMirror.allScopes length");
  for (var i = 0; i < names.length; i++) {
    var scope = exec_state.frame().scope(i);
    assertTrue(scope.isScope());
129
    assertEquals(names[i], scope.details().name())
130 131 132 133
  }
}


134 135 136
// Check that the scope contains at least minimum_content. For functions just
// check that there is a function.
function CheckScopeContent(minimum_content, number, exec_state) {
137
  var scope = exec_state.frame().scope(number);
138 139
  var minimum_count = 0;
  for (var p in minimum_content) {
140
    var property_mirror = scope.scopeObject().property(p);
141 142
    assertFalse(property_mirror.isUndefined(),
                'property ' + p + ' not found in scope');
143
    if (typeof(minimum_content[p]) === 'function') {
144 145
      assertTrue(property_mirror.value().isFunction());
    } else {
146 147
      assertEquals(minimum_content[p], property_mirror.value().value(),
                   'property ' + p + ' has unexpected value');
148
    }
149
    minimum_count++;
150
  }
151

152 153 154 155 156 157
  // 'arguments' and might be exposed in the local and closure scope. Just
  // ignore this.
  var scope_size = scope.scopeObject().properties().length;
  if (!scope.scopeObject().property('arguments').isUndefined()) {
    scope_size--;
  }
158 159 160 161
  // Ditto for 'this'.
  if (!scope.scopeObject().property('this').isUndefined()) {
    scope_size--;
  }
162 163
  // Temporary variables introduced by the parser have not been materialized.
  assertTrue(scope.scopeObject().property('').isUndefined());
164

165
  if (scope_size < minimum_count) {
166 167 168 169 170 171
    print('Names found in scope:');
    var names = scope.scopeObject().propertyNames();
    for (var i = 0; i < names.length; i++) {
      print(names[i]);
    }
  }
172
  assertTrue(scope_size >= minimum_count);
173 174
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
// Check that the scopes have positions as expected.
function CheckScopeChainPositions(positions, exec_state) {
  var all_scopes = exec_state.frame().allScopes();
  assertEquals(positions.length, all_scopes.length, "FrameMirror.allScopes length");
  for (var i = 0; i < positions.length; i++) {
    var scope = exec_state.frame().scope(i);
    assertTrue(scope.isScope());
    var position = positions[i];
    if (!position)
      continue;

    assertEquals(position.start, scope.details().startPosition())
    assertEquals(position.end, scope.details().endPosition())
  }
}
190 191 192 193 194 195 196 197 198 199

// Simple empty local scope.
BeginTest("Local 1");

function local_1() {
  debugger;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
200
                   debug.ScopeType.Script,
201 202
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({}, 0, exec_state);
203 204
};
local_1();
205 206 207 208 209 210 211 212 213 214 215 216
EndTest();


// Local scope with a parameter.
BeginTest("Local 2");

function local_2(a) {
  debugger;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
217
                   debug.ScopeType.Script,
218 219
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1}, 0, exec_state);
220 221
};
local_2(1);
222 223 224 225 226 227 228 229 230 231 232 233 234
EndTest();


// Local scope with a parameter and a local variable.
BeginTest("Local 3");

function local_3(a) {
  var x = 3;
  debugger;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
235
                   debug.ScopeType.Script,
236 237
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,x:3}, 0, exec_state);
238 239
};
local_3(1);
240 241 242 243 244 245 246 247 248 249 250 251 252 253
EndTest();


// Local scope with parameters and local variables.
BeginTest("Local 4");

function local_4(a, b) {
  var x = 3;
  var y = 4;
  debugger;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
254
                   debug.ScopeType.Script,
255 256
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
257 258
};
local_4(1, 2);
259 260 261 262 263 264 265 266 267 268 269 270 271
EndTest();


// Empty local scope with use of eval.
BeginTest("Local 5");

function local_5() {
  eval('');
  debugger;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
272
                   debug.ScopeType.Script,
273 274
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({}, 0, exec_state);
275 276
};
local_5();
277 278 279 280 281 282 283 284 285 286 287 288 289
EndTest();


// Local introducing local variable using eval.
BeginTest("Local 6");

function local_6() {
  eval('var i = 5');
  debugger;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
290
                   debug.ScopeType.Script,
291 292
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({i:5}, 0, exec_state);
293 294
};
local_6();
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
EndTest();


// Local scope with parameters, local variables and local variable introduced
// using eval.
BeginTest("Local 7");

function local_7(a, b) {
  var x = 3;
  var y = 4;
  eval('var i = 5');
  eval('var j = 6');
  debugger;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
312
                   debug.ScopeType.Script,
313 314
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 0, exec_state);
315 316
};
local_7(1, 2);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
EndTest();


// Single empty with block.
BeginTest("With 1");

function with_1() {
  with({}) {
    debugger;
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.Local,
332
                   debug.ScopeType.Script,
333 334
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({}, 0, exec_state);
335 336
};
with_1();
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
EndTest();


// Nested empty with blocks.
BeginTest("With 2");

function with_2() {
  with({}) {
    with({}) {
      debugger;
    }
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.With,
                   debug.ScopeType.Local,
355
                   debug.ScopeType.Script,
356 357 358
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({}, 0, exec_state);
  CheckScopeContent({}, 1, exec_state);
359 360
};
with_2();
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
EndTest();


// With block using an in-place object literal.
BeginTest("With 3");

function with_3() {
  with({a:1,b:2}) {
    debugger;
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.Local,
376
                   debug.ScopeType.Script,
377 378
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,b:2}, 0, exec_state);
379 380
};
with_3();
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
EndTest();


// Nested with blocks using in-place object literals.
BeginTest("With 4");

function with_4() {
  with({a:1,b:2}) {
    with({a:2,b:1}) {
      debugger;
    }
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.With,
                   debug.ScopeType.Local,
399
                   debug.ScopeType.Script,
400 401 402
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:2,b:1}, 0, exec_state);
  CheckScopeContent({a:1,b:2}, 1, exec_state);
403 404
};
with_4();
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
EndTest();


// Nested with blocks using existing object.
BeginTest("With 5");

var with_object = {c:3,d:4};
function with_5() {
  with(with_object) {
    with(with_object) {
      debugger;
    }
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.With,
                   debug.ScopeType.Local,
424
                   debug.ScopeType.Script,
425 426 427
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent(with_object, 0, exec_state);
  CheckScopeContent(with_object, 1, exec_state);
428 429
  assertEquals(exec_state.frame().scope(0).scopeObject(),
               exec_state.frame().scope(1).scopeObject());
430
  assertEquals(with_object, exec_state.frame().scope(1).scopeObject().value());
431 432
};
with_5();
433 434 435
EndTest();


436 437 438 439 440
// Nested with blocks using existing object in global code.
BeginTest("With 6");
listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.With,
441
                   debug.ScopeType.Script,
442 443 444
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent(with_object, 0, exec_state);
  CheckScopeContent(with_object, 1, exec_state);
445 446
  assertEquals(exec_state.frame().scope(0).scopeObject(),
               exec_state.frame().scope(1).scopeObject());
447 448 449 450 451 452 453 454 455 456 457 458
  assertEquals(with_object, exec_state.frame().scope(1).scopeObject().value());
};

var with_object = {c:3,d:4};
with(with_object) {
  with(with_object) {
    debugger;
  }
}
EndTest();


459 460 461 462 463 464 465 466 467 468 469 470 471
// With block in function that is marked for optimization while being executed.
BeginTest("With 7");

function with_7() {
  with({}) {
    %OptimizeFunctionOnNextCall(with_7);
    debugger;
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.Local,
472
                   debug.ScopeType.Script,
473 474 475 476 477 478 479
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({}, 0, exec_state);
};
with_7();
EndTest();


480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
// Simple closure formed by returning an inner function referering the outer
// functions arguments.
BeginTest("Closure 1");

function closure_1(a) {
  function f() {
    debugger;
    return a;
  };
  return f;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
495
                   debug.ScopeType.Script,
496 497
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1}, 1, exec_state);
498
  CheckScopeChainNames(["f", "closure_1", undefined, undefined], exec_state);
499 500
};
closure_1(1)();
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
EndTest();


// Simple closure formed by returning an inner function referering the outer
// functions arguments. Due to VM optimizations parts of the actual closure is
// missing from the debugger information.
BeginTest("Closure 2");

function closure_2(a, b) {
  var x = a + 2;
  var y = b + 2;
  function f() {
    debugger;
    return a + x;
  };
  return f;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
522
                   debug.ScopeType.Script,
523 524
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,x:3}, 1, exec_state);
525
  CheckScopeChainNames(["f", "closure_2", undefined, undefined], exec_state);
526 527
};
closure_2(1, 2)();
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
EndTest();


// Simple closure formed by returning an inner function referering the outer
// functions arguments. Using all arguments and locals from the outer function
// in the inner function makes these part of the debugger information on the
// closure.
BeginTest("Closure 3");

function closure_3(a, b) {
  var x = a + 2;
  var y = b + 2;
  function f() {
    debugger;
    return a + b + x + y;
  };
  return f;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
550
                   debug.ScopeType.Script,
551 552
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4}, 1, exec_state);
553
  CheckScopeChainNames(["f", "closure_3", undefined, undefined], exec_state);
554 555
};
closure_3(1, 2)();
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
EndTest();



// Simple closure formed by returning an inner function referering the outer
// functions arguments. Using all arguments and locals from the outer function
// in the inner function makes these part of the debugger information on the
// closure. Use the inner function as well...
BeginTest("Closure 4");

function closure_4(a, b) {
  var x = a + 2;
  var y = b + 2;
  function f() {
    debugger;
    if (f) {
      return a + b + x + y;
    }
  };
  return f;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
581
                   debug.ScopeType.Script,
582 583
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4,f:function(){}}, 1, exec_state);
584
  CheckScopeChainNames(["f", "closure_4", undefined, undefined], exec_state);
585 586
};
closure_4(1, 2)();
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
EndTest();



// Simple closure formed by returning an inner function referering the outer
// functions arguments. In the presence of eval all arguments and locals
// (including the inner function itself) from the outer function becomes part of
// the debugger infformation on the closure.
BeginTest("Closure 5");

function closure_5(a, b) {
  var x = 3;
  var y = 4;
  function f() {
    eval('');
    debugger;
    return 1;
  };
  return f;
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
611
                   debug.ScopeType.Script,
612 613
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4,f:function(){}}, 1, exec_state);
614
  CheckScopeChainNames(["f", "closure_5", undefined, undefined], exec_state)
615 616
};
closure_5(1, 2)();
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
EndTest();


// Two closures. Due to optimizations only the parts actually used are provided
// through the debugger information.
BeginTest("Closure 6");
function closure_6(a, b) {
  function f(a, b) {
    var x = 3;
    var y = 4;
    return function() {
      var x = 3;
      var y = 4;
      debugger;
      some_global = a;
      return f;
633
    };
634 635 636 637 638 639 640 641
  }
  return f(a, b);
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
                   debug.ScopeType.Closure,
642
                   debug.ScopeType.Script,
643 644 645
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({a:1}, 1, exec_state);
  CheckScopeContent({f:function(){}}, 2, exec_state);
646 647
  CheckScopeChainNames([undefined, "f", "closure_6", undefined, undefined],
                       exec_state);
648 649
};
closure_6(1, 2)();
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
EndTest();


// Two closures. In the presence of eval all information is provided as the
// compiler cannot determine which parts are used.
BeginTest("Closure 7");
function closure_7(a, b) {
  var x = 3;
  var y = 4;
  eval('var i = 5');
  eval('var j = 6');
  function f(a, b) {
    var x = 3;
    var y = 4;
    eval('var i = 5');
    eval('var j = 6');
    return function() {
      debugger;
      some_global = a;
      return f;
670
    };
671 672 673 674 675 676 677 678
  }
  return f(a, b);
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
                   debug.ScopeType.Closure,
679
                   debug.ScopeType.Script,
680 681 682 683
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({}, 0, exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 1, exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6,f:function(){}}, 2, exec_state);
684 685
  CheckScopeChainNames([undefined, "f", "closure_7", undefined, undefined],
                       exec_state);
686 687
};
closure_7(1, 2)();
688 689 690
EndTest();


691 692 693 694 695 696 697 698 699 700
// Closure that may be optimized out.
BeginTest("Closure 8");
function closure_8() {
  (function inner(x) {
    debugger;
  })(2);
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
701
                   debug.ScopeType.Script,
702 703
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({x: 2}, 0, exec_state);
704
  CheckScopeChainNames(["inner", undefined, undefined], exec_state);
705
};
706 707 708 709 710 711 712 713 714
closure_8();
EndTest();


BeginTest("Closure 9");
function closure_9() {
  eval("var y = 1;");
  eval("var z = 1;");
  (function inner(x) {
715
    y++;
716 717 718 719 720 721 722 723
    z++;
    debugger;
  })(2);
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Closure,
724
                   debug.ScopeType.Script,
725
                   debug.ScopeType.Global], exec_state);
726 727
  CheckScopeChainNames(["inner", "closure_9", undefined, undefined],
                       exec_state);
728
};
729 730 731 732
closure_9();
EndTest();


733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
// Test a mixture of scopes.
BeginTest("The full monty");
function the_full_monty(a, b) {
  var x = 3;
  var y = 4;
  eval('var i = 5');
  eval('var j = 6');
  function f(a, b) {
    var x = 9;
    var y = 10;
    eval('var i = 11');
    eval('var j = 12');
    with ({j:13}){
      return function() {
        var x = 14;
748
        with ({a:15}) {
749 750 751 752 753 754
          with ({b:16}) {
            debugger;
            some_global = a;
            return f;
          }
        }
755
      };
756 757 758 759 760 761 762 763 764 765 766 767
    }
  }
  return f(a, b);
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.With,
                   debug.ScopeType.Local,
                   debug.ScopeType.With,
                   debug.ScopeType.Closure,
                   debug.ScopeType.Closure,
768
                   debug.ScopeType.Script,
769 770 771 772 773 774 775
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({b:16}, 0, exec_state);
  CheckScopeContent({a:15}, 1, exec_state);
  CheckScopeContent({x:14}, 2, exec_state);
  CheckScopeContent({j:13}, 3, exec_state);
  CheckScopeContent({a:1,b:2,x:9,y:10,i:11,j:12}, 4, exec_state);
  CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6,f:function(){}}, 5, exec_state);
776 777
  CheckScopeChainNames([undefined, undefined, undefined, "f", "f",
                        "the_full_monty", undefined, undefined], exec_state);
778 779
};
the_full_monty(1, 2)();
780 781
EndTest();

782 783 784 785 786 787 788 789 790 791 792 793 794

BeginTest("Closure inside With 1");
function closure_in_with_1() {
  with({x:1}) {
    (function inner(x) {
      debugger;
    })(2);
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.With,
795
                   debug.ScopeType.Script,
796 797
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({x: 2}, 0, exec_state);
798
  CheckScopeContent({x: 1}, 1, exec_state);
799
};
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
closure_in_with_1();
EndTest();


BeginTest("Closure inside With 2");
function closure_in_with_2() {
  with({x:1}) {
    (function inner(x) {
      with({x:3}) {
        debugger;
      }
    })(2);
  }
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.Local,
                   debug.ScopeType.With,
819
                   debug.ScopeType.Script,
820 821 822 823
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({x: 3}, 0, exec_state);
  CheckScopeContent({x: 2}, 1, exec_state);
  CheckScopeContent({x: 1}, 2, exec_state);
824 825
  CheckScopeChainNames(["inner", "inner", "closure_in_with_2",
                        undefined, undefined], exec_state);
826
};
827 828 829 830 831 832 833 834 835 836 837 838 839 840
closure_in_with_2();
EndTest();


BeginTest("Closure inside With 3");
function createClosure(a) {
   var b = a + 1;
   return function closure() {
     var c = b;
     (function inner(x) {
       with({x:c}) {
         debugger;
       }
     })(2);
841
   };
842 843 844 845 846 847 848 849 850 851 852 853
}

function closure_in_with_3() {
  var f = createClosure(0);
  f();
}

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.Local,
                   debug.ScopeType.Closure,
                   debug.ScopeType.Closure,
854
                   debug.ScopeType.Script,
855
                   debug.ScopeType.Global], exec_state);
856 857
  CheckScopeChainNames(["inner", "inner", "closure", "createClosure",
                        undefined, undefined], exec_state);
858 859 860 861 862
}
closure_in_with_3();
EndTest();


863 864 865 866
BeginTest("Closure inside With 4");
listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.With,
867
                   debug.ScopeType.Script,
868 869 870
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({x: 2}, 0, exec_state);
  CheckScopeContent({x: 1}, 1, exec_state);
871 872
  CheckScopeChainNames([undefined, undefined, undefined, undefined],
                       exec_state);
873 874 875 876 877 878 879 880 881 882
};

with({x:1}) {
  (function(x) {
    debugger;
  })(2);
}
EndTest();


883 884 885
// Test global scope.
BeginTest("Global");
listener_delegate = function(exec_state) {
886 887 888
  CheckScopeChain([debug.ScopeType.Script, debug.ScopeType.Global],
                  exec_state);
  CheckScopeChainNames([undefined, undefined], exec_state);
889
};
890 891 892
debugger;
EndTest();

893 894 895 896 897 898 899 900 901 902 903 904 905 906

BeginTest("Catch block 1");
function catch_block_1() {
  try {
    throw 'Exception';
  } catch (e) {
    debugger;
  }
};


listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Catch,
                   debug.ScopeType.Local,
907
                   debug.ScopeType.Script,
908 909
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({e:'Exception'}, 0, exec_state);
910 911
  CheckScopeChainNames(["catch_block_1", "catch_block_1",
                        undefined, undefined], exec_state);
912 913
};
catch_block_1();
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
EndTest();


BeginTest("Catch block 2");
function catch_block_2() {
  try {
    throw 'Exception';
  } catch (e) {
    with({n:10}) {
      debugger;
    }
  }
};


listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.Catch,
                   debug.ScopeType.Local,
933
                   debug.ScopeType.Script,
934 935 936
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({n:10}, 0, exec_state);
  CheckScopeContent({e:'Exception'}, 1, exec_state);
937 938
  CheckScopeChainNames(["catch_block_2", "catch_block_2", "catch_block_2",
                        undefined, undefined], exec_state);
939 940
};
catch_block_2();
941 942 943 944
EndTest();


BeginTest("Catch block 3");
945
function catch_block_3() {
946 947 948 949 950 951 952 953 954 955 956 957 958 959
  // Do eval to dynamically declare a local variable so that the context's
  // extension slot is initialized with JSContextExtensionObject.
  eval("var y = 78;");
  try {
    throw 'Exception';
  } catch (e) {
    debugger;
  }
};


listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Catch,
                   debug.ScopeType.Local,
960
                   debug.ScopeType.Script,
961 962 963
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({e:'Exception'}, 0, exec_state);
  CheckScopeContent({y:78}, 1, exec_state);
964 965
  CheckScopeChainNames(["catch_block_3", "catch_block_3",
                        undefined, undefined], exec_state);
966 967
};
catch_block_3();
968 969 970 971
EndTest();


BeginTest("Catch block 4");
972
function catch_block_4() {
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
  // Do eval to dynamically declare a local variable so that the context's
  // extension slot is initialized with JSContextExtensionObject.
  eval("var y = 98;");
  try {
    throw 'Exception';
  } catch (e) {
    with({n:10}) {
      debugger;
    }
  }
};

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.With,
                   debug.ScopeType.Catch,
                   debug.ScopeType.Local,
989
                   debug.ScopeType.Script,
990 991 992 993
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({n:10}, 0, exec_state);
  CheckScopeContent({e:'Exception'}, 1, exec_state);
  CheckScopeContent({y:98}, 2, exec_state);
994 995
  CheckScopeChainNames(["catch_block_4", "catch_block_4", "catch_block_4",
                        undefined, undefined], exec_state);
996 997
};
catch_block_4();
998 999 1000
EndTest();


1001 1002 1003 1004
// Test catch in global scope.
BeginTest("Catch block 5");
listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Catch,
1005
                   debug.ScopeType.Script,
1006 1007
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({e:'Exception'}, 0, exec_state);
1008
  CheckScopeChainNames([undefined, undefined, undefined], exec_state);
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
};

try {
  throw 'Exception';
} catch (e) {
  debugger;
}

EndTest();


// Closure inside catch in global code.
BeginTest("Catch block 6");
listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Catch,
1025
                   debug.ScopeType.Script,
1026 1027 1028
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({x: 2}, 0, exec_state);
  CheckScopeContent({e:'Exception'}, 1, exec_state);
1029 1030
  CheckScopeChainNames([undefined, undefined, undefined, undefined],
                       exec_state);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
};

try {
  throw 'Exception';
} catch (e) {
  (function(x) {
    debugger;
  })(2);
}
EndTest();


1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
// Catch block in function that is marked for optimization while being executed.
BeginTest("Catch block 7");
function catch_block_7() {
  %OptimizeFunctionOnNextCall(catch_block_7);
  try {
    throw 'Exception';
  } catch (e) {
    debugger;
  }
};


listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Catch,
                   debug.ScopeType.Local,
1058
                   debug.ScopeType.Script,
1059 1060
                   debug.ScopeType.Global], exec_state);
  CheckScopeContent({e:'Exception'}, 0, exec_state);
1061 1062
  CheckScopeChainNames(["catch_block_7", "catch_block_7",
                        undefined, undefined], exec_state);
1063 1064 1065 1066 1067
};
catch_block_7();
EndTest();


1068 1069 1070 1071 1072 1073 1074
BeginTest("Classes and methods 1");

listener_delegate = function(exec_state) {
  "use strict"
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Script,
                   debug.ScopeType.Global], exec_state);
1075
  CheckScopeContent({}, 1, exec_state);
1076
  CheckScopeChainNames(["m", undefined, undefined], exec_state);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
};

(function() {
  "use strict";
  class C1 {
    m() {
      debugger;
    }
  }
  new C1().m();
})();

EndTest();

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
BeginTest("Scope positions");
var code1 = "function f() {        \n" +
            "  var a = 1;          \n" +
            "  function b() {      \n" +
            "    debugger;         \n" +
            "    return a + 1;     \n" +
            "  }                   \n" +
            "  b();                \n" +
            "}                     \n" +
            "f();                  \n";

listener_delegate = function(exec_state) {
1103 1104
  CheckScopeChainPositions(
      [{start: 58, end: 118}, {start: 10, end: 162}, {}, {}], exec_state);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
}
eval(code1);
EndTest();


function catch_block_2() {
  try {
    throw 'Exception';
  } catch (e) {
    with({n:10}) {
      debugger;
    }
  }
};

BeginTest("Scope positions in catch and 'with' statement");
var code2 = "function catch_block() {   \n" +
            "  try {                    \n" +
            "    throw 'Exception';     \n" +
            "  } catch (e) {            \n" +
            "    with({n : 10}) {       \n" +
            "      debugger;            \n" +
            "    }                      \n" +
            "  }                        \n" +
            "}                          \n" +
            "catch_block();             \n";

listener_delegate = function(exec_state) {
1133 1134 1135 1136
  CheckScopeChainPositions([{start: 131, end: 173},
                            {start: 94, end: 199},
                            {start: 20, end: 225},
                            {}, {}], exec_state);
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
}
eval(code2);
EndTest();

BeginTest("Scope positions in for statement");
var code3 = "function for_statement() {         \n" +
            "  for (let i = 0; i < 1; i++) {    \n" +
            "    debugger;                      \n" +
            "  }                                \n" +
            "}                                  \n" +
            "for_statement();                   \n";

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Block,
                   debug.ScopeType.Local,
                   debug.ScopeType.Script,
                   debug.ScopeType.Global], exec_state);
1154 1155
  CheckScopeChainPositions(
      [{start: 52, end: 111}, {start: 22, end: 145}, {}, {}], exec_state);
1156 1157 1158
}
eval(code3);
EndTest();
1159

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
BeginTest("Scope positions in for statement with lexical block");
var code4 = "function for_statement() {         \n" +
            "  for (let i = 0; i < 1; i++) {    \n" +
            "    let j;                         \n" +
            "    debugger;                      \n" +
            "  }                                \n" +
            "}                                  \n" +
            "for_statement();                   \n";

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Block,
                   debug.ScopeType.Block,
                   debug.ScopeType.Local,
                   debug.ScopeType.Script,
                   debug.ScopeType.Global], exec_state);
1175 1176 1177 1178
  CheckScopeChainPositions([{start: 66, end: 147},
                            {start: 52, end: 147},
                            {start: 22, end: 181},
                            {}, {}], exec_state);
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
}
eval(code4);
EndTest();

BeginTest("Scope positions in lexical for each statement");
var code5 = "function for_each_statement() {    \n" +
            "  for (let i of [0]) {             \n" +
            "    debugger;                      \n" +
            "  }                                \n" +
            "}                                  \n" +
            "for_each_statement();              \n";

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Block,
                   debug.ScopeType.Local,
                   debug.ScopeType.Script,
                   debug.ScopeType.Global], exec_state);
1196 1197
  CheckScopeChainPositions(
      [{start: 55, end: 111}, {start: 27, end: 145}, {}, {}], exec_state);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
}
eval(code5);
EndTest();

BeginTest("Scope positions in lexical for each statement with lexical block");
var code6 = "function for_each_statement() {    \n" +
            "  for (let i of [0]) {             \n" +
            "    let j;                         \n" +
            "    debugger;                      \n" +
            "  }                                \n" +
            "}                                  \n" +
            "for_each_statement();              \n";

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Block,
                   debug.ScopeType.Block,
                   debug.ScopeType.Local,
                   debug.ScopeType.Script,
                   debug.ScopeType.Global], exec_state);
1217 1218 1219 1220
  CheckScopeChainPositions([{start: 57, end: 147},
                            {start: 55, end: 147},
                            {start: 27, end: 181},
                            {}, {}], exec_state);
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
}
eval(code6);
EndTest();

BeginTest("Scope positions in non-lexical for each statement");
var code7 = "function for_each_statement() {    \n" +
            "  var i;                           \n" +
            "  for (i of [0]) {                 \n" +
            "    debugger;                      \n" +
            "  }                                \n" +
            "}                                  \n" +
            "for_each_statement();              \n";

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Local,
                   debug.ScopeType.Script,
                   debug.ScopeType.Global], exec_state);
  CheckScopeChainPositions([{start: 27, end: 181}, {}, {}], exec_state);
}
eval(code7);
EndTest();

1243 1244
BeginTest(
    "Scope positions in non-lexical for each statement with lexical block");
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
var code8 = "function for_each_statement() {    \n" +
            "  var i;                           \n" +
            "  for (i of [0]) {                 \n" +
            "    let j;                         \n" +
            "    debugger;                      \n" +
            "  }                                \n" +
            "}                                  \n" +
            "for_each_statement();              \n";

listener_delegate = function(exec_state) {
  CheckScopeChain([debug.ScopeType.Block,
                   debug.ScopeType.Local,
                   debug.ScopeType.Script,
                   debug.ScopeType.Global], exec_state);
1259 1260
  CheckScopeChainPositions(
      [{start: 89, end: 183}, {start: 27, end: 217}, {}, {}], exec_state);
1261 1262 1263 1264
}
eval(code8);
EndTest();

1265 1266 1267 1268
assertEquals(begin_test_count, break_count,
             'one or more tests did not enter the debugger');
assertEquals(begin_test_count, end_test_count,
             'one or more tests did not have its result checked');