Get rid of kotlinx.coroutines usage in saved script runner

to reduce dpendencies for the save dscipt running
This commit is contained in:
Ilya Chernikov
2020-01-16 11:59:24 +01:00
parent 891914167a
commit a4752087db
2 changed files with 43 additions and 7 deletions
@@ -58,7 +58,7 @@ fun KJvmCompiledScript<*>.saveToJar(outputJar: File) {
?.flatMap { it.classpath }
.orEmpty()
val dependenciesForMain = scriptCompilationClasspathFromContext(
KotlinPaths.Jar.ScriptingLib.baseName, KotlinPaths.Jar.ScriptingJvmLib.baseName, KotlinPaths.Jar.CoroutinesCore.baseName,
KotlinPaths.Jar.ScriptingLib.baseName, KotlinPaths.Jar.ScriptingJvmLib.baseName,
classLoader = this::class.java.classLoader,
wholeClasspath = false
)
@@ -5,7 +5,10 @@
package kotlin.script.experimental.jvm
import kotlinx.coroutines.runBlocking
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.host.createEvaluationConfigurationFromTemplate
import kotlin.script.experimental.jvm.impl.createScriptFromClassLoader
@@ -27,9 +30,42 @@ fun runCompiledScript(scriptClass: Class<*>, vararg args: String) {
mainArguments(args)
}
}
runBlocking {
evaluator(script, evaluationConfiguration)
}.onFailure {
it.reports.forEach(System.err::println)
runScriptSuspend {
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
}
}
}
}
}