blob: cf25c0c095d6d1dee90c624145e0f9a440fc15ba [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002// 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 Murdoch3ef787d2012-04-12 10:51:47 +010028// Flags: --expose-debug-as debug --expose-gc --allow-natives-syntax --inline-construct
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000029// 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
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037var 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}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000058
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) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010068 var expected_args = expected[i].args;
69 var expected_locals = expected[i].locals;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000070
Ben Murdoch3ef787d2012-04-12 10:51:47 +010071 // 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);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000077
Ben Murdoch3ef787d2012-04-12 10:51:47 +010078 // 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 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000083
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());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010088
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 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000098
99 // Evaluate in the inlined frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100100 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());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000125 } 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.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143 if (i == 4) {
144 assertEquals(testingConstructCall, frame.isConstructCall());
145 } else if (i == 2) {
146 assertTrue(frame.isConstructCall());
147 } else {
148 assertFalse(frame.isConstructCall());
149 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000150
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) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100172 exception = e.toString() + e.stack;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000173 };
174};
175
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100176for (var i = 0; i < 4; i++) f(input.length - 1, 11.11, 12.12);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000177%OptimizeFunctionOnNextCall(f);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100178f(input.length - 1, 11.11, 12.12);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000179
180// Add the debug event listener.
181Debug.setListener(listener);
182
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100183function 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;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000188 debugger; // Breakpoint.
189};
190
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100191function 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;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000198};
199
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100200function 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);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000206};
207
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208function 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);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000214};
215
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100216function 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);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222};
223
224// Test calling f normally and as a constructor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100225f(input.length - 1, 11.11, 12.12);
226f(input.length - 1, 11.11, 12.12, "");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000227testingConstructCall = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100228new f(input.length - 1, 11.11, 12.12);
229new f(input.length - 1, 11.11, 12.12, "");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000230
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100231// Make sure that the debug event listener was invoked.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000232assertFalse(exception, "exception in listener " + exception)
233assertTrue(listenerComplete);
234
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235//Throw away type information for next run.
236gc();
237
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000238Debug.setListener(null);