Refactor scriptDefinition in the common part

- get rid of selector (because of confusions), move it's data into
definition itself and make it more generic with heterogeneous map
- make main KotlinScript annotation accept complete definition
- make additional annotation for building definition from parts
- add ScriptingEnvironment and pass it as a constructor parameter to
the script handlers
- rename some classes for clarity
This commit is contained in:
Ilya Chernikov
2018-03-26 18:42:18 +02:00
parent 2c3a50e4b3
commit 1f0cac50d2
13 changed files with 147 additions and 75 deletions
@@ -9,7 +9,7 @@ interface ScriptCompiler {
suspend fun compile(
configuration: ScriptCompileConfiguration,
configurator: ScriptConfigurator? = null
configurator: ScriptCompilationConfigurator? = null
): ResultWithDiagnostics<CompiledScript<*>>
}
@@ -7,14 +7,19 @@
package kotlin.script.experimental.api
typealias ScriptCompileConfiguration = HeterogeneousMap
typealias ProcessedScriptData = HeterogeneousMap
interface ScriptConfigurator {
// with null scriptSource should return a generic configuration for the script type
suspend fun baseConfiguration(scriptSource: ScriptSource?) : ResultWithDiagnostics<ScriptCompileConfiguration>
interface ScriptCompilationConfigurator {
// constructor(environment: ScriptingEnvironment) // the constructor is expected from implementations
val defaultConfiguration: ScriptCompileConfiguration
suspend fun baseConfiguration(scriptSource: ScriptSource): ResultWithDiagnostics<ScriptCompileConfiguration>
suspend fun refineConfiguration(
configuration: ScriptCompileConfiguration,
@@ -10,6 +10,9 @@ package kotlin.script.experimental.api
import kotlin.reflect.KClass
object ScriptCompileConfigurationParams {
val baseClass by typedKey<KClass<*>>()
val scriptSourceFragments by typedKey<ScriptSourceFragments>()
val scriptSignature by typedKey<ScriptSignature>()
@@ -7,12 +7,15 @@
package kotlin.script.experimental.api
import kotlin.reflect.KClass
interface ScriptDefinition {
val baseClass: KClass<*>
val selector: ScriptSelector
val configurator: ScriptConfigurator
val runner: ScriptRunner<*>?
// constructor(environment: ScriptingEnvironment) // the constructor is expected from implementations
val properties: ScriptDefinitionPropertiesBag
val compilationConfigurator: ScriptCompilationConfigurator
val evaluator: ScriptEvaluator<*>?
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("unused")
package kotlin.script.experimental.api
typealias ScriptDefinitionPropertiesBag = HeterogeneousMap
object ScriptDefinitionProperties {
val name by typedKey<String>()
val fileExtension by typedKey<String>()
val makeTitle by typedKey<(String) -> String>()
open class Builder : HeterogeneousMapBuilder() {
inline fun <reified T> makeTitle(noinline fn: (String) -> String) {
add(makeTitle to fn)
}
}
}
@@ -27,9 +27,11 @@ typealias ScriptEvaluationEnvironment = HeterogeneousMap
data class EvaluationResult(val returnValue: Any?, val environment: ScriptEvaluationEnvironment)
// NOTE: name inconsistency: run vs evaluate
interface ScriptRunner<in ScriptBase : Any> {
interface ScriptEvaluator<in ScriptBase : Any> {
suspend fun run(
// constructor(environment: ScriptingEnvironment) // the constructor is expected from implementations
suspend fun eval(
compiledScript: CompiledScript<ScriptBase>,
scriptEvaluationEnvironment: ScriptEvaluationEnvironment
): ResultWithDiagnostics<EvaluationResult>
@@ -1,19 +0,0 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("unused")
package kotlin.script.experimental.api
interface ScriptSelector {
val name: String
val fileExtension: String // for preliminary selection by file type, e.g. in ide
fun makeScriptName(scriptFileName: String?): String
fun isKnownScript(script: ScriptSource): Boolean
}
@@ -31,6 +31,7 @@ class HeterogeneousMap(private val data: Map<TypedKey<*>, Any?> = hashMapOf()) {
fun HeterogeneousMap.cloneWith(vararg pairs: Pair<TypedKey<*>, Any?>) = HeterogeneousMap(this, *pairs)
fun HeterogeneousMap.cloneWith(pairs: Iterable<Pair<TypedKey<*>, Any?>>) = HeterogeneousMap(this, pairs)
open class HeterogeneousMapBuilder {
val pairs: MutableList<Pair<TypedKey<*>, Any?>> = arrayListOf()
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.api
import kotlin.reflect.KClass
typealias ScriptingEnvironment = HeterogeneousMap
object ScriptingEnvironmentParams {
// required by definitions that extract data from script base class annotations
val baseClass by typedKey<KClass<*>>()
open class Builder : HeterogeneousMapBuilder()
}