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.
7 #include "test/cctest/compiler/function-tester.h"
9 using namespace v8::internal;
10 using namespace v8::internal::compiler;
13 i::FLAG_turbo_exceptions = true;
14 FunctionTester T("(function(a,b) { if (a) { throw b; } else { return b; }})");
16 T.CheckThrows(T.true_value(), T.NewObject("new Error"));
17 T.CheckCall(T.Val(23), T.false_value(), T.Val(23));
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"
30 FunctionTester T(src);
31 v8::Handle<v8::Message> message;
33 message = T.CheckThrowsReturnMessage(T.Val(1), T.undefined());
34 CHECK_EQ(2, message->GetLineNumber());
35 CHECK_EQ(40, message->GetStartPosition());
37 message = T.CheckThrowsReturnMessage(T.Val(2), T.undefined());
38 CHECK_EQ(3, message->GetLineNumber());
39 CHECK_EQ(67, message->GetStartPosition());
41 message = T.CheckThrowsReturnMessage(T.Val(3), T.undefined());
42 CHECK_EQ(4, message->GetLineNumber());
43 CHECK_EQ(95, message->GetStartPosition());
47 TEST(ThrowMessageDirectly) {
48 i::FLAG_turbo_exceptions = true;
49 static const char* src =
51 " if (a) { throw b; } else { throw new Error(b); }"
53 FunctionTester T(src);
54 v8::Handle<v8::Message> message;
56 message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
57 CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?")));
59 message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
60 CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!")));
64 TEST(ThrowMessageIndirectly) {
65 i::FLAG_turbo_exceptions = true;
66 static const char* src =
69 " if (a) { throw b; } else { throw new Error(b); }"
71 " try { throw 'clobber'; } catch (e) { 'unclobber'; }"
74 FunctionTester T(src);
75 v8::Handle<v8::Message> message;
77 message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
78 CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?")));
80 message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
81 CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!")));
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).
90 i::FLAG_turbo_exceptions = true;
102 FunctionTester T(src);
104 T.CheckCall(T.Val("-A-B-"));
109 i::FLAG_turbo_exceptions = true;
126 FunctionTester T(src);
128 T.CheckCall(T.Val("-A-B-C-"));
133 i::FLAG_turbo_exceptions = true;
149 FunctionTester T(src);
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());
158 i::FLAG_turbo_exceptions = true;
164 " return r + 'B-' + fun();"
170 FunctionTester T(src);
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"));
180 i::FLAG_turbo_exceptions = true;
191 FunctionTester T(src);
193 T.CheckCall(T.Val("-A-B-"));
198 i::FLAG_turbo_exceptions = true;
213 FunctionTester T(src);
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());
222 i::FLAG_turbo_exceptions = true;
223 i::FLAG_turbo_deoptimization = true;
227 " %DeoptimizeFunction(f);"
233 FunctionTester T(src);
235 T.CheckCall(T.Val(2), T.Val(1));
240 i::FLAG_turbo_exceptions = true;
241 i::FLAG_turbo_deoptimization = true;
247 " %DeoptimizeFunction(f);"
251 FunctionTester T(src);
253 T.CheckCall(T.Val(2), T.Val(1));
257 TEST(DeoptFinallyReturn) {
258 i::FLAG_turbo_exceptions = true;
259 i::FLAG_turbo_deoptimization = true;
265 " %DeoptimizeFunction(f);"
269 FunctionTester T(src);
271 T.CheckCall(T.Val(2), T.Val(1));
275 TEST(DeoptFinallyReThrow) {
276 i::FLAG_turbo_exceptions = true;
277 i::FLAG_turbo_deoptimization = true;
283 " %DeoptimizeFunction(f);"
286 FunctionTester T(src);
288 #if 0 // TODO(mstarzinger): Enable once we can.
289 T.CheckThrows(T.NewObject("new Error"), T.Val(1));