debug-evaluate-locals.js revision a7e24c173cf37484693b9abb38e494fa7bd7baeb
1// Copyright 2008 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
29// Get the Debug object exposed from the debug context global object.
30Debug = debug.Debug
31
32listenerComplete = false;
33exception = false;
34
35
36function checkFrame0(name, value) {
37  assertTrue(name == 'a' || name == 'b');
38  if (name == 'a') {
39    assertEquals(1, value);
40  }
41  if (name == 'b') {
42    assertEquals(2, value);
43  }
44}
45
46
47function checkFrame1(name, value) {
48  assertTrue(name == '.arguments' || name == 'a');
49  if (name == 'a') {
50    assertEquals(3, value);
51  }
52}
53
54
55function checkFrame2(name, value) {
56  assertTrue(name == '.arguments' || name == 'a' ||
57             name == 'arguments' || name == 'b');
58  if (name == 'a') {
59    assertEquals(5, value);
60  }
61  if (name == 'b') {
62    assertEquals(0, value);
63  }
64}
65
66
67function listener(event, exec_state, event_data, data) {
68  try {
69    if (event == Debug.DebugEvent.Break)
70    {
71      // Frame 0 has normal variables a and b.
72      var frame0 = exec_state.frame(0);
73      checkFrame0(frame0.localName(0), frame0.localValue(0).value());
74      checkFrame0(frame0.localName(1), frame0.localValue(1).value());
75
76      // Frame 1 has normal variable a (and the .arguments variable).
77      var frame1 = exec_state.frame(1);
78      checkFrame1(frame1.localName(0), frame1.localValue(0).value());
79      checkFrame1(frame1.localName(1), frame1.localValue(1).value());
80
81      // Frame 2 has normal variables a and b (and both the .arguments and
82      // arguments variable).
83      var frame2 = exec_state.frame(2);
84      checkFrame2(frame2.localName(0), frame2.localValue(0).value());
85      checkFrame2(frame2.localName(1), frame2.localValue(1).value());
86      checkFrame2(frame2.localName(2), frame2.localValue(2).value());
87      checkFrame2(frame2.localName(3), frame2.localValue(3).value());
88
89      // Evaluating a and b on frames 0, 1 and 2 produces 1, 2, 3, 4, 5 and 6.
90      assertEquals(1, exec_state.frame(0).evaluate('a').value());
91      assertEquals(2, exec_state.frame(0).evaluate('b').value());
92      assertEquals(3, exec_state.frame(1).evaluate('a').value());
93      assertEquals(4, exec_state.frame(1).evaluate('b').value());
94      assertEquals(5, exec_state.frame(2).evaluate('a').value());
95      assertEquals(6, exec_state.frame(2).evaluate('b').value());
96
97      // Indicate that all was processed.
98      listenerComplete = true;
99    }
100  } catch (e) {
101    exception = e
102  };
103};
104
105// Add the debug event listener.
106Debug.setListener(listener);
107
108function h() {
109  var a = 1;
110  var b = 2;
111  debugger;  // Breakpoint.
112};
113
114function g() {
115  var a = 3;
116  eval("var b = 4;");
117  h();
118};
119
120function f() {
121  var a = 5;
122  var b = 0;
123  with ({b:6}) {
124    g();
125  }
126};
127
128f();
129
130// Make sure that the debug event listener vas invoked.
131assertTrue(listenerComplete);
132assertFalse(exception, "exception in listener")
133