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