-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunJS.java
39 lines (34 loc) · 1.54 KB
/
RunJS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class RunJS {
public static class IO {
private final ScriptEngine engine;
public IO(final ScriptEngine engine) {
this.engine = engine;
}
public void print(final String message) {
System.out.print(message);
}
public void println(final String message) {
System.out.println(message);
}
public void include(final String file) throws Exception {
engine.eval(new InputStreamReader(new FileInputStream(file), "UTF-8"));
}
}
public static void main(final String[] args) throws ScriptException, IOException {
final ScriptEngineManager factory = new ScriptEngineManager();
final ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.put("io", new IO(engine));
engine.put("global", engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE));
engine.eval("var println = function() { io.println(Array.prototype.map.call(arguments, String).join(' ')); };");
engine.eval("var print = function() { io.print (Array.prototype.map.call(arguments, String).join(' ')); };");
engine.eval("var include = function(file) { io.include(file); }");
engine.eval(new InputStreamReader(new FileInputStream("script.js"), "UTF-8"));
}
}