OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --expose-debug-as debug --noanalyze-environment-liveness | |
6 | |
7 var Debug = debug.Debug; | |
8 | |
9 function RunTest(name, formals_and_body, args, handler, continuation) { | |
10 var handler_called = false; | |
11 var exception = null; | |
12 | |
13 function listener(event, exec_state, event_data, data) { | |
14 try { | |
15 if (event == Debug.DebugEvent.Break) { | |
16 handler_called = true; | |
17 handler(exec_state); | |
18 } | |
19 } catch (e) { | |
20 exception = e; | |
21 } | |
22 } | |
23 | |
24 function run(thunk) { | |
25 handler_called = false; | |
26 exception = null; | |
27 | |
28 var res = thunk(); | |
29 if (continuation) | |
30 continuation(res); | |
31 | |
32 assertTrue(handler_called, "listener not called for " + name); | |
33 assertNull(exception, name + " / " + exception); | |
34 } | |
35 | |
36 var fun = Function.apply(null, formals_and_body); | |
37 var gen = (function*(){}).constructor.apply(null, formals_and_body); | |
38 | |
39 Debug.setListener(listener); | |
40 | |
41 run(function () { return fun.apply(null, args) }); | |
42 run(function () { return gen.apply(null, args).next().value }); | |
43 | |
44 // TODO(wingo): Uncomment after bug 2838 is fixed. | |
45 // Debug.setListener(null); | |
46 } | |
47 | |
48 // Check that two scope are the same. | |
49 function assertScopeMirrorEquals(scope1, scope2) { | |
50 assertEquals(scope1.scopeType(), scope2.scopeType()); | |
51 assertEquals(scope1.frameIndex(), scope2.frameIndex()); | |
52 assertEquals(scope1.scopeIndex(), scope2.scopeIndex()); | |
53 assertPropertiesEqual(scope1.scopeObject().value(), scope2.scopeObject().value
()); | |
54 } | |
55 | |
56 function CheckFastAllScopes(scopes, exec_state) { | |
57 var fast_all_scopes = exec_state.frame().allScopes(true); | |
58 var length = fast_all_scopes.length; | |
59 assertTrue(scopes.length >= length); | |
60 for (var i = 0; i < scopes.length && i < length; i++) { | |
61 var scope = fast_all_scopes[length - i - 1]; | |
62 assertTrue(scope.isScope()); | |
63 assertEquals(scopes[scopes.length - i - 1], scope.scopeType()); | |
64 } | |
65 } | |
66 | |
67 // Check that the scope chain contains the expected types of scopes. | |
68 function CheckScopeChain(scopes, exec_state) { | |
69 var all_scopes = exec_state.frame().allScopes(); | |
70 assertEquals(scopes.length, exec_state.frame().scopeCount()); | |
71 assertEquals(scopes.length, all_scopes.length, "FrameMirror.allScopes length")
; | |
72 for (var i = 0; i < scopes.length; i++) { | |
73 var scope = exec_state.frame().scope(i); | |
74 assertTrue(scope.isScope()); | |
75 assertEquals(scopes[i], scope.scopeType()); | |
76 assertScopeMirrorEquals(all_scopes[i], scope); | |
77 | |
78 // Check the global object when hitting the global scope. | |
79 if (scopes[i] == debug.ScopeType.Global) { | |
80 // Objects don't have same class (one is "global", other is "Object", | |
81 // so just check the properties directly. | |
82 assertPropertiesEqual(this, scope.scopeObject().value()); | |
83 } | |
84 } | |
85 CheckFastAllScopes(scopes, exec_state); | |
86 } | |
87 | |
88 // Check that the content of the scope is as expected. For functions just check | |
89 // that there is a function. | |
90 function CheckScopeContent(content, number, exec_state) { | |
91 var scope = exec_state.frame().scope(number); | |
92 var count = 0; | |
93 for (var p in content) { | |
94 var property_mirror = scope.scopeObject().property(p); | |
95 assertFalse(property_mirror.isUndefined(), | |
96 'property ' + p + ' not found in scope'); | |
97 if (typeof(content[p]) === 'function') { | |
98 assertTrue(property_mirror.value().isFunction()); | |
99 } else { | |
100 assertEquals(content[p], property_mirror.value().value(), | |
101 'property ' + p + ' has unexpected value'); | |
102 } | |
103 count++; | |
104 } | |
105 | |
106 // 'arguments' and might be exposed in the local and closure scope. Just | |
107 // ignore this. | |
108 var scope_size = scope.scopeObject().properties().length; | |
109 if (!scope.scopeObject().property('arguments').isUndefined()) { | |
110 scope_size--; | |
111 } | |
112 // Skip property with empty name. | |
113 if (!scope.scopeObject().property('').isUndefined()) { | |
114 scope_size--; | |
115 } | |
116 | |
117 if (count != scope_size) { | |
118 print('Names found in scope:'); | |
119 var names = scope.scopeObject().propertyNames(); | |
120 for (var i = 0; i < names.length; i++) { | |
121 print(names[i]); | |
122 } | |
123 } | |
124 assertEquals(count, scope_size); | |
125 } | |
126 | |
127 | |
128 // Simple empty local scope. | |
129 RunTest("Local 1", | |
130 ['debugger;'], | |
131 [], | |
132 function (exec_state) { | |
133 CheckScopeChain([debug.ScopeType.Local, | |
134 debug.ScopeType.Script, | |
135 debug.ScopeType.Global], exec_state); | |
136 CheckScopeContent({}, 0, exec_state); | |
137 }); | |
138 | |
139 // Local scope with a parameter. | |
140 RunTest("Local 2", | |
141 ['a', 'debugger;'], | |
142 [1], | |
143 function (exec_state) { | |
144 CheckScopeChain([debug.ScopeType.Local, | |
145 debug.ScopeType.Script, | |
146 debug.ScopeType.Global], exec_state); | |
147 CheckScopeContent({a:1}, 0, exec_state); | |
148 }); | |
149 | |
150 // Local scope with a parameter and a local variable. | |
151 RunTest("Local 3", | |
152 ['a', 'var x = 3; debugger;'], | |
153 [1], | |
154 function (exec_state) { | |
155 CheckScopeChain([debug.ScopeType.Local, | |
156 debug.ScopeType.Script, | |
157 debug.ScopeType.Global], exec_state); | |
158 CheckScopeContent({a:1,x:3}, 0, exec_state); | |
159 }); | |
160 | |
161 // Local scope with parameters and local variables. | |
162 RunTest("Local 4", | |
163 ['a', 'b', 'var x = 3; var y = 4; debugger;'], | |
164 [1, 2], | |
165 function (exec_state) { | |
166 CheckScopeChain([debug.ScopeType.Local, | |
167 debug.ScopeType.Script, | |
168 debug.ScopeType.Global], exec_state); | |
169 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state); | |
170 }); | |
171 | |
172 // Empty local scope with use of eval. | |
173 RunTest("Local 5", | |
174 ['eval(""); debugger;'], | |
175 [], | |
176 function (exec_state) { | |
177 CheckScopeChain([debug.ScopeType.Local, | |
178 debug.ScopeType.Script, | |
179 debug.ScopeType.Global], exec_state); | |
180 CheckScopeContent({}, 0, exec_state); | |
181 }); | |
182 | |
183 // Local introducing local variable using eval. | |
184 RunTest("Local 6", | |
185 ['eval("var i = 5"); debugger;'], | |
186 [], | |
187 function (exec_state) { | |
188 CheckScopeChain([debug.ScopeType.Local, | |
189 debug.ScopeType.Script, | |
190 debug.ScopeType.Global], exec_state); | |
191 CheckScopeContent({i:5}, 0, exec_state); | |
192 }); | |
193 | |
194 // Local scope with parameters, local variables and local variable introduced | |
195 // using eval. | |
196 RunTest("Local 7", | |
197 ['a', 'b', | |
198 "var x = 3; var y = 4;\n" | |
199 + "eval('var i = 5'); eval ('var j = 6');\n" | |
200 + "debugger;"], | |
201 [1, 2], | |
202 function (exec_state) { | |
203 CheckScopeChain([debug.ScopeType.Local, | |
204 debug.ScopeType.Script, | |
205 debug.ScopeType.Global], exec_state); | |
206 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 0, exec_state); | |
207 }); | |
208 | |
209 // Nested empty with blocks. | |
210 RunTest("With", | |
211 ["with ({}) { with ({}) { debugger; } }"], | |
212 [], | |
213 function (exec_state) { | |
214 CheckScopeChain([debug.ScopeType.With, | |
215 debug.ScopeType.With, | |
216 debug.ScopeType.Local, | |
217 debug.ScopeType.Script, | |
218 debug.ScopeType.Global], exec_state); | |
219 CheckScopeContent({}, 0, exec_state); | |
220 CheckScopeContent({}, 1, exec_state); | |
221 }); | |
222 | |
223 // Simple closure formed by returning an inner function referering the outer | |
224 // functions arguments. | |
225 RunTest("Closure 1", | |
226 ['a', 'return function() { debugger; return a; }'], | |
227 [1], | |
228 function (exec_state) { | |
229 CheckScopeChain([debug.ScopeType.Local, | |
230 debug.ScopeType.Closure, | |
231 debug.ScopeType.Script, | |
232 debug.ScopeType.Global], exec_state); | |
233 CheckScopeContent({a:1}, 1, exec_state); | |
234 }, | |
235 function (result) { result() }); | |
236 | |
237 RunTest("The full monty", | |
238 ['a', 'b', | |
239 "var x = 3;\n" + | |
240 "var y = 4;\n" + | |
241 "eval('var i = 5');\n" + | |
242 "eval('var j = 6');\n" + | |
243 "function f(a, b) {\n" + | |
244 " var x = 9;\n" + | |
245 " var y = 10;\n" + | |
246 " eval('var i = 11');\n" + | |
247 " eval('var j = 12');\n" + | |
248 " with ({j:13}){\n" + | |
249 " return function() {\n" + | |
250 " var x = 14;\n" + | |
251 " with ({a:15}) {\n" + | |
252 " with ({b:16}) {\n" + | |
253 " debugger;\n" + | |
254 " some_global = a;\n" + | |
255 " return f;\n" + | |
256 " }\n" + | |
257 " }\n" + | |
258 " };\n" + | |
259 " }\n" + | |
260 "}\n" + | |
261 "return f(a, b);"], | |
262 [1, 2], | |
263 function (exec_state) { | |
264 CheckScopeChain([debug.ScopeType.With, | |
265 debug.ScopeType.With, | |
266 debug.ScopeType.Local, | |
267 debug.ScopeType.With, | |
268 debug.ScopeType.Closure, | |
269 debug.ScopeType.Closure, | |
270 debug.ScopeType.Script, | |
271 debug.ScopeType.Global], exec_state); | |
272 CheckScopeContent({b:16}, 0, exec_state); | |
273 CheckScopeContent({a:15}, 1, exec_state); | |
274 CheckScopeContent({x:14}, 2, exec_state); | |
275 CheckScopeContent({j:13}, 3, exec_state); | |
276 CheckScopeContent({a:1,b:2,x:9,y:10,i:11,j:12}, 4, exec_state); | |
277 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6,f:function(){}}, 5, | |
278 exec_state); | |
279 }, | |
280 function (result) { result() }); | |
281 | |
282 RunTest("Catch block 1", | |
283 ["try { throw 'Exception'; } catch (e) { debugger; }"], | |
284 [], | |
285 function (exec_state) { | |
286 CheckScopeChain([debug.ScopeType.Catch, | |
287 debug.ScopeType.Local, | |
288 debug.ScopeType.Script, | |
289 debug.ScopeType.Global], exec_state); | |
290 CheckScopeContent({e:'Exception'}, 0, exec_state); | |
291 }); | |
OLD | NEW |