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
6
7var Debug = debug.Debug;
8
9function 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.
49function 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
56function 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.
68function 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  // Get the debug command processor.
88  var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
89
90  // Send a scopes request and check the result.
91  var json;
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.Script ||
101        scopes[i] == debug.ScopeType.Closure) {
102      assertTrue(response.body.scopes[i].object.ref < 0);
103    } else {
104      assertTrue(response.body.scopes[i].object.ref >= 0);
105    }
106    var found = false;
107    for (var j = 0; j < response.refs.length && !found; j++) {
108      found = response.refs[j].handle == response.body.scopes[i].object.ref;
109    }
110    assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
111  }
112}
113
114// Check that the content of the scope is as expected. For functions just check
115// that there is a function.
116function CheckScopeContent(content, number, exec_state) {
117  var scope = exec_state.frame().scope(number);
118  var count = 0;
119  for (var p in content) {
120    var property_mirror = scope.scopeObject().property(p);
121    assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
122    if (typeof(content[p]) === 'function') {
123      assertTrue(property_mirror.value().isFunction());
124    } else {
125      assertEquals(content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
126    }
127    count++;
128  }
129
130  // 'arguments' and might be exposed in the local and closure scope. Just
131  // ignore this.
132  var scope_size = scope.scopeObject().properties().length;
133  if (!scope.scopeObject().property('arguments').isUndefined()) {
134    scope_size--;
135  }
136  // Skip property with empty name.
137  if (!scope.scopeObject().property('').isUndefined()) {
138    scope_size--;
139  }
140
141  if (count != scope_size) {
142    print('Names found in scope:');
143    var names = scope.scopeObject().propertyNames();
144    for (var i = 0; i < names.length; i++) {
145      print(names[i]);
146    }
147  }
148  assertEquals(count, scope_size);
149
150  // Get the debug command processor.
151  var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
152
153  // Send a scope request for information on a single scope and check the
154  // result.
155  var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
156  request_json += scope.scopeIndex();
157  request_json += '}}';
158  var response_json = dcp.processDebugJSONRequest(request_json);
159  var response = JSON.parse(response_json);
160  assertEquals(scope.scopeType(), response.body.type);
161  assertEquals(number, response.body.index);
162  if (scope.scopeType() == debug.ScopeType.Local ||
163      scope.scopeType() == debug.ScopeType.Script ||
164      scope.scopeType() == debug.ScopeType.Closure) {
165    assertTrue(response.body.object.ref < 0);
166  } else {
167    assertTrue(response.body.object.ref >= 0);
168  }
169  var found = false;
170  for (var i = 0; i < response.refs.length && !found; i++) {
171    found = response.refs[i].handle == response.body.object.ref;
172  }
173  assertTrue(found, "Scope object " + response.body.object.ref + " not found");
174}
175
176
177// Simple empty local scope.
178RunTest("Local 1",
179        ['debugger;'],
180        [],
181        function (exec_state) {
182          CheckScopeChain([debug.ScopeType.Local,
183                           debug.ScopeType.Script,
184                           debug.ScopeType.Global], exec_state);
185          CheckScopeContent({}, 0, exec_state);
186        });
187
188// Local scope with a parameter.
189RunTest("Local 2",
190        ['a', 'debugger;'],
191        [1],
192        function (exec_state) {
193          CheckScopeChain([debug.ScopeType.Local,
194                           debug.ScopeType.Script,
195                           debug.ScopeType.Global], exec_state);
196          CheckScopeContent({a:1}, 0, exec_state);
197        });
198
199// Local scope with a parameter and a local variable.
200RunTest("Local 3",
201        ['a', 'var x = 3; debugger;'],
202        [1],
203        function (exec_state) {
204          CheckScopeChain([debug.ScopeType.Local,
205                           debug.ScopeType.Script,
206                           debug.ScopeType.Global], exec_state);
207          CheckScopeContent({a:1,x:3}, 0, exec_state);
208        });
209
210// Local scope with parameters and local variables.
211RunTest("Local 4",
212        ['a', 'b', 'var x = 3; var y = 4; debugger;'],
213        [1, 2],
214        function (exec_state) {
215          CheckScopeChain([debug.ScopeType.Local,
216                           debug.ScopeType.Script,
217                           debug.ScopeType.Global], exec_state);
218          CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
219        });
220
221// Empty local scope with use of eval.
222RunTest("Local 5",
223        ['eval(""); debugger;'],
224        [],
225        function (exec_state) {
226          CheckScopeChain([debug.ScopeType.Local,
227                           debug.ScopeType.Script,
228                           debug.ScopeType.Global], exec_state);
229          CheckScopeContent({}, 0, exec_state);
230        });
231
232// Local introducing local variable using eval.
233RunTest("Local 6",
234        ['eval("var i = 5"); debugger;'],
235        [],
236        function (exec_state) {
237          CheckScopeChain([debug.ScopeType.Local,
238                           debug.ScopeType.Script,
239                           debug.ScopeType.Global], exec_state);
240          CheckScopeContent({i:5}, 0, exec_state);
241        });
242
243// Local scope with parameters, local variables and local variable introduced
244// using eval.
245RunTest("Local 7",
246        ['a', 'b',
247         "var x = 3; var y = 4;\n"
248         + "eval('var i = 5'); eval ('var j = 6');\n"
249         + "debugger;"],
250        [1, 2],
251        function (exec_state) {
252          CheckScopeChain([debug.ScopeType.Local,
253                           debug.ScopeType.Script,
254                           debug.ScopeType.Global], exec_state);
255          CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 0, exec_state);
256        });
257
258// Nested empty with blocks.
259RunTest("With",
260        ["with ({}) { with ({}) { debugger; } }"],
261        [],
262        function (exec_state) {
263          CheckScopeChain([debug.ScopeType.With,
264                           debug.ScopeType.With,
265                           debug.ScopeType.Local,
266                           debug.ScopeType.Script,
267                           debug.ScopeType.Global], exec_state);
268          CheckScopeContent({}, 0, exec_state);
269          CheckScopeContent({}, 1, exec_state);
270        });
271
272// Simple closure formed by returning an inner function referering the outer
273// functions arguments.
274RunTest("Closure 1",
275        ['a', 'return function() { debugger; return a; }'],
276        [1],
277        function (exec_state) {
278          CheckScopeChain([debug.ScopeType.Local,
279                           debug.ScopeType.Closure,
280                           debug.ScopeType.Script,
281                           debug.ScopeType.Global], exec_state);
282          CheckScopeContent({a:1}, 1, exec_state);
283        },
284       function (result) { result() });
285
286RunTest("The full monty",
287        ['a', 'b',
288         "var x = 3;\n" +
289         "var y = 4;\n" +
290         "eval('var i = 5');\n" +
291         "eval('var j = 6');\n" +
292         "function f(a, b) {\n" +
293         "  var x = 9;\n" +
294         "  var y = 10;\n" +
295         "  eval('var i = 11');\n" +
296         "  eval('var j = 12');\n" +
297         "  with ({j:13}){\n" +
298         "    return function() {\n" +
299         "      var x = 14;\n" +
300         "      with ({a:15}) {\n" +
301         "        with ({b:16}) {\n" +
302         "          debugger;\n" +
303         "          some_global = a;\n" +
304         "          return f;\n" +
305         "        }\n" +
306         "      }\n" +
307         "    };\n" +
308         "  }\n" +
309         "}\n" +
310         "return f(a, b);"],
311        [1, 2],
312        function (exec_state) {
313          CheckScopeChain([debug.ScopeType.With,
314                           debug.ScopeType.With,
315                           debug.ScopeType.Local,
316                           debug.ScopeType.With,
317                           debug.ScopeType.Closure,
318                           debug.ScopeType.Closure,
319                           debug.ScopeType.Script,
320                           debug.ScopeType.Global], exec_state);
321          CheckScopeContent({b:16}, 0, exec_state);
322          CheckScopeContent({a:15}, 1, exec_state);
323          CheckScopeContent({x:14}, 2, exec_state);
324          CheckScopeContent({j:13}, 3, exec_state);
325          CheckScopeContent({a:1,b:2,x:9,y:10,i:11,j:12}, 4, exec_state);
326          CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6,f:function(){}}, 5, exec_state);
327        },
328        function (result) { result() });
329
330RunTest("Catch block 1",
331        ["try { throw 'Exception'; } catch (e) { debugger; }"],
332        [],
333        function (exec_state) {
334          CheckScopeChain([debug.ScopeType.Catch,
335                           debug.ScopeType.Local,
336                           debug.ScopeType.Script,
337                           debug.ScopeType.Global], exec_state);
338          CheckScopeContent({e:'Exception'}, 0, exec_state);
339        });
340