From 41d6dc5914c8912379b76f580fdf55437728f3e4 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Tue, 28 Jun 2022 11:28:01 +0300 Subject: [PATCH] [K/N] Allow to unset unhandled exception hook ^KT-49228 --- .../tests/runtime/exceptions/custom_hook_get.kt | 5 +++++ .../runtime/src/main/kotlin/kotlin/native/Runtime.kt | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt index 9ba9299eb1f..e5e8336fb7c 100644 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt +++ b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt @@ -18,4 +18,9 @@ fun main() { val hook1 = getUnhandledExceptionHook() assertEquals(exceptionHook, hook1) val hook2 = getUnhandledExceptionHook() + assertEquals(exceptionHook, hook2) + val hook3 = setUnhandledExceptionHook(null) + assertEquals(exceptionHook, hook3) + val hook4 = getUnhandledExceptionHook() + assertNull(hook4) } diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt index 9f0046a7f64..b47c55b9430 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt @@ -57,6 +57,17 @@ public typealias ReportUnhandledExceptionHook = Function1 * Hook must be a frozen lambda, so that it could be called from any thread/worker. */ @OptIn(FreezingIsDeprecated::class) +public fun setUnhandledExceptionHook(hook: ReportUnhandledExceptionHook?): ReportUnhandledExceptionHook? { + try { + return UnhandledExceptionHookHolder.hook.swap(hook) + } catch (e: InvalidMutabilityException) { + throw InvalidMutabilityException("Unhandled exception hook must be frozen") + } +} + +@Suppress("CONFLICTING_OVERLOADS") +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@OptIn(FreezingIsDeprecated::class) public fun setUnhandledExceptionHook(hook: ReportUnhandledExceptionHook): ReportUnhandledExceptionHook? { try { return UnhandledExceptionHookHolder.hook.swap(hook)