JS: coroutines: fix translation of try/finally block without catch

This commit is contained in:
Alexey Andreev
2016-11-09 17:39:44 +03:00
parent 0015f7518e
commit 2d315c3df8
5 changed files with 150 additions and 18 deletions
@@ -0,0 +1,48 @@
// WITH_RUNTIME
class Controller {
var result = ""
suspend fun <T> suspendAndLog(value: T, c: Continuation<T>) {
result += "suspend($value);"
c.resume(value)
}
suspend fun suspendLogAndThrow(exception: Throwable, c: Continuation<Nothing>) {
result += "throw(${exception.message});"
c.resumeWithException(exception)
}
operator fun handleException(exception: Throwable, c: Continuation<Nothing>) {
result += "caught(${exception.message});"
}
}
fun builder(coroutine c: Controller.() -> Continuation<Unit>): String {
val controller = Controller()
c(controller).resume(Unit)
return controller.result
}
fun box(): String {
val value = builder {
try {
try {
suspendAndLog("1")
suspendLogAndThrow(RuntimeException("exception"))
}
catch (e: RuntimeException) {
suspendAndLog("caught")
suspendLogAndThrow(RuntimeException("fromCatch"))
}
} finally {
suspendAndLog("finally")
}
suspendAndLog("ignore")
}
if (value != "suspend(1);throw(exception);suspend(caught);throw(fromCatch);suspend(finally);caught(fromCatch);") {
return "fail: $value"
}
return "OK"
}