Support new coroutine convention in JVM backend

This commit is contained in:
Denis Zharkov
2016-12-14 20:11:27 +03:00
committed by Stanislav Erokhin
parent 5a6b4a3224
commit 6649f64e9f
97 changed files with 1021 additions and 1373 deletions
+19 -25
View File
@@ -43,32 +43,26 @@ fun box(): String {
// LIBRARY CODE
fun <T> async(coroutine c: FutureController<T>.() -> Continuation<Unit>): CompletableFuture<T> {
val controller = FutureController<T>()
c(controller).resume(Unit)
return controller.future
}
class FutureController<T> {
fun <T> async(c: @Suspend() (() -> T)): CompletableFuture<T> {
val future = CompletableFuture<T>()
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
f.whenComplete { value, throwable ->
if (throwable == null)
machine.resume(value)
else
machine.resumeWithException(throwable)
c.startCoroutine(object : Continuation<T> {
override fun resume(data: T) {
future.complete(data)
}
Suspend
}
operator fun handleResult(value: T, c: Continuation<Nothing>) {
future.complete(value)
}
fun handleException(t: Throwable, c: Continuation<Nothing>) {
future.completeExceptionally(t)
}
override fun resumeWithException(exception: Throwable) {
future.completeExceptionally(exception)
}
})
return future
}
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
f.whenComplete { value, throwable ->
if (throwable == null)
machine.resume(value)
else
machine.resumeWithException(throwable)
}
SUSPENDED
}
+19 -24
View File
@@ -40,31 +40,26 @@ fun box(): String {
return "No exception"
}
fun <T> async(coroutine c: FutureController<T>.() -> Continuation<Unit>): CompletableFuture<T> {
val controller = FutureController<T>()
c(controller).resume(Unit)
return controller.future
}
class FutureController<T> {
fun <T> async(c: @Suspend() (() -> T)): CompletableFuture<T> {
val future = CompletableFuture<T>()
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
f.whenComplete { value, throwable ->
if (throwable == null)
machine.resume(value)
else
machine.resumeWithException(throwable)
c.startContinuation(object : Continuation<T> {
override fun resume(data: T) {
future.complete(data)
}
Suspend
}
operator fun handleResult(value: T, c: Continuation<Nothing>) {
future.complete(value)
}
operator fun handleException(t: Throwable, c: Continuation<Nothing>) {
future.completeExceptionally(t)
}
override fun resumeWithException(exception: Throwable) {
future.completeExceptionally(exception)
}
})
return future
}
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
f.whenComplete { value, throwable ->
if (throwable == null)
machine.resume(value)
else
machine.resumeWithException(throwable)
}
SUSPENDED
}