Hello, I'm using JSR-223 to evaluate a script. Aft...
# scripting
g
Hello, I'm using JSR-223 to evaluate a script. After evaluation I want to get the variables declared in that context but I don't seam to be able to. All I can get are the variables that I've manually added to the engine (with
ScriptEngine.put()
method) and 2 internal variables:
kotlin.script.engine
&
kotlin.script.state
. Is there a way to get the variables declared within the script?
i
Unfortunately, it is not supported yet. The mapping from JSR-223 bindings is performed towards script (so if you
ScriptEngine.put()
some variable to the engine, subsequent script will see it as a variable) but script variables are not written back. Needs to be addressed at some point, but not yet done. Feel free to file an issue in YT for easier tracking.
g
Thanks @ilya.chernikov. I've seen that as an alternative to JSR-223 there is another way to run Kotlin scripts using
kotlin.script.experimental.api.*
. Does this support access to declared variables?
i
Yes, via this API you can have an access to the instance of the script object, that contains all variables as properties. In generic case you'll need the reflection to get access to it though. Theoretically you can extract the same object from JSR-223 too (via
kotlin.script.state
), since the Kotlin JSR-223 engine is implemented on top of the same API, but this won't be easy.
g
This is great news. Is there any documentation about the experimental API that can get me started?
i
Real documentation is in progress, so I recommend to start from https://github.com/Kotlin/kotlin-script-examples
g
Thank you