Support calling experimental coroutines API in JVM

The support is very limited, though

 #KT-25683 Fixed
This commit is contained in:
Denis Zharkov
2018-08-20 14:46:33 +03:00
parent 20c7a97bcd
commit ca39cc47c9
9 changed files with 250 additions and 57 deletions
@@ -1,9 +1,18 @@
// FILE: A.kt
// LANGUAGE_VERSION: 1.2
// TODO: Unmute when automatic conversion experimental <-> release will be implemented
// IGNORE_BACKEND: JVM, JS, NATIVE, JVM_IR, JS_IR
// IGNORE_BACKEND: JS, NATIVE, JVM_IR, JS_IR
import kotlin.coroutines.experimental.*
var callback: (() -> Unit)? = null
fun join() {
while (callback != null) {
val x = callback!!
callback = null
x()
}
}
fun builder1(c: suspend () -> String): String {
var res = "FAIL"
c.startCoroutine(object : Continuation<String> {
@@ -15,6 +24,7 @@ fun builder1(c: suspend () -> String): String {
throw exception
}
})
join()
return res
}
@@ -29,23 +39,31 @@ fun builder2(c: suspend String.() -> String): String {
throw exception
}
})
join()
return res
}
// FILE: B.kt
// LANGUAGE_VERSION: 1.3
import kotlin.coroutines.experimental.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
fun ok(continuation: Continuation<String>): Any? {
return "OK"
suspend fun ok(): String {
return o() + k()
}
suspend fun o(): String = suspendCoroutine { continuation ->
callback = { continuation.resume("O") }
}
suspend fun k(): String = suspendCoroutine { continuation ->
callback = { continuation.resume("K") }
}
fun box(): String {
// if (builder1(::ok) != "OK") return "FAIL 1"
// if (builder1 { cont: Continuation<String> -> "OK" } != "OK") return "FAIL 2"
// if (builder1(fun (cont: Continuation<String>): Any? = "OK") != "OK") return "FAIL 3"
//
// if (builder2 { cont: Continuation<String> -> this + "K" } != "OK") return "FAIL 5"
// if (builder2(fun String.(cont: Continuation<String>): Any? = this + "K") != "OK") return "FAIL 6"
if (builder1(::ok) != "OK") return "FAIL 1"
if (builder1 { ok() } != "OK") return "FAIL 2"
if (builder2 { this + k() } != "OK") return "FAIL 5"
return "OK"
}
@@ -1,9 +1,18 @@
// FILE: A.kt
// LANGUAGE_VERSION: 1.2
// TODO: Unmute when automatic conversion experimental <-> release will be implemented
// IGNORE_BACKEND: JVM, JS, NATIVE, JVM_IR, JS_IR
// IGNORE_BACKEND: JS, NATIVE, JVM_IR, JS_IR
import kotlin.coroutines.experimental.*
var callback: (() -> Unit)? = null
fun join() {
while (callback != null) {
val x = callback!!
callback = null
x()
}
}
fun (suspend () -> String).builder(): String {
var res = "FAIL"
startCoroutine(object : Continuation<String> {
@@ -15,20 +24,31 @@ fun (suspend () -> String).builder(): String {
throw exception
}
})
join()
return res
}
// FILE: B.kt
// LANGUAGE_VERSION: 1.3
import kotlin.coroutines.experimental.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
fun ok(continuation: Continuation<String>): Any? {
return "OK"
suspend fun ok(): String {
return o() + k()
}
suspend fun o(): String = suspendCoroutine { continuation ->
callback = { continuation.resume("O") }
}
suspend fun k(): String = suspendCoroutine { continuation ->
callback = { continuation.resume("K") }
}
fun box(): String {
// if ((::ok).builder() != "OK") return "FAIL 1"
// if (({ cont: Continuation<String> -> "OK" }).builder() != "OK") return "FAIL 2"
// if ((fun (cont: Continuation<String>): Any? = "OK").builder() != "OK") return "FAIL 3"
if ((::ok).builder() != "OK") return "FAIL 1"
if (suspend { ok() }.builder() != "OK") return "FAIL 2"
return "OK"
}
@@ -1,49 +1,42 @@
// FILE: A.kt
// LANGUAGE_VERSION: 1.2
// TODO: Unmute when automatic conversion experimental <-> release will be implemented
// IGNORE_BACKEND: JVM, JS, NATIVE, JVM_IR, JS_IR
// IGNORE_BACKEND: JS, NATIVE, JVM_IR, JS_IR
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
suspend fun dummy() = "OK"
suspend fun String.dummy() = this + "K"
suspend fun String.dummy(s: String) = this + s
class C {
suspend fun dummy() = "OK"
var callback: (() -> Unit)? = null
suspend fun dummy(): String = suspendCoroutine {
callback = { it.resume("OK") }
}
class WithNested {
class Nested {
suspend fun dummy() = "OK"
}
}
class WithInner {
inner class Inner {
suspend fun dummy() = "OK"
}
}
// FILE: B.kt
// LANGUAGE_VERSION: 1.3
import kotlin.coroutines.experimental.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
fun builder(x: suspend () -> Unit) {
x.startCoroutine(object : Continuation<Any?> {
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: SuccessOrFailure<Any?>) {
result.getOrThrow()
}
})
while (callback != null) {
val x = callback!!
callback = null
x()
}
}
fun box(): String {
// val continuation = object : Continuation<String> {
// override val context = EmptyCoroutineContext
// override fun resume(value: String) {
// }
// override fun resumeWithException(exception: Throwable) {
// throw exception
// }
// }
// if (dummy(continuation) != "OK") return "FAIL 1"
// if ("O".dummy(continuation) != "OK") return "FAIL 2"
// if ("O".dummy("K", continuation) != "OK") return "FAIL 3"
// if (C().dummy(continuation) != "OK") return "FAIL 4"
// if (WithNested.Nested().dummy(continuation) != "OK") return "FAIL 5"
// if (WithInner().Inner().dummy(continuation) != "OK") return "FAIL 6"
return "OK"
var res = "fail"
builder {
res = dummy()
}
return res
}