From 99dc9e8e0cea3e6418870deecbc4c06e6dacbed3 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Tue, 2 Feb 2021 18:03:55 +0300 Subject: [PATCH] Relax setUnhandledExceptionHook requirements with the new MM (#4675) --- .../tests/runtime/exceptions/custom_hook.kt | 24 +++++++++++++++---- .../src/main/kotlin/kotlin/native/Runtime.kt | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt index edb2c682a6e..8ce6fe326aa 100644 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt +++ b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt @@ -6,17 +6,31 @@ import kotlin.test.* import kotlin.native.concurrent.* -fun main(args : Array) { +fun setHookLegacyMM(hook: ReportUnhandledExceptionHook) : ReportUnhandledExceptionHook? { assertFailsWith { setUnhandledExceptionHook { _ -> println("wrong") } } + return setUnhandledExceptionHook(hook.freeze()) +} + +fun setHookNewMM(hook: ReportUnhandledExceptionHook) : ReportUnhandledExceptionHook? { + return setUnhandledExceptionHook(hook) +} + +fun setHook(hook: ReportUnhandledExceptionHook) : ReportUnhandledExceptionHook? { + return when (kotlin.native.Platform.memoryModel) { + kotlin.native.MemoryModel.EXPERIMENTAL -> setHookNewMM(hook) + else -> setHookLegacyMM(hook) + } +} + +fun main() { val x = 42 - val old = setUnhandledExceptionHook({ + val old = setHook { throwable: Throwable -> println("value $x: ${throwable::class.simpleName}") - }.freeze()) + } assertNull(old) - throw Error("an error") -} \ No newline at end of file +} 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 4722db26c4f..28e4c74872d 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/Runtime.kt @@ -45,7 +45,7 @@ public typealias ReportUnhandledExceptionHook = Function1 * with custom exception hooks. */ public fun setUnhandledExceptionHook(hook: ReportUnhandledExceptionHook): ReportUnhandledExceptionHook? { - if (!hook.isFrozen) { + if (Platform.memoryModel != MemoryModel.EXPERIMENTAL && !hook.isFrozen) { throw InvalidMutabilityException("Unhandled exception hook must be frozen") } return setUnhandledExceptionHook0(hook)