blob: 9d539fe2824f0d726e7b364c188924dc62e24407 [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 Murdochb8a8cc12014-11-26 15:28:44 +000028// Flags: --expose-debug-as debug --expose-gc --allow-natives-syntax
29// Flags: --inline-construct
30
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000031// 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
Ben Murdoch3ef787d2012-04-12 10:51:47 +010039var expected = [
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040 { 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 Murdoch3ef787d2012-04-12 10:51:47 +010050];
51
52function arraySum(arr) {
53 return arr.reduce(function (a, b) { return a + b; }, 0);
54}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000055
56function 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 Murdoch3ef787d2012-04-12 10:51:47 +010065 var expected_args = expected[i].args;
66 var expected_locals = expected[i].locals;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000067
Ben Murdoch3ef787d2012-04-12 10:51:47 +010068 // 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 Murdoch3fb3ca82011-12-02 17:19:32 +000074
Ben Murdoch3ef787d2012-04-12 10:51:47 +010075 // 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 Murdochb8a8cc12014-11-26 15:28:44 +000078 assertEquals(expected_args.values[j],
79 frame.argumentValue(j).value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010080 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000081
Emily Bernierd0a1eb72015-03-24 16:35:39 -040082 // All frames except the bottom one have three scopes.
83 assertEquals(3, frame.scopeCount());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000084 assertEquals(debug.ScopeType.Local, frame.scope(0).scopeType());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040085 assertEquals(debug.ScopeType.Script, frame.scope(1).scopeType());
86 assertEquals(debug.ScopeType.Global, frame.scope(2).scopeType());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087
88 Object.keys(expected_locals).forEach(function (name) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 assertEquals(expected_locals[name],
90 frame.scope(0).scopeObject().value()[name]);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010091 });
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];
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 assertEquals(arg_value,
97 frame.scope(0).scopeObject().value()[arg_name]);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010098 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000099
100 // Evaluate in the inlined frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100101 Object.keys(expected_locals).forEach(function (name) {
102 assertEquals(expected_locals[name], frame.evaluate(name).value());
103 });
104
105 for (var j = 0; j < expected_args.names.length; j++) {
106 var arg_name = expected_args.names[j];
107 var arg_value = expected_args.values[j];
108 assertEquals(arg_value, frame.evaluate(arg_name).value());
109 assertEquals(arg_value, frame.evaluate('arguments['+j+']').value());
110 }
111
112 var expected_args_sum = arraySum(expected_args.values);
113 var expected_locals_sum =
114 arraySum(Object.keys(expected_locals).
115 map(function (k) { return expected_locals[k]; }));
116
117 assertEquals(expected_locals_sum + expected_args_sum,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 frame.evaluate(Object.keys(expected_locals).join('+') +
119 ' + ' +
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100120 expected_args.names.join('+')).value());
121
122 var arguments_sum = expected_args.names.map(function(_, idx) {
123 return "arguments[" + idx + "]";
124 }).join('+');
125 assertEquals(expected_args_sum,
126 frame.evaluate(arguments_sum).value());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000127 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400128 // The bottom frame only have the script scope and the global scope.
129 assertEquals(2, frame.scopeCount());
130 assertEquals(debug.ScopeType.Script, frame.scope(0).scopeType());
131 assertEquals(debug.ScopeType.Global, frame.scope(1).scopeType());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000132 }
133
134 // Check the frame function.
135 switch (i) {
136 case 0: assertEquals(h, frame.func().value()); break;
137 case 1: assertEquals(g3, frame.func().value()); break;
138 case 2: assertEquals(g2, frame.func().value()); break;
139 case 3: assertEquals(g1, frame.func().value()); break;
140 case 4: assertEquals(f, frame.func().value()); break;
141 case 5: break;
142 default: assertUnreachable();
143 }
144
145 // Check for construct call.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100146 if (i == 4) {
147 assertEquals(testingConstructCall, frame.isConstructCall());
148 } else if (i == 2) {
149 assertTrue(frame.isConstructCall());
150 } else {
151 assertFalse(frame.isConstructCall());
152 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000153
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 if (i > 4) {
155 assertFalse(frame.isOptimizedFrame());
156 assertFalse(frame.isInlinedFrame());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000157 }
158 }
159
160 // Indicate that all was processed.
161 listenerComplete = true;
162 }
163 } catch (e) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100164 exception = e.toString() + e.stack;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000165 };
166};
167
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100168for (var i = 0; i < 4; i++) f(expected.length - 1, 11, 12);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000169%OptimizeFunctionOnNextCall(f);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100170f(expected.length - 1, 11, 12);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000171
172// Add the debug event listener.
173Debug.setListener(listener);
174
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100175function h(i, x0, y0) {
176 var a0 = expected[i].locals.a0;
177 var b0 = expected[i].locals.b0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000178 debugger; // Breakpoint.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 return a0 + b0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100180}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000181
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100182function g3(i, x1, y1) {
183 var a1 = expected[i].locals.a1;
184 var b1 = expected[i].locals.b1;
185 h(i - 1, a1, b1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 return a1 + b1;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100187}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000188
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100189function g2(i) {
190 var a2 = expected[i].locals.a2;
191 var b2 = expected[i].locals.b2;
192 g3(i - 1, a2, b2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 return a2 + b2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100194}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000195
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100196function g1(i, x3, y3, z3) {
197 var a3 = expected[i].locals.a3;
198 var b3 = expected[i].locals.b3;
199 new g2(i - 1, a3, b3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 return a3 + b3;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100201}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000202
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100203function f(i, x4, y4) {
204 var a4 = expected[i].locals.a4;
205 var b4 = expected[i].locals.b4;
206 g1(i - 1, a4, b4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 return a4 + b4;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000209
210// Test calling f normally and as a constructor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100211f(expected.length - 1, 11, 12);
212f(expected.length - 1, 11, 12, 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000213testingConstructCall = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100214new f(expected.length - 1, 11, 12);
215new f(expected.length - 1, 11, 12, 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000216
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100217// Make sure that the debug event listener was invoked.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000218assertFalse(exception, "exception in listener " + exception)
219assertTrue(listenerComplete);
220
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100221// Throw away type information for next run.
222gc();
223
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000224Debug.setListener(null);