Pass script source directly instead of via configuration
This commit is contained in:
@@ -8,6 +8,7 @@ package kotlin.script.experimental.api
|
||||
interface ScriptCompiler {
|
||||
|
||||
suspend fun compile(
|
||||
script: ScriptSource,
|
||||
configuration: ScriptCompileConfiguration,
|
||||
configurator: ScriptCompilationConfigurator? = null
|
||||
): ResultWithDiagnostics<CompiledScript<*>>
|
||||
|
||||
+6
-2
@@ -29,9 +29,13 @@ abstract class BasicScriptingHost<ScriptBase : Any>(
|
||||
) {
|
||||
open fun <T> runInCoroutineContext(block: suspend CoroutineScope.() -> T): T = runBlocking { block() }
|
||||
|
||||
open fun eval(configuration: ScriptCompileConfiguration, environment: ScriptEvaluationEnvironment): ResultWithDiagnostics<EvaluationResult> =
|
||||
open fun eval(
|
||||
script: ScriptSource,
|
||||
configuration: ScriptCompileConfiguration,
|
||||
environment: ScriptEvaluationEnvironment
|
||||
): ResultWithDiagnostics<EvaluationResult> =
|
||||
runInCoroutineContext {
|
||||
val compiled = compiler.compile(configuration, configurator)
|
||||
val compiled = compiler.compile(script, configuration, configurator)
|
||||
when (compiled) {
|
||||
is ResultWithDiagnostics.Failure -> compiled
|
||||
is ResultWithDiagnostics.Success -> {
|
||||
|
||||
@@ -5,11 +5,9 @@
|
||||
|
||||
package kotlin.script.experimental.host
|
||||
|
||||
import kotlin.script.experimental.api.ScriptSource
|
||||
import kotlin.script.experimental.api.ScriptSourceFragments
|
||||
import kotlin.script.experimental.api.ScriptSourceNamedFragment
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
import kotlin.script.experimental.api.*
|
||||
|
||||
fun ScriptSourceFragments.isWholeFile(): Boolean = fragments?.isEmpty() ?: true
|
||||
|
||||
@@ -20,14 +18,15 @@ fun ScriptSource.getScriptText(): String = when {
|
||||
else -> throw RuntimeException("unable to get text from null script")
|
||||
}
|
||||
|
||||
fun ScriptSourceFragments.getMergedScriptText(): String {
|
||||
val originalScriptText = originalSource.getScriptText()
|
||||
return if (isWholeFile()) {
|
||||
fun getMergedScriptText(script: ScriptSource, configuration: ScriptCompileConfiguration): String {
|
||||
val originalScriptText = script.getScriptText()
|
||||
val sourceFragments = configuration.getOrNull(ScriptCompileConfigurationParams.scriptSourceFragments)
|
||||
return if (sourceFragments == null || sourceFragments.isWholeFile()) {
|
||||
originalScriptText
|
||||
} else {
|
||||
val sb = StringBuilder(originalScriptText.length)
|
||||
var prevFragment: ScriptSourceNamedFragment? = null
|
||||
for (fragment in fragments!!) {
|
||||
for (fragment in sourceFragments!!.fragments!!) {
|
||||
val fragmentStartPos = fragment.range.start.absolutePos
|
||||
val fragmentEndPos = fragment.range.end.absolutePos
|
||||
if (fragmentStartPos == null || fragmentEndPos == null)
|
||||
|
||||
+2
-1
@@ -69,6 +69,7 @@ class KJVMCompiledScript<out ScriptBase : Any>(
|
||||
class KJVMCompilerImpl : KJVMCompilerProxy {
|
||||
|
||||
override fun compile(
|
||||
script: ScriptSource,
|
||||
scriptCompilerConfiguration: ScriptCompileConfiguration,
|
||||
configurator: ScriptCompilationConfigurator?
|
||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
@@ -147,7 +148,7 @@ class KJVMCompilerImpl : KJVMCompilerProxy {
|
||||
val analyzerWithCompilerReport = AnalyzerWithCompilerReport(messageCollector, environment.configuration.languageVersionSettings)
|
||||
|
||||
val psiFileFactory: PsiFileFactoryImpl = PsiFileFactory.getInstance(environment.project) as PsiFileFactoryImpl
|
||||
val scriptText = scriptCompilerConfiguration[ScriptCompileConfigurationParams.scriptSourceFragments].getMergedScriptText()
|
||||
val scriptText = getMergedScriptText(script, scriptCompilerConfiguration)
|
||||
val scriptFileName = "script" // TODO: extract from file/url if available
|
||||
val virtualFile = LightVirtualFile(
|
||||
"$scriptFileName${KotlinParserDefinition.STD_SCRIPT_EXT}",
|
||||
|
||||
@@ -14,7 +14,11 @@ open class JvmScriptCompiler(
|
||||
val cache: CompiledJvmScriptsCache
|
||||
) : ScriptCompiler {
|
||||
|
||||
override suspend fun compile(configuration: ScriptCompileConfiguration, configurator: ScriptCompilationConfigurator?): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
override suspend fun compile(
|
||||
script: ScriptSource,
|
||||
configuration: ScriptCompileConfiguration,
|
||||
configurator: ScriptCompilationConfigurator?
|
||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
val refinedConfiguration = configurator?.refineConfiguration(configuration)?.let {
|
||||
when (it) {
|
||||
is ResultWithDiagnostics.Failure -> return it
|
||||
@@ -22,11 +26,11 @@ open class JvmScriptCompiler(
|
||||
?: return ResultWithDiagnostics.Failure("Null script compile configuration received".asErrorDiagnostics())
|
||||
}
|
||||
} ?: configuration
|
||||
val cached = cache[refinedConfiguration[ScriptCompileConfigurationParams.scriptSourceFragments]]
|
||||
val cached = cache.get(script, refinedConfiguration)
|
||||
|
||||
if (cached != null) return cached.asSuccess()
|
||||
|
||||
return compilerProxy.compile(refinedConfiguration, configurator).also {
|
||||
return compilerProxy.compile(script, refinedConfiguration, configurator).also {
|
||||
if (it is ResultWithDiagnostics.Success) {
|
||||
cache.store(it.value as CompiledScript<*>)
|
||||
}
|
||||
@@ -35,19 +39,20 @@ open class JvmScriptCompiler(
|
||||
}
|
||||
|
||||
interface CompiledJvmScriptsCache {
|
||||
operator fun get(script: ScriptSourceFragments): CompiledScript<*>?
|
||||
fun get(script: ScriptSource, configuration: ScriptCompileConfiguration): CompiledScript<*>?
|
||||
fun store(compiledScript: CompiledScript<*>)
|
||||
}
|
||||
|
||||
interface KJVMCompilerProxy {
|
||||
fun compile(
|
||||
script: ScriptSource,
|
||||
scriptCompilerConfiguration: ScriptCompileConfiguration,
|
||||
configurator: ScriptCompilationConfigurator?
|
||||
): ResultWithDiagnostics<CompiledScript<*>>
|
||||
}
|
||||
|
||||
class DummyCompiledJvmScriptCache : CompiledJvmScriptsCache {
|
||||
override operator fun get(script: ScriptSourceFragments): CompiledScript<*>? = null
|
||||
override fun get(script: ScriptSource, configuration: ScriptCompileConfiguration): CompiledScript<*>? = null
|
||||
override fun store(compiledScript: CompiledScript<*>) {}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user