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(2, frame.scopeCount()); 93 assertEquals(debug.ScopeType.Local, frame.scope(0).scopeType()); 94 assertEquals(debug.ScopeType.Global, frame.scope(1).scopeType()); 95 96 Object.keys(expected_locals).forEach(function (name) { 97 assertEquals(expected_locals[name], 98 frame.scope(0).scopeObject().value()[name]); 99 }); 100 101 for (var j = 0; j < expected_args.names.length; j++) { 102 var arg_name = expected_args.names[j]; 103 var arg_value = expected_args.values[j]; 104 assertEquals(arg_value, 105 frame.scope(0).scopeObject().value()[arg_name]); 106 } 107 108 // Evaluate in the inlined frame. 109 Object.keys(expected_locals).forEach(function (name) { 110 assertEquals(expected_locals[name], frame.evaluate(name).value()); 111 }); 112 113 for (var j = 0; j < expected_args.names.length; j++) { 114 var arg_name = expected_args.names[j]; 115 var arg_value = expected_args.values[j]; 116 assertEquals(arg_value, frame.evaluate(arg_name).value()); 117 assertEquals(arg_value, frame.evaluate('arguments['+j+']').value()); 118 } 119 120 var expected_args_sum = arraySum(expected_args.values); 121 var expected_locals_sum = 122 arraySum(Object.keys(expected_locals). 123 map(function (k) { return expected_locals[k]; })); 124 125 assertEquals(expected_locals_sum + expected_args_sum, 126 frame.evaluate(Object.keys(expected_locals).join('+') + 127 ' + ' + 128 expected_args.names.join('+')).value()); 129 130 var arguments_sum = expected_args.names.map(function(_, idx) { 131 return "arguments[" + idx + "]"; 132 }).join('+'); 133 assertEquals(expected_args_sum, 134 frame.evaluate(arguments_sum).value()); 135 } else { 136 // The bottom frame only have the global scope. 137 assertEquals(1, frame.scopeCount()); 138 assertEquals(debug.ScopeType.Global, frame.scope(0).scopeType()); 139 } 140 141 // Check the frame function. 142 switch (i) { 143 case 0: assertEquals(h, frame.func().value()); break; 144 case 1: assertEquals(g3, frame.func().value()); break; 145 case 2: assertEquals(g2, frame.func().value()); break; 146 case 3: assertEquals(g1, frame.func().value()); break; 147 case 4: assertEquals(f, frame.func().value()); break; 148 case 5: break; 149 default: assertUnreachable(); 150 } 151 152 // Check for construct call. 153 if (i == 4) { 154 assertEquals(testingConstructCall, frame.isConstructCall()); 155 } else if (i == 2) { 156 assertTrue(frame.isConstructCall()); 157 } else { 158 assertFalse(frame.isConstructCall()); 159 } 160 161 if (i > 4) { 162 assertFalse(frame.isOptimizedFrame()); 163 assertFalse(frame.isInlinedFrame()); 164 } 165 } 166 167 // Indicate that all was processed. 168 listenerComplete = true; 169 } 170 } catch (e) { 171 exception = e.toString() + e.stack; 172 }; 173}; 174 175for (var i = 0; i < 4; i++) f(input.length - 1, 11.11, 12.12); 176%OptimizeFunctionOnNextCall(f); 177f(input.length - 1, 11.11, 12.12); 178 179// Add the debug event listener. 180Debug.setListener(listener); 181 182function h(i, x0, y0) { 183 var a0 = input[i].a; 184 var b0 = input[i].b; 185 a0 = a0 + a0 / 100; 186 b0 = b0 + b0 / 100; 187 debugger; // Breakpoint. 188 return a0 + b0; 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 return a2 + b2; 207}; 208 209function g1(i, x3, y3, z3) { 210 var a3 = input[i].a; 211 var b3 = input[i].b; 212 a3 = a3 + a3 / 100; 213 b3 = b3 + b3 / 100; 214 new g2(i - 1, a3, b3); 215 return a3 + b3; 216}; 217 218function f(i, x4, y4) { 219 var a4 = input[i].a; 220 var b4 = input[i].b; 221 a4 = a4 + a4 / 100; 222 b4 = b4 + b4 / 100; 223 g1(i - 1, a4, b4); 224 return a4 + b4; 225}; 226 227// Test calling f normally and as a constructor. 228f(input.length - 1, 11.11, 12.12); 229f(input.length - 1, 11.11, 12.12, ""); 230testingConstructCall = true; 231new f(input.length - 1, 11.11, 12.12); 232new f(input.length - 1, 11.11, 12.12, ""); 233 234// Make sure that the debug event listener was invoked. 235assertFalse(exception, "exception in listener " + exception) 236assertTrue(listenerComplete); 237 238//Throw away type information for next run. 239gc(); 240 241Debug.setListener(null); 242