2014-07-30 13:54:45 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
#include "src/objects-inl.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "test/cctest/compiler/function-tester.h"
|
|
|
|
|
2015-10-30 09:16:26 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
TEST(Throw) {
|
|
|
|
FunctionTester T("(function(a,b) { if (a) { throw b; } else { return b; }})");
|
|
|
|
|
|
|
|
T.CheckThrows(T.true_value(), T.NewObject("new Error"));
|
|
|
|
T.CheckCall(T.Val(23), T.false_value(), T.Val(23));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-06 10:15:49 +00:00
|
|
|
TEST(ThrowMessagePosition) {
|
2014-07-30 13:54:45 +00:00
|
|
|
static const char* src =
|
|
|
|
"(function(a, b) { \n"
|
|
|
|
" if (a == 1) throw 1; \n"
|
|
|
|
" if (a == 2) {throw 2} \n"
|
|
|
|
" if (a == 3) {0;throw 3}\n"
|
|
|
|
" throw 4; \n"
|
|
|
|
"}) ";
|
|
|
|
FunctionTester T(src);
|
2015-10-28 13:09:46 +00:00
|
|
|
v8::Local<v8::Message> message;
|
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
message = T.CheckThrowsReturnMessage(T.Val(1), T.undefined());
|
2015-10-28 13:09:46 +00:00
|
|
|
CHECK_EQ(2, message->GetLineNumber(context).FromMaybe(-1));
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK_EQ(40, message->GetStartPosition());
|
|
|
|
|
|
|
|
message = T.CheckThrowsReturnMessage(T.Val(2), T.undefined());
|
2015-10-28 13:09:46 +00:00
|
|
|
CHECK_EQ(3, message->GetLineNumber(context).FromMaybe(-1));
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK_EQ(67, message->GetStartPosition());
|
|
|
|
|
|
|
|
message = T.CheckThrowsReturnMessage(T.Val(3), T.undefined());
|
2015-10-28 13:09:46 +00:00
|
|
|
CHECK_EQ(4, message->GetLineNumber(context).FromMaybe(-1));
|
2014-07-30 13:54:45 +00:00
|
|
|
CHECK_EQ(95, message->GetStartPosition());
|
|
|
|
}
|
2015-02-03 13:09:40 +00:00
|
|
|
|
|
|
|
|
2015-03-06 10:15:49 +00:00
|
|
|
TEST(ThrowMessageDirectly) {
|
|
|
|
static const char* src =
|
|
|
|
"(function(a, b) {"
|
|
|
|
" if (a) { throw b; } else { throw new Error(b); }"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
2015-10-28 13:09:46 +00:00
|
|
|
v8::Local<v8::Message> message;
|
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
|
|
|
v8::Maybe<bool> t = v8::Just(true);
|
2015-03-06 10:15:49 +00:00
|
|
|
|
|
|
|
message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
|
2015-10-28 13:09:46 +00:00
|
|
|
CHECK(t == message->Get()->Equals(context, v8_str("Uncaught Error: Wat?")));
|
2015-03-06 10:15:49 +00:00
|
|
|
|
|
|
|
message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
|
2015-10-28 13:09:46 +00:00
|
|
|
CHECK(t == message->Get()->Equals(context, v8_str("Uncaught Kaboom!")));
|
2015-03-06 10:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ThrowMessageIndirectly) {
|
|
|
|
static const char* src =
|
|
|
|
"(function(a, b) {"
|
|
|
|
" try {"
|
|
|
|
" if (a) { throw b; } else { throw new Error(b); }"
|
|
|
|
" } finally {"
|
|
|
|
" try { throw 'clobber'; } catch (e) { 'unclobber'; }"
|
|
|
|
" }"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
2015-10-28 13:09:46 +00:00
|
|
|
v8::Local<v8::Message> message;
|
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
|
|
|
v8::Maybe<bool> t = v8::Just(true);
|
2015-03-06 10:15:49 +00:00
|
|
|
|
|
|
|
message = T.CheckThrowsReturnMessage(T.false_value(), T.Val("Wat?"));
|
2015-10-28 13:09:46 +00:00
|
|
|
CHECK(t == message->Get()->Equals(context, v8_str("Uncaught Error: Wat?")));
|
2015-03-06 10:15:49 +00:00
|
|
|
|
|
|
|
message = T.CheckThrowsReturnMessage(T.true_value(), T.Val("Kaboom!"));
|
2015-10-28 13:09:46 +00:00
|
|
|
CHECK(t == message->Get()->Equals(context, v8_str("Uncaught Kaboom!")));
|
2015-03-06 10:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-03 13:09:40 +00:00
|
|
|
TEST(Catch) {
|
|
|
|
const char* src =
|
|
|
|
"(function(a,b) {"
|
|
|
|
" var r = '-';"
|
|
|
|
" try {"
|
|
|
|
" r += 'A-';"
|
|
|
|
" throw 'B-';"
|
|
|
|
" } catch (e) {"
|
|
|
|
" r += e;"
|
|
|
|
" }"
|
|
|
|
" return r;"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val("-A-B-"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CatchNested) {
|
|
|
|
const char* src =
|
|
|
|
"(function(a,b) {"
|
|
|
|
" var r = '-';"
|
|
|
|
" try {"
|
|
|
|
" r += 'A-';"
|
|
|
|
" throw 'C-';"
|
|
|
|
" } catch (e) {"
|
|
|
|
" try {"
|
|
|
|
" throw 'B-';"
|
|
|
|
" } catch (e) {"
|
|
|
|
" r += e;"
|
|
|
|
" }"
|
|
|
|
" r += e;"
|
|
|
|
" }"
|
|
|
|
" return r;"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val("-A-B-C-"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CatchBreak) {
|
|
|
|
const char* src =
|
|
|
|
"(function(a,b) {"
|
|
|
|
" var r = '-';"
|
|
|
|
" L: try {"
|
|
|
|
" r += 'A-';"
|
|
|
|
" if (a) break L;"
|
|
|
|
" r += 'B-';"
|
|
|
|
" throw 'C-';"
|
|
|
|
" } catch (e) {"
|
|
|
|
" if (b) break L;"
|
|
|
|
" r += e;"
|
|
|
|
" }"
|
|
|
|
" r += 'D-';"
|
|
|
|
" return r;"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val("-A-D-"), T.true_value(), T.false_value());
|
|
|
|
T.CheckCall(T.Val("-A-B-D-"), T.false_value(), T.true_value());
|
|
|
|
T.CheckCall(T.Val("-A-B-C-D-"), T.false_value(), T.false_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 09:54:49 +00:00
|
|
|
TEST(CatchCall) {
|
|
|
|
const char* src =
|
|
|
|
"(function(fun) {"
|
|
|
|
" var r = '-';"
|
|
|
|
" try {"
|
|
|
|
" r += 'A-';"
|
2015-02-20 10:48:05 +00:00
|
|
|
" return r + 'B-' + fun();"
|
2015-02-20 09:54:49 +00:00
|
|
|
" } catch (e) {"
|
|
|
|
" r += e;"
|
|
|
|
" }"
|
|
|
|
" return r;"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
CompileRun("function thrower() { throw 'T-'; }");
|
|
|
|
T.CheckCall(T.Val("-A-T-"), T.NewFunction("thrower"));
|
2015-02-20 10:48:05 +00:00
|
|
|
CompileRun("function returner() { return 'R-'; }");
|
|
|
|
T.CheckCall(T.Val("-A-B-R-"), T.NewFunction("returner"));
|
2015-02-20 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-03 13:09:40 +00:00
|
|
|
TEST(Finally) {
|
|
|
|
const char* src =
|
|
|
|
"(function(a,b) {"
|
|
|
|
" var r = '-';"
|
|
|
|
" try {"
|
|
|
|
" r += 'A-';"
|
|
|
|
" } finally {"
|
|
|
|
" r += 'B-';"
|
|
|
|
" }"
|
|
|
|
" return r;"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val("-A-B-"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FinallyBreak) {
|
|
|
|
const char* src =
|
|
|
|
"(function(a,b) {"
|
|
|
|
" var r = '-';"
|
|
|
|
" L: try {"
|
|
|
|
" r += 'A-';"
|
|
|
|
" if (a) return r;"
|
|
|
|
" r += 'B-';"
|
|
|
|
" if (b) break L;"
|
|
|
|
" r += 'C-';"
|
|
|
|
" } finally {"
|
|
|
|
" r += 'D-';"
|
|
|
|
" }"
|
|
|
|
" return r;"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val("-A-"), T.true_value(), T.false_value());
|
|
|
|
T.CheckCall(T.Val("-A-B-D-"), T.false_value(), T.true_value());
|
|
|
|
T.CheckCall(T.Val("-A-B-C-D-"), T.false_value(), T.false_value());
|
|
|
|
}
|
2015-03-03 17:23:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(DeoptTry) {
|
|
|
|
const char* src =
|
|
|
|
"(function f(a) {"
|
|
|
|
" try {"
|
|
|
|
" %DeoptimizeFunction(f);"
|
|
|
|
" throw a;"
|
|
|
|
" } catch (e) {"
|
|
|
|
" return e + 1;"
|
|
|
|
" }"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val(2), T.Val(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(DeoptCatch) {
|
|
|
|
const char* src =
|
|
|
|
"(function f(a) {"
|
|
|
|
" try {"
|
|
|
|
" throw a;"
|
|
|
|
" } catch (e) {"
|
|
|
|
" %DeoptimizeFunction(f);"
|
|
|
|
" return e + 1;"
|
|
|
|
" }"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val(2), T.Val(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(DeoptFinallyReturn) {
|
|
|
|
const char* src =
|
|
|
|
"(function f(a) {"
|
|
|
|
" try {"
|
|
|
|
" throw a;"
|
|
|
|
" } finally {"
|
|
|
|
" %DeoptimizeFunction(f);"
|
|
|
|
" return a + 1;"
|
|
|
|
" }"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckCall(T.Val(2), T.Val(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(DeoptFinallyReThrow) {
|
|
|
|
const char* src =
|
|
|
|
"(function f(a) {"
|
|
|
|
" try {"
|
|
|
|
" throw a;"
|
|
|
|
" } finally {"
|
|
|
|
" %DeoptimizeFunction(f);"
|
|
|
|
" }"
|
|
|
|
"})";
|
|
|
|
FunctionTester T(src);
|
|
|
|
|
|
|
|
T.CheckThrows(T.NewObject("new Error"), T.Val(1));
|
|
|
|
}
|
2015-10-30 09:16:26 +00:00
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|