Skip to content

Commit 799f290

Browse files
committed
Added gitter and a nice example.
1 parent ed3373e commit 799f290

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ output = [
77
link(shield('License Apache', 'license', 'Apache', 'blue'), 'https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)'),
88
'',
99
link(shield('Changelog', 'changelog', '{{version}}', 'brightgreen'), 'CHANGES.md'),
10-
link(image('Travis CI', 'https://travis-ci.org/{{org}}/{{name}}.svg?branch=master'), 'https://travis-ci.org/{{org}}/{{name}}')
10+
link(image('Travis CI', 'https://travis-ci.org/{{org}}/{{name}}.svg?branch=master'), 'https://travis-ci.org/{{org}}/{{name}}'),
11+
link(shield('Live chat', 'gitter', 'live chat', 'brightgreen'), 'https://gitter.im/{{org}}/{{name}}')
1112
].join('\n');
1213
-->
1314
[![Maven artifact](https://img.shields.io/badge/mavenCentral-com.diffplug.jscriptbox%3Ajscriptbox-blue.svg)](https://bintray.com/diffplug/opensource/jscriptbox/view)
@@ -17,17 +18,41 @@ output = [
1718

1819
[![Changelog](https://img.shields.io/badge/changelog-3.1.0--SNAPSHOT-brightgreen.svg)](CHANGES.md)
1920
[![Travis CI](https://travis-ci.org/diffplug/jscriptbox.svg?branch=master)](https://travis-ci.org/diffplug/jscriptbox)
21+
[![Live chat](https://img.shields.io/badge/gitter-live_chat-brightgreen.svg)](https://gitter.im/diffplug/jscriptbox)
2022
<!---freshmark /shields -->
2123

24+
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).
25+
26+
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.
27+
28+
## Example
2229

2330
<!---freshmark javadoc
2431
output = prefixDelimiterReplace(input, 'https://{{org}}.github.io/{{name}}/javadoc/', '/', stable);
2532
-->
2633

34+
```java
35+
private int square(int x) {
36+
return x * x;
37+
}
38+
39+
@Test
40+
public void example() throws ScriptException {
41+
TypedScriptEngine engine = JScriptBox.create()
42+
.set("square").toFunc1(this::square)
43+
.set("x").toValue(9)
44+
.buildTyped(Nashorn.language());
45+
int squareOfX = engine.eval("square(x)", Integer.class);
46+
Assert.assertEquals(81, squareOfX);
47+
}
48+
```
49+
50+
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 just as easily.
51+
2752
<!---freshmark /javadoc -->
2853

2954
## Acknowledgements
30-
* Scripts run by [JScriptBox](https://github.com/diffplug/jscriptbox).
55+
* Readme formatting by [FreshMark](https://github.com/diffplug/freshmark).
3156
* Bugs found by [findbugs](http://findbugs.sourceforge.net/), [as such](https://github.com/diffplug/durian-rx/blob/v1.0/build.gradle?ts=4#L92-L116).
3257
* Scripts in the `.ci` folder are inspired by [Ben Limmer's work](http://benlimmer.com/2013/12/26/automatically-publish-javadoc-to-gh-pages-with-travis-ci/).
3358
* Built by [gradle](http://gradle.org/).

src/test/java/com/diffplug/scriptbox/javascript/JScriptBoxNashornTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,24 @@
2525
import org.junit.Test;
2626

2727
import com.diffplug.jscriptbox.JScriptBox;
28+
import com.diffplug.jscriptbox.TypedScriptEngine;
2829
import com.diffplug.jscriptbox.javascript.Nashorn;
2930

3031
public class JScriptBoxNashornTest {
32+
private int square(int x) {
33+
return x * x;
34+
}
35+
36+
@Test
37+
public void example() throws ScriptException {
38+
TypedScriptEngine engine = JScriptBox.create()
39+
.set("square").toFunc1(this::square)
40+
.set("x").toValue(9)
41+
.buildTyped(Nashorn.language());
42+
int squareOfX = engine.eval("square(x)", Integer.class);
43+
Assert.assertEquals(81, squareOfX);
44+
}
45+
3146
@Test
3247
public void testBasicExpressions() throws ScriptException {
3348
ScriptEngine engine = JScriptBox.create().build(Nashorn.language());

0 commit comments

Comments
 (0)