From 1723ff658d9ab5f3415a5faf41ba71ca47a1a47a Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Fri, 2 Mar 2018 19:04:47 +0100 Subject: [PATCH] Refactoring heterogeneous map handling, add dsl builders --- .../experimental/api/scriptConfiguration.kt | 36 -------------- .../api/scriptConfigurationParams.kt | 49 +++++++++++++++++++ .../experimental/api/scriptEvaluation.kt | 19 +++++-- .../api/scriptProcessedDataParams.kt | 18 +++++++ .../script/experimental/api/scriptUtil.kt | 34 ++++++++++--- 5 files changed, 109 insertions(+), 47 deletions(-) create mode 100644 libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfigurationParams.kt create mode 100644 libraries/scripting/common/src/kotlin/script/experimental/api/scriptProcessedDataParams.kt diff --git a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfiguration.kt b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfiguration.kt index 7f1bbdefc49..14f068e0c47 100644 --- a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfiguration.kt +++ b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfiguration.kt @@ -7,43 +7,10 @@ package kotlin.script.experimental.api -import kotlin.reflect.KClass - -object ScriptCompileConfigurationParams { - - val scriptSourceFragments by typedKey() - - val scriptSignature by typedKey() - - val importedPackages by typedKey>() - - val restrictions by typedKey() - - val importedScripts by typedKey>() - - val dependencies by typedKey>() - - val compilerOptions by typedKey>() // Q: CommonCompilerOptions instead? - - val updateConfigurationOnAnnotations by typedKey>>() - - val updateConfigurationOnSections by typedKey>() -} - typealias ScriptCompileConfiguration = HeterogeneousMap -fun ScriptSource.toScriptCompileConfiguration(vararg pairs: Pair, Any?>) = - ScriptCompileConfiguration(ScriptCompileConfigurationParams.scriptSourceFragments to ScriptSourceFragments(this, null), *pairs) - -object ProcessedScriptDataParams { - val annotations by typedKey>() - - val fragments by typedKey>() -} - typealias ProcessedScriptData = HeterogeneousMap - interface ScriptConfigurator { // with null scriptSource should return a generic configuration for the script type @@ -55,6 +22,3 @@ interface ScriptConfigurator { ): ResultWithDiagnostics } -fun ScriptSource?.toConfigEntry(): Pair, Any?> = - ScriptCompileConfigurationParams.scriptSourceFragments to this?.let { ScriptSourceFragments(this, null) } - diff --git a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfigurationParams.kt b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfigurationParams.kt new file mode 100644 index 00000000000..7e62668e403 --- /dev/null +++ b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptConfigurationParams.kt @@ -0,0 +1,49 @@ +/* + * 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 + +import kotlin.reflect.KClass + +object ScriptCompileConfigurationParams { + val scriptSourceFragments by typedKey() + + val scriptSignature by typedKey() + + val importedPackages by typedKey>() + + val restrictions by typedKey() + + val importedScripts by typedKey>() + + val dependencies by typedKey>() + + val compilerOptions by typedKey>() // Q: CommonCompilerOptions instead? + + val updateConfigurationOnAnnotations by typedKey>>() + + val updateConfigurationOnSections by typedKey>() + + open class Builder : HeterogeneousMapBuilder() { + inline fun signature(providedDeclarations: ProvidedDeclarations = ProvidedDeclarations.Empty) { + add(scriptSignature to ScriptSignature(T::class, providedDeclarations)) + } + } +} + +// DSL +inline +fun scriptConfiguration(from: HeterogeneousMap = HeterogeneousMap(), body: ScriptCompileConfigurationParams.Builder.() -> Unit) = + ScriptCompileConfigurationParams.Builder().build(from, body) + + +fun ScriptSource.toScriptCompileConfiguration(vararg pairs: Pair, Any?>) = + ScriptCompileConfiguration(ScriptCompileConfigurationParams.scriptSourceFragments to ScriptSourceFragments(this, null), *pairs) + +fun ScriptSource?.toConfigEntry(): Pair, Any?> = + ScriptCompileConfigurationParams.scriptSourceFragments to this?.let { ScriptSourceFragments(this, null) } + diff --git a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptEvaluation.kt b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptEvaluation.kt index ebeeddae4ff..2693ead393c 100644 --- a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptEvaluation.kt +++ b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptEvaluation.kt @@ -7,13 +7,22 @@ package kotlin.script.experimental.api -object ScriptEvaluationEnvironmentParams { - val implicitReceivers by typedKey>() - val contextVariables by typedKey>() // external variables - val constructorArgs by typedKey>() - val runArgs by typedKey>() +open class ScriptEvaluationEnvironmentParams : HeterogeneousMapBuilder() { + companion object { + val implicitReceivers by typedKey>() + + val contextVariables by typedKey>() // external variables + + val constructorArgs by typedKey>() + + val runArgs by typedKey>() + } } +inline +fun scriptEvaluationEnvironment(from: HeterogeneousMap = HeterogeneousMap(), body: ScriptEvaluationEnvironmentParams.() -> Unit) = + ScriptEvaluationEnvironmentParams().build(from, body) + typealias ScriptEvaluationEnvironment = HeterogeneousMap data class EvaluationResult(val returnValue: Any?, val environment: ScriptEvaluationEnvironment) diff --git a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptProcessedDataParams.kt b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptProcessedDataParams.kt new file mode 100644 index 00000000000..1817cfd7db7 --- /dev/null +++ b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptProcessedDataParams.kt @@ -0,0 +1,18 @@ +/* + * 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. + */ + +package kotlin.script.experimental.api + +class ProcessedScriptDataParams : HeterogeneousMapBuilder() { + companion object { + val annotations by typedKey>() + + val fragments by typedKey>() + } +} + +inline +fun processedScriptData(from: HeterogeneousMap = HeterogeneousMap(), body: ProcessedScriptDataParams.() -> Unit) = + ProcessedScriptDataParams().build(from, body) \ No newline at end of file diff --git a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptUtil.kt b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptUtil.kt index fd222e486d1..df34f4e2969 100644 --- a/libraries/scripting/common/src/kotlin/script/experimental/api/scriptUtil.kt +++ b/libraries/scripting/common/src/kotlin/script/experimental/api/scriptUtil.kt @@ -7,6 +7,8 @@ package kotlin.script.experimental.api import kotlin.reflect.KProperty +// copy placed into package org.jetbrains.kotlin.utils.addToStdlib as well + data class TypedKey(val name: String) class TypedKeyDelegate { @@ -15,15 +17,35 @@ class TypedKeyDelegate { fun typedKey() = TypedKeyDelegate() -class HeterogeneousMap(val data: Map, Any?> = hashMapOf()) { +class HeterogeneousMap(private val data: Map, Any?> = hashMapOf()) { constructor(vararg pairs: Pair, Any?>) : this(hashMapOf(*pairs)) + constructor(from: HeterogeneousMap, vararg pairs: Pair, Any?>) : this(HashMap(from.data).apply { putAll(pairs) }) + constructor(from: HeterogeneousMap, pairs: Iterable, Any?>>) : this(HashMap(from.data).apply { putAll(pairs) }) + + operator fun get(key: TypedKey): T = + if (data.containsKey(key)) data[key] as T + else throw IllegalArgumentException("Unknown key $key") + + fun getOrNull(key: TypedKey): T? = data[key] as T? } -fun HeterogeneousMap.cloneWith(vararg pairs: Pair, Any?>) = HeterogeneousMap(HashMap(data).apply { putAll(pairs) }) +fun HeterogeneousMap.cloneWith(vararg pairs: Pair, Any?>) = HeterogeneousMap(this, *pairs) -operator fun HeterogeneousMap.get(key: TypedKey): T = - if (data.containsKey(key)) data[key] as T - else throw IllegalArgumentException("Unknown key $key") -fun HeterogeneousMap.getOrNull(key: TypedKey): T? = data[key] as T? +open class HeterogeneousMapBuilder { + val pairs: MutableList, Any?>> = arrayListOf() + open operator fun TypedKey.invoke(v: T) { + pairs.add(this to v) + } + + fun add(pair: Pair, Any?>) { + pairs.add(pair) + } +} + +inline +fun T.build(from: HeterogeneousMap = HeterogeneousMap(), body: T.() -> Unit): HeterogeneousMap { + body() + return HeterogeneousMap(from, pairs) +}