Skip to content

Commit aa461cf

Browse files
committed
Minimize cut-and-pasted code between JS and JVM
1 parent 3145290 commit aa461cf

File tree

105 files changed

+1281
-3590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1281
-3590
lines changed

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/AbstractContinuation.kt renamed to common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/AbstractContinuation.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
package kotlinx.coroutines.experimental
1818

19-
import kotlinx.atomicfu.atomic
20-
import kotlinx.atomicfu.loop
21-
import kotlin.coroutines.experimental.Continuation
22-
import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED
19+
import kotlinx.atomicfu.*
20+
import kotlin.coroutines.experimental.*
21+
import kotlin.coroutines.experimental.intrinsics.*
2322

2423
private const val UNDECIDED = 0
2524
private const val SUSPENDED = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016-2017 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// NOTE: We are defining them in a special internalAnnotations package because they would break
18+
// user code that uses kotlinx.coroutines library otherwise, see https://youtrack.jetbrains.com/issue/KT-23727
19+
package kotlinx.coroutines.experimental.internalAnnotations
20+
21+
@Target(AnnotationTarget.FILE)
22+
internal expect annotation class JvmName(val name: String)
23+
24+
@Target(AnnotationTarget.FILE)
25+
internal expect annotation class JvmMultifileClass()
26+
27+
internal expect annotation class JvmField()
28+
29+
internal expect annotation class Volatile()

js/kotlinx-coroutines-core-js/src/main/kotlin/kotlinx/coroutines/experimental/Builders.kt renamed to common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/Builders.common.kt

+54-12
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
// :todo: Remove after transition to Kotlin 1.2.30+
18-
@file:Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
17+
@file:JvmMultifileClass
18+
@file:JvmName("BuildersKt")
1919

2020
package kotlinx.coroutines.experimental
2121

22+
import kotlinx.coroutines.experimental.internalAnnotations.*
2223
import kotlinx.coroutines.experimental.intrinsics.*
2324
import kotlin.coroutines.experimental.*
2425
import kotlin.coroutines.experimental.intrinsics.*
2526

27+
// --------------- basic coroutine builders ---------------
28+
2629
/**
2730
* Launches new coroutine without blocking current thread and returns a reference to the coroutine as a [Job].
2831
* The coroutine is cancelled when the resulting job is [cancelled][Job.cancel].
@@ -54,7 +57,7 @@ import kotlin.coroutines.experimental.intrinsics.*
5457
* @param onCompletion optional completion handler for the coroutine (see [Job.invokeOnCompletion]).
5558
* @param block the coroutine code.
5659
*/
57-
public actual fun launch(
60+
public fun launch(
5861
context: CoroutineContext = DefaultDispatcher,
5962
start: CoroutineStart = CoroutineStart.DEFAULT,
6063
parent: Job? = null,
@@ -69,6 +72,31 @@ public actual fun launch(
6972
coroutine.start(start, coroutine, block)
7073
return coroutine
7174
}
75+
/** @suppress **Deprecated**: Binary compatibility */
76+
@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
77+
public fun launch(
78+
context: CoroutineContext = DefaultDispatcher,
79+
start: CoroutineStart = CoroutineStart.DEFAULT,
80+
parent: Job? = null,
81+
block: suspend CoroutineScope.() -> Unit
82+
): Job = launch(context, start, parent, block = block)
83+
84+
/** @suppress **Deprecated**: Binary compatibility */
85+
@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
86+
public fun launch(
87+
context: CoroutineContext = DefaultDispatcher,
88+
start: CoroutineStart = CoroutineStart.DEFAULT,
89+
block: suspend CoroutineScope.() -> Unit
90+
): Job =
91+
launch(context, start, block = block)
92+
93+
/**
94+
* @suppress **Deprecated**: Use `start = CoroutineStart.XXX` parameter
95+
*/
96+
@Deprecated(message = "Use `start = CoroutineStart.XXX` parameter",
97+
replaceWith = ReplaceWith("launch(context, if (start) CoroutineStart.DEFAULT else CoroutineStart.LAZY, block)"))
98+
public fun launch(context: CoroutineContext, start: Boolean, block: suspend CoroutineScope.() -> Unit): Job =
99+
launch(context, if (start) CoroutineStart.DEFAULT else CoroutineStart.LAZY, block = block)
72100

73101
/**
74102
* Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns
@@ -86,7 +114,7 @@ public actual fun launch(
86114
* Other options can be specified via `start` parameter. See [CoroutineStart] for details.
87115
* A value of [CoroutineStart.LAZY] is not supported and produces [IllegalArgumentException].
88116
*/
89-
public actual suspend fun <T> withContext(
117+
public suspend fun <T> withContext(
90118
context: CoroutineContext,
91119
start: CoroutineStart = CoroutineStart.DEFAULT,
92120
block: suspend () -> T
@@ -111,17 +139,34 @@ public actual suspend fun <T> withContext(
111139
val completion = RunCompletion(
112140
context = newContext,
113141
delegate = cont,
114-
resumeMode = if (start == CoroutineStart.ATOMIC) MODE_ATOMIC_DEFAULT else MODE_CANCELLABLE)
142+
resumeMode = if (start == CoroutineStart.ATOMIC) MODE_ATOMIC_DEFAULT else MODE_CANCELLABLE
143+
)
115144
completion.initParentJobInternal(newContext[Job]) // attach to job
145+
@Suppress("DEPRECATION")
116146
start(block, completion)
117147
completion.getResult()
118148
}
119149

150+
/** @suppress **Deprecated**: Renamed to [withContext]. */
151+
@Deprecated(message = "Renamed to `withContext`", level=DeprecationLevel.WARNING,
152+
replaceWith = ReplaceWith("withContext(context, start, block)"))
153+
public suspend fun <T> run(
154+
context: CoroutineContext,
155+
start: CoroutineStart = CoroutineStart.DEFAULT,
156+
block: suspend () -> T
157+
): T =
158+
withContext(context, start, block)
159+
160+
/** @suppress **Deprecated** */
161+
@Deprecated(message = "It is here for binary compatibility only", level=DeprecationLevel.HIDDEN)
162+
public suspend fun <T> run(context: CoroutineContext, block: suspend () -> T): T =
163+
withContext(context, start = CoroutineStart.ATOMIC, block = block)
164+
120165
// --------------- implementation ---------------
121166

122167
private open class StandaloneCoroutine(
123-
private val parentContext: CoroutineContext,
124-
active: Boolean
168+
private val parentContext: CoroutineContext,
169+
active: Boolean
125170
) : AbstractCoroutine<Unit>(parentContext, active) {
126171
override fun hasOnFinishingHandler(update: Any?) = update is CompletedExceptionally
127172
override fun onFinishingInternal(update: Any?) {
@@ -131,8 +176,8 @@ private open class StandaloneCoroutine(
131176
}
132177

133178
private class LazyStandaloneCoroutine(
134-
parentContext: CoroutineContext,
135-
private val block: suspend CoroutineScope.() -> Unit
179+
parentContext: CoroutineContext,
180+
private val block: suspend CoroutineScope.() -> Unit
136181
) : StandaloneCoroutine(parentContext, active = false) {
137182
override fun onStart() {
138183
block.startCoroutineCancellable(this, this)
@@ -144,12 +189,9 @@ private class RunContinuationDirect<in T>(
144189
continuation: Continuation<T>
145190
) : Continuation<T> by continuation
146191

147-
148192
@Suppress("UNCHECKED_CAST")
149193
private class RunCompletion<in T>(
150194
override val context: CoroutineContext,
151195
delegate: Continuation<T>,
152196
resumeMode: Int
153197
) : AbstractContinuation<T>(delegate, resumeMode)
154-
155-

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CancellableContinuation.kt renamed to common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/CancellableContinuation.kt

+26-26
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
// :todo: Remove after transition to Kotlin 1.2.30+
18-
@file:Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
19-
2017
package kotlinx.coroutines.experimental
2118

22-
import kotlinx.coroutines.experimental.internal.LockFreeLinkedListNode
23-
import kotlin.coroutines.experimental.Continuation
24-
import kotlin.coroutines.experimental.CoroutineContext
25-
import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn
26-
import kotlin.coroutines.experimental.suspendCoroutine
19+
import kotlinx.coroutines.experimental.internal.*
20+
import kotlinx.coroutines.experimental.internalAnnotations.*
21+
import kotlin.coroutines.experimental.*
22+
import kotlin.coroutines.experimental.intrinsics.*
2723

2824
// --------------- cancellable continuations ---------------
2925

@@ -61,24 +57,24 @@ import kotlin.coroutines.experimental.suspendCoroutine
6157
*
6258
* ```
6359
*/
64-
public actual interface CancellableContinuation<in T> : Continuation<T>, Job {
60+
public interface CancellableContinuation<in T> : Continuation<T>, Job {
6561
/**
6662
* Returns `true` when this continuation is active -- it has not completed or cancelled yet.
6763
*/
68-
public actual override val isActive: Boolean
64+
public override val isActive: Boolean
6965

7066
/**
7167
* Returns `true` when this continuation has completed for any reason. A continuation
7268
* that was cancelled is also considered complete.
7369
*/
74-
public actual override val isCompleted: Boolean
70+
public override val isCompleted: Boolean
7571

7672
/**
7773
* Returns `true` if this continuation was [cancelled][cancel].
7874
*
7975
* It implies that [isActive] is `false` and [isCompleted] is `true`.
8076
*/
81-
public actual override val isCancelled: Boolean
77+
public override val isCancelled: Boolean
8278

8379
/**
8480
* Tries to resume this continuation with a given value and returns non-null object token if it was successful,
@@ -90,7 +86,7 @@ public actual interface CancellableContinuation<in T> : Continuation<T>, Job {
9086
*
9187
* @suppress **This is unstable API and it is subject to change.**
9288
*/
93-
public actual fun tryResume(value: T, idempotent: Any? = null): Any?
89+
public fun tryResume(value: T, idempotent: Any? = null): Any?
9490

9591
/**
9692
* Tries to resume this continuation with a given exception and returns non-null object token if it was successful,
@@ -106,20 +102,20 @@ public actual interface CancellableContinuation<in T> : Continuation<T>, Job {
106102
*
107103
* @suppress **This is unstable API and it is subject to change.**
108104
*/
109-
public actual fun completeResume(token: Any)
105+
public fun completeResume(token: Any)
110106

111107
/**
112108
* Makes this continuation cancellable. Use it with `holdCancellability` optional parameter to
113109
* [suspendCancellableCoroutine] function. It throws [IllegalStateException] if invoked more than once.
114110
*/
115-
public actual fun initCancellability()
111+
public fun initCancellability()
116112

117113
/**
118114
* Cancels this continuation with an optional cancellation [cause]. The result is `true` if this continuation was
119115
* cancelled as a result of this invocation and `false` otherwise.
120116
*/
121117
@Suppress("DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE")
122-
public actual override fun cancel(cause: Throwable? = null): Boolean
118+
public override fun cancel(cause: Throwable? = null): Boolean
123119

124120
/**
125121
* Registers handler that is **synchronously** invoked once on completion of this continuation.
@@ -135,23 +131,23 @@ public actual interface CancellableContinuation<in T> : Continuation<T>, Job {
135131
* Installed [handler] should not throw any exceptions. If it does, they will get caught,
136132
* wrapped into [CompletionHandlerException], and rethrown, potentially causing crash of unrelated code.
137133
*/
138-
public actual override fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle
134+
public override fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle
139135

140136
/**
141137
* Resumes this continuation with a given [value] in the invoker thread without going though
142138
* [dispatch][CoroutineDispatcher.dispatch] function of the [CoroutineDispatcher] in the [context].
143139
* This function is designed to be used only by the [CoroutineDispatcher] implementations themselves.
144140
* **It should not be used in general code**.
145141
*/
146-
public actual fun CoroutineDispatcher.resumeUndispatched(value: T)
142+
public fun CoroutineDispatcher.resumeUndispatched(value: T)
147143

148144
/**
149145
* Resumes this continuation with a given [exception] in the invoker thread without going though
150146
* [dispatch][CoroutineDispatcher.dispatch] function of the [CoroutineDispatcher] in the [context].
151147
* This function is designed to be used only by the [CoroutineDispatcher] implementations themselves.
152148
* **It should not be used in general code**.
153149
*/
154-
public actual fun CoroutineDispatcher.resumeUndispatchedWithException(exception: Throwable)
150+
public fun CoroutineDispatcher.resumeUndispatchedWithException(exception: Throwable)
155151
}
156152

157153
/**
@@ -163,7 +159,7 @@ public actual interface CancellableContinuation<in T> : Continuation<T>, Job {
163159
*
164160
* See [suspendAtomicCancellableCoroutine] for suspending functions that need *atomic cancellation*.
165161
*/
166-
public actual inline suspend fun <T> suspendCancellableCoroutine(
162+
public suspend inline fun <T> suspendCancellableCoroutine(
167163
holdCancellability: Boolean = false,
168164
crossinline block: (CancellableContinuation<T>) -> Unit
169165
): T =
@@ -172,7 +168,7 @@ public actual inline suspend fun <T> suspendCancellableCoroutine(
172168
if (!holdCancellability) cancellable.initCancellability()
173169
block(cancellable)
174170
cancellable.getResult()
175-
}
171+
}
176172

177173
/**
178174
* Suspends coroutine similar to [suspendCancellableCoroutine], but with *atomic cancellation*.
@@ -182,7 +178,7 @@ public actual inline suspend fun <T> suspendCancellableCoroutine(
182178
* continue to execute even after it was cancelled from the same thread in the case when the continuation
183179
* was already resumed and was posted for execution to the thread's queue.
184180
*/
185-
public actual inline suspend fun <T> suspendAtomicCancellableCoroutine(
181+
public suspend inline fun <T> suspendAtomicCancellableCoroutine(
186182
holdCancellability: Boolean = false,
187183
crossinline block: (CancellableContinuation<T>) -> Unit
188184
): T =
@@ -198,15 +194,15 @@ public actual inline suspend fun <T> suspendAtomicCancellableCoroutine(
198194
* @suppress **This is unstable API and it is subject to change.**
199195
*/
200196
public fun CancellableContinuation<*>.removeOnCancel(node: LockFreeLinkedListNode): DisposableHandle =
201-
invokeOnCompletion(handler = RemoveOnCancel(this, node))
197+
invokeOnCompletion(handler = RemoveOnCancel(this, node).asHandler)
202198

203199
// --------------- implementation details ---------------
204200

205201
private class RemoveOnCancel(
206202
cont: CancellableContinuation<*>,
207-
val node: LockFreeLinkedListNode
203+
@JvmField val node: LockFreeLinkedListNode
208204
) : JobNode<CancellableContinuation<*>>(cont) {
209-
override fun invoke(reason: Throwable?) {
205+
override fun invoke(cause: Throwable?) {
210206
if (job.isCancelled)
211207
node.remove()
212208
}
@@ -283,12 +279,16 @@ internal class CancellableContinuationImpl<in T>(
283279

284280
override fun nameString(): String =
285281
"CancellableContinuation(${delegate.toDebugString()})"
282+
283+
// todo: This workaround for KT-21968, should be removed in the future
284+
public override fun cancel(cause: Throwable?): Boolean =
285+
super.cancel(cause)
286286
}
287287

288288
private class CompletedIdempotentResult(
289289
@JvmField val idempotentResume: Any?,
290290
@JvmField val result: Any?,
291-
@JvmField val token: JobSupport.Incomplete
291+
@JvmField val token: Incomplete
292292
) {
293293
override fun toString(): String = "CompletedIdempotentResult[$result]"
294294
}

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/CommonBuilders.kt

-35
This file was deleted.

0 commit comments

Comments
 (0)