Skip to content
Language-independent scripting environment
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.ci
gradle/wrapper
src
.gitignore
.travis.yml
CHANGES.md
CONTRIBUTING.md
LICENSE
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
jscriptbox.png
jscriptbox.svg
spotless.eclipseformat.xml
spotless.importorder
spotless.license.java

README.md

JScriptBox: Make your scripting API language-independent

Maven artifact Latest version Javadoc License Apache

Changelog Travis CI Live chat

When exposing a scripting API, you provide some variables and functions to the script, run the script, then look at which outputs were set and/or functions were called. JScriptBox provides a mechanism for exposing a Java API to a scripting language in a way which is independent of the script language. This means that if you write your code using JScriptBox, script authors can use any language supported by JSR-223, such as JavaScript, Ruby, Python, Scheme, etc.

At present, only JavaScript is being used in the wild (in the FreshMark project), but PR's which enhance support for other languages are welcome.

Example

private int square(int x) {
	return x * x;
}

@Test
public void example() throws ScriptException {
	TypedScriptEngine engine = JScriptBox.create()
		.set("square").toFunc1(this::square)
		.set("x").toValue(9)
		.buildTyped(Nashorn.language());
	int squareOfX = engine.eval("square(x)", Integer.class);
	Assert.assertEquals(81, squareOfX);
}

In the code above, we provide a square function and an x variable. We then build a Nashorn Javascript engine, but we could have passed any other language too. To add a new language, just implement Language (the existing Nashorn can be a helpful starting point).

Acknowledgements

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.