JSR-223 Scripting
Java scripting with BoxLang
Last updated
Java scripting with BoxLang
Last updated
JSR 223, also known as "Scripting for the Java Platform," is a specification that allows Java applications to integrate with scripting languages such as BoxLang, JavaScript, Groovy, Python, and others. It provides a standard API for accessing and embedding these scripting languages within Java code, enabling developers to mix and match Java and scripting languages seamlessly. This flexibility can enhance productivity and facilitate the rapid development of applications that require dynamic scripting capabilities.
BoxLang offers the JSR-233 runtime to integrate with BoxLang from any JVM language.
The BoxLang scripting package can be found here: ortus.boxlang.runtime.scripting
. The classes that will assist you are:
BoxCompiledScript
- Implements the JSR CompiledScript
interface (https://docs.oracle.com/en/java/javase/17/docs/api/java.scripting/javax/script/CompiledScript.html)
BoxScopeBindings
- Implements the JSR Bindings
interface (https://docs.oracle.com/en/java/javase/17/docs/api/java.scripting/javax/script/Bindings.html)
BoxScriptingContext
- Implements the JSR ScriptContext
interface (https://docs.oracle.com/en/java/javase/17/docs/api/java.scripting/javax/script/ScriptContext.html)
BoxScriptingEngine
- Implements the JSR ScriptEngine
and Compilable
https://docs.oracle.com/en/java/javase/17/docs/api/java.scripting/javax/script/ScriptEngine.html
https://docs.oracle.com/en/java/javase/17/docs/api//java.scripting/javax/script/Compilable.html
BoxScriptingFactory
- implements the JSR ScriptEngineFactory
https://docs.oracle.com/en/java/javase/17/docs/api/java.scripting/javax/script/ScriptEngineFactory.html
Script Factory - creates scripting engines and gets metadata about scripting engines.
Script Engine - provides a way to create bindings, scripts, and run statements.
Invocable - Our BoxLang engine also implements the scripting Invocable
interface so you can declare functions and classes (coming soon) and then execute them from the calling language.
Bindings - these are like scopes to BoxLang. The bridge between Java and BoxLang
Scripting Context - Like the BoxLang context object, it provides scope lookups and access to bindings.
Bindings are under the hood HashMaps
. They are used to bind your Java code to the BoxLang code. By default, in BoxLang, we provide three scopes you can bind bindings to:
Engine Scope
- The default scope which maps to the BoxLang variables
scope
Request Scope
- The JSR request scope maps to the BoxLang request
scope
Global Scope
- The JSR global scope maps to the BoxLang server
scope
To find out what engines are available in your platform you can run the following:
To get started, you need to get an instance of the BoxLang Scripting Engine. You can do so by using the Java ScriptEngineManager()
class or importing our BoxScriptingEngine
class.
You can also cast it to our class to get enhanced methods and functionality
If you ever need to send debugging information to the console from the BoxRuntime in the scripting engine, you can create a new script engine and pass the debug
flag to it.
This will start up the BoxRuntime
in debug mode.
Once you access the script engine, you can use the plethora of eval()
methods to execute the BoxLang source and bind with specific dynamic bindings. You can execute scripts from strings or reader objects. You can also compile a script/class into a CompiledScript
and then execute it at a later point in time via the compile()
methods.
You can use eval with the following signatures
Data can be passed into the engine by defining a Bindings object and passing it as a second parameter to the eval function. You will do so by using the createBindings()
method. If you casted the engine to our BoxScriptingEngine
class, you will also get a createBindings( Map m )
so you can quickly create bindings from a map of data.
Once you bind the engine with bindings before execution, you must get the modified bindings via the engine.getBindings()
method. If you don't do this, you will only have access to the simple hashmap to bind the engine.
You can also use the eval()
method to define functions, closures, or lambdas in BoxLang and execute them in your host language. We do so by evaluating the script, casting the engine to Invocable,
and using the invokeFunction()
method.
You can also use the invokeMethod( object, name, args )
function, which allows you to target a specific object, such as a BoxLang class, member method, struct, lambda, closure or collection of functions.
This is indeed truly powerful as you can not only invoke functions on objects, but also member methods in any valid BoxLang type.
Apart from executing strings, you can also compile BoxLang scripts and evaluate them using the compileScript( String ) or compileScript( Reader )
methods. You will get a Box CompiledScript
class, which you can then use the eval()
methods and binding methods at a later point in time.
JSR223 also allows you to dynamically create interface proxies for any functions or classes you map in the dynamic language. Let's say you want to create a nice BoxLang function that maps to a Java Runnable. In our example, we will create the run
function and then map that via JSR223 to the Runnable
interface so we can execute it as a runnable object.
As you can see from the sample above, you can use the getInterface( class<?> )
method to map the evaluated code to any interface of your choosing. Here are the two methods you can use for interfaces:
getInterface( Class<T> )
- Build a dynamic proxy from the evaluated function and the passed in class
getInterface( Object, Class<T> )
- Build a dynamic proxy from the passed in Object
and the passed in class.
Let's finish this section with another example. Using a struct and anonymous functions, let's build a BoxLang virtual object and treat it as a Runnable
interface.
We have also added the capability for your host language to seed your own String Writers into the engine so you can capture output. BoxLang can produce two types of output
System output - Bifs and components that send output to the System.out
Buffer output - A BoxLang request has an output String buffer that can be used to produce output which can be sent to console, web, etc.
The runtime source code can be found here: https://github.com/ortus-boxlang/BoxLang/tree/development/src/main/java/ortus/boxlang/runtime/scripting
We welcome any pull requests, testing, docs, etc.