Generate state machine for named functions in their bodies

Inline functions aren't supported yet in the change

 #KT-17585 In Progress
This commit is contained in:
Denis Zharkov
2017-04-25 15:10:48 +03:00
parent 59d89a1ae3
commit c3a032ea0b
10 changed files with 345 additions and 133 deletions
@@ -35,7 +35,7 @@ abstract 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
@JvmField
protected var label: Int = if (completion != null) 0 else -1
var label: Int = if (completion != null) 0 else -1
private val _context: CoroutineContext? = completion?.context
@@ -71,3 +71,35 @@ abstract class CoroutineImpl(
throw IllegalStateException("create(Any?;Continuation) has not been overridden")
}
}
abstract class CoroutineImplForNamedFunction(
completion: Continuation<Any?>?
) : CoroutineImpl(0, completion), Continuation<Any?> {
companion object {
private const val LAST_BIT_MASK = 1 shl 31
}
@JvmField
var data: Any? = null
@JvmField
var exception: Throwable? = null
fun checkAndFlushLastBit(): Boolean {
if (label and LAST_BIT_MASK != 0) {
label -= LAST_BIT_MASK
return true
}
return false
}
override fun doResume(data: Any?, exception: Throwable?): Any? {
this.data = data
this.exception = exception
this.label = this.label or LAST_BIT_MASK
return doResume()
}
protected abstract fun doResume(): Any?
}