JS: coroutines: fixes after code review

This commit is contained in:
Alexey Andreev
2016-11-16 12:13:30 +03:00
parent 43525abda3
commit e2dc7ba37e
10 changed files with 156 additions and 85 deletions
@@ -38,13 +38,14 @@ fun box(): String {
}
result += "ignore"
}
result += "*"
}
finally {
result += "finally"
}
result += "."
}
if (value != "AC!ED!@finally.") return "fail: $value"
if (value != "AC!ED!@*finally.") return "fail: $value"
return "OK"
}
@@ -0,0 +1,28 @@
// WITH_RUNTIME
class Controller {
var result = ""
suspend fun <T> suspendWithResult(value: T, c: Continuation<T>) {
c.resume(value)
}
}
fun builder(coroutine c: Controller.() -> Continuation<Unit>): String {
val controller = Controller()
c(controller).resume(Unit)
return controller.result
}
fun box(): String {
val value = builder {
for (x in listOf("O", "$", "K")) {
if (x == "$") continue
result += suspendWithResult(x)
}
result += "."
}
if (value != "OK.") return "fail: suspend in for body: $value"
return "OK"
}
@@ -0,0 +1,34 @@
// WITH_RUNTIME
// TODO: fix bug in JVM backend and remove this directive
// TARGET_BACKEND: JS
class Controller {
var result = ""
suspend fun <T> suspendWithResult(value: T, c: Continuation<T>) {
result += "["
c.resume(value)
}
}
fun builder(coroutine c: Controller.() -> Continuation<Unit>): String {
val controller = Controller()
c(controller).resume(Unit)
return controller.result
}
fun box(): String {
var value = builder {
for (v in listOf("A", "B", "C")) {
when (v) {
"A" -> result += "A;"
"B" -> result += suspendWithResult(v) + "]"
else -> result += suspendWithResult(v) + "]!"
}
}
}
if (value != "A;[B][C]!") return "fail: suspend as if condition: $value"
return "OK"
}