KotlinScript

class KotlinScript(nullGlobalContext: Boolean = false) : AbstractScript

A script engine wrapper for dynamically evaluating Kotlin source code using the kts extension.

Stores the variables in a single ScriptVariables holder, the only JSR 223 binding, and generates Kotlin val declarations that read each one back and cast it to a type the script can name. bindings and __variables are reserved names. A value whose runtime class cannot be named, such as the private list class behind listOf(1, 2), is cast to its nearest public class or interface.

Common literal JVM-termination calls (System.exit, exitProcess, Runtime.getRuntime().exit/halt) are rejected on a best-effort basis via ScriptGuards. This is a convenience against accidental termination, not a security sandbox (see ScriptGuards) — run untrusted scripts in an isolated process or JVM.

close does nothing, because the Kotlin engine holds no resources to release.

Parameters

nullGlobalContext

if true, sets the global scope bindings to null on initialization

See also

Constructors

Link copied to clipboard
constructor(nullGlobalContext: Boolean = false)

Properties

Link copied to clipboard
Link copied to clipboard

Generates Kotlin val declarations that retrieve every bound variable from the ScriptVariables holder and cast it to a type the script can name, with its type arguments.

Functions

Link copied to clipboard
open fun add(name: String, value: Any, vararg types: KType)
Link copied to clipboard
open override fun close()
Link copied to clipboard

Evaluates Kotlin code and returns its result.

Link copied to clipboard
open fun params(name: String, types: Array<out KType>): String
Link copied to clipboard
fun resetContext(nullGlobalContext: Boolean)
Link copied to clipboard
open fun resetForReuse(nullGlobalContext: Boolean)