Relax setUnhandledExceptionHook requirements with the new MM (#4675)
This commit is contained in:
committed by
Vasily Levchenko
parent
2b69a0d03a
commit
99dc9e8e0c
@@ -6,17 +6,31 @@ import kotlin.test.*
|
||||
|
||||
import kotlin.native.concurrent.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
fun setHookLegacyMM(hook: ReportUnhandledExceptionHook) : ReportUnhandledExceptionHook? {
|
||||
assertFailsWith<InvalidMutabilityException> {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public typealias ReportUnhandledExceptionHook = Function1<Throwable, Unit>
|
||||
* 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)
|
||||
|
||||
Reference in New Issue
Block a user