Coroutines lowering

This commit is contained in:
Igor Chevdar
2017-04-27 17:04:03 +03:00
parent 7f9c5e5c8b
commit bd6a091088
16 changed files with 1348 additions and 68 deletions
@@ -3,6 +3,14 @@ package konan.internal
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
@Intrinsic
@PublishedApi
internal fun <T> getContinuation(): Continuation<T> = throw AssertionError("Call to getContinuation should've been lowered")
@Intrinsic
@PublishedApi
internal fun <T> returnIfSuspended(value: Any?): T = throw AssertionError("Call to returnIfSuspended should've been lowered")
// Single-threaded continuation.
class SafeContinuation<in T>
constructor(
@@ -67,6 +75,7 @@ internal fun <T> interceptContinuationIfNeeded(
* @suppress
*/
@ExportForCompiler
@PublishedApi
internal fun <T> normalizeContinuation(continuation: Continuation<T>): Continuation<T> =
(continuation as? CoroutineImpl)?.facade ?: continuation
@@ -80,7 +89,8 @@ abstract internal class CoroutineImpl(
// label == -1 when coroutine cannot be started (it is just a factory object) or has already finished execution
// label == 0 in initial part of the coroutine
protected var label: Int = if (completion != null) 0L else -1L
// TODO: use IntPtr.
protected var label: Long = if (completion != null) 0L else -1L
private val _context: CoroutineContext? = completion?.context
@@ -19,8 +19,7 @@ package kotlin.coroutines.experimental.intrinsics
import kotlin.coroutines.experimental.Continuation
import kotlin.coroutines.experimental.CoroutineContext
import kotlin.coroutines.experimental.processBareContinuationResume
import konan.internal.interceptContinuationIfNeeded
import konan.internal.CoroutineImpl
import konan.internal.*
/**
* Obtains the current continuation instance inside suspend functions and either suspend
@@ -40,7 +39,8 @@ import konan.internal.CoroutineImpl
* continuation instance.
*/
@kotlin.internal.InlineOnly
public inline suspend fun <T> suspendCoroutineOrReturn(crossinline block: (Continuation<T>) -> Any?): T = throw Error("Should've been intrinsified")
public inline suspend fun <T> suspendCoroutineOrReturn(crossinline block: (Continuation<T>) -> Any?): T =
returnIfSuspended<T>(block(normalizeContinuation<T>(getContinuation<T>())))
/**
* This value is used as a return value of [suspendCoroutineOrReturn] `block` argument to state that