summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/es6/generators-debug-scopes.js
blob: 126572d16fa0e6c9c5277e7814687c901767c758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --expose-debug-as debug

var Debug = debug.Debug;

function RunTest(name, formals_and_body, args, handler, continuation) {
  var handler_called = false;
  var exception = null;

  function listener(event, exec_state, event_data, data) {
    try {
      if (event == Debug.DebugEvent.Break) {
        handler_called = true;
        handler(exec_state);
      }
    } catch (e) {
      exception = e;
    }
  }

  function run(thunk) {
    handler_called = false;
    exception = null;

    var res = thunk();
    if (continuation)
      continuation(res);

    assertTrue(handler_called, "listener not called for " + name);
    assertNull(exception, name + " / " + exception);
  }

  var fun = Function.apply(null, formals_and_body);
  var gen = (function*(){}).constructor.apply(null, formals_and_body);

  Debug.setListener(listener);

  run(function () { return fun.apply(null, args) });
  run(function () { return gen.apply(null, args).next().value });

  // TODO(wingo): Uncomment after bug 2838 is fixed.
  // Debug.setListener(null);
}

// Check that two scope are the same.
function assertScopeMirrorEquals(scope1, scope2) {
  assertEquals(scope1.scopeType(), scope2.scopeType());
  assertEquals(scope1.frameIndex(), scope2.frameIndex());
  assertEquals(scope1.scopeIndex(), scope2.scopeIndex());
  assertPropertiesEqual(scope1.scopeObject().value(), scope2.scopeObject().value());
}

function CheckFastAllScopes(scopes, exec_state) {
  var fast_all_scopes = exec_state.frame().allScopes(true);
  var length = fast_all_scopes.length;
  assertTrue(scopes.length >= length);
  for (var i = 0; i < scopes.length && i < length; i++) {
    var scope = fast_all_scopes[length - i - 1];
    assertTrue(scope.isScope());
    assertEquals(scopes[scopes.length - i - 1], scope.scopeType());
  }
}

// Check that the scope chain contains the expected types of scopes.
function CheckScopeChain(scopes, exec_state) {
  var all_scopes = exec_state.frame().allScopes();
  assertEquals(scopes.length, exec_state.frame().scopeCount());
  assertEquals(scopes.length, all_scopes.length, "FrameMirror.allScopes length");
  for (var i = 0; i < scopes.length; i++) {
    var scope = exec_state.frame().scope(i);
    assertTrue(scope.isScope());
    assertEquals(scopes[i], scope.scopeType());
    assertScopeMirrorEquals(all_scopes[i], scope);

    // Check the global object when hitting the global scope.
    if (scopes[i] == debug.ScopeType.Global) {
      // Objects don't have same class (one is "global", other is "Object",
      // so just check the properties directly.
      assertPropertiesEqual(this, scope.scopeObject().value());
    }
  }
  CheckFastAllScopes(scopes, exec_state);

  // Get the debug command processor.
  var dcp = exec_state.debugCommandProcessor("unspecified_running_state");

  // Send a scopes request and check the result.
  var json;
  var request_json = '{"seq":0,"type":"request","command":"scopes"}';
  var response_json = dcp.processDebugJSONRequest(request_json);
  var response = JSON.parse(response_json);
  assertEquals(scopes.length, response.body.scopes.length);
  for (var i = 0; i < scopes.length; i++) {
    assertEquals(i, response.body.scopes[i].index);
    assertEquals(scopes[i], response.body.scopes[i].type);
    if (scopes[i] == debug.ScopeType.Local ||
        scopes[i] == debug.ScopeType.Script ||
        scopes[i] == debug.ScopeType.Closure) {
      assertTrue(response.body.scopes[i].object.ref < 0);
    } else {
      assertTrue(response.body.scopes[i].object.ref >= 0);
    }
    var found = false;
    for (var j = 0; j < response.refs.length && !found; j++) {
      found = response.refs[j].handle == response.body.scopes[i].object.ref;
    }
    assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
  }
}

