deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / cctest / compiler / test-run-jsexceptions.cc
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 #include "src/v8.h"
6
7 #include "test/cctest/compiler/function-tester.h"
8
9 using namespace v8::internal;
10 using namespace v8::internal::compiler;
11
12 TEST(Throw) {
13   i::FLAG_turbo_exceptions = true;
14   FunctionTester T("(function(a,b) { if (a) { throw b; } else { return b; }})");
15
16   T.CheckThrows(T.true_value(), T.NewObject("new Error"));
17   T.CheckCall(T.Val(23), T.false_value(), T.Val(23));
18 }
19
20
21 TEST(ThrowMessagePosition) {
22   i::FLAG_turbo_exceptions = true;
23   static const char* src =
24       "(function(a, b) {        \n"
25       "  if (a == 1) throw 1;   \n"
26       "  if (a == 2) {throw 2}  \n"
27       "  if (a == 3) {0;throw 3}\n"
28       "  throw 4;               \n"
29       "})                       ";
30   FunctionTester T(src);
31   v8::Handle<v8::Message> message;
32
33   message = T.CheckThrowsReturnMessage(T.Val(1), T.undefined());
34   CHECK_EQ(2, message->GetLineNumber());
35   CHECK_EQ(40, message->GetStartPosition());
36
37   message = T.CheckThrowsReturnMessage(T.Val(2), T.undefined());
38   CHECK_EQ(3, message->GetLineNumber());
39   CHECK_EQ(67, message->GetStartPosition());
40
41   message = T.CheckThrowsReturnMessage(T.Val(3), T.undefined());
42   CHECK_EQ(4, message->GetLineNumber());
43   CHECK_EQ(95, message->GetStartPosition());
44 }
45
46
47 TEST(ThrowMessageDirectly) {
48   i::FLAG_turbo_exceptions = true;
49   static const char* src =
50       "(function(a, b) {"
51       "  if (a) { throw b; } else { throw new Error(b); }"
52       "})";
53   FunctionTester T(src);
54   v8::Handle<v8::Message> message;
55
56   message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
57   CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?")));
58
59   message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
60   CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!")));
61 }
62
63
64 TEST(ThrowMessageIndirectly) {
65   i::FLAG_turbo_exceptions = true;
66   static const char* src =
67       "(function(a, b) {"
68       "  try {"
69       "    if (a) { throw b; } else { throw new Error(b); }"
70       "  } finally {"
71       "    try { throw 'clobber'; } catch (e) { 'unclobber'; }"
72       "  }"
73       "})";
74   FunctionTester T(src);
75   v8::Handle<v8::Message> message;
76
77   message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
78   CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?")));
79
80   message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
81   CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!")));
82 }
83
84
85 // TODO(mstarzinger): Increase test coverage by having similar tests within the
86 // mjsunit suite to also test integration with other components (e.g. OSR).
87
88
89 TEST(Catch) {
90   i::FLAG_turbo_exceptions = true;
91   const char* src =
92       "(function(a,b) {"
93       "  var r = '-';"
94       "  try {"
95       "    r += 'A-';"
96       "    throw 'B-';"
97       "  } catch (e) {"
98       "    r += e;"
99       "  }"
100       "  return r;"
101       "})";
102   FunctionTester T(src);
103
104   T.CheckCall(T.Val("-A-B-"));
105 }
106
107
108 TEST(CatchNested) {
109   i::FLAG_turbo_exceptions = true;
110   const char* src =
111       "(function(a,b) {"
112       "  var r = '-';"
113       "  try {"
114       "    r += 'A-';"
115       "    throw 'C-';"
116       "  } catch (e) {"
117       "    try {"
118       "      throw 'B-';"
119       "    } catch (e) {"
120       "      r += e;"
121       "    }"
122       "    r += e;"
123       "  }"
124       "  return r;"
125       "})";
126   FunctionTester T(src);
127
128   T.CheckCall(T.Val("-A-B-C-"));
129 }
130
131
132 TEST(CatchBreak) {
133   i::FLAG_turbo_exceptions = true;
134   const char* src =
135       "(function(a,b) {"
136       "  var r = '-';"
137       "  L: try {"
138       "    r += 'A-';"
139       "    if (a) break L;"
140       "    r += 'B-';"
141       "    throw 'C-';"
142       "  } catch (e) {"
143       "    if (b) break L;"
144       "    r += e;"
145       "  }"
146       "  r += 'D-';"
147       "  return r;"
148       "})";
149   FunctionTester T(src);
150
151   T.CheckCall(T.Val("-A-D-"), T.true_value(), T.false_value());
152   T.CheckCall(T.Val("-A-B-D-"), T.false_value(), T.true_value());
153   T.CheckCall(T.Val("-A-B-C-D-"), T.false_value(), T.false_value());
154 }
155
156
157 TEST(CatchCall) {
158   i::FLAG_turbo_exceptions = true;
159   const char* src =
160       "(function(fun) {"
161       "  var r = '-';"
162       "  try {"
163       "    r += 'A-';"
164       "    return r + 'B-' + fun();"
165       "  } catch (e) {"
166       "    r += e;"
167       "  }"
168       "  return r;"
169       "})";
170   FunctionTester T(src);
171
172   CompileRun("function thrower() { throw 'T-'; }");
173   T.CheckCall(T.Val("-A-T-"), T.NewFunction("thrower"));
174   CompileRun("function returner() { return 'R-'; }");
175   T.CheckCall(T.Val("-A-B-R-"), T.NewFunction("returner"));
176 }
177
178
179 TEST(Finally) {
180   i::FLAG_turbo_exceptions = true;
181   const char* src =
182       "(function(a,b) {"
183       "  var r = '-';"
184       "  try {"
185       "    r += 'A-';"
186       "  } finally {"
187       "    r += 'B-';"
188       "  }"
189       "  return r;"
190       "})";
191   FunctionTester T(src);
192
193   T.CheckCall(T.Val("-A-B-"));
194 }
195
196
197 TEST(FinallyBreak) {
198   i::FLAG_turbo_exceptions = true;
199   const char* src =
200       "(function(a,b) {"
201       "  var r = '-';"
202       "  L: try {"
203       "    r += 'A-';"
204       "    if (a) return r;"
205       "    r += 'B-';"
206       "    if (b) break L;"
207       "    r += 'C-';"
208       "  } finally {"
209       "    r += 'D-';"
210       "  }"
211       "  return r;"
212       "})";
213   FunctionTester T(src);
214
215   T.CheckCall(T.Val("-A-"), T.true_value(), T.false_value());
216   T.CheckCall(T.Val("-A-B-D-"), T.false_value(), T.true_value());
217   T.CheckCall(T.Val("-A-B-C-D-"), T.false_value(), T.false_value());
218 }
219
220
221 TEST(DeoptTry) {
222   i::FLAG_turbo_exceptions = true;
223   i::FLAG_turbo_deoptimization = true;
224   const char* src =
225       "(function f(a) {"
226       "  try {"
227       "    %DeoptimizeFunction(f);"
228       "    throw a;"
229       "  } catch (e) {"
230       "    return e + 1;"
231       "  }"
232       "})";
233   FunctionTester T(src);
234
235   T.CheckCall(T.Val(2), T.Val(1));
236 }
237
238
239 TEST(DeoptCatch) {
240   i::FLAG_turbo_exceptions = true;
241   i::FLAG_turbo_deoptimization = true;
242   const char* src =
243       "(function f(a) {"
244       "  try {"
245       "    throw a;"
246       "  } catch (e) {"
247       "    %DeoptimizeFunction(f);"
248       "    return e + 1;"
249       "  }"
250       "})";
251   FunctionTester T(src);
252
253   T.CheckCall(T.Val(2), T.Val(1));
254 }
255
256
257 TEST(DeoptFinallyReturn) {
258   i::FLAG_turbo_exceptions = true;
259   i::FLAG_turbo_deoptimization = true;
260   const char* src =
261       "(function f(a) {"
262       "  try {"
263       "    throw a;"
264       "  } finally {"
265       "    %DeoptimizeFunction(f);"
266       "    return a + 1;"
267       "  }"
268       "})";
269   FunctionTester T(src);
270
271   T.CheckCall(T.Val(2), T.Val(1));
272 }
273
274
275 TEST(DeoptFinallyReThrow) {
276   i::FLAG_turbo_exceptions = true;
277   i::FLAG_turbo_deoptimization = true;
278   const char* src =
279       "(function f(a) {"
280       "  try {"
281       "    throw a;"
282       "  } finally {"
283       "    %DeoptimizeFunction(f);"
284       "  }"
285       "})";
286   FunctionTester T(src);
287
288 #if 0  // TODO(mstarzinger): Enable once we can.
289   T.CheckThrows(T.NewObject("new Error"), T.Val(1));
290 #endif
291 }