From 7837d736f7c8fb0bed800e89f683a6b387987fac Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 11 Jan 2017 20:22:49 +0300 Subject: [PATCH] Merge RestrictedCoroutineImpl into CoroutineImpl --- .../codegen/coroutines/CoroutineCodegen.kt | 2 +- .../CoroutineTransformationClassBuilder.kt | 4 +-- .../kotlin/resolve/jvm/AsmTypes.java | 1 - .../codegen/box/coroutines/illegalState.kt | 2 +- .../src/kotlin/jvm/internal/CoroutineImpl.kt | 35 +++++++------------ .../reference-public-api/kotlin-runtime.txt | 14 +++----- 6 files changed, 20 insertions(+), 38 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt index edb545447b9..ac4914c7953 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt @@ -187,7 +187,7 @@ class CoroutineCodegen( // .resume(Unit) StackValue.putUnitInstance(this) invokevirtual( - AsmTypes.RESTRICTED_COROUTINE_IMPL.internalName, + AsmTypes.COROUTINE_IMPL.internalName, CONTINUATION_RESUME_METHOD_NAME.identifier, Type.getMethodDescriptor(Type.VOID_TYPE, AsmTypes.OBJECT_TYPE), false diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformationClassBuilder.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformationClassBuilder.kt index 428c8b23e61..60a53921040 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformationClassBuilder.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformationClassBuilder.kt @@ -110,7 +110,7 @@ class CoroutineTransformerMethodVisitor( VarInsnNode(Opcodes.ALOAD, 0), FieldInsnNode( Opcodes.GETFIELD, - AsmTypes.RESTRICTED_COROUTINE_IMPL.internalName, + AsmTypes.COROUTINE_IMPL.internalName, COROUTINE_LABEL_FIELD_NAME, Type.INT_TYPE.descriptor ), TableSwitchInsnNode(0, @@ -295,7 +295,7 @@ class CoroutineTransformerMethodVisitor( VarInsnNode(Opcodes.ALOAD, 0), *withInstructionAdapter { iconst(id) }.toArray(), FieldInsnNode( - Opcodes.PUTFIELD, AsmTypes.RESTRICTED_COROUTINE_IMPL.internalName, COROUTINE_LABEL_FIELD_NAME, + Opcodes.PUTFIELD, AsmTypes.COROUTINE_IMPL.internalName, COROUTINE_LABEL_FIELD_NAME, Type.INT_TYPE.descriptor ) ) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/AsmTypes.java b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/AsmTypes.java index 2058ff2fdb4..ed1ed8b8b0e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/AsmTypes.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/AsmTypes.java @@ -41,7 +41,6 @@ public class AsmTypes { public static final Type MUTABLE_PROPERTY_REFERENCE1 = Type.getObjectType("kotlin/jvm/internal/MutablePropertyReference1"); public static final Type MUTABLE_PROPERTY_REFERENCE2 = Type.getObjectType("kotlin/jvm/internal/MutablePropertyReference2"); public static final Type COROUTINE_IMPL = Type.getObjectType("kotlin/jvm/internal/CoroutineImpl"); - public static final Type RESTRICTED_COROUTINE_IMPL = Type.getObjectType("kotlin/jvm/internal/RestrictedCoroutineImpl"); public static final Type COROUTINES_INTRINSICS_FILE_FACADE = Type.getObjectType("kotlin/coroutines/intrinsics/IntrinsicsKt"); public static final Type CONTINUATION = Type.getObjectType("kotlin/coroutines/Continuation"); diff --git a/compiler/testData/codegen/box/coroutines/illegalState.kt b/compiler/testData/codegen/box/coroutines/illegalState.kt index 6cefd4a0e2f..edc61314b27 100644 --- a/compiler/testData/codegen/box/coroutines/illegalState.kt +++ b/compiler/testData/codegen/box/coroutines/illegalState.kt @@ -15,7 +15,7 @@ fun builder1(c: suspend () -> Unit) { fun builder2(c: suspend () -> Unit) { val continuation = c.createCoroutine(EmptyContinuation) - val declaredField = continuation.javaClass.superclass.superclass.getDeclaredField("label") + val declaredField = continuation.javaClass.superclass.getDeclaredField("label") declaredField.setAccessible(true) declaredField.set(continuation, -3) continuation.resume(Unit) diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/CoroutineImpl.kt b/core/runtime.jvm/src/kotlin/jvm/internal/CoroutineImpl.kt index f7bf6d2c8ee..5b5cf2fd3da 100644 --- a/core/runtime.jvm/src/kotlin/jvm/internal/CoroutineImpl.kt +++ b/core/runtime.jvm/src/kotlin/jvm/internal/CoroutineImpl.kt @@ -19,8 +19,18 @@ package kotlin.jvm.internal import kotlin.coroutines.* import kotlin.coroutines.intrinsics.SUSPENDED_MARKER -abstract class CoroutineImpl : RestrictedCoroutineImpl, DispatchedContinuation { - private val _dispatcher: ContinuationDispatcher? +abstract class CoroutineImpl( + arity: Int, + @JvmField + protected var completion: Continuation? +) : DispatchedContinuation, Lambda(arity), Continuation { + + // label == -1 when coroutine cannot be started (it is just a factory object) or has already finished execution + // label == 0 in initial part of the coroutine + @JvmField + protected var label: Int = if (completion != null) 0 else -1 + + private val _dispatcher: ContinuationDispatcher? = (completion as? DispatchedContinuation<*>)?.dispatcher override val dispatcher: ContinuationDispatcher? get() = _dispatcher @@ -34,27 +44,6 @@ abstract class CoroutineImpl : RestrictedCoroutineImpl, DispatchedContinuation?) : super(arity, completion) { - _dispatcher = (completion as? DispatchedContinuation<*>)?.dispatcher - } -} - -abstract class RestrictedCoroutineImpl : Lambda, Continuation { - @JvmField - protected var completion: Continuation? - - // label == -1 when coroutine cannot be started (it is just a factory object) or has already finished execution - // label == 0 in initial part of the coroutine - @JvmField - protected var label: Int - - // this constructor is used to create a continuation instance for coroutine - constructor(arity: Int, completion: Continuation?) : super(arity) { - this.completion = completion - label = if (completion != null) 0 else -1 - } - override fun resume(value: Any?) { try { val result = doResume(value, null) diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-runtime.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-runtime.txt index dc201ae3a3b..4f671f7fca2 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-runtime.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-runtime.txt @@ -468,8 +468,11 @@ public class kotlin/jvm/internal/CollectionToArray { public static fun toArray (Ljava/util/Collection;[Ljava/lang/Object;)[Ljava/lang/Object; } -public abstract class kotlin/jvm/internal/CoroutineImpl : kotlin/jvm/internal/RestrictedCoroutineImpl, kotlin/jvm/internal/DispatchedContinuation { +public abstract class kotlin/jvm/internal/CoroutineImpl : kotlin/jvm/internal/Lambda, kotlin/coroutines/Continuation, kotlin/jvm/internal/DispatchedContinuation { + protected field completion Lkotlin/coroutines/Continuation; + protected field label I public fun (ILkotlin/coroutines/Continuation;)V + protected abstract fun doResume (Ljava/lang/Object;Ljava/lang/Throwable;)Ljava/lang/Object; public fun getDispatcher ()Lkotlin/coroutines/ContinuationDispatcher; public final fun getFacade ()Lkotlin/coroutines/Continuation; public fun resume (Ljava/lang/Object;)V @@ -867,15 +870,6 @@ public class kotlin/jvm/internal/ReflectionFactory { public fun renderLambdaToString (Lkotlin/jvm/internal/Lambda;)Ljava/lang/String; } -public abstract class kotlin/jvm/internal/RestrictedCoroutineImpl : kotlin/jvm/internal/Lambda, kotlin/coroutines/Continuation { - protected field completion Lkotlin/coroutines/Continuation; - protected field label I - public fun (ILkotlin/coroutines/Continuation;)V - protected abstract fun doResume (Ljava/lang/Object;Ljava/lang/Throwable;)Ljava/lang/Object; - public fun resume (Ljava/lang/Object;)V - public fun resumeWithException (Ljava/lang/Throwable;)V -} - public final class kotlin/jvm/internal/ShortCompanionObject { public static final field INSTANCE Lkotlin/jvm/internal/ShortCompanionObject; public static final field MAX_VALUE S