Add shared script instances support, fix and refactor evaluation accordingly

also fix arguments order in the evaluator
This commit is contained in:
Ilya Chernikov
2019-01-07 18:10:57 +01:00
parent 7bb4233e17
commit 4571e273a4
7 changed files with 130 additions and 57 deletions
@@ -7,6 +7,7 @@
package kotlin.script.experimental.api
import kotlin.reflect.KClass
import kotlin.script.experimental.util.PropertiesCollection
interface ScriptEvaluationConfigurationKeys
@@ -46,11 +47,27 @@ val ScriptEvaluationConfigurationKeys.providedProperties by PropertiesCollection
*/
val ScriptEvaluationConfigurationKeys.constructorArgs by PropertiesCollection.key<List<Any?>>()
/**
* A map that is used to store evaluated script instances; if provided - the evaluator will try to get imported script from the map and
* only create/evaluate instances if not found, and evaluator will put newly created instances into the map
* This allows to have a single instance of the script if it is imported several times via different import paths.
*/
val ScriptEvaluationConfigurationKeys.scriptsInstancesSharingMap by PropertiesCollection.key<MutableMap<KClass<*>, Any>>()
/**
* A helper to enable scriptsInstancesSharingMap with default implementation
*/
fun ScriptEvaluationConfiguration.Builder.enableScriptsInstancesSharing() {
this {
scriptsInstancesSharingMap(HashMap())
}
}
/**
* The script evaluation result value
*/
sealed class ResultValue {
class Value(val name: String, val value: Any?, val type: String) : ResultValue() {
class Value(val name: String, val value: Any?, val type: String, val scriptInstance: Any) : ResultValue() {
override fun toString(): String = "$name: $type = $value"
}
@@ -66,15 +66,14 @@ fun File.toScriptSource(): SourceCode = FileScriptSource(this)
/**
* The implementation of the ScriptSource for a script in a String
*/
open class StringScriptSource(val source: String) : SourceCode {
open class StringScriptSource(val source: String, override val name: String? = null) : SourceCode {
override val text: String get() = source
override val name: String? = null
override val locationId: String? = null
}
/**
* Converts the String into the SourceCode
*/
fun String.toScriptSource(): SourceCode = StringScriptSource(this)
fun String.toScriptSource(name: String? = null): SourceCode = StringScriptSource(this, name)