1 /* 2 * Copyright 2016-2021 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.coroutines.intrinsics.* 10 import kotlin.jvm.* 11 12 /** 13 * This is a coroutine instance that is created by [coroutineScope] builder. 14 */ 15 internal open class ScopeCoroutine<in T>( 16 context: CoroutineContext, 17 @JvmField val uCont: Continuation<T> // unintercepted continuation 18 ) : AbstractCoroutine<T>(context, true, true), CoroutineStackFrame { 19 20 final override val callerFrame: CoroutineStackFrame? get() = uCont as? CoroutineStackFrame getStackTraceElementnull21 final override fun getStackTraceElement(): StackTraceElement? = null 22 23 final override val isScopedCoroutine: Boolean get() = true 24 25 override fun afterCompletion(state: Any?) { 26 // Resume in a cancellable way by default when resuming from another context 27 uCont.intercepted().resumeCancellableWith(recoverResult(state, uCont)) 28 } 29 afterResumenull30 override fun afterResume(state: Any?) { 31 // Resume direct because scope is already in the correct context 32 uCont.resumeWith(recoverResult(state, uCont)) 33 } 34 } 35 36 internal class ContextScope(context: CoroutineContext) : CoroutineScope { 37 override val coroutineContext: CoroutineContext = context 38 // CoroutineScope is used intentionally for user-friendly representation toStringnull39 override fun toString(): String = "CoroutineScope(coroutineContext=$coroutineContext)" 40 } 41