From a4752087db7b50c5ab3a7d26d1c5d57d195bebc8 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 16 Jan 2020 11:59:24 +0100 Subject: [PATCH] Get rid of kotlinx.coroutines usage in saved script runner to reduce dpendencies for the save dscipt running --- .../experimental/jvmhost/jvmScriptSaving.kt | 2 +- .../kotlin/script/experimental/jvm/runner.kt | 48 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/libraries/scripting/jvm-host/src/kotlin/script/experimental/jvmhost/jvmScriptSaving.kt b/libraries/scripting/jvm-host/src/kotlin/script/experimental/jvmhost/jvmScriptSaving.kt index 8f8a7e804f2..e0bd9cd4b5b 100644 --- a/libraries/scripting/jvm-host/src/kotlin/script/experimental/jvmhost/jvmScriptSaving.kt +++ b/libraries/scripting/jvm-host/src/kotlin/script/experimental/jvmhost/jvmScriptSaving.kt @@ -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 ) diff --git a/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/runner.kt b/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/runner.kt index 13770f7ee32..c8a04dfe372 100644 --- a/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/runner.kt +++ b/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/runner.kt @@ -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) + } } -} \ No newline at end of file +} + +// 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 { + override val context: CoroutineContext + get() = EmptyCoroutineContext + + @Suppress("RESULT_CLASS_IN_RETURN_TYPE") + var result: Result? = null + + override fun resumeWith(result: Result) = 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 + } + } + } + } +} +