https://github.com/diffplug/jscriptbox
Language-independent scripting environment
https://github.com/diffplug/jscriptbox
Last synced: 5 months ago
JSON representation
Language-independent scripting environment
- Host: GitHub
- URL: https://github.com/diffplug/jscriptbox
- Owner: diffplug
- License: apache-2.0
- Created: 2015-09-18T23:22:04.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2023-02-05T09:03:26.000Z (almost 3 years ago)
- Last Synced: 2025-04-23T08:08:03.664Z (8 months ago)
- Language: Java
- Size: 583 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
#
JScriptBox: Make your scripting API language-independent
[](https://central.sonatype.com/artifact/com.diffplug.jscriptbox/jscriptbox/3.0.1)
[](CHANGELOG.md)
[](https://javadoc.io/doc/com.diffplug.jscriptbox/jscriptbox)
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.](http://stackoverflow.com/a/14864450/1153071)
At present, only JavaScript is being used in the wild (in the [FreshMark](https://github.com/diffplug/freshmark) project), but PR's which enhance support for other languages are welcome.
## Example
```java
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](src/main/java/com/diffplug/jscriptbox/Language.java) (the existing [Nashorn](src/main/java/com/diffplug/jscriptbox/javascript/Nashorn.java) can be a helpful starting point).
## Acknowledgements
* Built by [gradle](http://gradle.org/).
* Tested by [junit](http://junit.org/).
* Maintained by [DiffPlug](http://www.diffplug.com/).