4747 * An adapter of the JavaScript interpreter to the SciJava scripting interface.
4848 *
4949 * @author Curtis Rueden
50+ * @author Ulrik Guenther
5051 * @see ScriptEngine
5152 */
5253@ Plugin (type = ScriptLanguage .class , name = "JavaScript" )
@@ -55,6 +56,15 @@ public class JavaScriptScriptLanguage extends AdaptedScriptLanguage {
5556 @ Parameter
5657 private LogService log ;
5758
59+ static {
60+ // GraalVM's Graal.js needs Nashorn compatibility activated explicitly and we need
61+ // that to get importPackage, etc. One way to activate this is to set a system property,
62+ // which is done here in case we are running on GraalVM.
63+ if (System .getProperty ("java.vm.name" ).contains ("GraalVM" )) {
64+ System .setProperty ("polyglot.js.nashorn-compat" , "true" );
65+ }
66+ }
67+
5868 public JavaScriptScriptLanguage () {
5969 super ("javascript" );
6070 }
@@ -79,6 +89,17 @@ public boolean isRhino() {
7989 return getEngineName ().contains ("Rhino" );
8090 }
8191
92+ /**
93+ * Returns true iff this script language is using the
94+ * <a href="https://github.com/graalvm/graaljs">Graal.js</a> scripting engine.
95+ * Also see <a href="https://github.com/graalvm/graaljs/blob/master/docs/user/NashornMigrationGuide.md">The
96+ * Nashorn migration guide</a>.
97+ * True for GraalVM.
98+ */
99+ public boolean isGraalJS () {
100+ return getEngineName ().contains ("Graal.js" );
101+ }
102+
82103 /** Returns true iff the JVM appears to be the OpenJDK version of Java. */
83104 public boolean isOpenJDK () {
84105 final String vmName = System .getProperty ("java.vm.name" );
@@ -150,6 +171,7 @@ public Object decode(final Object object) {
150171 // -- Helper methods --
151172
152173 private String contextClass (final ScriptEngine engine ) {
174+ if (isGraalJS ()) return "org.graalvm.polyglot.Context" ;
153175 if (isNashorn ()) return "jdk.nashorn.internal.runtime.Context" ;
154176
155177 final String engineClassName = engine .getClass ().getName ();
0 commit comments