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