Skip to content

Commit b8e61bd

Browse files
committed
#601: fixed JavaScript handling for Java 15+
1 parent 58338d7 commit b8e61bd

5 files changed

Lines changed: 123 additions & 6 deletions

File tree

build.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@
165165
<include name="jmathplot.jar" />
166166
<include name="antlr-runtime-4.5.3.jar" />
167167
<include name="nico-camargo-toolbar-icons-4.0.jar" />
168+
<include name="nashorn-core-15.7.jar" />
169+
<include name="asm-7.3.1.jar" />
170+
<include name="asm-commons-7.3.1.jar" />
171+
<include name="asm-tree-7.3.1.jar" />
172+
<include name="asm-util-7.3.1.jar" />
173+
<include name="asm-analysis-7.3.1.jar" />
168174
<include name="vstaricon.png" />
169175
</zipfileset>
170176
<zipfileset file="VStar.sh" prefix="vstar" filemode="755" />
@@ -200,6 +206,12 @@
200206
<include name="jmathplot.jar" />
201207
<include name="antlr-runtime-4.5.3.jar" />
202208
<include name="nico-camargo-toolbar-icons-4.0.jar" />
209+
<include name="nashorn-core-15.7.jar" />
210+
<include name="asm-7.3.1.jar" />
211+
<include name="asm-commons-7.3.1.jar" />
212+
<include name="asm-tree-7.3.1.jar" />
213+
<include name="asm-util-7.3.1.jar" />
214+
<include name="asm-analysis-7.3.1.jar" />
203215
<include name="vstaricon.png" />
204216
</zipfileset>
205217
<zipfileset file="${install_files}/VStar.exe" prefix="vstar" />

extlib/ReadMe.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ code.
2525
o nico-camargo-toolbar-icons-4.0.jar
2626
Toolbar icons created by Citizen Sky participant, Nico Camargo.
2727

28+
o nashorn-core-15.7.jar
29+
Standalone OpenJDK Nashorn JavaScript engine. Required for
30+
Tool -> Run Script... on Java 15+, where Nashorn is no longer
31+
bundled with the JDK.
32+
GPL-2 with Classpath exception (same family as OpenJDK). VStar
33+
remains AGPL-3; the Classpath exception is what permits linking.
34+
Do not copy Nashorn sources into VStar files. Corresponding source:
35+
https://github.com/openjdk/nashorn
36+
37+
o asm-7.3.1.jar
38+
o asm-commons-7.3.1.jar
39+
o asm-tree-7.3.1.jar
40+
o asm-util-7.3.1.jar
41+
o asm-analysis-7.3.1.jar
42+
ASM bytecode library, a dependency of Nashorn.
43+
BSD 3-Clause. Copyright (c) 2000-2011 INRIA, France Telecom.
44+
https://asm.ow2.io/
45+
2846
o vstaricon.png
2947
This image was also created by Nico Camargo and is used for the
3048
splash screen and desktop icon.
@@ -63,6 +81,22 @@ See the URL for the web page of each project for more information.
6381

6482
* ANTLR4 http://www.antlr.org
6583

84+
o GNU General Public License, version 2 with Classpath exception
85+
gpl-2.0.txt (GPL-2 text)
86+
gpl-2.0-classpath-exception.txt (Classpath exception addendum)
87+
Full Nashorn LICENSE (GPL-2 + exception):
88+
https://github.com/openjdk/nashorn/blob/main/LICENSE
89+
GPL-2 without the Classpath exception is not compatible with
90+
AGPL-3; the exception is required for shipping Nashorn with VStar.
91+
92+
* OpenJDK Nashorn https://github.com/openjdk/nashorn
93+
94+
o BSD 3-Clause License
95+
asm-bsd-3-clause.txt
96+
https://asm.ow2.io/license.html
97+
98+
* ASM https://asm.ow2.io/
99+
66100
If you are a developer who wishes to contribute toward coding or
67101
testing VStar, the following libraries are required for unit testing
68102
and CI purposes. None of these libraries are redistributed as part

script/create_mac_app.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ cp extlib/jfreechart-1.5.6.jar VStar.app/Contents/MacOS/extlib
1717
cp extlib/jmathplot.jar VStar.app/Contents/MacOS/extlib
1818
cp extlib/antlr-runtime-4.5.3.jar VStar.app/Contents/MacOS/extlib
1919
cp extlib/nico-camargo-toolbar-icons-4.0.jar VStar.app/Contents/MacOS/extlib
20+
cp extlib/nashorn-core-15.7.jar VStar.app/Contents/MacOS/extlib
21+
cp extlib/asm-7.3.1.jar VStar.app/Contents/MacOS/extlib
22+
cp extlib/asm-commons-7.3.1.jar VStar.app/Contents/MacOS/extlib
23+
cp extlib/asm-tree-7.3.1.jar VStar.app/Contents/MacOS/extlib
24+
cp extlib/asm-util-7.3.1.jar VStar.app/Contents/MacOS/extlib
25+
cp extlib/asm-analysis-7.3.1.jar VStar.app/Contents/MacOS/extlib
2026
cp extlib/vstaricon.png VStar.app/Contents/MacOS/extlib

