From 81811e8c3a08397a4592a850d6729909bb914eae Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Mon, 21 Nov 2016 17:36:36 +0300 Subject: [PATCH] TEST: throw keyword support. --- backend.native/tests/build.gradle | 9 +++++++-- backend.native/tests/runtime/basic/throw0.kt | 8 ++++++++ runtime/src/main/cpp/Exceptions.cpp | 7 +++++++ runtime/src/main/cpp/Exceptions.h | 2 ++ runtime/src/main/cpp/Types.h | 1 + runtime/src/main/kotlin/kotlin/Throwable.kt | 1 + 6 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 backend.native/tests/runtime/basic/throw0.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 1238070a30c..24a13b19b75 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -380,9 +380,14 @@ task intrinsic(type: UnitKonanTest) { task link(type: LinkKonanTest) { source = "link/src/bar.kt" lib = "link/lib" -} +} task for0(type: RunKonanTest) { goldValue = "2\n3\n4\n" source = "runtime/basic/for0.kt" -} \ No newline at end of file +} + +task throw0(type: RunKonanTest) { + goldValue = "Done\n" + source = "runtime/basic/throw0.kt" +} diff --git a/backend.native/tests/runtime/basic/throw0.kt b/backend.native/tests/runtime/basic/throw0.kt new file mode 100644 index 00000000000..b899ca0acf1 --- /dev/null +++ b/backend.native/tests/runtime/basic/throw0.kt @@ -0,0 +1,8 @@ +fun main(args : Array) { + val cond = 1 + if (cond == 2) throw RuntimeException() + if (cond == 3) throw NoSuchElementException("no such element") + if (cond == 4) throw Error("error happens") + + println("Done") +} diff --git a/runtime/src/main/cpp/Exceptions.cpp b/runtime/src/main/cpp/Exceptions.cpp index 2765d0f92e5..eb29b99a18e 100644 --- a/runtime/src/main/cpp/Exceptions.cpp +++ b/runtime/src/main/cpp/Exceptions.cpp @@ -4,6 +4,7 @@ #include "Assert.h" #include "Exceptions.h" +#include "Types.h" #ifdef __cplusplus extern "C" { @@ -35,6 +36,12 @@ void ThrowClassCastException() { RuntimeAssert(false, "Throwing is unsupported"); } +void ThrowException(KRef exception) { + RuntimeAssert(exception != nullptr && IsInstance(exception, theThrowableTypeInfo), + "Throwing something non-throwable"); + RuntimeAssert(false, "Throwing is unsupported"); +} + #ifdef __cplusplus } // extern "C" diff --git a/runtime/src/main/cpp/Exceptions.h b/runtime/src/main/cpp/Exceptions.h index 185843331d7..07d39d5aa03 100644 --- a/runtime/src/main/cpp/Exceptions.h +++ b/runtime/src/main/cpp/Exceptions.h @@ -13,6 +13,8 @@ void ThrowNullPointerException(); void ThrowArrayIndexOutOfBoundsException(); // Throws class cast exception. void ThrowClassCastException(); +// Throws arbitrary exception. +void ThrowException(KRef exception); #ifdef __cplusplus } // extern "C" diff --git a/runtime/src/main/cpp/Types.h b/runtime/src/main/cpp/Types.h index e6a1f11d261..c39f67e85b2 100644 --- a/runtime/src/main/cpp/Types.h +++ b/runtime/src/main/cpp/Types.h @@ -33,6 +33,7 @@ extern const TypeInfo* theFloatArrayTypeInfo; extern const TypeInfo* theDoubleArrayTypeInfo; extern const TypeInfo* theBooleanArrayTypeInfo; extern const TypeInfo* theStringTypeInfo; +extern const TypeInfo* theThrowableTypeInfo; KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info); void CheckCast(const ObjHeader* obj, const TypeInfo* type_info); diff --git a/runtime/src/main/kotlin/kotlin/Throwable.kt b/runtime/src/main/kotlin/kotlin/Throwable.kt index 0bca7ee392e..3b6e1cb3505 100644 --- a/runtime/src/main/kotlin/kotlin/Throwable.kt +++ b/runtime/src/main/kotlin/kotlin/Throwable.kt @@ -6,6 +6,7 @@ package kotlin * @param message the detail message string. * @param cause the cause of this throwable. */ +@ExportTypeInfo("theThrowableTypeInfo") public open class Throwable(open val message: String?, open val cause: Throwable?) { constructor(message: String?) : this(message, null)