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 --harmony-generators | |
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 gen = (function*(){}).constructor.apply(null, formals_and_body); | |
37 | |
38 Debug.setListener(listener); | |
39 | |
40 run(function () { return gen.apply(null, args).next().value }); | |
41 | |
42 Debug.setListener(null); | |
43 } | |
44 | |
45 // Check that two scope are the same. | |
46 function assertScopeMirrorEquals(scope1, scope2) { | |
47 assertEquals(scope1.scopeType(), scope2.scopeType()); | |
48 assertEquals(scope1.frameIndex(), scope2.frameIndex()); | |
49 assertEquals(scope1.scopeIndex(), scope2.scopeIndex()); | |
50 assertPropertiesEqual(scope1.scopeObject().value(), scope2.scopeObject().value
()); | |
51 } | |
52 | |
53 function CheckFastAllScopes(scopes, exec_state) { | |
54 var fast_all_scopes = exec_state.frame().allScopes(true); | |
55 var length = fast_all_scopes.length; | |
56 assertTrue(scopes.length >= length); | |
57 for (var i = 0; i < scopes.length && i < length; i++) { | |
58 var scope = fast_all_scopes[length - i - 1]; | |
59 assertTrue(scope.isScope()); | |
60 assertEquals(scopes[scopes.length - i - 1], scope.scopeType()); | |
61 } | |
62 } | |
63 | |
64 // Check that the scope chain contains the expected types of scopes. | |
65 function CheckScopeChain(scopes, exec_state) { | |
66 var all_scopes = exec_state.frame().allScopes(); | |
67 assertEquals(scopes.length, exec_state.frame().scopeCount()); | |
68 assertEquals(scopes.length, all_scopes.length, "FrameMirror.allScopes length")
; | |
69 for (var i = 0; i < scopes.length; i++) { | |
70 var scope = exec_state.frame().scope(i); | |
71 assertTrue(scope.isScope()); | |
72 assertEquals(scopes[i], scope.scopeType()); | |
73 assertScopeMirrorEquals(all_scopes[i], scope); | |
74 | |
75 // Check the global object when hitting the global scope. | |
76 if (scopes[i] == debug.ScopeType.Global) { | |
77 // Objects don't have same class (one is "global", other is "Object", | |
78 // so just check the properties directly. | |
79 assertPropertiesEqual(this, scope.scopeObject().value()); | |
80 } | |
81 } | |
82 CheckFastAllScopes(scopes, exec_state); | |
83 | |
84 // Get the debug command processor. | |
85 var dcp = exec_state.debugCommandProcessor("unspecified_running_state"); | |
86 | |
87 // Send a scopes request and check the result. | |
88 var json; | |
89 var request_json = '{"seq":0,"type":"request","command":"scopes"}'; | |
90 var response_json = dcp.processDebugJSONRequest(request_json); | |
91 var response = JSON.parse(response_json); | |
92 assertEquals(scopes.length, response.body.scopes.length); | |
93 for (var i = 0; i < scopes.length; i++) { | |
94 assertEquals(i, response.body.scopes[i].index); | |
95 assertEquals(scopes[i], response.body.scopes[i].type); | |
96 if (scopes[i] == debug.ScopeType.Local || | |
97 scopes[i] == debug.ScopeType.Closure) { | |
98 assertTrue(response.body.scopes[i].object.ref < 0); | |
99 } else { | |
100 assertTrue(response.body.scopes[i].object.ref >= 0); | |
101 } | |
102 var found = false; | |
103 for (var j = 0; j < response.refs.length && !found; j++) { | |
104 found = response.refs[j].handle == response.body.scopes[i].object.ref; | |
105 } | |
106 assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " n
ot found"); | |
107 } | |
108 } | |
109 | |
110 // Check that the content of the scope is as expected. For functions just check | |
111 // that there is a function. | |
112 function CheckScopeContent(content, number, exec_state) { | |
113 var scope = exec_state.frame().scope(number); | |
114 var count = 0; | |
115 for (var p in content) { | |
116 var property_mirror = scope.scopeObject().property(p); | |
117 assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in
scope'); | |
118 if (typeof(content[p]) === 'function') { | |
119 assertTrue(property_mirror.value().isFunction()); | |
120 } else { | |
121 assertEquals(content[p], property_mirror.value().value(), 'property ' + p
+ ' has unexpected value'); | |
122 } | |
123 count++; | |
124 } | |
125 | |
126 // 'arguments' and might be exposed in the local and closure scope. Just | |
127 // ignore this. | |
128 var scope_size = scope.scopeObject().properties().length; | |
129 if (!scope.scopeObject().property('arguments').isUndefined()) { | |
130 scope_size--; | |
131 } | |
132 // Skip property with empty name. | |
133 if (!scope.scopeObject().property('').isUndefined()) { | |
134 scope_size--; | |
135 } | |
136 | |
137 if (count != scope_size) { | |
138 print('Names found in scope:'); | |
139 var names = scope.scopeObject().propertyNames(); | |
140 for (var i = 0; i < names.length; i++) { | |
141 print(names[i]); | |
142 } | |
143 } | |
144 assertEquals(count, scope_size); | |
145 | |
146 // Get the debug command processor. | |
147 var dcp = exec_state.debugCommandProcessor("unspecified_running_state"); | |
148 | |
149 // Send a scope request for information on a single scope and check the | |
150 // result. | |
151 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"
number":'; | |
152 request_json += scope.scopeIndex(); | |
153 request_json += '}}'; | |
154 var response_json = dcp.processDebugJSONRequest(request_json); | |
155 var response = JSON.parse(response_json); | |
156 assertEquals(scope.scopeType(), response.body.type); | |
157 assertEquals(number, response.body.index); | |
158 if (scope.scopeType() == debug.ScopeType.Local || | |
159 scope.scopeType() == debug.ScopeType.Closure) { | |
160 assertTrue(response.body.object.ref < 0); | |
161 } else { | |
162 assertTrue(response.body.object.ref >= 0); | |
163 } | |
164 var found = false; | |
165 for (var i = 0; i < response.refs.length && !found; i++) { | |
166 found = response.refs[i].handle == response.body.object.ref; | |
167 } | |
168 assertTrue(found, "Scope object " + response.body.object.ref + " not found"); | |
169 } | |
170 | |
171 | |
172 // Simple empty local scope. | |
173 RunTest("Local 1", | |
174 ['debugger;'], | |
175 [], | |
176 function (exec_state) { | |
177 CheckScopeChain([debug.ScopeType.Local, | |
178 debug.ScopeType.Global], exec_state); | |
179 CheckScopeContent({}, 0, exec_state); | |
180 }); | |
181 | |
182 // Local scope with a parameter. | |
183 RunTest("Local 2", | |
184 ['a', 'debugger;'], | |
185 [1], | |
186 function (exec_state) { | |
187 CheckScopeChain([debug.ScopeType.Local, | |
188 debug.ScopeType.Global], exec_state); | |
189 CheckScopeContent({a:1}, 0, exec_state); | |
190 }); | |
191 | |
192 // Local scope with a parameter and a local variable. | |
193 RunTest("Local 3", | |
194 ['a', 'var x = 3; debugger;'], | |
195 [1], | |
196 function (exec_state) { | |
197 CheckScopeChain([debug.ScopeType.Local, | |
198 debug.ScopeType.Global], exec_state); | |
199 CheckScopeContent({a:1,x:3}, 0, exec_state); | |
200 }); | |
201 | |
202 // Local scope with parameters and local variables. | |
203 RunTest("Local 4", | |
204 ['a', 'b', 'var x = 3; var y = 4; debugger;'], | |
205 [1, 2], | |
206 function (exec_state) { | |
207 CheckScopeChain([debug.ScopeType.Local, | |
208 debug.ScopeType.Global], exec_state); | |
209 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state); | |
210 }); | |
211 | |
212 // Empty local scope with use of eval. | |
213 RunTest("Local 5", | |
214 ['eval(""); debugger;'], | |
215 [], | |
216 function (exec_state) { | |
217 CheckScopeChain([debug.ScopeType.Local, | |
218 debug.ScopeType.Global], exec_state); | |
219 CheckScopeContent({}, 0, exec_state); | |
220 }); | |
221 | |
222 // Local introducing local variable using eval. | |
223 RunTest("Local 6", | |
224 ['eval("var i = 5"); debugger;'], | |
225 [], | |
226 function (exec_state) { | |
227 CheckScopeChain([debug.ScopeType.Local, | |
228 debug.ScopeType.Global], exec_state); | |
229 CheckScopeContent({i:5}, 0, exec_state); | |
230 }); | |
231 | |
232 // Local scope with parameters, local variables and local variable introduced | |
233 // using eval. | |
234 RunTest("Local 7", | |
235 ['a', 'b', | |
236 "var x = 3; var y = 4;\n" | |
237 + "eval('var i = 5'); eval ('var j = 6');\n" | |
238 + "debugger;"], | |
239 [1, 2], | |
240 function (exec_state) { | |
241 CheckScopeChain([debug.ScopeType.Local, | |
242 debug.ScopeType.Global], exec_state); | |
243 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 0, exec_state); | |
244 }); | |
245 | |
246 // Nested empty with blocks. | |
247 RunTest("With", | |
248 ["with ({}) { with ({}) { debugger; } }"], | |
249 [], | |
250 function (exec_state) { | |
251 CheckScopeChain([debug.ScopeType.With, | |
252 debug.ScopeType.With, | |
253 debug.ScopeType.Local, | |
254 debug.ScopeType.Global], exec_state); | |
255 CheckScopeContent({}, 0, exec_state); | |
256 CheckScopeContent({}, 1, exec_state); | |
257 }); | |
258 | |
259 // Simple closure formed by returning an inner function referering the outer | |
260 // functions arguments. | |
261 RunTest("Closure 1", | |
262 ['a', 'return function() { debugger; return a; }'], | |
263 [1], | |
264 function (exec_state) { | |
265 CheckScopeChain([debug.ScopeType.Local, | |
266 debug.ScopeType.Closure, | |
267 debug.ScopeType.Global], exec_state); | |
268 CheckScopeContent({a:1}, 1, exec_state); | |
269 }, | |
270 function (result) { result() }); | |
271 | |
272 RunTest("The full monty", | |
273 ['a', 'b', | |
274 "var x = 3;\n" + | |
275 "var y = 4;\n" + | |
276 "eval('var i = 5');\n" + | |
277 "eval('var j = 6');\n" + | |
278 "function f(a, b) {\n" + | |
279 " var x = 9;\n" + | |
280 " var y = 10;\n" + | |
281 " eval('var i = 11');\n" + | |
282 " eval('var j = 12');\n" + | |
283 " with ({j:13}){\n" + | |
284 " return function() {\n" + | |
285 " var x = 14;\n" + | |
286 " with ({a:15}) {\n" + | |
287 " with ({b:16}) {\n" + | |
288 " debugger;\n" + | |
289 " some_global = a;\n" + | |
290 " return f;\n" + | |
291 " }\n" + | |
292 " }\n" + | |
293 " };\n" + | |
294 " }\n" + | |
295 "}\n" + | |
296 "return f(a, b);"], | |
297 [1, 2], | |
298 function (exec_state) { | |
299 CheckScopeChain([debug.ScopeType.With, | |
300 debug.ScopeType.With, | |
301 debug.ScopeType.Local, | |
302 debug.ScopeType.With, | |
303 debug.ScopeType.Closure, | |
304 debug.ScopeType.Closure, | |
305 debug.ScopeType.Global], exec_state); | |
306 CheckScopeContent({b:16}, 0, exec_state); | |
307 CheckScopeContent({a:15}, 1, exec_state); | |
308 CheckScopeContent({x:14}, 2, exec_state); | |
309 CheckScopeContent({j:13}, 3, exec_state); | |
310 CheckScopeContent({a:1,b:2,x:9,y:10,i:11,j:12}, 4, exec_state); | |
311 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6,f:function(){}}, 5, exec_st
ate); | |
312 }, | |
313 function (result) { result() }); | |
314 | |
315 RunTest("Catch block 1", | |
316 ["try { throw 'Exception'; } catch (e) { debugger; }"], | |
317 [], | |
318 function (exec_state) { | |
319 CheckScopeChain([debug.ScopeType.Catch, | |
320 debug.ScopeType.Local, | |
321 debug.ScopeType.Global], exec_state); | |
322 CheckScopeContent({e:'Exception'}, 0, exec_state); | |
323 }); | |
OLD | NEW |