// Check that the content of the scope is as expected. For functions just check
// that there is a function.
function CheckScopeContent(content, number, exec_state) {
  var scope = exec_state.frame().scope(number);
  var count = 0;
  for (var p in content) {
    var property_mirror = scope.scopeObject().property(p);
    assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
    if (typeof(content[p]) === 'function') {
      assertTrue(property_mirror.value().isFunction());
    } else {
      assertEquals(content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
    }
    count++;
  }

  // 'arguments' and might be exposed in the local and closure scope. Just
  // ignore this.
  var scope_size = scope.scopeObject().properties().length;
  if (!scope.scopeObject().property('arguments').isUndefined()) {
    scope_size--;
  }
  // Skip property with empty name.
  if (!scope.scopeObject().property('').isUndefined()) {
    scope_size--;
  }

  if (count != scope_size) {
    print('Names found in scope:');
    var names = scope.scopeObject().propertyNames();
    for (var i = 0; i < names.length; i++) {
      print(names[i]);
    }
  }
  assertEquals(count, scope_size);

  // Get the debug command processor.
  var dcp = exec_state.debugCommandProcessor("unspecified_running_state");

  // Send a scope request for information on a single scope and check the
  // result.
  var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
  request_json += scope.scopeIndex();
  request_json += '}}';
  var response_json = dcp.processDebugJSONRequest(request_json);
  var response = JSON.parse(response_json);
  assertEquals(scope.scopeType(), response.body.type);
  assertEquals(number, response.body.index);
  if (scope.scopeType() == debug.ScopeType.Local ||
      scope.scopeType() == debug.ScopeType.Script ||
      scope.scopeType() == debug.ScopeType.Closure) {
    assertTrue(response.body.object.ref < 0);
  } else {
    assertTrue(response.body.object.ref >= 0);
  }
  var found = false;
  for (var i = 0; i < response.refs.length && !found; i++) {
    found = response.refs[i].handle == response.body.object.ref;
  }
  assertTrue(found, "Scope object " + response.body.object.ref + " not found");
}


// Simple empty local scope.
RunTest("Local 1",
        ['debugger;'],
        [],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({}, 0, exec_state);
        });

// Local scope with a parameter.
RunTest("Local 2",
        ['a', 'debugger;'],
        [1],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({a:1}, 0, exec_state);
        });

// Local scope with a parameter and a local variable.
RunTest("Local 3",
        ['a', 'var x = 3; debugger;'],
        [1],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({a:1,x:3}, 0, exec_state);
        });

// Local scope with parameters and local variables.
RunTest("Local 4",
        ['a', 'b', 'var x = 3; var y = 4; debugger;'],
        [1, 2],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
        });

// Empty local scope with use of eval.
RunTest("Local 5",
        ['eval(""); debugger;'],
        [],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({}, 0, exec_state);
        });

// Local introducing local variable using eval.
RunTest("Local 6",
        ['eval("var i = 5"); debugger;'],
        [],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({i:5}, 0, exec_state);
        });

// Local scope with parameters, local variables and local variable introduced
// using eval.
RunTest("Local 7",
        ['a', 'b',
         "var x = 3; var y = 4;\n"
         + "eval('var i = 5'); eval ('var j = 6');\n"
         + "debugger;"],
        [1, 2],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 0, exec_state);
        });

// Nested empty with blocks.
RunTest("With",
        ["with ({}) { with ({}) { debugger; } }"],
        [],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.With,
                           debug.ScopeType.With,
                           debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({}, 0, exec_state);
          CheckScopeContent({}, 1, exec_state);
        });

// Simple closure formed by returning an inner function referering the outer
// functions arguments.
RunTest("Closure 1",
        ['a', 'return function() { debugger; return a; }'],
        [1],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Local,
                           debug.ScopeType.Closure,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({a:1}, 1, exec_state);
        },
       function (result) { result() });

RunTest("The full monty",
        ['a', 'b',
         "var x = 3;\n" +
         "var y = 4;\n" +
         "eval('var i = 5');\n" +
         "eval('var j = 6');\n" +
         "function f(a, b) {\n" +
         "  var x = 9;\n" +
         "  var y = 10;\n" +
         "  eval('var i = 11');\n" +
         "  eval('var j = 12');\n" +
         "  with ({j:13}){\n" +
         "    return function() {\n" +
         "      var x = 14;\n" +
         "      with ({a:15}) {\n" +
         "        with ({b:16}) {\n" +
         "          debugger;\n" +
         "          some_global = a;\n" +
         "          return f;\n" +
         "        }\n" +
         "      }\n" +
         "    };\n" +
         "  }\n" +
         "}\n" +
         "return f(a, b);"],
        [1, 2],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.With,
                           debug.ScopeType.With,
                           debug.ScopeType.Local,
                           debug.ScopeType.With,
                           debug.ScopeType.Closure,
                           debug.ScopeType.Closure,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({b:16}, 0, exec_state);
          CheckScopeContent({a:15}, 1, exec_state);
          CheckScopeContent({x:14}, 2, exec_state);
          CheckScopeContent({j:13}, 3, exec_state);
          CheckScopeContent({a:1,b:2,x:9,y:10,i:11,j:12}, 4, exec_state);
          CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6,f:function(){}}, 5, exec_state);
        },
        function (result) { result() });

RunTest("Catch block 1",
        ["try { throw 'Exception'; } catch (e) { debugger; }"],
        [],
        function (exec_state) {
          CheckScopeChain([debug.ScopeType.Catch,
                           debug.ScopeType.Local,
                           debug.ScopeType.Script,
                           debug.ScopeType.Global], exec_state);
          CheckScopeContent({e:'Exception'}, 0, exec_state);
        });