mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 08:31:26 +00:00
Implement basic support for coroutines in JVM backend
This commit is contained in:
81
compiler/testData/codegen/java8/box/async.kt
vendored
Normal file
81
compiler/testData/codegen/java8/box/async.kt
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
fun foo(): CompletableFuture<String> = CompletableFuture.supplyAsync { "foo" }
|
||||
fun bar(v: String): CompletableFuture<String> = CompletableFuture.supplyAsync { "bar with $v" }
|
||||
fun exception(v: String): CompletableFuture<String> = CompletableFuture.supplyAsync { throw RuntimeException(v) }
|
||||
|
||||
fun foobar(x: String, y: String) = x + y
|
||||
|
||||
fun box(): String {
|
||||
var result = ""
|
||||
fun log(x: String) {
|
||||
synchronized(result) {
|
||||
if (result.isNotEmpty()) result += "\n"
|
||||
result += x
|
||||
}
|
||||
}
|
||||
|
||||
val future = async<String> {
|
||||
log("start")
|
||||
val x = await(foo())
|
||||
log("got '$x'")
|
||||
val y = foobar("123 ", await(bar(x)))
|
||||
log("got '$y' after '$x'")
|
||||
y
|
||||
}
|
||||
|
||||
future.whenComplete { value, t ->
|
||||
log("completed with '$value'")
|
||||
}
|
||||
|
||||
future.join()
|
||||
java.lang.Thread.sleep(1000)
|
||||
|
||||
val readResult = synchronized(result) {
|
||||
result
|
||||
}
|
||||
|
||||
val expectedResult =
|
||||
"""
|
||||
|start
|
||||
|got 'foo'
|
||||
|got '123 bar with foo' after 'foo'
|
||||
|completed with '123 bar with foo'""".trimMargin().trim('\n', ' ')
|
||||
|
||||
if (expectedResult != readResult) return readResult
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
// 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> {
|
||||
val future = CompletableFuture<T>()
|
||||
|
||||
|
||||
suspend fun <V> await(f: CompletableFuture<V>, machine: Continuation<V>) {
|
||||
f.whenComplete { value, throwable ->
|
||||
if (throwable == null)
|
||||
machine.resume(value)
|
||||
else
|
||||
machine.resumeWithException(throwable)
|
||||
}
|
||||
}
|
||||
|
||||
fun handleResult(value: T, c: Continuation<Nothing>) {
|
||||
future.complete(value)
|
||||
}
|
||||
|
||||
fun handleException(t: Throwable, c: Continuation<Nothing>) {
|
||||
future.completeExceptionally(t)
|
||||
}
|
||||
}
|
||||
61
compiler/testData/codegen/java8/box/asyncException.kt
vendored
Normal file
61
compiler/testData/codegen/java8/box/asyncException.kt
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
fun exception(v: String): CompletableFuture<String> = CompletableFuture.supplyAsync { throw RuntimeException(v) }
|
||||
|
||||
fun foobar(x: String, y: String) = x + y
|
||||
|
||||
fun box(): String {
|
||||
var result = ""
|
||||
|
||||
val future = async<String>() {
|
||||
try {
|
||||
await(exception("OK"))
|
||||
} catch (e: Exception) {
|
||||
result = e.cause?.message!!
|
||||
}
|
||||
"56"
|
||||
}
|
||||
|
||||
future.join()
|
||||
|
||||
if (future.get() != "56") return "fail: ${future.get()}"
|
||||
|
||||
java.lang.Thread.sleep(1000)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
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> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun handleResult(value: T, c: Continuation<Nothing>) {
|
||||
future.complete(value)
|
||||
}
|
||||
|
||||
fun handleException(t: Throwable, c: Continuation<Nothing>) {
|
||||
future.completeExceptionally(t)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user