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