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

Rob Elliot

10/11/2021, 3:44 PM
Is there a way to guarantee that a script is pure? So it has zero side effects and just returns a value? My idea is that Kotlin is expressive enough to write typesafe configuration files directly in it - so an application could specify a data class or interface representing its config, and then configurers can write a
.kts
file that returns an instance of that type, and by setting up their IDE to have the classpath available then they will find out when configuring it whether or not their configuration will compile, and the application can gain compilation validation of its config by evaluating the file. But that means evaluating code that could theoretically do anything. I’ve implemented it using JSR-223 & a
SecurityManager
to try and prevent unwanted execution here: https://gist.github.com/Mahoney/8979e520b7477c9a9771fe3e1da3472a but it feels a bit clunky, and the
SecurityManager
is deprecated in Java 17 for removal in future Java releases. As I’ve just found out that Kotlin has its own scripting API, I wondered if it could be run in such a way?
v

Vampire

10/11/2021, 4:10 PM
I have no idea whether it will work, but maybe you could have some kotlin compiler plugin that will then verify only allowed APIs are used.
🙏 1
i

ilya.chernikov

10/11/2021, 5:26 PM
Unfortunately there is no explicit compiler-enforceable pureness that you can reuse in scripting yet. We have thought about similar scenarios and there are plans to add something like precise import control to the scripting API, that would allow some sandboxing, but it is not implemented yet. And also whether it will be sufficient for secure sandboxing - remains to be investigated too. But there is some control that you have right now, so you can experiment with it, if you like. With scripting API you can control the script compilation classpath (including stdlib and reflect) and also evaluation classloaders, so this may give you some sandboxing already.
🙏 1
👍 1
e

elect

10/28/2021, 3:29 PM
@Rob Elliot Did you go further with this? I'm also interested and I'd like to hear your experience with it, if any
r

Rob Elliot

11/03/2021, 6:35 PM
Nope, not yet.
3 Views