https://kotlinlang.org logo
#scripting
Title
# scripting
m

molikuner

01/20/2023, 7:55 PM
Hi, I was playing around with custom scripting definitions and I found the
scriptExecutionWrapper
for
ScriptEvaluationConfiguration
. I though I could use this to do some custom action based on the result of the script. This works when using the following function from within a simple main:
Copy code
fun File.evaluate(args: List<String>): ResultWithDiagnostics<EvaluationResult> {
  return BasicJvmScriptingHost().evalWithTemplate<CustomScript>(this.toScriptSource()) {
    constructorArgs(args.toTypedArray())
  }
}
Unfortunately, it fails when trying to use the script definition for standalone scripting like this:
Copy code
kotlinc -cp kt-custom-scripting-1.0-SNAPSHOT-jar-with-dependencies.jar -script example.custom.kts
I can see, that the script itself gets executed, but afterwards there is a
ClassCastException
, due to different class loaders. I can also verify that, using this scriptExecutionWrapper:
Copy code
scriptExecutionWrapper<Any> { script ->
    val executionClassLoader = script::class.java.classLoader

    println("executionClassLoader: $executionClassLoader")

    val scriptInstance = script()

    val scriptInstanceClassLoader = scriptInstance::class.java.classLoader

    println("scriptInstanceClassLoader: $scriptInstanceClassLoader")

    scriptInstance as CustomScript // this fails due to different ClassLoader

    // some action, that I want to do
    println(scriptInstance.someProperty)

    scriptInstance
}
Here is the output, whey trying to run this:
Copy code
executionClassLoader: java.net.URLClassLoader@3e08ff24
Hello from custom script!
scriptInstanceClassLoader: org.jetbrains.kotlin.scripting.compiler.plugin.impl.CompiledScriptClassLoader@75af9a18
error: class Example_custom cannot be cast to class com.molikuner.custom.scripting.CustomScript (Example_custom is in unnamed module of loader org.jetbrains.kotlin.scripting.compiler.plugin.impl.CompiledScriptClassLoader @75af9a18; com.molikuner.custom.scripting.CustomScript is in unnamed module of loader java.net.URLClassLoader @3a50b75c) (example.custom.kts): java.lang.ClassCastException: class Example_custom cannot be cast to class com.molikuner.custom.scripting.CustomScript (Example_custom is in unnamed module of loader org.jetbrains.kotlin.scripting.compiler.plugin.impl.CompiledScriptClassLoader @75af9a18; com.molikuner.custom.scripting.CustomScript is in unnamed module of loader java.net.URLClassLoader @3a50b75c)
The script itself contains this:
Copy code
someProperty = "foo"
println("Hello from custom script!")
Is there any way to work around this or is there an other way to run a custom action after the script was executed including having access to the data of the script?
i

ilya.chernikov

01/24/2023, 12:54 PM
Hi. This wasn't designed as a generic feature, but rather as a solution for some repl-specific requests. So it is not implemented in the evaluator embedded into the
kotlinc
. Please, file an issue to our YT, and we'll try to consider support for it eventually.
m

molikuner

01/24/2023, 1:43 PM
Hi, thanks for the response. I've created https://youtrack.jetbrains.com/issue/KT-56144 Please update/add info as needed.
🙏 1
16 Views