JavaScript

A script engine wrapper for dynamically compiling and evaluating Java source code.

Supports adding named variables with type parameters, import declarations, and configurable isolation levels. The Java engine needs a global scope: resetForReuse always keeps one, while resetContext(true) removes it, after which evalScript fails.

Variable declarations use fully-qualified type names, so they need no imports. A value whose runtime class cannot be named, such as the private list class behind listOf(1, 2), is declared as its nearest public class or interface.

Common literal JVM-termination calls (System.exit, 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 Java engine holds no resources to release.

See also

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
Link copied to clipboard

Generates Java import statements for all registered import classes.

Link copied to clipboard

Generates Java-style public field declarations for all registered variables.

Functions

Link copied to clipboard
open fun add(name: String, value: Any, vararg types: KType)
Link copied to clipboard
fun <T> addImport(clazz: Class<T>)

Registers a Java class to be imported in generated scripts; the same as import, under a name Java can call.

Link copied to clipboard
fun assignIsolation(isolation: Isolation)

Sets the isolation level for the underlying JavaScriptEngine.

Link copied to clipboard
open override fun close()
Link copied to clipboard
fun eval(expr: String, action: String = "", verbose: Boolean = false): Any?

Evaluates a Java expr (optionally preceded by an action statement block) inside a generated Main.getValue() method, prepending any registered imports and variable declarations.

Link copied to clipboard
fun evalScript(script: String, verbose: Boolean = false): Any?

Evaluates a raw Java script string, prepending any registered import declarations.

Link copied to clipboard
fun <T> import(clazz: Class<T>)

Registers a Java class to be imported in generated scripts, by its canonical name, so a nested class is imported as Outer.Inner. Java callers use addImport, since import is a Java keyword.

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 override fun resetForReuse(nullGlobalContext: Boolean)

Resets the context and also clears the imports and restores the default isolation, so the next borrower from a pool starts clean.