blob: 3d72549d2ac7b9d64ce500187a72dee21121c68e [file] [log] [blame]
Ben Murdoch61f157c2016-09-16 13:49:30 +01001// Copyright 2016 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: --harmony-async-await --expose-debug-as debug
6
7var Debug = debug.Debug;
8
9var AsyncFunction = (async function() {}).constructor;
10
11async function thrower() { throw 'Exception'; }
12
13async function test(name, func, args, handler, continuation) {
14 var handler_called = false;
15 var exception = null;
16
17 function listener(event, exec_state, event_data, data) {
18 try {
19 if (event == Debug.DebugEvent.Break) {
20 handler_called = true;
21 handler(exec_state);
22 }
23 } catch (e) {
24 exception = e;
25 }
26 }
27
28 Debug.setListener(listener);
29
30 var result;
31 if (typeof func === "object")
32 result = await func.method.apply(func, args);
33 else
34 result = await func.apply(null, args);
35
36 if (typeof continuation === "function") {
37 await continuation(result);
38 }
39
40 assertTrue(handler_called, `Expected ${name} handler to be called`);
41 if (exception) {
42 exception.message = `${name} / ${exception.message}`;
43 print(exception.stack);
44 quit(1);
45 }
46
47 Debug.setListener(null);
48}
49
50async function runTests() {
51
52// Simple
53await test(
54 "(AsyncFunctionExpression) Local 1",
55 async function() { debugger; }, [],
56 exec_state => {
57 CheckScopeChain([debug.ScopeType.Local,
58 debug.ScopeType.Closure,
59 debug.ScopeType.Script,
60 debug.ScopeType.Global], exec_state);
61 CheckScopeContent({}, 0, exec_state);
62 });
63
64await test(
65 "(AsyncFunctionExpression) Local 1 --- resume normal",
66 async function() { let z = await 2; debugger; }, [],
67 exec_state => {
68 CheckScopeChain([debug.ScopeType.Local,
69 debug.ScopeType.Closure,
70 debug.ScopeType.Script,
71 debug.ScopeType.Global], exec_state);
72 CheckScopeContent({z: 2}, 0, exec_state);
73 });
74
75await test(
76 "(AsyncFunctionExpression) Local 1 --- resume throw",
77 async function() { let q = await 1;
78 try { let z = await thrower(); }
79 catch (e) { debugger; } }, [],
80 exec_state => {
81 CheckScopeChain([debug.ScopeType.Catch,
82 debug.ScopeType.Local,
83 debug.ScopeType.Closure,
84 debug.ScopeType.Script,
85 debug.ScopeType.Global], exec_state);
86 CheckScopeContent({e: 'Exception'}, 0, exec_state);
87 CheckScopeContent({q: 1}, 1, exec_state);
88
89 });
90
91// Simple With Parameter
92await test(
93 "(AsyncFunctionExpression) Local 2",
94 async function(a) { debugger; }, [1],
95 exec_state => {
96 CheckScopeChain([debug.ScopeType.Local,
97 debug.ScopeType.Closure,
98 debug.ScopeType.Script,
99 debug.ScopeType.Global], exec_state);
100 CheckScopeContent({ a: 1 }, 0, exec_state);
101 });
102
103await test(
104 "(AsyncFunctionExpression) Local 2 --- resume normal",
105 async function(a) { let z = await 2; debugger; }, [1],
106 exec_state => {
107 CheckScopeChain([debug.ScopeType.Local,
108 debug.ScopeType.Closure,
109 debug.ScopeType.Script,
110 debug.ScopeType.Global], exec_state);
111 CheckScopeContent({ a: 1, z: 2 }, 0, exec_state);
112 });
113
114await test(
115 "(AsyncFunctionExpression) Local 2 --- resume throw",
116 async function(a) { let z = await 2;
117 try { await thrower(); } catch (e) { debugger; } }, [1],
118 exec_state => {
119 CheckScopeChain([debug.ScopeType.Catch,
120 debug.ScopeType.Local,
121 debug.ScopeType.Closure,
122 debug.ScopeType.Script,
123 debug.ScopeType.Global], exec_state);
124 CheckScopeContent({ e: 'Exception' }, 0, exec_state);
125 CheckScopeContent({ a: 1, z: 2 }, 1, exec_state);
126 });
127
128// Simple With Parameter and Variable
129await test(
130 "(AsyncFunctionExpression) Local 3",
131 async function(a) { var b = 2; debugger; }, [1],
132 exec_state => {
133 CheckScopeChain([debug.ScopeType.Local,
134 debug.ScopeType.Closure,
135 debug.ScopeType.Script,
136 debug.ScopeType.Global], exec_state);
137 CheckScopeContent({ a: 1, b: 2 }, 0, exec_state);
138 });
139
140await test(
141 "(AsyncFunctionExpression) Local 3 --- resume normal",
142 async function(a) { let y = await 3; var b = 2; let z = await 4;
143 debugger; }, [1],
144 exec_state => {
145 CheckScopeChain([debug.ScopeType.Local,
146 debug.ScopeType.Closure,
147 debug.ScopeType.Script,
148 debug.ScopeType.Global], exec_state);
149 CheckScopeContent({ a: 1, b: 2, y: 3, z: 4 }, 0, exec_state);
150 });
151
152await test(
153 "(AsyncFunctionExpression) Local 3 --- resume throw",
154 async function(a) { let y = await 3;
155 try { var b = 2; let z = await thrower(); }
156 catch (e) { debugger; } }, [1],
157 exec_state => {
158 CheckScopeChain([debug.ScopeType.Catch,
159 debug.ScopeType.Local,
160 debug.ScopeType.Closure,
161 debug.ScopeType.Script,
162 debug.ScopeType.Global], exec_state);
163 CheckScopeContent({ e: 'Exception' }, 0, exec_state);
164 CheckScopeContent({ a: 1, b: 2, y: 3 }, 1, exec_state);
165 });
166
167// Local scope with parameters and local variables.
168await test(
169 "(AsyncFunctionExpression) Local 4",
170 async function(a, b) { var x = 3; var y = 4; debugger; }, [1, 2],
171 exec_state => {
172 CheckScopeChain([debug.ScopeType.Local,
173 debug.ScopeType.Closure,
174 debug.ScopeType.Script,
175 debug.ScopeType.Global], exec_state);
176 CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
177 });
178
179await test(
180 "(AsyncFunctionExpression) Local 4 --- resume normal",
181 async function(a, b) { let q = await 5; var x = 3; var y = 4;
182 let r = await 6; debugger; }, [1, 2],
183 exec_state => {
184 CheckScopeChain([debug.ScopeType.Local,
185 debug.ScopeType.Closure,
186 debug.ScopeType.Script,
187 debug.ScopeType.Global], exec_state);
188 CheckScopeContent({a:1,b:2,x:3,y:4, q: 5, r: 6}, 0, exec_state);
189 });
190
191await test(
192 "(AsyncFunctionExpression) Local 4 --- resume throw",
193 async function(a, b) { let q = await 5; var x = 3; var y = 4;
194 try { let r = await thrower(); }
195 catch (e) { debugger; } }, [1, 2],
196 exec_state => {
197 CheckScopeChain([debug.ScopeType.Catch,
198 debug.ScopeType.Local,
199 debug.ScopeType.Closure,
200 debug.ScopeType.Script,
201 debug.ScopeType.Global], exec_state);
202 CheckScopeContent({e: 'Exception'}, 0, exec_state);
203 CheckScopeContent({a:1,b:2,x:3,y:4, q: 5}, 1, exec_state);
204 });
205
206// Empty local scope with use of eval.
207await test(
208 "(AsyncFunctionExpression) Local 5",
209 async function() { eval(""); debugger; }, [],
210 exec_state => {
211 CheckScopeChain([debug.ScopeType.Local,
212 debug.ScopeType.Closure,
213 debug.ScopeType.Script,
214 debug.ScopeType.Global], exec_state);
215 CheckScopeContent({}, 0, exec_state);
216 });
217
218await test(
219 "(AsyncFunctionExpression) Local 5 --- resume normal",
220 async function() { let x = await 1; eval(""); let y = await 2;
221 debugger; }, [],
222 exec_state => {
223 CheckScopeChain([debug.ScopeType.Local,
224 debug.ScopeType.Closure,
225 debug.ScopeType.Script,
226 debug.ScopeType.Global], exec_state);
227 CheckScopeContent({ x: 1, y: 2 }, 0, exec_state);
228 });
229
230await test(
231 "(AsyncFunctionExpression) Local 5 --- resume throw",
232 async function() { let x = await 1; eval("");
233 try { let y = await thrower(); }
234 catch (e) { debugger; } }, [],
235 exec_state => {
236 CheckScopeChain([debug.ScopeType.Catch,
237 debug.ScopeType.Local,
238 debug.ScopeType.Closure,
239 debug.ScopeType.Script,
240 debug.ScopeType.Global], exec_state);
241 CheckScopeContent({ e: 'Exception' }, 0, exec_state);
242 CheckScopeContent({ x: 1 }, 1, exec_state);
243 });
244
245// Local introducing local variable using eval.
246await test(
247 "(AsyncFunctionExpression) Local 6",
248 async function() { eval("var i = 5"); debugger; }, [],
249 exec_state => {
250 CheckScopeChain([debug.ScopeType.Local,
251 debug.ScopeType.Closure,
252 debug.ScopeType.Script,
253 debug.ScopeType.Global], exec_state);
254 CheckScopeContent({i:5}, 0, exec_state);
255 });
256
257await test(
258 "(AsyncFunctionExpression) Local 6 --- resume normal",
259 async function() { let x = await 1; eval("var i = 5"); let y = await 2;
260 debugger; }, [],
261 exec_state => {
262 CheckScopeChain([debug.ScopeType.Local,
263 debug.ScopeType.Closure,
264 debug.ScopeType.Script,
265 debug.ScopeType.Global], exec_state);
266 CheckScopeContent({i:5, x: 1, y: 2}, 0, exec_state);
267 });
268
269await test(
270 "(AsyncFunctionExpression) Local 6 --- resume throw",
271 async function() { let x = await 1; eval("var i = 5");
272 try { let y = await thrower(); }
273 catch (e) { debugger; } }, [],
274 exec_state => {
275 CheckScopeChain([debug.ScopeType.Catch,
276 debug.ScopeType.Local,
277 debug.ScopeType.Closure,
278 debug.ScopeType.Script,
279 debug.ScopeType.Global], exec_state);
280 CheckScopeContent({e: 'Exception' }, 0, exec_state);
281 CheckScopeContent({i:5, x: 1}, 1, exec_state);
282 });
283
284// Local scope with parameters, local variables and local variable introduced
285// using eval.
286await test(
287 "(AsyncFunctionExpression) Local 7",
288 async function(a, b) { var x = 3; var y = 4;
289 eval("var i = 5;"); eval("var j = 6");
290 debugger; }, [1, 2],
291 exec_state => {
292 CheckScopeChain([debug.ScopeType.Local,
293 debug.ScopeType.Closure,
294 debug.ScopeType.Script,
295 debug.ScopeType.Global], exec_state);
296 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6}, 0, exec_state);
297 });
298
299await test(
300 "(AsyncFunctionExpression) Local 7 --- resume normal",
301 async function(a, b) { let z = await 7; var x = 3; var y = 4;
302 eval("var i = 5;"); eval("var j = 6");
303 let q = await 8;
304 debugger; }, [1, 2],
305 exec_state => {
306 CheckScopeChain([debug.ScopeType.Local,
307 debug.ScopeType.Closure,
308 debug.ScopeType.Script,
309 debug.ScopeType.Global], exec_state);
310 CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6, z:7, q:8}, 0, exec_state);
311 });
312
313await test(
314 "(AsyncFunctionExpression) Local 7 --- resume throw",
315 async function(a, b) { let z = await 7; var x = 3; var y = 4;
316 eval("var i = 5;"); eval("var j = 6");
317 try { let q = await thrower(); }
318 catch (e) { debugger; } }, [1, 2],
319 exec_state => {
320 CheckScopeChain([debug.ScopeType.Catch,
321 debug.ScopeType.Local,
322 debug.ScopeType.Closure,
323 debug.ScopeType.Script,
324 debug.ScopeType.Global], exec_state);
325 CheckScopeContent({e: 'Exception'}, 0, exec_state);
326 //CheckScopeContent({a:1,b:2,x:3,y:4,i:5,j:6, z:7}, 1, exec_state);
327 });
328
329// Nested empty with blocks.
330await test(
331 "(AsyncFunctionExpression) With",
332 async function() { with ({}) { with ({}) { debugger; } } }, [],
333 exec_state => {
334 CheckScopeChain([debug.ScopeType.With,
335 debug.ScopeType.With,
336 debug.ScopeType.Local,
337 debug.ScopeType.Closure,
338 debug.ScopeType.Script,
339 debug.ScopeType.Global], exec_state);
340 CheckScopeContent({}, 0, exec_state);
341 CheckScopeContent({}, 1, exec_state);
342 });
343
344await test(
345 "(AsyncFunctionExpression) With --- resume normal",
346 async function() { let x = await 1; with ({}) { with ({}) {
347 let y = await 2; debugger; } } }, [],
348 exec_state => {
349 CheckScopeChain([debug.ScopeType.Block,
350 debug.ScopeType.With,
351 debug.ScopeType.With,
352 debug.ScopeType.Local,
353 debug.ScopeType.Closure,
354 debug.ScopeType.Script,
355 debug.ScopeType.Global], exec_state);
356 CheckScopeContent({y:2}, 0, exec_state);
357 CheckScopeContent({}, 1, exec_state);
358 CheckScopeContent({}, 2, exec_state);
359 CheckScopeContent({x:1}, 3, exec_state);
360 });
361
362await test(
363 "(AsyncFunctionExpression) With --- resume throw",
364 async function() { let x = await 1; with ({}) { with ({}) {
365 try { let y = await thrower(); }
366 catch (e) { debugger; } } } }, [],
367 exec_state => {
368 CheckScopeChain([debug.ScopeType.Catch,
369 debug.ScopeType.With,
370 debug.ScopeType.With,
371 debug.ScopeType.Local,
372 debug.ScopeType.Closure,
373 debug.ScopeType.Script,
374 debug.ScopeType.Global], exec_state);
375 CheckScopeContent({ e: 'Exception'}, 0, exec_state);
376 CheckScopeContent({}, 1, exec_state);
377 CheckScopeContent({}, 2, exec_state);
378 CheckScopeContent({x:1}, 3, exec_state);
379 });
380
381// Simple closure formed by returning an inner function referering the outer
382// functions arguments.
383await test(
384 "(AsyncFunctionExpression) Closure 1",
385 async function(a) { return function() { debugger; return a; } }, [1],
386 exec_state => {
387 CheckScopeChain([debug.ScopeType.Local,
388 debug.ScopeType.Closure,
389 debug.ScopeType.Closure,
390 debug.ScopeType.Script,
391 debug.ScopeType.Global], exec_state);
392 CheckScopeContent({a:1}, 1, exec_state);
393 },
394 result => result());
395
396await test(
397 "(AsyncFunctionExpression) Closure 1 --- resume normal",
398 async function(a) { let x = await 2;
399 return function() { debugger; return a; } }, [1],
400 exec_state => {
401 CheckScopeChain([debug.ScopeType.Local,
402 debug.ScopeType.Closure,
403 debug.ScopeType.Closure,
404 debug.ScopeType.Script,
405 debug.ScopeType.Global], exec_state);
406 CheckScopeContent({a:1, x: 2}, 1, exec_state);
407 },
408 result => result());
409
410await test(
411 "(AsyncFunctionExpression) Closure 1 --- resume throw",
412 async function(a) { let x = await 2;
413 return async function() {
414 try { await thrower(); }
415 catch (e) { debugger; } return a; }; }, [1],
416 exec_state => {
417 CheckScopeChain([debug.ScopeType.Catch,
418 debug.ScopeType.Local,
419 debug.ScopeType.Closure,
420 debug.ScopeType.Closure,
421 debug.ScopeType.Script,
422 debug.ScopeType.Global], exec_state);
423 CheckScopeContent({e: 'Exception'}, 0, exec_state);
424 CheckScopeContent({a:1, x: 2}, 2, exec_state);
425 },
426 result => result());
427
428await test(
429 "(AsyncFunctionExpression) Catch block 1",
430 async function() { try { throw 'Exception'; } catch (e) { debugger; } }, [],
431 exec_state => {
432 CheckScopeChain([debug.ScopeType.Catch,
433 debug.ScopeType.Local,
434 debug.ScopeType.Closure,
435 debug.ScopeType.Script,
436 debug.ScopeType.Global], exec_state);
437 CheckScopeContent({e:'Exception'}, 0, exec_state);
438 });
439
440await test(
441 "(AsyncFunctionExpression) Catch block 1 --- resume normal",
442 async function() {
443 let x = await 1;
444 try { throw 'Exception'; } catch (e) { let y = await 2; debugger; } }, [],
445 exec_state => {
446 CheckScopeChain([debug.ScopeType.Block,
447 debug.ScopeType.Catch,
448 debug.ScopeType.Local,
449 debug.ScopeType.Closure,
450 debug.ScopeType.Script,
451 debug.ScopeType.Global], exec_state);
452 CheckScopeContent({y: 2}, 0, exec_state);
453 CheckScopeContent({e:'Exception'}, 1, exec_state);
454 CheckScopeContent({x: 1}, 2, exec_state);
455 });
456
457await test(
458 "(AsyncFunctionExpression) Catch block 1 --- resume throw",
459 async function() {
460 let x = await 1;
461 try { throw 'Exception!'; } catch (e) {
462 try { let y = await thrower(); } catch (e) { debugger; } } }, [],
463 exec_state => {
464 CheckScopeChain([debug.ScopeType.Catch,
465 debug.ScopeType.Catch,
466 debug.ScopeType.Local,
467 debug.ScopeType.Closure,
468 debug.ScopeType.Script,
469 debug.ScopeType.Global], exec_state);
470 CheckScopeContent({e:'Exception'}, 0, exec_state);
471 CheckScopeContent({e:'Exception!'}, 1, exec_state);
472 CheckScopeContent({x: 1}, 2, exec_state);
473 });
474}
475
476runTests().catch(error => {
477 print(error.stack);
478 quit(1);
479})
480
481// Check that two scope are the same.
482function assertScopeMirrorEquals(scope1, scope2) {
483 assertEquals(scope1.scopeType(), scope2.scopeType());
484 assertEquals(scope1.frameIndex(), scope2.frameIndex());
485 assertEquals(scope1.scopeIndex(), scope2.scopeIndex());
486 assertPropertiesEqual(
487 scope1.scopeObject().value(), scope2.scopeObject().value());
488}
489
490function CheckFastAllScopes(scopes, exec_state) {
491 var fast_all_scopes = exec_state.frame().allScopes(true);
492 var length = fast_all_scopes.length;
493 assertTrue(scopes.length >= length);
494 for (var i = 0; i < scopes.length && i < length; i++) {
495 var scope = fast_all_scopes[length - i - 1];
496 assertTrue(scope.isScope());
497 assertEquals(scopes[scopes.length - i - 1], scope.scopeType());
498 }
499}
500
501// Check that the scope chain contains the expected types of scopes.
502function CheckScopeChain(scopes, exec_state) {
503 var all_scopes = exec_state.frame().allScopes();
504 assertEquals(
505 scopes.length, all_scopes.length, "FrameMirror.allScopes length");
506 for (var i = 0; i < scopes.length; i++) {
507 var scope = exec_state.frame().scope(i);
508 assertTrue(scope.isScope());
509 assertEquals(scopes[i], scope.scopeType());
510 assertScopeMirrorEquals(all_scopes[i], scope);
511
512 // Check the global object when hitting the global scope.
513 if (scopes[i] == debug.ScopeType.Global) {
514 // Objects don't have same class (one is "global", other is "Object",
515 // so just check the properties directly.
516 assertPropertiesEqual(this, scope.scopeObject().value());
517 }
518 }
519 CheckFastAllScopes(scopes, exec_state);
520
521 // Get the debug command processor.
522 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
523
524 // Send a scopes request and check the result.
525 var json;
526 var request_json = '{"seq":0,"type":"request","command":"scopes"}';
527 var response_json = dcp.processDebugJSONRequest(request_json);
528 var response = JSON.parse(response_json);
529 assertEquals(scopes.length, response.body.scopes.length);
530 for (var i = 0; i < scopes.length; i++) {
531 var scopeRef = response.body.scopes[i].object.ref;
532 assertEquals(i, response.body.scopes[i].index);
533 assertEquals(scopes[i], response.body.scopes[i].type);
534 if (scopes[i] == debug.ScopeType.Local ||
535 scopes[i] == debug.ScopeType.Script ||
536 scopes[i] == debug.ScopeType.Closure) {
537 assertTrue(response.body.scopes[i].object.ref < 0);
538 } else {
539 assertTrue(response.body.scopes[i].object.ref >= 0);
540 }
541 var found = false;
542 for (var j = 0; j < response.refs.length && !found; j++) {
543 found = response.refs[j].handle == response.body.scopes[i].object.ref;
544 }
545 assertTrue(found, `Scope object ${scopeRef} not found`);
546 }
547}
548
549// Check that the content of the scope is as expected. For functions just check
550// that there is a function.
551function CheckScopeContent(content, number, exec_state) {
552 var scope = exec_state.frame().scope(number);
553 var count = 0;
554 for (var p in content) {
555 var property_mirror = scope.scopeObject().property(p);
556 assertFalse(property_mirror.isUndefined(),
557 `property ${p} not found in scope`);
558 if (typeof(content[p]) === 'function') {
559 assertTrue(property_mirror.value().isFunction());
560 } else {
561 assertEquals(content[p], property_mirror.value().value(),
562 `property ${p} has unexpected value`);
563 }
564 count++;
565 }
566
567 // 'arguments' and might be exposed in the local and closure scope. Just
568 // ignore this.
569 var scope_size = scope.scopeObject().properties().length;
570 if (!scope.scopeObject().property('arguments').isUndefined()) {
571 scope_size--;
572 }
573 // Skip property with empty name.
574 if (!scope.scopeObject().property('').isUndefined()) {
575 scope_size--;
576 }
577
578 if (count != scope_size) {
579 print('Names found in scope:');
580 var names = scope.scopeObject().propertyNames();
581 for (var i = 0; i < names.length; i++) {
582 print(names[i]);
583 }
584 }
585 assertEquals(count, scope_size);
586
587 // Get the debug command processor.
588 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
589
590 // Send a scope request for information on a single scope and check the
591 // result.
592 var request_json = `{
593 "seq": 0,
594 "type": "request",
595 "command": "scope",
596 "arguments": {
597 "number": `;
598 request_json += scope.scopeIndex();
599 request_json += '}}';
600 var response_json = dcp.processDebugJSONRequest(request_json);
601 var response = JSON.parse(response_json);
602 assertEquals(scope.scopeType(), response.body.type);
603 assertEquals(number, response.body.index);
604 if (scope.scopeType() == debug.ScopeType.Local ||
605 scope.scopeType() == debug.ScopeType.Script ||
606 scope.scopeType() == debug.ScopeType.Closure) {
607 assertTrue(response.body.object.ref < 0);
608 } else {
609 assertTrue(response.body.object.ref >= 0);
610 }
611 var found = false;
612 for (var i = 0; i < response.refs.length && !found; i++) {
613 found = response.refs[i].handle == response.body.object.ref;
614 }
615 assertTrue(found, "Scope object " + response.body.object.ref + " not found");
616}