debug-evaluate-locals-optimized.js 8.02 KB
Newer Older
1
// Copyright 2012 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 29 30
// Flags: --expose-debug-as debug --expose-gc --allow-natives-syntax
// Flags: --inline-construct

31 32 33
// Get the Debug object exposed from the debug context global object.
Debug = debug.Debug

34 35 36 37
var listenerComplete = false;
var exception = false;

var testingConstructCall = false;
38

39
var expected = [
40 41 42 43 44 45 46 47 48 49
  { locals: {a0: 1, b0: 2},
    args: { names: ["i", "x0", "y0"], values: [0, 3, 4] } },
  { locals: {a1: 3, b1: 4},
    args: { names: ["i", "x1", "y1"], values: [1, 5, 6] } },
  { locals: {a2: 5, b2: 6},
    args: { names: ["i"], values: [2] } },
  { locals: {a3: 7, b3: 8},
    args: { names: ["i", "x3", "y3", "z3"], values: [3, 9, 10, undefined] } },
  { locals: {a4: 9, b4: 10},
    args: { names: ["i", "x4", "y4"], values: [4, 11, 12] } }
50 51 52 53 54
];

function arraySum(arr) {
  return arr.reduce(function (a, b) { return a + b; }, 0);
}
55 56 57 58 59 60 61 62 63 64

function listener(event, exec_state, event_data, data) {
  try {
    if (event == Debug.DebugEvent.Break)
    {
      assertEquals(6, exec_state.frameCount());

      for (var i = 0; i < exec_state.frameCount(); i++) {
        var frame = exec_state.frame(i);
        if (i < exec_state.frameCount() - 1) {
65 66 67 68 69 70 71 72 73 74 75 76 77
          var expected_args = expected[i].args;
          var expected_locals = expected[i].locals;

          // All frames except the bottom one have expected locals.
          var locals = {};
          for (var j = 0; j < frame.localCount(); j++) {
            locals[frame.localName(j)] = frame.localValue(j).value();
          }
          assertPropertiesEqual(expected_locals, locals);

          // All frames except the bottom one have expected arguments.
          for (var j = 0; j < expected_args.names.length; j++) {
            assertEquals(expected_args.names[j], frame.argumentName(j));
78 79
            assertEquals(expected_args.values[j],
                         frame.argumentValue(j).value());
80
          }
81

82 83
          // All frames except the bottom one have three scopes.
          assertEquals(3, frame.scopeCount());
84
          assertEquals(debug.ScopeType.Local, frame.scope(0).scopeType());
85 86
          assertEquals(debug.ScopeType.Script, frame.scope(1).scopeType());
          assertEquals(debug.ScopeType.Global, frame.scope(2).scopeType());
87 88

          Object.keys(expected_locals).forEach(function (name) {
89 90
            assertEquals(expected_locals[name],
                         frame.scope(0).scopeObject().value()[name]);
91 92 93 94 95
          });

          for (var j = 0; j < expected_args.names.length; j++) {
            var arg_name = expected_args.names[j];
            var arg_value = expected_args.values[j];
96 97
            assertEquals(arg_value,
                         frame.scope(0).scopeObject().value()[arg_name]);
98
          }
99 100

          // Evaluate in the inlined frame.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
          Object.keys(expected_locals).forEach(function (name) {
            assertEquals(expected_locals[name], frame.evaluate(name).value());
          });

          for (var j = 0; j < expected_args.names.length; j++) {
            var arg_name = expected_args.names[j];
            var arg_value = expected_args.values[j];
            assertEquals(arg_value, frame.evaluate(arg_name).value());
            assertEquals(arg_value, frame.evaluate('arguments['+j+']').value());
          }

          var expected_args_sum = arraySum(expected_args.values);
          var expected_locals_sum =
              arraySum(Object.keys(expected_locals).
                       map(function (k) { return expected_locals[k]; }));

          assertEquals(expected_locals_sum + expected_args_sum,
118 119
                       frame.evaluate(Object.keys(expected_locals).join('+') +
                                      ' + ' +
120 121 122 123 124 125 126
                                      expected_args.names.join('+')).value());

          var arguments_sum = expected_args.names.map(function(_, idx) {
            return "arguments[" + idx + "]";
          }).join('+');
          assertEquals(expected_args_sum,
                       frame.evaluate(arguments_sum).value());
127
        } else {
128 129 130 131
          // The bottom frame only have the script scope and the global scope.
          assertEquals(2, frame.scopeCount());
          assertEquals(debug.ScopeType.Script, frame.scope(0).scopeType());
          assertEquals(debug.ScopeType.Global, frame.scope(1).scopeType());
132 133
        }

134 135 136 137 138 139 140 141 142 143 144
        // Check the frame function.
        switch (i) {
          case 0: assertEquals(h, frame.func().value()); break;
          case 1: assertEquals(g3, frame.func().value()); break;
          case 2: assertEquals(g2, frame.func().value()); break;
          case 3: assertEquals(g1, frame.func().value()); break;
          case 4: assertEquals(f, frame.func().value()); break;
          case 5: break;
          default: assertUnreachable();
        }

145
        // Check for construct call.
146 147 148 149 150 151 152
        if (i == 4) {
          assertEquals(testingConstructCall, frame.isConstructCall());
        } else if (i == 2) {
          assertTrue(frame.isConstructCall());
        } else {
          assertFalse(frame.isConstructCall());
        }
153

154 155 156
        if (i > 4) {
          assertFalse(frame.isOptimizedFrame());
          assertFalse(frame.isInlinedFrame());
157 158 159 160 161 162 163
        }
      }

      // Indicate that all was processed.
      listenerComplete = true;
    }
  } catch (e) {
164
    exception = e.toString() + e.stack;
165 166 167
  };
};

168
for (var i = 0; i < 4; i++) f(expected.length - 1, 11, 12);
169
%OptimizeFunctionOnNextCall(f);
170
f(expected.length - 1, 11, 12);
171 172 173 174

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

175 176 177
function h(i, x0, y0) {
  var a0 = expected[i].locals.a0;
  var b0 = expected[i].locals.b0;
178
  debugger;  // Breakpoint.
179
  return a0 + b0;
180 181 182 183 184 185
}

function g3(i, x1, y1) {
  var a1 = expected[i].locals.a1;
  var b1 = expected[i].locals.b1;
  h(i - 1, a1, b1);
186
  return a1 + b1;
187 188 189 190 191 192
}

function g2(i) {
  var a2 = expected[i].locals.a2;
  var b2 = expected[i].locals.b2;
  g3(i - 1, a2, b2);
193
  return a2 + b2;
194 195 196 197 198
}

function g1(i, x3, y3, z3) {
  var a3 = expected[i].locals.a3;
  var b3 = expected[i].locals.b3;
199
  new g2(i - 1, a3, b3);
200
  return a3 + b3;
201 202 203 204 205 206
}

function f(i, x4, y4) {
  var a4 = expected[i].locals.a4;
  var b4 = expected[i].locals.b4;
  g1(i - 1, a4, b4);
207
  return a4 + b4;
208
}
209

210
// Test calling f normally and as a constructor.
211 212
f(expected.length - 1, 11, 12);
f(expected.length - 1, 11, 12, 0);
213
testingConstructCall = true;
214 215
new f(expected.length - 1, 11, 12);
new f(expected.length - 1, 11, 12, 0);
216

217
// Make sure that the debug event listener was invoked.
218 219 220
assertFalse(exception, "exception in listener " + exception)
assertTrue(listenerComplete);

221 222 223
// Throw away type information for next run.
gc();

224
Debug.setListener(null);