Refactor configurations handling:

- rearrange compile call parameters for clarity, more defaults
- remove baseConfiguration method - it is now covered by defaultConfiguration
for static part, and refineConfiguration call if a config parameter is
specified
- implement proper configurations chaining, documented in comment to compile function
- minor renamings and refactorings
This commit is contained in:
Ilya Chernikov
2018-03-28 16:08:45 +02:00
parent 4a4edf3458
commit 191b1cfefa
10 changed files with 48 additions and 35 deletions
@@ -9,8 +9,8 @@ interface ScriptCompiler {
suspend fun compile(
script: ScriptSource,
configuration: ScriptCompileConfiguration,
configurator: ScriptCompilationConfigurator? = null
configurator: ScriptCompilationConfigurator? = null,
additionalConfiguration: ScriptCompileConfiguration? = null // overrides parameters from configurator.defaultConfiguration
): ResultWithDiagnostics<CompiledScript<*>>
}
@@ -21,8 +21,6 @@ interface ScriptCompilationConfigurator {
val defaultConfiguration: ScriptCompileConfiguration
suspend fun baseConfiguration(scriptSource: ScriptSource): ResultWithDiagnostics<ScriptCompileConfiguration>
suspend fun refineConfiguration(
scriptSource: ScriptSource,
configuration: ScriptCompileConfiguration,
@@ -33,8 +33,10 @@ object ScriptCompileConfigurationProperties {
val compilerOptions by typedKey<List<String>>() // Q: CommonCompilerOptions instead?
val updateConfigurationOnAnnotations by typedKey<List<KClass<out Annotation>>>()
val refineBeforeParsing by typedKey<Boolean>() // default: false
val updateConfigurationOnSections by typedKey<List<String>>()
val refineConfigurationOnAnnotations by typedKey<List<KClass<out Annotation>>>()
val refineConfigurationOnSections by typedKey<List<String>>()
}
@@ -12,9 +12,6 @@ class PassThroughCompilationConfigurator(val environment: ScriptingEnvironment)
override val defaultConfiguration = ScriptCompileConfiguration(environment)
override suspend fun baseConfiguration(scriptSource: ScriptSource): ResultWithDiagnostics<ScriptCompileConfiguration> =
defaultConfiguration.asSuccess()
override suspend fun refineConfiguration(
script: ScriptSource,
configuration: ScriptCompileConfiguration,
@@ -35,7 +35,7 @@ abstract class BasicScriptingHost<ScriptBase : Any>(
environment: ScriptEvaluationEnvironment
): ResultWithDiagnostics<EvaluationResult> =
runInCoroutineContext {
val compiled = compiler.compile(script, compileConfiguration, configurator)
val compiled = compiler.compile(script, configurator, compileConfiguration)
when (compiled) {
is ResultWithDiagnostics.Failure -> compiled
is ResultWithDiagnostics.Success -> {
@@ -17,12 +17,19 @@ class TypedKeyDelegate<T> {
fun <T> typedKey() = TypedKeyDelegate<T>()
class ChainedPropertyBag(private val parent: ChainedPropertyBag? = null, pairs: Iterable<Pair<TypedKey<*>, Any?>>) {
class ChainedPropertyBag private constructor(private val parent: ChainedPropertyBag?, private val data: Map<TypedKey<*>, Any?>) {
constructor(parent: ChainedPropertyBag? = null, pairs: Iterable<Pair<TypedKey<*>, Any?>>) :
this(parent, HashMap<TypedKey<*>, Any?>().also { it.putAll(pairs) })
constructor(pairs: Iterable<Pair<TypedKey<*>, Any?>>) : this(null, pairs)
constructor(parent: ChainedPropertyBag, vararg pairs: Pair<TypedKey<*>, Any?>) : this(parent, pairs.asIterable())
constructor(vararg pairs: Pair<TypedKey<*>, Any?>) : this(null, pairs.asIterable())
private val data = HashMap<TypedKey<*>, Any?>().also { it.putAll(pairs) }
fun cloneWithNewParent(newParent: ChainedPropertyBag?): ChainedPropertyBag = when {
newParent == null -> this
parent == null -> ChainedPropertyBag(newParent, data)
else -> ChainedPropertyBag(parent.cloneWithNewParent(newParent), data)
}
inline operator fun <reified T> get(key: TypedKey<T>): T = getUnchecked(key) as T