Added some tests on coroutines
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = try {
|
||||
f1()
|
||||
} catch (t: Throwable) {
|
||||
f2()
|
||||
} finally {
|
||||
s1()
|
||||
}
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
val x = try {
|
||||
f1()
|
||||
} catch (t: Throwable) {
|
||||
f2()
|
||||
} finally {
|
||||
s1()
|
||||
}
|
||||
result = x
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
try {
|
||||
result = f1()
|
||||
} catch (t: Throwable) {
|
||||
result = f2()
|
||||
} finally {
|
||||
s1()
|
||||
}
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
try {
|
||||
result = s1()
|
||||
} catch (t: Throwable) {
|
||||
result = f2()
|
||||
} finally {
|
||||
println("finally")
|
||||
}
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = f3(if (f1() > 100) s1() else f2(), 42)
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
if (f1() > 100)
|
||||
result = s1() + f2()
|
||||
else
|
||||
result = 42
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
inline suspend fun inline_s2(): Int {
|
||||
return 42
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = inline_s2()
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
inline suspend fun inline_s2(): Int {
|
||||
var x = s1()
|
||||
return x
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = inline_s2()
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
inline suspend fun inline_s2(): Int {
|
||||
var x = 0
|
||||
if (f1() > 0)
|
||||
x = s1()
|
||||
else x = f2()
|
||||
return x
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = inline_s2()
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
val x = try {
|
||||
s1()
|
||||
} catch (t: Throwable) {
|
||||
f2()
|
||||
}
|
||||
result = x
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = try {
|
||||
s1()
|
||||
} catch (t: Throwable) {
|
||||
f2()
|
||||
}
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun s2(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s2")
|
||||
x.resumeWithException(Error())
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = try {
|
||||
s2()
|
||||
} catch (t: Throwable) {
|
||||
f2()
|
||||
}
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun s2(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s2")
|
||||
x.resumeWithException(Error())
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
val x = try {
|
||||
s2()
|
||||
} catch (t: Throwable) {
|
||||
f2()
|
||||
}
|
||||
result = x
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun s2(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s2")
|
||||
x.resumeWithException(Error("Error"))
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = try {
|
||||
s2()
|
||||
} catch (t: Throwable) {
|
||||
val x = s1()
|
||||
println(t.message)
|
||||
x
|
||||
}
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun s2(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s2")
|
||||
x.resumeWithException(Error("Error"))
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun s3(value: Int): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s3")
|
||||
x.resume(value)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
while (s3(result) < 3)
|
||||
++result
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun s2(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s2")
|
||||
x.resumeWithException(Error("Error"))
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun s3(value: Int): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s3")
|
||||
x.resume(value)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun f3(x: Int, y: Int): Int {
|
||||
println("f3")
|
||||
return x + y
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
while (result < 3)
|
||||
result = s3(result) + 1
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun f1(): Int {
|
||||
println("f1")
|
||||
return 117
|
||||
}
|
||||
|
||||
fun f2(): Int {
|
||||
println("f2")
|
||||
return 1
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = f1() + s1() + f2()
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun s1(): Unit = suspendCoroutineOrReturn { x ->
|
||||
println("s1")
|
||||
x.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
inline suspend fun inline_s2(x: Int): Unit {
|
||||
println(x)
|
||||
s1()
|
||||
}
|
||||
|
||||
suspend fun s3(x: Int) {
|
||||
inline_s2(x)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
s3(117)
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
suspend fun suspendHere(): Int = suspendCoroutineOrReturn { x ->
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = suspendHere()
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resume(value: Any?) {}
|
||||
override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
}
|
||||
|
||||
class Controller {
|
||||
suspend fun suspendHere(): Int = suspendCoroutineOrReturn { x ->
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
}
|
||||
|
||||
fun builder(c: suspend Controller.() -> Unit) {
|
||||
c.startCoroutine(Controller(), EmptyContinuation)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var result = 0
|
||||
|
||||
builder {
|
||||
result = suspendHere()
|
||||
}
|
||||
|
||||
println(result)
|
||||
}
|
||||
Reference in New Issue
Block a user