Created
November 23, 2017 11:27
-
-
Save dant3/601366c98df4284fab43198e3176548a to your computer and use it in GitHub Desktop.
KotlinScriptDemo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.script.ScriptEngine | |
import javax.script.ScriptEngineManager | |
import kotlin.system.exitProcess | |
fun ScriptEngine.bindVal(name: String, type: String, value: Any) { | |
put(name, value) | |
eval("val $name = bindings[\"$name\"] as $type") | |
} | |
fun main(args: Array<String>) { | |
val factory = ScriptEngineManager().getEngineByExtension("kts").factory | |
val engine = factory!!.scriptEngine | |
engine.bindToLocal("a", "Int", 123) | |
engine.bindToLocal("b", "List<Int>", listOf(1, 2, 3)) | |
println(engine.eval("b.sum() + a")) | |
exitProcess(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment