Get rid of kotlinx-coroutines usage in scripting libs and plugins
the dependency on the coroutines library caused various problems like KT-30778, or stdlib/runtime version conflicts. The only function used was `runBlocking`, so this change replaces it with the internal implementation based on the similar internal thing from the stdlib. #KT-30778 fixed
This commit is contained in:
committed by
TeamCityServer
parent
9b1de90452
commit
0cd29adcc7
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.impl
|
||||
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
import kotlin.coroutines.startCoroutine
|
||||
|
||||
// Copied with modifications form kotlin.coroutines.jvm.internal.runSuspend/RunSuspend
|
||||
// to use as an equivalent of runBlocking without dependency on the kotlinx.coroutines
|
||||
|
||||
@Deprecated("For internal use only, use kotlinx.coroutines instead", level = DeprecationLevel.ERROR)
|
||||
fun <T> internalScriptingRunSuspend(block: suspend () -> T) : T {
|
||||
val run = InternalScriptingRunSuspend<T>()
|
||||
block.startCoroutine(run)
|
||||
return run.await()
|
||||
}
|
||||
|
||||
private class InternalScriptingRunSuspend<T> : Continuation<T> {
|
||||
override val context: CoroutineContext
|
||||
get() = EmptyCoroutineContext
|
||||
|
||||
@Suppress("RESULT_CLASS_IN_RETURN_TYPE")
|
||||
var result: Result<T>? = null
|
||||
|
||||
override fun resumeWith(result: Result<T>) = synchronized(this) {
|
||||
this.result = result
|
||||
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") (this as Object).notifyAll()
|
||||
}
|
||||
|
||||
fun await(): T = synchronized(this) {
|
||||
while (true) {
|
||||
when (val result: Result<T>? = this.result) {
|
||||
null -> @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") (this as Object).wait()
|
||||
else -> break
|
||||
}
|
||||
}
|
||||
return result!!.getOrThrow()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user