JS: optimize variable representation in coroutines

Don't convert local variables to fields of coroutine object
when variable is both used and defined in a single block.
This commit is contained in:
Alexey Andreev
2017-09-15 13:20:35 +03:00
parent b852f73dd2
commit 3b2d634cea
6 changed files with 192 additions and 14 deletions
@@ -0,0 +1,49 @@
// EXPECTED_REACHABLE_NODES: 1124
// DECLARES_VARIABLE: function=doResume name=k
// PROPERTY_READ_COUNT: name=local$o count=1
// PROPERTY_WRITE_COUNT: name=local$o count=2
import kotlin.coroutines.experimental.*
var next: () -> Unit = {}
var complete = false
var log = ""
suspend fun foo(x: String): String = suspendCoroutine { continuation ->
log += "[$x]"
next = { continuation.resume(x) }
}
fun build(x: suspend () -> Unit) {
next = {
x.startCoroutine(object : Continuation<Unit> {
override val context = EmptyCoroutineContext
override fun resume(x: Unit) {
complete = true
}
override fun resumeWithException(x: Throwable) {
complete = true
}
})
}
}
fun box(): String {
build {
val o = foo("O")
log += "-"
val k = foo("K")
log += ":"
log += "{$o$k}"
}
while (!complete) {
next()
log += "#"
}
if (log != "[O]#-[K]#:{OK}#") return "fail: $log"
return "OK"
}