From 23e5079391a44f206e38e2f6f7091202d5e75a04 Mon Sep 17 00:00:00 2001 From: Elena Lepilkina Date: Mon, 25 Oct 2021 17:15:14 +0300 Subject: [PATCH] [K/N] Added test for checking C callback set with Kotlin function throwing exception --- kotlin-native/backend.native/tests/build.gradle | 13 +++++++++++++ .../tests/interop/exceptions/cCallback.cpp | 9 +++++++++ .../tests/interop/exceptions/cCallback.def | 5 +++++ .../tests/interop/exceptions/cCallback.kt | 11 +++++++++++ 4 files changed, 38 insertions(+) create mode 100644 kotlin-native/backend.native/tests/interop/exceptions/cCallback.cpp create mode 100644 kotlin-native/backend.native/tests/interop/exceptions/cCallback.def create mode 100644 kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 079f987d95f..b449ad04a79 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -4338,6 +4338,11 @@ createInterop("exceptions_throwThroughBridge") { it.extraOpts "-Xcompile-source", "$projectDir/interop/exceptions/throwThroughBridgeInterop.cpp" } +createInterop("exceptions_cCallback") { + it.defFile "interop/exceptions/cCallback.def" + it.extraOpts "-Xcompile-source", "$projectDir/interop/exceptions/cCallback.cpp" +} + /** * Creates a task for the interop test. Configures runner and adds library and test build tasks. */ @@ -5127,6 +5132,14 @@ dynamicTest("interop_exceptions_throwThroughBridge") { interop = "exceptions_throwThroughBridge" } +interopTest("interop_exceptions_cCallback") { + disabled = (project.testTarget == 'wasm32') // Uses exceptions + source = "interop/exceptions/cCallback.kt" + expectedExitStatusChecker = { it == 0 } + outputChecker = { s -> s.contains("CATCH IN C++") } + interop = "exceptions_cCallback" +} + tasks.register("library_ir_provider_mismatch", KonanDriverTest) { def dir = buildDir.absolutePath def lib = "$projectDir/link/ir_providers/library/empty.kt" diff --git a/kotlin-native/backend.native/tests/interop/exceptions/cCallback.cpp b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.cpp new file mode 100644 index 00000000000..be05f5cda28 --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.cpp @@ -0,0 +1,9 @@ +#include + +extern "C" void runAndCatch(void(*f)(void)) { + try { + f(); + } catch (...) { + printf("CATCH IN C++!\n"); + } +} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/exceptions/cCallback.def b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.def new file mode 100644 index 00000000000..e37047b646d --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.def @@ -0,0 +1,5 @@ +language = C + +--- + +void runAndCatch(void(*f)(void)); \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt new file mode 100644 index 00000000000..4a02d9e676f --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt @@ -0,0 +1,11 @@ +import kotlinx.cinterop.staticCFunction +import cCallback.runAndCatch + +fun throwingCallback() { + throw IllegalStateException("Kotlin Exception!") +} + + +fun main() { + runAndCatch(staticCFunction(::throwingCallback)) +} \ No newline at end of file