Skip to content

diffplug/jscriptbox

Repository files navigation

JScriptBox: Make your scripting API language-independent

Maven artifact Changelog Javadoc

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

About

Language-independent scripting environment

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages