Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1 | // Copyright 2008 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 | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 28 | // Flags: --expose-debug-as debug --allow-natives-syntax |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 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 | |
| 38 | function listener(event, exec_state, event_data, data) { |
| 39 | try { |
| 40 | if (event == Debug.DebugEvent.Break) |
| 41 | { |
| 42 | assertEquals(6, exec_state.frameCount()); |
| 43 | |
| 44 | for (var i = 0; i < exec_state.frameCount(); i++) { |
| 45 | var frame = exec_state.frame(i); |
| 46 | if (i < exec_state.frameCount() - 1) { |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 47 | var expected_a = i * 2 + 1; |
| 48 | var expected_b = i * 2 + 2; |
| 49 | var expected_x = (i + 1) * 2 + 1; |
| 50 | var expected_y = (i + 1) * 2 + 2; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 51 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 52 | // All frames except the bottom one has normal variables a and b. |
| 53 | var a = ('a' === frame.localName(0)) ? 0 : 1; |
| 54 | var b = 1 - a; |
| 55 | assertEquals('a', frame.localName(a)); |
| 56 | assertEquals('b', frame.localName(b)); |
| 57 | assertEquals(expected_a, frame.localValue(a).value()); |
| 58 | assertEquals(expected_b, frame.localValue(b).value()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 59 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 60 | // All frames except the bottom one has arguments variables x and y. |
| 61 | assertEquals('x', frame.argumentName(0)); |
| 62 | assertEquals('y', frame.argumentName(1)); |
| 63 | assertEquals(expected_x, frame.argumentValue(0).value()); |
| 64 | assertEquals(expected_y, frame.argumentValue(1).value()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 65 | |
| 66 | // All frames except the bottom one have two scopes. |
| 67 | assertEquals(2, frame.scopeCount()); |
| 68 | assertEquals(debug.ScopeType.Local, frame.scope(0).scopeType()); |
| 69 | assertEquals(debug.ScopeType.Global, frame.scope(1).scopeType()); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 70 | assertEquals(expected_a, frame.scope(0).scopeObject().value()['a']); |
| 71 | assertEquals(expected_b, frame.scope(0).scopeObject().value()['b']); |
| 72 | assertEquals(expected_x, frame.scope(0).scopeObject().value()['x']); |
| 73 | assertEquals(expected_y, frame.scope(0).scopeObject().value()['y']); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 74 | |
| 75 | // Evaluate in the inlined frame. |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 76 | assertEquals(expected_a, frame.evaluate('a').value()); |
| 77 | assertEquals(expected_x, frame.evaluate('x').value()); |
| 78 | assertEquals(expected_x, frame.evaluate('arguments[0]').value()); |
| 79 | assertEquals(expected_a + expected_b + expected_x + expected_y, |
| 80 | frame.evaluate('a + b + x + y').value()); |
| 81 | assertEquals(expected_x + expected_y, |
| 82 | frame.evaluate('arguments[0] + arguments[1]').value()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 83 | } else { |
| 84 | // The bottom frame only have the global scope. |
| 85 | assertEquals(1, frame.scopeCount()); |
| 86 | assertEquals(debug.ScopeType.Global, frame.scope(0).scopeType()); |
| 87 | } |
| 88 | |
| 89 | // Check the frame function. |
| 90 | switch (i) { |
| 91 | case 0: assertEquals(h, frame.func().value()); break; |
| 92 | case 1: assertEquals(g3, frame.func().value()); break; |
| 93 | case 2: assertEquals(g2, frame.func().value()); break; |
| 94 | case 3: assertEquals(g1, frame.func().value()); break; |
| 95 | case 4: assertEquals(f, frame.func().value()); break; |
| 96 | case 5: break; |
| 97 | default: assertUnreachable(); |
| 98 | } |
| 99 | |
| 100 | // Check for construct call. |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 101 | assertEquals(testingConstructCall && i == 4, frame.isConstructCall()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 102 | |
| 103 | // When function f is optimized (1 means YES, see runtime.cc) we |
| 104 | // expect an optimized frame for f with g1, g2 and g3 inlined. |
| 105 | if (%GetOptimizationStatus(f) == 1) { |
| 106 | if (i == 1 || i == 2 || i == 3) { |
| 107 | assertTrue(frame.isOptimizedFrame()); |
| 108 | assertTrue(frame.isInlinedFrame()); |
| 109 | assertEquals(4 - i, frame.inlinedFrameIndex()); |
| 110 | } else if (i == 4) { |
| 111 | assertTrue(frame.isOptimizedFrame()); |
| 112 | assertFalse(frame.isInlinedFrame()); |
| 113 | } else { |
| 114 | assertFalse(frame.isOptimizedFrame()); |
| 115 | assertFalse(frame.isInlinedFrame()); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Indicate that all was processed. |
| 121 | listenerComplete = true; |
| 122 | } |
| 123 | } catch (e) { |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 124 | exception = e.stack; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 125 | }; |
| 126 | }; |
| 127 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 128 | f();f();f(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 129 | %OptimizeFunctionOnNextCall(f); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 130 | f(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 131 | |
| 132 | // Add the debug event listener. |
| 133 | Debug.setListener(listener); |
| 134 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 135 | function h(x, y) { |
| 136 | var a = 1; |
| 137 | var b = 2; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 138 | debugger; // Breakpoint. |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 139 | }; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 140 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 141 | function g3(x, y) { |
| 142 | var a = 3; |
| 143 | var b = 4; |
| 144 | h(a, b); |
| 145 | }; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 146 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 147 | function g2(x, y) { |
| 148 | var a = 5; |
| 149 | var b = 6; |
| 150 | g3(a, b); |
| 151 | }; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 152 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 153 | function g1(x, y) { |
| 154 | var a = 7; |
| 155 | var b = 8; |
| 156 | g2(a, b); |
| 157 | }; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 158 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 159 | function f(x, y) { |
| 160 | var a = 9; |
| 161 | var b = 10; |
| 162 | g1(a, b); |
| 163 | }; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 164 | |
| 165 | // Test calling f normally and as a constructor. |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 166 | f(11, 12); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 167 | testingConstructCall = true; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 168 | new f(11, 12); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 169 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 170 | // Make sure that the debug event listener vas invoked. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 171 | assertFalse(exception, "exception in listener " + exception) |
| 172 | assertTrue(listenerComplete); |
| 173 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 174 | Debug.setListener(null); |