KotlinScriptEngine

Executes Kotlin scripts in runtime.

Wraps around the official implementation of the JSR-223 ScriptEngine for the Kotlin language.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

Direct reference to the wrapped JSR-223 ScriptEngine.

Functions

Link copied to clipboard
fun evaluate(scriptFile: FileHandle): Any?

Executes the selected scriptFile. Returns the last script's expression as the result. If unable to execute the script, ScriptEngineException will be thrown.

fun evaluate(script: String): Any?

Executes the selected script. Returns the last script's expression as the result. If unable to execute the script, ScriptEngineException will be thrown.

Link copied to clipboard
inline fun <T> evaluateAs(scriptFile: FileHandle): T

Executes the selected scriptFile and returns an instance of T. If the script is not an instance of T, ClassCastException will be thrown. If unable to execute the script, ScriptEngineException will be thrown.

inline fun <T> evaluateAs(script: String): T

Executes the selected script and returns an instance of T. If the script is not an instance of T, ClassCastException will be thrown. If unable to execute the script, ScriptEngineException will be thrown.

Link copied to clipboard
fun evaluateOn(receiver: Any, scriptFile: FileHandle, receiverVariableName: String = getRandomReceiverName())

Executes the selected scriptFile on the receiver object. The receiver will be available as this throughout the script, as well as under the value of receiverVariableName. If no variable name is given, it will be chosen randomly.

fun evaluateOn(receiver: Any, script: String, receiverVariableName: String = getRandomReceiverName())

Executes the selected script on the receiver object. The receiver will be available as this throughout the script, as well as under the value of receiverVariableName. If no variable name is given, it will be chosen randomly.

Link copied to clipboard
inline operator fun <T> get(variable: String): T?

Retrieves the value assigned to variable in the context of this engine.

Link copied to clipboard
fun import(import: String, alias: String? = null)

Imports the selected import using an optional alias. Wildcard imports using * are supported, but they cannot have an alias.

Link copied to clipboard
fun importAll(vararg imports: String)
fun importAll(imports: Iterable<String>)

Imports the selected imports within the script context. Wildcard imports using * are accepted. The imports will be available in future scripts.

Link copied to clipboard
fun remove(variable: String): Any?

Removes the variable from the context of this engine. Returns the value assigned to the variable. Returns null if no value is assigned to variable.

Link copied to clipboard
operator fun <T> set(variable: String, value: T)

Assigns the selected value to the variable name in the context of this engine. The variable will be available in the future scripts.

Link copied to clipboard
fun setPackage(name: String)

Sets the package of the future scripts to name.