script/webstart/jnlp/vstar.jnlp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
<jar href="extlib/javacsv.jar"/>
4141
<jar href="extlib/antlr-runtime-4.5.3.jar"/>
4242
<jar href="extlib/nico-camargo-toolbar-icons-4.0.jar"/>
43+
<jar href="extlib/nashorn-core-15.7.jar"/>
44+
<jar href="extlib/asm-7.3.1.jar"/>
45+
<jar href="extlib/asm-commons-7.3.1.jar"/>
46+
<jar href="extlib/asm-tree-7.3.1.jar"/>
47+
<jar href="extlib/asm-util-7.3.1.jar"/>
48+
<jar href="extlib/asm-analysis-7.3.1.jar"/>
4349

4450
</resources>
4551

src/org/aavso/tools/vstar/scripting/ScriptRunner.java

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
/**
3838
* This class runs a VStar script.
39+
*
40+
* JavaScript is executed by the standalone OpenJDK Nashorn engine
41+
* (nashorn-core on the classpath). Nashorn was removed from the JDK
42+
* in Java 15, which caused Tool -> Run Script... to fail (issue #601).
3943
*/
4044
public class ScriptRunner {
4145

@@ -54,11 +58,38 @@ public class ScriptRunner {
5458
* Constructor
5559
*/
5660
public ScriptRunner(boolean fromFileChooser) {
57-
manager = new ScriptEngineManager();
58-
jsEngine = manager.getEngineByName("javascript");
59-
compilable = (Compilable) jsEngine;
60-
bindings = jsEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
61-
bindings.put("vstar", VStarScriptingAPI.getInstance());
61+
try {
62+
// Use this class's loader so the standalone Nashorn engine (required
63+
// since the JDK removed it in Java 15) is found when running from a jar.
64+
manager = new ScriptEngineManager(ScriptRunner.class.getClassLoader());
65+
jsEngine = manager.getEngineByName("javascript");
66+
if (jsEngine == null) {
67+
jsEngine = manager.getEngineByName("nashorn");
68+
}
69+
if (jsEngine == null) {
70+
error = "No JavaScript engine is available. "
71+
+ "Nashorn was removed from the JDK in Java 15; "
72+
+ "ensure nashorn-core and its ASM jars are on the classpath.";
73+
} else {
74+
compilable = (Compilable) jsEngine;
75+
bindings = jsEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
76+
if (bindings == null) {
77+
bindings = jsEngine.getBindings(ScriptContext.ENGINE_SCOPE);
78+
}
79+
if (bindings == null) {
80+
bindings = jsEngine.createBindings();
81+
jsEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
82+
}
83+
bindings.put("vstar", VStarScriptingAPI.getInstance());
84+
// Nashorn provides print() but not println(); some sample scripts use println.
85+
jsEngine.eval("if (typeof println === 'undefined') { println = print; }");
86+
}
87+
} catch (Throwable t) {
88+
jsEngine = null;
89+
compilable = null;
90+
error = "Failed to initialise JavaScript engine: "
91+
+ t.getLocalizedMessage();
92+
}
6293
if (fromFileChooser) {
6394
scriptFileChooser = new JFileChooser();
6495
}
@@ -75,6 +106,13 @@ public static ScriptRunner getInstance() {
75106
* Run script from a chosen file.
76107
*/
77108
public void runScript() {
109+
if (jsEngine == null || compilable == null) {
110+
MessageBox.showErrorDialog("Script Error",
111+
error != null ? error
112+
: "No JavaScript engine is available.");
113+
return;
114+
}
115+
78116
File scriptFile = null;
79117

80118
int returnVal = scriptFileChooser.showOpenDialog(DocumentManager
@@ -92,6 +130,13 @@ public void runScript(File scriptFile) {
92130
FileReader reader = null;
93131

94132
try {
133+
if (jsEngine == null || compilable == null) {
134+
MessageBox.showErrorDialog("Script Error",
135+
error != null ? error
136+
: "No JavaScript engine is available.");
137+
return;
138+
}
139+
95140
setError(null);
96141
setWarning(null);
97142

@@ -161,6 +206,20 @@ public void setWarning(String warning) {
161206
* The value to which to bind.
162207
*/
163208
public void bind(String name, Object value) {
164-
bindings.put(name, value);
209+
if (bindings != null) {
210+
bindings.put(name, value);
211+
}
212+
}
213+
214+
/**
215+
* Evaluate a JavaScript snippet in the current engine context.
216+
* Package-private for unit tests.
217+
*/
218+
Object eval(String script) throws ScriptException {
219+
if (jsEngine == null || compilable == null) {
220+
throw new ScriptException(error != null ? error
221+
: "No JavaScript engine is available.");
222+
}
223+
return compilable.compile(script).eval();
165224
}
166225
}

0 commit comments

Comments
 (0)