• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     internal val parent: Job? get() = parentHandle?.parent
25 
26     override fun afterCompletion(state: Any?) {
27         // Resume in a cancellable way by default when resuming from another context
28         uCont.intercepted().resumeCancellableWith(recoverResult(state, uCont))
29     }
30 
afterResumenull31     override fun afterResume(state: Any?) {
32         // Resume direct because scope is already in the correct context
33         uCont.resumeWith(recoverResult(state, uCont))
34     }
35 }
36 
37 internal class ContextScope(context: CoroutineContext) : CoroutineScope {
38     override val coroutineContext: CoroutineContext = context
39     // CoroutineScope is used intentionally for user-friendly representation
toStringnull40     override fun toString(): String = "CoroutineScope(coroutineContext=$coroutineContext)"
41 }
42