Refactor scripting properties:

- flatten the structure
- add some parameters
- replace iterables with lists
- renaming all scope objects uniformily
also:
- fix passing and usage of script sources
- renamings and other minor improvements
This commit is contained in:
Ilya Chernikov
2018-03-28 12:59:10 +02:00
parent 2ddcc280a9
commit 5058c66e8c
18 changed files with 123 additions and 130 deletions
@@ -3,10 +3,13 @@
* that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("unused")
package kotlin.script.experimental.api
object ProcessedScriptDataParams {
val annotations by typedKey<Iterable<Annotation>>()
object ProcessedScriptDataProperties {
val foundAnnotations by typedKey<List<Annotation>>()
val fragments by typedKey<Iterable<ScriptSourceNamedFragment>>()
val foundFragments by typedKey<List<ScriptSourceNamedFragment>>()
}
@@ -22,6 +22,7 @@ interface ScriptCompilationConfigurator {
suspend fun baseConfiguration(scriptSource: ScriptSource): ResultWithDiagnostics<ScriptCompileConfiguration>
suspend fun refineConfiguration(
scriptSource: ScriptSource,
configuration: ScriptCompileConfiguration,
processedScriptData: ProcessedScriptData = ProcessedScriptData()
): ResultWithDiagnostics<ScriptCompileConfiguration>
@@ -1,34 +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
import kotlin.reflect.KClass
object ScriptCompileConfigurationParams {
val baseClass by typedKey<KClass<*>>()
val scriptSourceFragments by typedKey<ScriptSourceFragments>()
val scriptSignature by typedKey<ScriptSignature>()
val importedPackages by typedKey<Iterable<String>>()
val restrictions by typedKey<ResolvingRestrictions>()
val importedScripts by typedKey<Iterable<ScriptSource>>()
val dependencies by typedKey<Iterable<ScriptDependency>>()
val compilerOptions by typedKey<Iterable<String>>() // Q: CommonCompilerOptions instead?
val updateConfigurationOnAnnotations by typedKey<Iterable<KClass<out Annotation>>>()
val updateConfigurationOnSections by typedKey<Iterable<String>>()
}
@@ -0,0 +1,39 @@
/*
* 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
import kotlin.reflect.KType
object ScriptCompileConfigurationProperties {
val sourceFragments by typedKey<List<ScriptSourceNamedFragment>>()
val baseClass = ScriptingEnvironmentProperties.baseClass
val scriptBodyTarget by typedKey<ScriptBodyTarget>()
val scriptImplicitReceivers by typedKey<List<KType>>() // in the order from outer to inner scope
val contextVariables by typedKey<Map<String, KType>>() // external variables
val importedPackages by typedKey<List<String>>()
val restrictions by typedKey<List<ResolvingRestrictionRule>>()
val importedScripts by typedKey<List<ScriptSource>>()
val dependencies by typedKey<List<ScriptDependency>>()
val compilerOptions by typedKey<List<String>>() // Q: CommonCompilerOptions instead?
val updateConfigurationOnAnnotations by typedKey<List<KClass<out Annotation>>>()
val updateConfigurationOnSections by typedKey<List<String>>()
}
@@ -22,37 +22,22 @@ interface ScriptSource {
data class ScriptSourceNamedFragment(val name: String?, val range: ScriptSource.Range)
open class ScriptSourceFragments(
val originalSource: ScriptSource,
val fragments: List<ScriptSourceNamedFragment>?)
open class ProvidedDeclarations(
val implicitReceivers: List<KType> = emptyList(), // previous scripts, etc.
val contextVariables: Map<String, KType> = emptyMap() // external variables
// Q: do we need context constants and/or types here, e.g.
// val contextConstants: Map<String, Any?> // or with KType as well
// val contextTypes: List<KType> // additional (to the classpath) types provided by the environment
// alternatively:
// val contextDeclarations: List<Tuple<DeclarationKind, String?, KType, Any?> // kind, name, type, value
// OR: it should be a HeterogeneousMap too
) {
object Empty : ProvidedDeclarations()
enum class ScriptBodyTarget {
Constructor,
SingleAbstractMethod
}
open class ScriptSignature(
val scriptBase: KClass<*>,
val providedDeclarations: ProvidedDeclarations
)
open class ResolvingRestrictions {
data class Rule(
val allow: Boolean,
val pattern: String // FQN wildcard
)
val rules: Iterable<Rule> = arrayListOf()
data class ResolvingRestrictionRule(
val action: Action,
val pattern: String // FQN wildcard
) {
enum class Action {
Allow,
Deny
}
}
interface ScriptDependency {
// Q: anything generic here?
}
@@ -9,8 +9,8 @@ package kotlin.script.experimental.api
object ScriptDefinitionProperties {
val name by typedKey<String>()
val name by typedKey<String>() // Name of the script type, by default "Kotlin script"
val fileExtension by typedKey<String>()
val fileExtension by typedKey<String>() // default: "kts"
}
@@ -10,14 +10,13 @@ import kotlin.script.experimental.api.*
class PassThroughCompilationConfigurator(val environment: ChainedPropertyBag) : ScriptCompilationConfigurator {
override val defaultConfiguration = ScriptCompileConfiguration(
ScriptCompileConfigurationParams.baseClass to environment[ScriptingEnvironmentProperties.baseClass]
)
override val defaultConfiguration = ScriptCompileConfiguration(environment)
override suspend fun baseConfiguration(scriptSource: ScriptSource): ResultWithDiagnostics<ScriptCompileConfiguration> =
defaultConfiguration.asSuccess()
override suspend fun refineConfiguration(
script: ScriptSource,
configuration: ScriptCompileConfiguration,
processedScriptData: ProcessedScriptData
): ResultWithDiagnostics<ScriptCompileConfiguration> =
@@ -9,8 +9,6 @@ import java.io.File
import java.net.URL
import kotlin.script.experimental.api.*
fun ScriptSourceFragments.isWholeFile(): Boolean = fragments?.isEmpty() ?: true
fun ScriptSource.getScriptText(): String = when {
text != null -> text!!
location != null ->
@@ -20,13 +18,13 @@ fun ScriptSource.getScriptText(): String = when {
fun getMergedScriptText(script: ScriptSource, configuration: ScriptCompileConfiguration): String {
val originalScriptText = script.getScriptText()
val sourceFragments = configuration.getOrNull(ScriptCompileConfigurationParams.scriptSourceFragments)
return if (sourceFragments == null || sourceFragments.isWholeFile()) {
val sourceFragments = configuration.getOrNull(ScriptCompileConfigurationProperties.sourceFragments)
return if (sourceFragments == null || sourceFragments.isEmpty()) {
originalScriptText
} else {
val sb = StringBuilder(originalScriptText.length)
var prevFragment: ScriptSourceNamedFragment? = null
for (fragment in sourceFragments!!.fragments!!) {
for (fragment in sourceFragments) {
val fragmentStartPos = fragment.range.start.absolutePos
val fragmentEndPos = fragment.range.end.absolutePos
if (fragmentStartPos == null || fragmentEndPos == null)