debug-evaluate-locals-optimized-double.js revision c7cc028aaeedbbfa11c11d0b7b243b3d9e837ed9
1// Copyright 2012 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// Get the Debug object exposed from the debug context global object.
30Debug = debug.Debug
31
32var listenerComplete = false;
33var exception = false;
34
35var testingConstructCall = false;
36
37var input = [
38  {a: 1, b: 2},
39  {a: 3, b: 4},
40  {a: 5, b: 6},
41  {a: 7, b: 8},
42  {a: 9, b: 10}
43];
44
45var expected = [
46  { locals: {a0: 1.01, b0: 2.02}, args: { names: ["i", "x0", "y0"], values: [0, 3.03, 4.04] } },
47  { locals: {a1: 3.03, b1: 4.04}, args: { names: ["i", "x1", "y1"], values: [1, 5.05, 6.06] } },
48  { locals: {a2: 5.05, b2: 6.06}, args: { names: ["i"], values: [2] } },
49  { locals: {a3: 7.07, b3: 8.08}, args: { names: ["i", "x3", "y3", "z3"],
50                                          values: [3, 9.09, 10.10, undefined] }
51  },
52  { locals: {a4: 9.09, b4: 10.10}, args: { names: ["i", "x4", "y4"], values: [4, 11.11, 12.12] } }
53];
54
55function arraySum(arr) {
56  return arr.reduce(function (a, b) { return a + b; }, 0);
57}
58
59function listener(event, exec_state, event_data, data) {
60  try {
61    if (event == Debug.DebugEvent.Break)
62    {
63      assertEquals(6, exec_state.frameCount());
64
65      for (var i = 0; i < exec_state.frameCount(); i++) {
66        var frame = exec_state.frame(i);
67        if (i < exec_state.frameCount() - 1) {
68          var expected_args = expected[i].args;
69          var expected_locals = expected[i].locals;
70
71          // All frames except the bottom one have expected locals.
72          var locals = {};
73          for (var j = 0; j < frame.localCount(); j++) {
74            locals[frame.localName(j)] = frame.localValue(j).value();
75          }
76          assertPropertiesEqual(expected_locals, locals);
77
78          // All frames except the bottom one have expected arguments.
79          for (var j = 0; j < expected_args.names.length; j++) {
80            assertEquals(expected_args.names[j], frame.argumentName(j));
81            assertEquals(expected_args.values[j], frame.argumentValue(j).value());
82          }
83
84          // All frames except the bottom one have two scopes.
85          assertEquals(2, frame.scopeCount());
86          assertEquals(debug.ScopeType.Local, frame.scope(0).scopeType());
87          assertEquals(debug.ScopeType.Global, frame.scope(1).scopeType());
88
89          Object.keys(expected_locals).forEach(function (name) {
90            assertEquals(expected_locals[name], frame.scope(0).scopeObject().value()[name]);
91          });
92
93          for (var j = 0; j < expected_args.names.length; j++) {
94            var arg_name = expected_args.names[j];
95            var arg_value = expected_args.values[j];
96            assertEquals(arg_value, frame.scope(0).scopeObject().value()[arg_name]);
97          }
98
99          // Evaluate in the inlined frame.
100          Object.keys(expected_locals).forEach(function (name) {
101            assertEquals(expected_locals[name], frame.evaluate(name).value());
102          });
103
104          for (var j = 0; j < expected_args.names.length; j++) {
105            var arg_name = expected_args.names[j];
106            var arg_value = expected_args.values[j];
107            assertEquals(arg_value, frame.evaluate(arg_name).value());
108            assertEquals(arg_value, frame.evaluate('arguments['+j+']').value());
109          }
110
111          var expected_args_sum = arraySum(expected_args.values);
112          var expected_locals_sum =
113              arraySum(Object.keys(expected_locals).
114                       map(function (k) { return expected_locals[k]; }));
115
116          assertEquals(expected_locals_sum + expected_args_sum,
117                       frame.evaluate(Object.keys(expected_locals).join('+') + ' + ' +
118                                      expected_args.names.join('+')).value());
119
120          var arguments_sum = expected_args.names.map(function(_, idx) {
121            return "arguments[" + idx + "]";
122          }).join('+');
123          assertEquals(expected_args_sum,
124                       frame.evaluate(arguments_sum).value());
125        } else {
126          // The bottom frame only have the global scope.
127          assertEquals(1, frame.scopeCount());
128          assertEquals(debug.ScopeType.Global, frame.scope(0).scopeType());
129        }
130
131        // Check the frame function.
132        switch (i) {
133          case 0: assertEquals(h, frame.func().value()); break;
134          case 1: assertEquals(g3, frame.func().value()); break;
135          case 2: assertEquals(g2, frame.func().value()); break;
136          case 3: assertEquals(g1, frame.func().value()); break;
137          case 4: assertEquals(f, frame.func().value()); break;
138          case 5: break;
139          default: assertUnreachable();
140        }
141
142        // Check for construct call.
143        assertEquals(testingConstructCall && i == 4, frame.isConstructCall());
144
145        // When function f is optimized (1 means YES, see runtime.cc) we
146        // expect an optimized frame for f with g1, g2 and g3 inlined.
147        if (%GetOptimizationStatus(f) == 1) {
148          if (i == 1 || i == 2 || i == 3) {
149            assertTrue(frame.isOptimizedFrame());
150            assertTrue(frame.isInlinedFrame());
151            assertEquals(4 - i, frame.inlinedFrameIndex());
152          } else if (i == 4) {
153            assertTrue(frame.isOptimizedFrame());
154            assertFalse(frame.isInlinedFrame());
155          } else {
156            assertFalse(frame.isOptimizedFrame());
157            assertFalse(frame.isInlinedFrame());
158          }
159        }
160      }
161
162      // Indicate that all was processed.
163      listenerComplete = true;
164    }
165  } catch (e) {
166    exception = e.toString() + e.stack;
167  };
168};
169
170for (var i = 0; i < 4; i++) f(input.length - 1, 11.11, 12.12);
171%OptimizeFunctionOnNextCall(f);
172f(input.length - 1, 11.11, 12.12);
173
174// Add the debug event listener.
175Debug.setListener(listener);
176
177function h(i, x0, y0) {
178  var a0 = input[i].a;
179  var b0 = input[i].b;
180  a0 = a0 + a0 / 100;
181  b0 = b0 + b0 / 100;
182  debugger;  // Breakpoint.
183};
184
185function g3(i, x1, y1) {
186  var a1 = input[i].a;
187  var b1 = input[i].b;
188  a1 = a1 + a1 / 100;
189  b1 = b1 + b1 / 100;
190  h(i - 1, a1, b1);
191  return a1+b1;
192};
193
194function g2(i) {
195  var a2 = input[i].a;
196  var b2 = input[i].b;
197  a2 = a2 + a2 / 100;
198  b2 = b2 + b2 / 100;
199  g3(i - 1, a2, b2);
200};
201
202function g1(i, x3, y3, z3) {
203  var a3 = input[i].a;
204  var b3 = input[i].b;
205  a3 = a3 + a3 / 100;
206  b3 = b3 + b3 / 100;
207  g2(i - 1, a3, b3);
208};
209
210function f(i, x4, y4) {
211  var a4 = input[i].a;
212  var b4 = input[i].b;
213  a4 = a4 + a4 / 100;
214  b4 = b4 + b4 / 100;
215  g1(i - 1, a4, b4);
216};
217
218// Test calling f normally and as a constructor.
219f(input.length - 1, 11.11, 12.12);
220f(input.length - 1, 11.11, 12.12, "");
221testingConstructCall = true;
222new f(input.length - 1, 11.11, 12.12);
223new f(input.length - 1, 11.11, 12.12, "");
224
225// Make sure that the debug event listener vas invoked.
226assertFalse(exception, "exception in listener " + exception)
227assertTrue(listenerComplete);
228
229Debug.setListener(null);
230