OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-promises --expose-debug-as debug | 5 // Flags: --harmony-promises --expose-debug-as debug |
6 | 6 |
7 // Test debug events when an exception is thrown inside a Promise, which is | 7 // Test debug events when an exception is thrown inside a Promise, which is |
8 // caught by a custom promise, which throws a new exception in its reject | 8 // caught by a custom promise, which throws a new exception in its reject |
9 // handler. We expect an Exception debug event with a promise to be triggered. | 9 // handler. We expect a PendingExceptionInPromise event to be triggered. |
10 | 10 |
11 Debug = debug.Debug; | 11 Debug = debug.Debug; |
12 | 12 |
13 var log = []; | 13 var log = []; |
14 var step = 0; | 14 var step = 0; |
15 | 15 |
16 var p = new Promise(function(resolve, reject) { | 16 var p = new Promise(function(resolve, reject) { |
17 log.push("resolve"); | 17 log.push("resolve"); |
18 resolve(); | 18 resolve(); |
19 }); | 19 }); |
20 | 20 |
21 function MyPromise(resolver) { | 21 function MyPromise(resolver) { |
22 var reject = function() { | 22 var reject = function() { |
23 log.push("throw reject"); | 23 log.push("throw reject"); |
24 throw new Error("reject"); // event | 24 throw new Error("reject"); |
25 }; | 25 }; |
26 var resolve = function() { }; | 26 var resolve = function() { }; |
27 log.push("construct"); | 27 log.push("construct"); |
28 resolver(resolve, reject); | 28 resolver(resolve, reject); |
29 }; | 29 }; |
30 | 30 |
31 MyPromise.prototype = p; | 31 MyPromise.prototype = p; |
32 p.constructor = MyPromise; | 32 p.constructor = MyPromise; |
33 | 33 |
34 var q = p.chain( | 34 var q = p.chain( |
35 function() { | 35 function() { |
36 log.push("throw caught"); | 36 log.push("throw caught"); |
37 throw new Error("caught"); | 37 throw new Error("caught"); |
38 }); | 38 }); |
39 | 39 |
40 function listener(event, exec_state, event_data, data) { | 40 function listener(event, exec_state, event_data, data) { |
41 try { | 41 try { |
42 if (event == Debug.DebugEvent.Exception) { | 42 if (event == Debug.DebugEvent.PendingExceptionInPromise) { |
43 assertEquals(["resolve", "construct", "end main", | 43 assertEquals(["resolve", "construct", "end main", |
44 "throw caught", "throw reject"], log); | 44 "throw caught", "throw reject"], log); |
45 assertEquals("reject", event_data.exception().message); | 45 assertEquals("caught", event_data.exception().message); |
46 assertEquals(q, event_data.promise()); | 46 } else if (event == Debug.DebugEvent.Exception) { |
47 assertTrue(exec_state.frame(0).sourceLineText().indexOf('// event') > 0); | 47 assertUnreachable(); |
48 } | 48 } |
49 } catch (e) { | 49 } catch (e) { |
50 // Signal a failure with exit code 1. This is necessary since the | 50 // Signal a failure with exit code 1. This is necessary since the |
51 // debugger swallows exceptions and we expect the chained function | 51 // debugger swallows exceptions and we expect the chained function |
52 // and this listener to be executed after the main script is finished. | 52 // and this listener to be executed after the main script is finished. |
53 print("Unexpected exception: " + e + "\n" + e.stack); | 53 print("Unexpected exception: " + e + "\n" + e.stack); |
54 quit(1); | 54 quit(1); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 Debug.setBreakOnUncaughtException(); | 58 Debug.setBreakOnUncaughtException(); |
59 Debug.setListener(listener); | 59 Debug.setListener(listener); |
60 | 60 |
61 log.push("end main"); | 61 log.push("end main"); |
OLD | NEW |