blob: 34d3afd77ecb93cd2e191c01b29a20518bb3adc8 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +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: --expose-debug-as debug --allow-natives-syntax
6
7// Test debug event catch prediction for thrown exceptions. We distinguish
8// between "caught" and "uncaught" based on the following assumptions:
9// 1) try-catch : Will always catch the exception.
10// 2) try-finally : Will always re-throw the exception.
11
12Debug = debug.Debug;
13
14var log = [];
15
16function listener(event, exec_state, event_data, data) {
17 try {
18 if (event == Debug.DebugEvent.Exception) {
19 log.push([event_data.exception(), event_data.uncaught()]);
20 }
21 } catch (e) {
22 %AbortJS(e + "\n" + e.stack);
23 }
24}
25
26Debug.setBreakOnException();
27Debug.setListener(listener);
28
29(function TryCatch() {
30 log = []; // Clear log.
31 function f(a) {
32 try {
33 throw "boom" + a;
34 } catch(e) {
35 return e;
36 }
37 }
38 assertEquals("boom1", f(1));
39 assertEquals("boom2", f(2));
40 %OptimizeFunctionOnNextCall(f);
41 assertEquals("boom3", f(3));
42 print("Collect log:", log);
43 assertEquals([["boom1",false], ["boom2",false], ["boom3",false]], log);
44})();
45
46(function TryFinally() {
47 log = []; // Clear log.
48 function f(a) {
49 try {
50 throw "baem" + a;
51 } finally {
52 return a + 10;
53 }
54 }
55 assertEquals(11, f(1));
56 assertEquals(12, f(2));
57 %OptimizeFunctionOnNextCall(f);
58 assertEquals(13, f(3));
59 print("Collect log:", log);
60 assertEquals([["baem1",true], ["baem2",true], ["baem3",true]], log);
61})();
62
63(function TryCatchFinally() {
64 log = []; // Clear log.
65 function f(a) {
66 try {
67 throw "wosh" + a;
68 } catch(e) {
69 return e + a;
70 } finally {
71 // Nothing.
72 }
73 }
74 assertEquals("wosh11", f(1));
75 assertEquals("wosh22", f(2));
76 %OptimizeFunctionOnNextCall(f);
77 assertEquals("wosh33", f(3));
78 print("Collect log:", log);
79 assertEquals([["wosh1",false], ["wosh2",false], ["wosh3",false]], log);
80})();
81
82(function TryCatchNestedFinally() {
83 log = []; // Clear log.
84 function f(a) {
85 try {
86 try {
87 throw "bang" + a;
88 } finally {
89 // Nothing.
90 }
91 } catch(e) {
92 return e + a;
93 }
94 }
95 assertEquals("bang11", f(1));
96 assertEquals("bang22", f(2));
97 %OptimizeFunctionOnNextCall(f);
98 assertEquals("bang33", f(3));
99 print("Collect log:", log);
100 assertEquals([["bang1",false], ["bang2",false], ["bang3",false]], log);
101})();
102
103(function TryFinallyNestedCatch() {
104 log = []; // Clear log.
105 function f(a) {
106 try {
107 try {
108 throw "peng" + a;
109 } catch(e) {
110 return e
111 }
112 } finally {
113 return a + 10;
114 }
115 }
116 assertEquals(11, f(1));
117 assertEquals(12, f(2));
118 %OptimizeFunctionOnNextCall(f);
119 assertEquals(13, f(3));
120 print("Collect log:", log);
121 assertEquals([["peng1",false], ["peng2",false], ["peng3",false]], log);
122})();
123
124(function TryFinallyNestedFinally() {
125 log = []; // Clear log.
126 function f(a) {
127 try {
128 try {
129 throw "oops" + a;
130 } finally {
131 // Nothing.
132 }
133 } finally {
134 return a + 10;
135 }
136 }
137 assertEquals(11, f(1));
138 assertEquals(12, f(2));
139 %OptimizeFunctionOnNextCall(f);
140 assertEquals(13, f(3));
141 print("Collect log:", log);
142 assertEquals([["oops1",true], ["oops2",true], ["oops3",true]], log);
143})();