Skip to content

Commit 16cd066

Browse files
committed
JavaScriptScriptLanguage: make Nashorn work
Nashorn does not come with the Mozilla-specific loadClass and loadPackage functions available by default. But it does have a lovely backwards compatibility script that you can source, which adds them. See: http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html
1 parent 24fa775 commit 16cd066

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

‎src/main/java/org/scijava/plugins/scripting/javascript/JavaScriptScriptLanguage.java‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,19 @@ public boolean isRhino() {
8585
public ScriptEngine getScriptEngine() {
8686
final ScriptEngine engine = super.getScriptEngine();
8787
try {
88-
engine.eval("function load(path) {\n"
89-
+ " importClass(Packages." + contextClass(engine) + ");\n"
90-
+ " importClass(Packages.java.io.FileReader);\n"
91-
+ " var cx = Context.getCurrentContext();\n"
92-
+ " cx.evaluateReader(this, new FileReader(path), path, 1, null);\n"
93-
+ "}");
88+
if (isNashorn()) {
89+
// for Rhino compatibility, importClass and importPackage in particular
90+
engine.eval("load(\"nashorn:mozilla_compat.js\");");
91+
}
92+
if (isRhino()) {
93+
// for the load function, which is somehow otherwise unavailable (?)
94+
engine.eval("function load(path) {\n"
95+
+ " importClass(Packages." + contextClass(engine) + ");\n"
96+
+ " importClass(Packages.java.io.FileReader);\n"
97+
+ " var cx = Context.getCurrentContext();\n"
98+
+ " cx.evaluateReader(this, new FileReader(path), path, 1, null);\n"
99+
+ "}");
100+
}
94101
}
95102
catch (ScriptException e) {
96103
e.printStackTrace();

0 commit comments

Comments
 (0)