1 /* 2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.internal 6 7 import kotlinx.coroutines.* 8 import kotlin.coroutines.* 9 import kotlin.jvm.* 10 11 /** 12 * This is a coroutine instance that is created by [coroutineScope] builder. 13 */ 14 internal open class ScopeCoroutine<in T>( 15 context: CoroutineContext, 16 @JvmField val uCont: Continuation<T> // unintercepted continuation 17 ) : AbstractCoroutine<T>(context, true), CoroutineStackFrame { 18 final override val callerFrame: CoroutineStackFrame? get() = uCont as CoroutineStackFrame? getStackTraceElementnull19 final override fun getStackTraceElement(): StackTraceElement? = null 20 final override val isScopedCoroutine: Boolean get() = true 21 22 override val defaultResumeMode: Int get() = MODE_DIRECT 23 24 internal val parent: Job? get() = parentContext[Job] 25 26 @Suppress("UNCHECKED_CAST") 27 override fun afterCompletionInternal(state: Any?, mode: Int) { 28 if (state is CompletedExceptionally) { 29 val exception = if (mode == MODE_IGNORE) state.cause else recoverStackTrace(state.cause, uCont) 30 uCont.resumeUninterceptedWithExceptionMode(exception, mode) 31 } else { 32 uCont.resumeUninterceptedMode(state as T, mode) 33 } 34 } 35 } 36 tryRecovernull37internal fun AbstractCoroutine<*>.tryRecover(exception: Throwable): Throwable { 38 val cont = (this as? ScopeCoroutine<*>)?.uCont ?: return exception 39 return recoverStackTrace(exception, cont) 40 } 41 42 internal class ContextScope(context: CoroutineContext) : CoroutineScope { 43 override val coroutineContext: CoroutineContext = context 44 } 45