Files
kotlin-fork/libraries/stdlib/js/irRuntime/coroutinesInternal.kt
T
Roman Artemev c62e4b4fcf [JS IR BE] Support coroutines
* Move FinallyBlockLowering to common part
* Fix catching of dynamic exception
* Fix bridges for suspend functions
* Disable explicit cast to Unit
* Run lowering per module
* Update some test data
2018-08-08 18:33:39 +03:00

70 lines
2.4 KiB
Kotlin

/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
import kotlin.coroutines.experimental.*
internal fun <T> getContinuation(): Continuation<T> { throw Exception("Implemented as intrinsic") }
// Do we really need this intrinsic in JS?
internal suspend fun <T> returnIfSuspended(@Suppress("UNUSED_PARAMETER") argument: Any?): T {
throw Exception("Implemented as intrinsic")
}
fun <T> normalizeContinuation(continuation: Continuation<T>): Continuation<T> =
(continuation as? CoroutineImpl)?.facade ?: continuation
internal fun <T> interceptContinuationIfNeeded(
context: CoroutineContext,
continuation: Continuation<T>
) = context[ContinuationInterceptor]?.interceptContinuation(continuation) ?: continuation
@SinceKotlin("1.2")
@Suppress("WRONG_MODIFIER_TARGET")
public suspend val coroutineContext: CoroutineContext
get() {
throw Exception("Implemented as intrinsic")
}
internal abstract class CoroutineImpl(private val completion: Continuation<Any?>) : Continuation<Any?> {
protected var exceptionState = 0
protected var label: Int = 0
protected var pendingException: dynamic = null
public override val context: CoroutineContext get() = completion?.context
val facade: Continuation<Any?> get() {
return if (context != null) interceptContinuationIfNeeded(context, this)
else this
}
override fun resume(value: Any?) {
doResumeWrapper(value, null)
}
override fun resumeWithException(exception: Throwable) {
// TODO: once we have arrays working refact it with exception table
label = exceptionState
pendingException = exception
doResumeWrapper(null, exception)
}
protected fun doResumeWrapper(data: Any?, exception: Throwable?) {
processBareContinuationResume(completion) { doResume(data, exception) }
}
protected abstract fun doResume(data: Any?, exception: Throwable?): Any?
open fun create(completion: Continuation<*>): Continuation<Unit> {
throw IllegalStateException("create(Continuation) has not been overridden")
}
open fun create(value: Any?, completion: Continuation<*>): Continuation<Unit> {
throw IllegalStateException("create(Any?;Continuation) has not been overridden")
}
}