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:
Ilya Chernikov
2021-07-16 11:02:17 +02:00
committed by TeamCityServer
parent 9b1de90452
commit 0cd29adcc7
20 changed files with 89 additions and 67 deletions
@@ -7,7 +7,6 @@ project.updateJvmTarget("1.6")
dependencies {
compile(kotlinStdlib())
compile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core"))
compileOnly(project(":kotlin-reflect-api"))
testCompile(commonDep("junit"))
}
@@ -18,9 +18,8 @@
package kotlin.script.experimental.host
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import kotlin.script.experimental.api.*
import kotlin.script.experimental.impl.internalScriptingRunSuspend
/**
* The base class for scripting host implementations
@@ -32,7 +31,9 @@ abstract class BasicScriptingHost(
/**
* The overridable wrapper for executing evaluation in a desired coroutines context
*/
open fun <T> runInCoroutineContext(block: suspend CoroutineScope.() -> T): T = runBlocking { block() }
open fun <T> runInCoroutineContext(block: suspend () -> T): T =
@Suppress("DEPRECATION_ERROR")
internalScriptingRunSuspend { block() }
/**
* The default implementation of the evaluation function
@@ -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()
}
}
@@ -33,6 +33,7 @@ dependencies {
testImplementation(commonDep("junit"))
testRuntimeOnly("org.slf4j:slf4j-nop:1.7.30")
testImplementation(kotlin("reflect"))
testImplementation(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core"))
}
sourceSets {
@@ -8,8 +8,8 @@ project.updateJvmTarget("1.6")
dependencies {
compile(kotlinStdlib())
compile(project(":kotlin-scripting-common"))
testCompile(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core"))
testCompile(commonDep("junit"))
testImplementation(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core"))
}
sourceSets {
@@ -18,6 +18,7 @@ dependencies {
testCompile(projectTests(":compiler:tests-common"))
testCompile(project(":kotlin-scripting-compiler"))
testCompile(project(":daemon-common")) // TODO: fix import (workaround for jps build)
testImplementation(commonDep("org.jetbrains.kotlinx", "kotlinx-coroutines-core"))
testRuntimeOnly(project(":kotlin-compiler"))
testRuntimeOnly(project(":kotlin-reflect"))
@@ -5,7 +5,6 @@
package kotlin.script.experimental.jvmhost.repl
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.backend.common.push
import org.jetbrains.kotlin.cli.common.repl.*
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.KJvmReplCompilerBase
@@ -15,6 +14,7 @@ import org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplCodeAnalyzerBase
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.write
import kotlin.script.experimental.api.*
import kotlin.script.experimental.impl.internalScriptingRunSuspend
import kotlin.script.experimental.host.ScriptingHostConfiguration
import kotlin.script.experimental.host.withDefaultsFrom
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
@@ -56,7 +56,8 @@ class JvmReplCompiler(
}()
}
when (val res = runBlocking { replCompiler.compile(listOf(snippet), scriptCompilationConfiguration) }) {
@Suppress("DEPRECATION_ERROR")
when (val res = internalScriptingRunSuspend { replCompiler.compile(listOf(snippet), scriptCompilationConfiguration) }) {
is ResultWithDiagnostics.Success -> {
val lineId = LineId(codeLine.no, 0, snippet.hashCode())
replCompilerState.apply {
@@ -5,13 +5,13 @@
package kotlin.script.experimental.jvmhost.repl
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.cli.common.repl.*
import org.jetbrains.kotlin.cli.common.repl.ReplEvaluator
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.write
import kotlin.reflect.KClass
import kotlin.script.experimental.api.*
import kotlin.script.experimental.impl.internalScriptingRunSuspend
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator
import kotlin.script.experimental.jvm.baseClassLoader
import kotlin.script.experimental.jvm.impl.KJvmCompiledScript
@@ -60,7 +60,8 @@ class JvmReplEvaluator(
}
}
val res = runBlocking { scriptEvaluator(compiledScript, currentConfiguration) }
@Suppress("DEPRECATION_ERROR")
val res = internalScriptingRunSuspend { scriptEvaluator(compiledScript, currentConfiguration) }
when (res) {
is ResultWithDiagnostics.Success -> {
@@ -5,7 +5,6 @@
package kotlin.script.experimental.jvm.impl
import kotlinx.coroutines.runBlocking
import java.io.File
import kotlin.script.dependencies.Environment
import kotlin.script.dependencies.ScriptContents
@@ -16,6 +15,7 @@ import kotlin.script.experimental.dependencies.ScriptDependencies
import kotlin.script.experimental.dependencies.ScriptReport
import kotlin.script.experimental.host.FileScriptSource
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.impl.internalScriptingRunSuspend
import kotlin.script.experimental.jvm.JvmDependency
import kotlin.script.experimental.jvm.compat.mapToLegacyScriptReportPosition
import kotlin.script.experimental.jvm.compat.mapToLegacyScriptReportSeverity
@@ -27,7 +27,8 @@ class BridgeDependenciesResolver(
) : AsyncDependenciesResolver {
override fun resolve(scriptContents: ScriptContents, environment: Environment): DependenciesResolver.ResolveResult =
runBlocking {
@Suppress("DEPRECATION_ERROR")
internalScriptingRunSuspend {
resolveAsync(scriptContents, environment)
}
@@ -5,13 +5,13 @@
package kotlin.script.experimental.jvm
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine
import kotlin.script.experimental.api.*
import kotlin.script.experimental.api.ScriptCompilationConfiguration
import kotlin.script.experimental.api.baseClass
import kotlin.script.experimental.api.hostConfiguration
import kotlin.script.experimental.api.onFailure
import kotlin.script.experimental.host.createEvaluationConfigurationFromTemplate
import kotlin.script.experimental.host.withDefaultsFrom
import kotlin.script.experimental.impl.internalScriptingRunSuspend
import kotlin.script.experimental.jvm.impl.createScriptFromClassLoader
@Suppress("unused") // script codegen generates a call to it
@@ -29,42 +29,11 @@ fun runCompiledScript(scriptClass: Class<*>, vararg args: String) {
mainArguments(args)
}
}
runScriptSuspend {
@Suppress("DEPRECATION_ERROR")
internalScriptingRunSuspend {
evaluator(script, evaluationConfiguration).onFailure {
it.reports.forEach(System.err::println)
}
}
}
// Copied form kotlin.coroutines.jvm.internal.runSuspend/RunSuspend to create a runner without dependency on the kotlinx.coroutines
private fun runScriptSuspend(block: suspend () -> Unit) {
val run = RunScriptSuspend()
block.startCoroutine(run)
run.await()
}
private class RunScriptSuspend : Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
@Suppress("RESULT_CLASS_IN_RETURN_TYPE")
var result: Result<Unit>? = null
override fun resumeWith(result: Result<Unit>) = synchronized(this) {
this.result = result
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") (this as Object).notifyAll()
}
fun await() = synchronized(this) {
while (true) {
when (val result = this.result) {
null -> @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") (this as Object).wait()
else -> {
result.getOrThrow() // throw up failure
return
}
}
}
}
}