1 package kotlinx.coroutines.internal
2 
3 import kotlin.coroutines.*
4 
5 /**
6  * Tries to recover stacktrace for given [exception] and [continuation].
7  * Stacktrace recovery tries to restore [continuation] stack frames using its debug metadata with [CoroutineStackFrame] API
8  * and then reflectively instantiate exception of given type with original exception as a cause and
9  * sets new stacktrace for wrapping exception.
10  * Some frames may be missing due to tail-call elimination.
11  *
12  * Works only on JVM with enabled debug-mode.
13  */
recoverStackTracenull14 internal expect fun <E: Throwable> recoverStackTrace(exception: E, continuation: Continuation<*>): E
15 
16 /**
17  * initCause on JVM, nop on other platforms
18  */
19 @Suppress("EXTENSION_SHADOWED_BY_MEMBER")
20 internal expect fun Throwable.initCause(cause: Throwable)
21 
22 /**
23  * Tries to recover stacktrace for given [exception]. Used in non-suspendable points of awaiting.
24  * Stacktrace recovery tries to instantiate exception of given type with original exception as a cause.
25  * Wrapping exception will have proper stacktrace as it's instantiated in the right context.
26  *
27  * Works only on JVM with enabled debug-mode.
28  */
29 internal expect fun <E: Throwable> recoverStackTrace(exception: E): E
30 
31 // Name conflict with recoverStackTrace
32 @Suppress("NOTHING_TO_INLINE")
33 internal expect suspend inline fun recoverAndThrow(exception: Throwable): Nothing
34 
35 /**
36  * The opposite of [recoverStackTrace].
37  * It is guaranteed that `unwrap(recoverStackTrace(e)) === e`
38  */
39 @PublishedApi // Used from kotlinx-coroutines-test and reactor modules via suppress, not part of ABI
40 internal expect fun <E: Throwable> unwrap(exception: E): E
41 
42 internal expect class StackTraceElement
43 
44 internal expect interface CoroutineStackFrame {
45     public val callerFrame: CoroutineStackFrame?
46     public fun getStackTraceElement(): StackTraceElement?
47 }
48