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. 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 if (i == 4) { 144 assertEquals(testingConstructCall, frame.isConstructCall()); 145 } else if (i == 2) { 146 assertTrue(frame.isConstructCall()); 147 } else { 148 assertFalse(frame.isConstructCall()); 149 } 150 151 // When function f is optimized (1 means YES, see runtime.cc) we 152 // expect an optimized frame for f with g1, g2 and g3 inlined. 153 if (%GetOptimizationStatus(f) == 1) { 154 if (i == 1 || i == 2 || i == 3) { 155 assertTrue(frame.isOptimizedFrame()); 156 assertTrue(frame.isInlinedFrame()); 157 assertEquals(4 - i, frame.inlinedFrameIndex()); 158 } else if (i == 4) { 159 assertTrue(frame.isOptimizedFrame()); 160 assertFalse(frame.isInlinedFrame()); 161 } else { 162 assertFalse(frame.isOptimizedFrame()); 163 assertFalse(frame.isInlinedFrame()); 164 } 165 } 166 } 167 168 // Indicate that all was processed. 169 listenerComplete = true; 170 } 171 } catch (e) { 172 exception = e.toString() + e.stack; 173 }; 174}; 175 176for (var i = 0; i < 4; i++) f(input.length - 1, 11.11, 12.12); 177%OptimizeFunctionOnNextCall(f); 178f(input.length - 1, 11.11, 12.12); 179 180// Add the debug event listener. 181Debug.setListener(listener); 182 183function h(i, x0, y0) { 184 var a0 = input[i].a; 185 var b0 = input[i].b; 186 a0 = a0 + a0 / 100; 187 b0 = b0 + b0 / 100; 188 debugger; // Breakpoint. 189}; 190 191function g3(i, x1, y1) { 192 var a1 = input[i].a; 193 var b1 = input[i].b; 194 a1 = a1 + a1 / 100; 195 b1 = b1 + b1 / 100; 196 h(i - 1, a1, b1); 197 return a1+b1; 198}; 199 200function g2(i) { 201 var a2 = input[i].a; 202 var b2 = input[i].b; 203 a2 = a2 + a2 / 100; 204 b2 = b2 + b2 / 100; 205 g3(i - 1, a2, b2); 206}; 207 208function g1(i, x3, y3, z3) { 209 var a3 = input[i].a; 210 var b3 = input[i].b; 211 a3 = a3 + a3 / 100; 212 b3 = b3 + b3 / 100; 213 new g2(i - 1, a3, b3); 214}; 215 216function f(i, x4, y4) { 217 var a4 = input[i].a; 218 var b4 = input[i].b; 219 a4 = a4 + a4 / 100; 220 b4 = b4 + b4 / 100; 221 g1(i - 1, a4, b4); 222}; 223 224// Test calling f normally and as a constructor. 225f(input.length - 1, 11.11, 12.12); 226f(input.length - 1, 11.11, 12.12, ""); 227testingConstructCall = true; 228new f(input.length - 1, 11.11, 12.12); 229new f(input.length - 1, 11.11, 12.12, ""); 230 231// Make sure that the debug event listener was invoked. 232assertFalse(exception, "exception in listener " + exception) 233assertTrue(listenerComplete); 234 235//Throw away type information for next run. 236gc(); 237 238Debug.setListener(null); 239