Add a link to hostConfiguration from compilation and evaluation ones

also add possibility to supply functional defaults for keys in PropertiesCollection
This commit is contained in:
Ilya Chernikov
2019-05-08 11:31:08 +02:00
parent 30c6147223
commit 7ae2054cba
4 changed files with 27 additions and 7 deletions
@@ -9,6 +9,7 @@ package kotlin.script.experimental.api
import java.io.Serializable import java.io.Serializable
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.script.experimental.host.ScriptingHostConfiguration
import kotlin.script.experimental.util.PropertiesCollection import kotlin.script.experimental.util.PropertiesCollection
interface ScriptCompilationConfigurationKeys interface ScriptCompilationConfigurationKeys
@@ -113,6 +114,11 @@ val ScriptCompilationConfigurationKeys.refineConfigurationBeforeCompiling by Pro
*/ */
val ScriptCompilationConfigurationKeys.sourceFragments by PropertiesCollection.key<List<ScriptSourceNamedFragment>>() val ScriptCompilationConfigurationKeys.sourceFragments by PropertiesCollection.key<List<ScriptSourceNamedFragment>>()
/**
* Scripting host configuration
*/
val ScriptCompilationConfigurationKeys.hostConfiguration by PropertiesCollection.key<ScriptingHostConfiguration>()
/** /**
* The sub-builder DSL for configuring refinement callbacks * The sub-builder DSL for configuring refinement callbacks
*/ */
@@ -9,6 +9,7 @@ package kotlin.script.experimental.api
import java.io.Serializable import java.io.Serializable
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.script.experimental.host.ScriptingHostConfiguration
import kotlin.script.experimental.util.PropertiesCollection import kotlin.script.experimental.util.PropertiesCollection
interface ScriptEvaluationConfigurationKeys interface ScriptEvaluationConfigurationKeys
@@ -67,6 +68,11 @@ val ScriptEvaluationConfigurationKeys.scriptsInstancesSharingMap by PropertiesCo
*/ */
val ScriptEvaluationConfigurationKeys.scriptsInstancesSharing by PropertiesCollection.key<Boolean>(false) val ScriptEvaluationConfigurationKeys.scriptsInstancesSharing by PropertiesCollection.key<Boolean>(false)
/**
* Scripting host configuration
*/
val ScriptEvaluationConfigurationKeys.hostConfiguration by PropertiesCollection.key<ScriptingHostConfiguration>()
/** /**
* The callback that will be called on the script compilation immediately before starting the compilation * The callback that will be called on the script compilation immediately before starting the compilation
*/ */
@@ -39,6 +39,7 @@ fun createCompilationConfigurationFromTemplate(
val mainAnnotation: KotlinScript = templateClass.kotlinScriptAnnotation val mainAnnotation: KotlinScript = templateClass.kotlinScriptAnnotation
return ScriptCompilationConfiguration(scriptConfigInstance(mainAnnotation.compilationConfiguration)) { return ScriptCompilationConfiguration(scriptConfigInstance(mainAnnotation.compilationConfiguration)) {
hostConfiguration(hostConfiguration)
propertiesFromTemplate(templateClass, baseClassType, mainAnnotation) propertiesFromTemplate(templateClass, baseClassType, mainAnnotation)
body() body()
} }
@@ -62,7 +63,10 @@ fun createEvaluationConfigurationFromTemplate(
val mainAnnotation = templateClass.kotlinScriptAnnotation val mainAnnotation = templateClass.kotlinScriptAnnotation
return ScriptEvaluationConfiguration(scriptConfigInstance(mainAnnotation.evaluationConfiguration), body = body) return ScriptEvaluationConfiguration(scriptConfigInstance(mainAnnotation.evaluationConfiguration)) {
hostConfiguration(hostConfiguration)
body()
}
} }
private fun ScriptCompilationConfiguration.Builder.propertiesFromTemplate( private fun ScriptCompilationConfiguration.Builder.propertiesFromTemplate(
@@ -13,7 +13,9 @@ import kotlin.script.experimental.api.KotlinType
open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = emptyMap()) : Serializable { open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = emptyMap()) : Serializable {
class Key<T>(val name: String, @Transient val defaultValue: T? = null) : Serializable { class Key<T>(val name: String, @Transient val getDefaultValue: PropertiesCollection.() -> T?) : Serializable {
constructor(name: String, defaultValue: T? = null) : this(name, { defaultValue })
override fun equals(other: Any?): Boolean = if (other is Key<*>) name == other.name else false override fun equals(other: Any?): Boolean = if (other is Key<*>) name == other.name else false
override fun hashCode(): Int = name.hashCode() override fun hashCode(): Int = name.hashCode()
@@ -25,9 +27,11 @@ open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = empt
} }
} }
class PropertyKeyDelegate<T>(private val defaultValue: T? = null) { class PropertyKeyDelegate<T>(private val getDefaultValue: PropertiesCollection.() -> T?) {
constructor(defaultValue: T?) : this({ defaultValue })
operator fun getValue(thisRef: Any?, property: KProperty<*>): Key<T> = operator fun getValue(thisRef: Any?, property: KProperty<*>): Key<T> =
Key(property.name, defaultValue) Key(property.name, getDefaultValue)
} }
class PropertyKeyCopyDelegate<T>(val source: Key<T>) { class PropertyKeyCopyDelegate<T>(val source: Key<T>) {
@@ -36,8 +40,7 @@ open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = empt
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
operator fun <T> get(key: PropertiesCollection.Key<T>): T? = operator fun <T> get(key: PropertiesCollection.Key<T>): T? =
if (key.defaultValue == null) properties[key] as T? (properties[key] ?: if (properties.containsKey(key)) null else key.getDefaultValue(this)) as? T
else properties.getOrDefault(key, key.defaultValue) as T?
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
fun <T> getNoDefault(key: PropertiesCollection.Key<T>): T? = fun <T> getNoDefault(key: PropertiesCollection.Key<T>): T? =
@@ -55,10 +58,11 @@ open class PropertiesCollection(private val properties: Map<Key<*>, Any?> = empt
companion object { companion object {
fun <T> key(defaultValue: T? = null) = PropertyKeyDelegate(defaultValue) fun <T> key(defaultValue: T? = null) = PropertyKeyDelegate(defaultValue)
fun <T> key(getDefaultValue: PropertiesCollection.() -> T?) = PropertyKeyDelegate(getDefaultValue)
fun <T> keyCopy(source: Key<T>) = PropertyKeyCopyDelegate(source) fun <T> keyCopy(source: Key<T>) = PropertyKeyCopyDelegate(source)
@JvmStatic @JvmStatic
private val serialVersionUID = 0L private val serialVersionUID = 1L
} }
// properties builder base class (DSL for building properties collection) // properties builder base class (DSL for building properties collection)