Make createCoroutine return a safe continuation
#KT-15718 Fixed
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// WITH_COROUTINES
|
||||||
|
import kotlin.coroutines.experimental.*
|
||||||
|
import kotlin.coroutines.experimental.intrinsics.*
|
||||||
|
|
||||||
|
fun builder(c: suspend () -> Unit) {
|
||||||
|
val x = c.createCoroutine(EmptyContinuation)
|
||||||
|
|
||||||
|
x.resume(Unit)
|
||||||
|
x.resume(Unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
var result = ""
|
||||||
|
|
||||||
|
try {
|
||||||
|
builder {
|
||||||
|
result = "OK"
|
||||||
|
}
|
||||||
|
} catch (e: IllegalStateException) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
return "fail: $result"
|
||||||
|
}
|
||||||
@@ -15,9 +15,14 @@ fun builder1(c: suspend () -> Unit) {
|
|||||||
|
|
||||||
fun builder2(c: suspend () -> Unit) {
|
fun builder2(c: suspend () -> Unit) {
|
||||||
val continuation = c.createCoroutine(EmptyContinuation)
|
val continuation = c.createCoroutine(EmptyContinuation)
|
||||||
val declaredField = continuation.javaClass.superclass.getDeclaredField("label")
|
|
||||||
|
val delegateField = continuation.javaClass.getDeclaredField("delegate")
|
||||||
|
delegateField.setAccessible(true)
|
||||||
|
val originalContinuation = delegateField.get(continuation)
|
||||||
|
|
||||||
|
val declaredField = originalContinuation.javaClass.superclass.getDeclaredField("label")
|
||||||
declaredField.setAccessible(true)
|
declaredField.setAccessible(true)
|
||||||
declaredField.set(continuation, -3)
|
declaredField.set(originalContinuation, -3)
|
||||||
continuation.resume(Unit)
|
continuation.resume(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
@kotlin.Metadata
|
||||||
|
public final class CoroutineUtilKt {
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.experimental.Continuation
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.experimental.Continuation
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public final class CreateCoroutineSafeKt {
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||||
|
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public class EmptyContinuation {
|
||||||
|
public final static field Companion: EmptyContinuation.Companion
|
||||||
|
private final @org.jetbrains.annotations.NotNull field context: kotlin.coroutines.experimental.CoroutineContext
|
||||||
|
inner class EmptyContinuation/Companion
|
||||||
|
public @synthetic.kotlin.jvm.GeneratedByJvmOverloads method <init>(): void
|
||||||
|
public method <init>(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.experimental.CoroutineContext): void
|
||||||
|
public synthetic method <init>(p0: kotlin.coroutines.experimental.CoroutineContext, p1: int, p2: kotlin.jvm.internal.DefaultConstructorMarker): void
|
||||||
|
public @org.jetbrains.annotations.NotNull method getContext(): kotlin.coroutines.experimental.CoroutineContext
|
||||||
|
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
|
||||||
|
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public final static class EmptyContinuation/Companion {
|
||||||
|
inner class EmptyContinuation/Companion
|
||||||
|
private method <init>(): void
|
||||||
|
}
|
||||||
+6
@@ -4667,6 +4667,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("createCoroutineSafe.kt")
|
||||||
|
public void testCreateCoroutineSafe() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutineSafe.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("createCoroutinesOnManualInstances.kt")
|
@TestMetadata("createCoroutinesOnManualInstances.kt")
|
||||||
public void testCreateCoroutinesOnManualInstances() throws Exception {
|
public void testCreateCoroutinesOnManualInstances() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt");
|
||||||
|
|||||||
@@ -4667,6 +4667,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("createCoroutineSafe.kt")
|
||||||
|
public void testCreateCoroutineSafe() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutineSafe.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("createCoroutinesOnManualInstances.kt")
|
@TestMetadata("createCoroutinesOnManualInstances.kt")
|
||||||
public void testCreateCoroutinesOnManualInstances() throws Exception {
|
public void testCreateCoroutinesOnManualInstances() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt");
|
||||||
|
|||||||
@@ -28,7 +28,11 @@ import kotlin.coroutines.experimental.intrinsics.*
|
|||||||
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> = this.asDynamic()(receiver, completion, true)
|
): Continuation<Unit> =
|
||||||
|
SafeContinuation(
|
||||||
|
this.asDynamic()(receiver, completion, true),
|
||||||
|
COROUTINE_SUSPENDED
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts coroutine with receiver type [R] and result type [T].
|
* Starts coroutine with receiver type [R] and result type [T].
|
||||||
@@ -40,8 +44,7 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
|||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
) {
|
) {
|
||||||
val coroutine: Continuation<Any?> = this.asDynamic()(receiver, completion, true)
|
createCoroutine(receiver, completion).resume(Unit)
|
||||||
coroutine.resume(null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,7 +56,11 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
|||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
public fun <T> (suspend () -> T).createCoroutine(
|
public fun <T> (suspend () -> T).createCoroutine(
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> = this.asDynamic()(completion, true)
|
): Continuation<Unit> =
|
||||||
|
SafeContinuation(
|
||||||
|
this.asDynamic()(completion, true),
|
||||||
|
COROUTINE_SUSPENDED
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts coroutine without receiver and with result type [T].
|
* Starts coroutine without receiver and with result type [T].
|
||||||
@@ -64,8 +71,7 @@ public fun <T> (suspend () -> T).createCoroutine(
|
|||||||
public fun <T> (suspend () -> T).startCoroutine(
|
public fun <T> (suspend () -> T).startCoroutine(
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
) {
|
) {
|
||||||
val coroutine: Continuation<Any?> = this.asDynamic()(completion, true)
|
createCoroutine(completion).resume(Unit)
|
||||||
coroutine.resume(null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,11 +135,15 @@ private val UNDECIDED: Any? = Any()
|
|||||||
private val RESUMED: Any? = Any()
|
private val RESUMED: Any? = Any()
|
||||||
private class Fail(val exception: Throwable)
|
private class Fail(val exception: Throwable)
|
||||||
|
|
||||||
internal class SafeContinuation<in T> internal constructor(private val delegate: Continuation<T>) : Continuation<T> {
|
internal class SafeContinuation<in T>
|
||||||
|
@PublishedApi internal constructor(
|
||||||
|
private val delegate: Continuation<T>,
|
||||||
|
initialResult: Any? = UNDECIDED
|
||||||
|
) : Continuation<T> {
|
||||||
public override val context: CoroutineContext
|
public override val context: CoroutineContext
|
||||||
get() = delegate.context
|
get() = delegate.context
|
||||||
|
|
||||||
private var result: Any? = UNDECIDED
|
private var result: Any? = initialResult
|
||||||
|
|
||||||
override fun resume(value: T) {
|
override fun resume(value: T) {
|
||||||
when {
|
when {
|
||||||
|
|||||||
@@ -5352,6 +5352,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("createCoroutineSafe.kt")
|
||||||
|
public void testCreateCoroutineSafe() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutineSafe.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("createCoroutinesOnManualInstances.kt")
|
@TestMetadata("createCoroutinesOnManualInstances.kt")
|
||||||
public void testCreateCoroutinesOnManualInstances() throws Exception {
|
public void testCreateCoroutinesOnManualInstances() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt");
|
||||||
|
|||||||
@@ -36,14 +36,16 @@ import kotlin.coroutines.experimental.jvm.internal.interceptContinuationIfNeeded
|
|||||||
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> {
|
): Continuation<Unit> =
|
||||||
if (this !is CoroutineImpl) {
|
SafeContinuation(
|
||||||
return buildContinuationByInvokeCall(completion) {
|
if (this !is CoroutineImpl)
|
||||||
(this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
buildContinuationByInvokeCall(completion) {
|
||||||
}
|
(this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
||||||
}
|
}
|
||||||
return ((this as CoroutineImpl).create(receiver, completion) as CoroutineImpl).facade
|
else
|
||||||
}
|
((this as CoroutineImpl).create(receiver, completion) as CoroutineImpl).facade,
|
||||||
|
COROUTINE_SUSPENDED
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts coroutine with receiver type [R] and result type [T].
|
* Starts coroutine with receiver type [R] and result type [T].
|
||||||
@@ -69,14 +71,16 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
public fun <T> (suspend () -> T).createCoroutine(
|
public fun <T> (suspend () -> T).createCoroutine(
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> {
|
): Continuation<Unit> =
|
||||||
if (this !is CoroutineImpl) {
|
SafeContinuation(
|
||||||
return buildContinuationByInvokeCall(completion) {
|
if (this !is CoroutineImpl)
|
||||||
(this as Function1<Continuation<T>, Any?>).invoke(completion)
|
buildContinuationByInvokeCall(completion) {
|
||||||
}
|
(this as Function1<Continuation<T>, Any?>).invoke(completion)
|
||||||
}
|
}
|
||||||
return ((this as CoroutineImpl).create(completion) as CoroutineImpl).facade
|
else
|
||||||
}
|
((this as CoroutineImpl).create(completion) as CoroutineImpl).facade,
|
||||||
|
COROUTINE_SUSPENDED
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts coroutine without receiver and with result type [T].
|
* Starts coroutine without receiver and with result type [T].
|
||||||
@@ -147,12 +151,16 @@ private val RESUMED: Any? = Any()
|
|||||||
private class Fail(val exception: Throwable)
|
private class Fail(val exception: Throwable)
|
||||||
|
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal class SafeContinuation<in T> @PublishedApi internal constructor(private val delegate: Continuation<T>) : Continuation<T> {
|
internal class SafeContinuation<in T>
|
||||||
|
@PublishedApi internal constructor(
|
||||||
|
private val delegate: Continuation<T>,
|
||||||
|
initialResult: Any? = UNDECIDED
|
||||||
|
) : Continuation<T> {
|
||||||
public override val context: CoroutineContext
|
public override val context: CoroutineContext
|
||||||
get() = delegate.context
|
get() = delegate.context
|
||||||
|
|
||||||
@Volatile
|
@Volatile
|
||||||
private var result: Any? = UNDECIDED
|
private var result: Any? = initialResult
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@@ -197,9 +205,9 @@ internal class SafeContinuation<in T> @PublishedApi internal constructor(private
|
|||||||
result = this.result // reread volatile var
|
result = this.result // reread volatile var
|
||||||
}
|
}
|
||||||
when {
|
when {
|
||||||
result === RESUMED -> return COROUTINE_SUSPENDED // already called continuation, indicate SUSPENDED_MARKER upstream
|
result === RESUMED -> return COROUTINE_SUSPENDED // already called continuation, indicate COROUTINE_SUSPENDED upstream
|
||||||
result is Fail -> throw result.exception
|
result is Fail -> throw result.exception
|
||||||
else -> return result // either SUSPENDED_MARKER or data
|
else -> return result // either COROUTINE_SUSPENDED or data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user