Hi guys! I have a question about changing the defi...
# scripting
j
Hi guys! I have a question about changing the definition of a class/jar used by my scripts. I have a server that takes code (a String) as input, compiles/evaluates the code and returns some result. To do that I instantiate a BasicJvmScriptingHost that is shared between requests (code of the script host is more or less https://pastebin.com/gtPvPaT1). Scripts are allowed to depend on some JARs, and one of those JARs contains a single generated class whose definition will change during the server lifetime. Is it possible to make my scripts depend on the latest definition of this JAR without needing to restart my server or recreating a host ? If necessary this generated class could also be contained in a Kotlin Script that we would make accessible to the other scripts (if it's possible). Thanks 🙂
c
The traditional way of implementing this is to use a wrapper. The downside is that you have to start two JVM's (one for the wrapper, and one for the actual jar file).
j
Do you have any example of what you are describing ? 🙂
c
Sure, the Kobalt source 🙂
There's
kobaltw
which is a small piece of code that looks up existing downloaded jar files, checks if a newer version is available, possibly downloads it. Then launch the
main
in that jar file.
(actually the version check is only to display at the end of the run "A new version is available, install it with
./kobaltw --update
, or something like that).
e
You can update the compilation configuration with the new dependency right there in the evaluate function. You can create a new compilation configuration from an existing one using the constructor that takes in a
baseConfigurations
arg. Something like
Copy code
class ScriptHost {
  // ...
  private val compilationConfiguration = createCompilationConfigurationFromTemplate( ... ) {
    jvm {
      // ... Remove dynamic jar declaration
    }
  }
 
  fun evaluate(code: String): CodeEvaluationResult {
    // ...

    val newConfig = ScriptCompilationConfiguration(
      compilationConfiguration
    ) {
        dependencies.append(JvmDependency(<updated dynamic jar file>))
    }

    val evaluationResult = 
      jvmHost.eval(scriptSource, newConfig, null)

    // ...
  }
}