Add configurable scripting host to Olive
- Fix normalization again for reused scripting host - Reduce verbosity
This commit is contained in:
@@ -25,11 +25,18 @@ private fun <T> Event<T>.restoreListeners(store: Map<Event<*>, List<(Any) -> Uni
|
|||||||
listeners.retainAll(store[this] ?: emptyList<T>())
|
listeners.retainAll(store[this] ?: emptyList<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum class OliveScriptHost {
|
||||||
|
JSR223,
|
||||||
|
JSR223_REUSE,
|
||||||
|
KOTLIN_SCRIPT
|
||||||
|
}
|
||||||
|
|
||||||
class Olive<P : Program>(val resources: Resources? = null) : Extension {
|
class Olive<P : Program>(val resources: Resources? = null) : Extension {
|
||||||
override var enabled: Boolean = true
|
override var enabled: Boolean = true
|
||||||
var session: Session? = null
|
var session: Session? = null
|
||||||
|
var scriptHost = OliveScriptHost.JSR223_REUSE
|
||||||
|
|
||||||
internal var scriptChange: (String)->Unit = {}
|
internal var scriptChange: (String) -> Unit = {}
|
||||||
|
|
||||||
var script = "src/main/kotlin/live.kts"
|
var script = "src/main/kotlin/live.kts"
|
||||||
set(value) {
|
set(value) {
|
||||||
@@ -70,7 +77,7 @@ class Olive<P : Program>(val resources: Resources? = null) : Extension {
|
|||||||
|
|
||||||
trackedListeners.forEach { it.saveListeners(store) }
|
trackedListeners.forEach { it.saveListeners(store) }
|
||||||
|
|
||||||
fun setupScript(scriptFile:String) {
|
fun setupScript(scriptFile: String) {
|
||||||
watcher?.stop()
|
watcher?.stop()
|
||||||
val f = File(scriptFile)
|
val f = File(scriptFile)
|
||||||
if (!f.exists()) {
|
if (!f.exists()) {
|
||||||
@@ -94,14 +101,23 @@ class Olive<P : Program>(val resources: Resources? = null) : Extension {
|
|||||||
""".trimIndent())
|
""".trimIndent())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val jsr233ObjectLoader = if (scriptHost == OliveScriptHost.JSR223_REUSE) ScriptObjectLoader() else null
|
||||||
|
|
||||||
watcher = program.watchFile(File(script)) {
|
watcher = program.watchFile(File(script)) {
|
||||||
try {
|
try {
|
||||||
|
logger.info("change detected, reloading script")
|
||||||
val futureFunc = GlobalScope.async {
|
val futureFunc = GlobalScope.async {
|
||||||
val start = System.currentTimeMillis()
|
val start = System.currentTimeMillis()
|
||||||
val f = loadFromScriptKSH<P.() -> Unit>(it)
|
|
||||||
|
val loadedFunction = when (scriptHost) {
|
||||||
|
OliveScriptHost.JSR223_REUSE -> loadFromScript(it, jsr233ObjectLoader!!)
|
||||||
|
OliveScriptHost.JSR223 -> loadFromScript(it)
|
||||||
|
OliveScriptHost.KOTLIN_SCRIPT -> loadFromScriptKSH<P.() -> Unit>(it)
|
||||||
|
}
|
||||||
|
|
||||||
val end = System.currentTimeMillis()
|
val end = System.currentTimeMillis()
|
||||||
logger.info { "loading script took ${end - start}ms" }
|
logger.info { "loading script took ${end - start}ms" }
|
||||||
f
|
loadedFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
program.launch {
|
program.launch {
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package org.openrndr.extra.olive
|
package org.openrndr.extra.olive
|
||||||
|
|
||||||
|
import mu.KotlinLogging
|
||||||
import kotlin.reflect.KMutableProperty1
|
import kotlin.reflect.KMutableProperty1
|
||||||
import kotlin.reflect.KProperty1
|
import kotlin.reflect.KProperty1
|
||||||
import kotlin.reflect.full.declaredMemberProperties
|
import kotlin.reflect.full.declaredMemberProperties
|
||||||
import kotlin.reflect.jvm.jvmName
|
import kotlin.reflect.jvm.jvmName
|
||||||
|
|
||||||
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
private val store = mutableMapOf<String, Any>()
|
private val store = mutableMapOf<String, Any>()
|
||||||
|
|
||||||
@@ -19,11 +21,9 @@ fun clearReloadables() {
|
|||||||
* A class with which persistent state can be reloaded from inside Olive scripts.
|
* A class with which persistent state can be reloaded from inside Olive scripts.
|
||||||
*/
|
*/
|
||||||
open class Reloadable {
|
open class Reloadable {
|
||||||
|
|
||||||
|
|
||||||
// -- since kotlin 1.3.61 the scripting host prepends class names with the host id
|
|
||||||
private fun normalizeClassName(name: String): String {
|
private fun normalizeClassName(name: String): String {
|
||||||
return name.replace(Regex("ScriptingHost[0-9a-f]+_"), "")
|
return name.replace(Regex("ScriptingHost[0-9a-f]+_"), // -- since kotlin 1.3.61 the scripting host prepends class names with the host id
|
||||||
|
"").replace(Regex("Line_[0-9]+"),"") // -- when reusing the script engine the line number increments.
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,14 +41,14 @@ open class Reloadable {
|
|||||||
val value = (e as KProperty1<Any, Any?>).get(existing)
|
val value = (e as KProperty1<Any, Any?>).get(existing)
|
||||||
val mp = (p as KMutableProperty1<Any, Any?>)
|
val mp = (p as KMutableProperty1<Any, Any?>)
|
||||||
mp.set(this, value as Any)
|
mp.set(this, value as Any)
|
||||||
println("reloaded property ${p.name} <- ${value}")
|
logger.info("reloaded property ${p.name} <- ${value}")
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
println("error while reloading property ${p.name}: ${e.message}")
|
logger.warn("error while reloading property ${p.name}: ${e.message}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println("no existing store found for $className")
|
logger.info("no existing store found for $className")
|
||||||
}
|
}
|
||||||
store[normalizeClassName(this::class.jvmName)] = this
|
store[normalizeClassName(this::class.jvmName)] = this
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user