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.
5 // Flags: --expose-debug-as debug --harmony-generators
7 var Debug = debug.Debug;
9 function RunTest(name, formals_and_body, args, handler, continuation) {
10 var handler_called = false;
13 function listener(event, exec_state, event_data, data) {
15 if (event == Debug.DebugEvent.Break) {
16 handler_called = true;
25 handler_called = false;
32 assertTrue(handler_called, "listener not called for " + name);
33 assertNull(exception, name + " / " + exception);
36 var fun = Function.apply(null, formals_and_body);
37 var gen = (function*(){}).constructor.apply(null, formals_and_body);
39 Debug.setListener(listener);
41 run(function () { return fun.apply(null, args) });
42 run(function () { return gen.apply(null, args).next().value });
44 // TODO(wingo): Uncomment after bug 2838 is fixed.
45 // Debug.setListener(null);
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());
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());
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);
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());
85 CheckFastAllScopes(scopes, exec_state);
87 // Get the debug command processor.
88 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
90 // Send a scopes request and check the result.
92 var request_json = '{"seq":0,"type":"request","command":"scopes"}';
93 var response_json = dcp.processDebugJSONRequest(request_json);
94 var response = JSON.parse(response_json);
95 assertEquals(scopes.length, response.body.scopes.length);
96 for (var i = 0; i < scopes.length; i++) {
97 assertEquals(i, response.body.scopes[i].index);
98 assertEquals(scopes[i], response.body.scopes[i].type);
99 if (scopes[i] == debug.ScopeType.Local ||
100 scopes[i] == debug.ScopeType.Closure) {
101 assertTrue(response.body.scopes[i].object.ref < 0);
103 assertTrue(response.body.scopes[i].object.ref >= 0);
106 for (var j = 0; j < response.refs.length && !found; j++) {
107 found = response.refs[j].handle == response.body.scopes[i].object.ref;
109 assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
113 // Check that the content of the scope is as expected. For functions just check
114 // that there is a function.
115 function CheckScopeContent(content, number, exec_state) {
116 var scope = exec_state.frame().scope(number);
118 for (var p in content) {
119 var property_mirror = scope.scopeObject().property(p);
120 assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
121 if (typeof(content[p]) === 'function') {
122 assertTrue(property_mirror.value().isFunction());
124 assertEquals(content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
129 // 'arguments' and might be exposed in the local and closure scope. Just
131 var scope_size = scope.scopeObject().properties().length;
132 if (!scope.scopeObject().property('arguments').isUndefined()) {
135 // Skip property with empty name.
136 if (!scope.scopeObject().property('').isUndefined()) {
140 if (count != scope_size) {
141 print('Names found in scope:');
142 var names = scope.scopeObject().propertyNames();
143 for (var i = 0; i < names.length; i++) {
147 assertEquals(count, scope_size);
149 // Get the debug command processor.
150 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
152 // Send a scope request for information on a single scope and check the
154 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
155 request_json += scope.scopeIndex();
156 request_json += '}}';
157 var response_json = dcp.processDebugJSONRequest(request_json);
158 var response = JSON.parse(response_json);
159 assertEquals(scope.scopeType(), response.body.type);
160 assertEquals(number, response.body.index);
161 if (scope.scopeType() == debug.ScopeType.Local ||
162 scope.scopeType() == debug.ScopeType.Closure) {
163 assertTrue(response.body.object.ref < 0);
165 assertTrue(response.body.object.ref >= 0);
168 for (var i = 0; i < response.refs.length && !found; i++) {
169 found = response.refs[i].handle == response.body.object.ref;
171 assertTrue(found, "Scope object " + response.body.object.ref + " not found");
175 // Simple empty local scope.
179 function (exec_state) {
180 CheckScopeChain([debug.ScopeType.Local,
181 debug.ScopeType.Global], exec_state);
182 CheckScopeContent({}, 0, exec_state);
185 // Local scope with a parameter.
189 function (exec_state) {
190 CheckScopeChain([debug.ScopeType.Local,
191 debug.ScopeType.Global], exec_state);
192 CheckScopeContent({a:1}, 0, exec_state);
195 // Local scope with a parameter and a local variable.
197 ['a', 'var x = 3; debugger;'],
199 function (exec_state) {
200 CheckScopeChain([debug.ScopeType.Local,
201 debug.ScopeType.Global], exec_state);
202 CheckScopeContent({a:1,x:3}, 0, exec_state);
205 // Local scope with parameters and local variables.
207 ['a', 'b', 'var x = 3; var y = 4; debugger;'],
209 function (exec_state) {
210 CheckScopeChain([debug.ScopeType.Local,
211 debug.ScopeType.Global], exec_state);
212 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
215 // Empty local scope with use of eval.
217 ['eval(""); debugger;'],
219 function (exec_state) {
220 CheckScopeChain([debug.ScopeType.Local,
221 debug.ScopeType.Global], exec_state);
222 CheckScopeContent({}, 0, exec_state);
225 // Local introducing local variable using eval.
227 ['eval("var i = 5"); debugger;'],
229 function (exec_state) {
230 CheckScopeChain([debug.ScopeType.Local,
231 debug.ScopeType.Global], exec_state);
232 CheckScopeContent({i:5}, 0, exec_state);
235 // Local scope with parameters, local variables and local variable introduced
239 "var x = 3; var y = 4;\n"
240 + "eval('var i = 5'); eval ('var j = 6');\n"
243 function (exec_state) {
244 CheckScopeChain([debug.ScopeType.Local,
245 debug.ScopeType.Global], exec_state);
246 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 0, exec_state);
249 // Nested empty with blocks.
251 ["with ({}) { with ({}) { debugger; } }"],
253 function (exec_state) {
254 CheckScopeChain([debug.ScopeType.With,
255 debug.ScopeType.With,
256 debug.ScopeType.Local,
257 debug.ScopeType.Global], exec_state);
258 CheckScopeContent({}, 0, exec_state);
259 CheckScopeContent({}, 1, exec_state);
262 // Simple closure formed by returning an inner function referering the outer
263 // functions arguments.
265 ['a', 'return function() { debugger; return a; }'],
267 function (exec_state) {
268 CheckScopeChain([debug.ScopeType.Local,
269 debug.ScopeType.Closure,
270 debug.ScopeType.Global], exec_state);
271 CheckScopeContent({a:1}, 1, exec_state);
273 function (result) { result() });
275 RunTest("The full monty",
279 "eval('var i = 5');\n" +
280 "eval('var j = 6');\n" +
281 "function f(a, b) {\n" +
284 " eval('var i = 11');\n" +
285 " eval('var j = 12');\n" +
286 " with ({j:13}){\n" +
287 " return function() {\n" +
289 " with ({a:15}) {\n" +
290 " with ({b:16}) {\n" +
292 " some_global = a;\n" +
301 function (exec_state) {
302 CheckScopeChain([debug.ScopeType.With,
303 debug.ScopeType.With,
304 debug.ScopeType.Local,
305 debug.ScopeType.With,
306 debug.ScopeType.Closure,
307 debug.ScopeType.Closure,
308 debug.ScopeType.Global], exec_state);
309 CheckScopeContent({b:16}, 0, exec_state);
310 CheckScopeContent({a:15}, 1, exec_state);
311 CheckScopeContent({x:14}, 2, exec_state);
312 CheckScopeContent({j:13}, 3, exec_state);
313 CheckScopeContent({a:1,b:2,x:9,y:10,i:11,j:12}, 4, exec_state);
314 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6,f:function(){}}, 5, exec_state);
316 function (result) { result() });
318 RunTest("Catch block 1",
319 ["try { throw 'Exception'; } catch (e) { debugger; }"],
321 function (exec_state) {
322 CheckScopeChain([debug.ScopeType.Catch,
323 debug.ScopeType.Local,
324 debug.ScopeType.Global], exec_state);
325 CheckScopeContent({e:'Exception'}, 0, exec_state);