mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 08:31:26 +00:00
Support 'handleException' operator in JVM backend
This commit is contained in:
@@ -25,7 +25,21 @@ fun box(): String {
|
||||
|
||||
java.lang.Thread.sleep(1000)
|
||||
|
||||
return result
|
||||
if (result != "OK") return "fail notOk"
|
||||
|
||||
val future2 = async<String>() {
|
||||
await(exception("OK"))
|
||||
"fail"
|
||||
}
|
||||
|
||||
try {
|
||||
future2.get()
|
||||
} catch (e: Exception) {
|
||||
if (e.cause!!.message != "OK") return "fail message: ${e.cause!!.message}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
return "No exception"
|
||||
}
|
||||
|
||||
fun <T> async(coroutine c: FutureController<T>.() -> Continuation<Unit>): CompletableFuture<T> {
|
||||
@@ -37,17 +51,12 @@ fun <T> async(coroutine c: FutureController<T>.() -> Continuation<Unit>): Comple
|
||||
class FutureController<T> {
|
||||
val future = CompletableFuture<T>()
|
||||
|
||||
|
||||
suspend fun <V> await(f: CompletableFuture<V>, machine: Continuation<V>) {
|
||||
f.whenComplete { value, throwable ->
|
||||
try {
|
||||
if (throwable == null)
|
||||
machine.resume(value)
|
||||
else
|
||||
machine.resumeWithException(throwable)
|
||||
} catch (e: Exception) {
|
||||
future.completeExceptionally(e)
|
||||
}
|
||||
if (throwable == null)
|
||||
machine.resume(value)
|
||||
else
|
||||
machine.resumeWithException(throwable)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +64,7 @@ class FutureController<T> {
|
||||
future.complete(value)
|
||||
}
|
||||
|
||||
fun handleException(t: Throwable, c: Continuation<Nothing>) {
|
||||
operator fun handleException(t: Throwable, c: Continuation<Nothing>) {
|
||||
future.completeExceptionally(t)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user