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 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "test/cctest/compiler/function-tester.h" | 7 #include "test/cctest/compiler/function-tester.h" |
8 | 8 |
9 using namespace v8::internal; | 9 using namespace v8::internal; |
10 using namespace v8::internal::compiler; | 10 using namespace v8::internal::compiler; |
11 | 11 |
12 TEST(Throw) { | 12 TEST(Throw) { |
13 i::FLAG_turbo_exceptions = true; | 13 i::FLAG_turbo_exceptions = true; |
14 FunctionTester T("(function(a,b) { if (a) { throw b; } else { return b; }})"); | 14 FunctionTester T("(function(a,b) { if (a) { throw b; } else { return b; }})"); |
15 | 15 |
16 T.CheckThrows(T.true_value(), T.NewObject("new Error")); | 16 T.CheckThrows(T.true_value(), T.NewObject("new Error")); |
17 T.CheckCall(T.Val(23), T.false_value(), T.Val(23)); | 17 T.CheckCall(T.Val(23), T.false_value(), T.Val(23)); |
18 } | 18 } |
19 | 19 |
20 | 20 |
21 TEST(ThrowSourcePosition) { | 21 TEST(ThrowMessagePosition) { |
22 i::FLAG_turbo_exceptions = true; | 22 i::FLAG_turbo_exceptions = true; |
23 static const char* src = | 23 static const char* src = |
24 "(function(a, b) { \n" | 24 "(function(a, b) { \n" |
25 " if (a == 1) throw 1; \n" | 25 " if (a == 1) throw 1; \n" |
26 " if (a == 2) {throw 2} \n" | 26 " if (a == 2) {throw 2} \n" |
27 " if (a == 3) {0;throw 3}\n" | 27 " if (a == 3) {0;throw 3}\n" |
28 " throw 4; \n" | 28 " throw 4; \n" |
29 "}) "; | 29 "}) "; |
30 FunctionTester T(src); | 30 FunctionTester T(src); |
31 v8::Handle<v8::Message> message; | 31 v8::Handle<v8::Message> message; |
32 | 32 |
33 message = T.CheckThrowsReturnMessage(T.Val(1), T.undefined()); | 33 message = T.CheckThrowsReturnMessage(T.Val(1), T.undefined()); |
34 CHECK(!message.IsEmpty()); | 34 CHECK(!message.IsEmpty()); |
35 CHECK_EQ(2, message->GetLineNumber()); | 35 CHECK_EQ(2, message->GetLineNumber()); |
36 CHECK_EQ(40, message->GetStartPosition()); | 36 CHECK_EQ(40, message->GetStartPosition()); |
37 | 37 |
38 message = T.CheckThrowsReturnMessage(T.Val(2), T.undefined()); | 38 message = T.CheckThrowsReturnMessage(T.Val(2), T.undefined()); |
39 CHECK(!message.IsEmpty()); | 39 CHECK(!message.IsEmpty()); |
40 CHECK_EQ(3, message->GetLineNumber()); | 40 CHECK_EQ(3, message->GetLineNumber()); |
41 CHECK_EQ(67, message->GetStartPosition()); | 41 CHECK_EQ(67, message->GetStartPosition()); |
42 | 42 |
43 message = T.CheckThrowsReturnMessage(T.Val(3), T.undefined()); | 43 message = T.CheckThrowsReturnMessage(T.Val(3), T.undefined()); |
44 CHECK(!message.IsEmpty()); | 44 CHECK(!message.IsEmpty()); |
45 CHECK_EQ(4, message->GetLineNumber()); | 45 CHECK_EQ(4, message->GetLineNumber()); |
46 CHECK_EQ(95, message->GetStartPosition()); | 46 CHECK_EQ(95, message->GetStartPosition()); |
47 } | 47 } |
48 | 48 |
49 | 49 |
| 50 TEST(ThrowMessageDirectly) { |
| 51 i::FLAG_turbo_exceptions = true; |
| 52 static const char* src = |
| 53 "(function(a, b) {" |
| 54 " if (a) { throw b; } else { throw new Error(b); }" |
| 55 "})"; |
| 56 FunctionTester T(src); |
| 57 v8::Handle<v8::Message> message; |
| 58 |
| 59 message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?")); |
| 60 CHECK(!message.IsEmpty()); |
| 61 CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?"))); |
| 62 |
| 63 message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!")); |
| 64 CHECK(!message.IsEmpty()); |
| 65 CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!"))); |
| 66 } |
| 67 |
| 68 |
| 69 TEST(ThrowMessageIndirectly) { |
| 70 i::FLAG_turbo_exceptions = true; |
| 71 static const char* src = |
| 72 "(function(a, b) {" |
| 73 " try {" |
| 74 " if (a) { throw b; } else { throw new Error(b); }" |
| 75 " } finally {" |
| 76 " try { throw 'clobber'; } catch (e) { 'unclobber'; }" |
| 77 " }" |
| 78 "})"; |
| 79 FunctionTester T(src); |
| 80 v8::Handle<v8::Message> message; |
| 81 |
| 82 message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?")); |
| 83 CHECK(!message.IsEmpty()); |
| 84 CHECK(message->Get()->Equals(v8_str("Uncaught Error: Wat?"))); |
| 85 |
| 86 message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!")); |
| 87 CHECK(!message.IsEmpty()); |
| 88 CHECK(message->Get()->Equals(v8_str("Uncaught Kaboom!"))); |
| 89 } |
| 90 |
| 91 |
50 // TODO(mstarzinger): Increase test coverage by having similar tests within the | 92 // TODO(mstarzinger): Increase test coverage by having similar tests within the |
51 // mjsunit suite to also test integration with other components (e.g. OSR). | 93 // mjsunit suite to also test integration with other components (e.g. OSR). |
52 | 94 |
53 | 95 |
54 TEST(Catch) { | 96 TEST(Catch) { |
55 i::FLAG_turbo_exceptions = true; | 97 i::FLAG_turbo_exceptions = true; |
56 const char* src = | 98 const char* src = |
57 "(function(a,b) {" | 99 "(function(a,b) {" |
58 " var r = '-';" | 100 " var r = '-';" |
59 " try {" | 101 " try {" |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 " } finally {" | 291 " } finally {" |
250 " %DeoptimizeFunction(f);" | 292 " %DeoptimizeFunction(f);" |
251 " }" | 293 " }" |
252 "})"; | 294 "})"; |
253 FunctionTester T(src); | 295 FunctionTester T(src); |
254 | 296 |
255 #if 0 // TODO(mstarzinger): Enable once we can. | 297 #if 0 // TODO(mstarzinger): Enable once we can. |
256 T.CheckThrows(T.NewObject("new Error"), T.Val(1)); | 298 T.CheckThrows(T.NewObject("new Error"), T.Val(1)); |
257 #endif | 299 #endif |
258 } | 300 } |
OLD | NEW |