Graal.JS/JDK-15 Fixes

Dropped Rhino to fully support Nashorn fallback with Graal.JS. this should work for pre-jdk-8 and jdk-15+
This commit is contained in:
Konloch 2021-07-21 06:05:09 -07:00
parent 605721b378
commit 6a121dd9e1
2 changed files with 27 additions and 10 deletions

10
pom.xml
View file

@ -243,6 +243,16 @@
<artifactId>webp-imageio</artifactId> <artifactId>webp-imageio</artifactId>
<version>0.2.1</version> <version>0.2.1</version>
</dependency> </dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>21.2.0</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>21.2.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -36,8 +36,10 @@ import java.util.ArrayList;
*/ */
public class JavascriptPluginLaunchStrategy implements PluginLaunchStrategy public class JavascriptPluginLaunchStrategy implements PluginLaunchStrategy
{ {
public static final String firstPickEngine = "rhino"; //attempt to use nashorn
public static final String fallBackEngine = "nashorn"; public static final String firstPickEngine = "nashorn";
//fallback to graal.js
public static final String fallBackEngine = "graal.js";
@Override @Override
public Plugin run(File file) throws Throwable public Plugin run(File file) throws Throwable
@ -45,26 +47,29 @@ public class JavascriptPluginLaunchStrategy implements PluginLaunchStrategy
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(firstPickEngine); ScriptEngine engine = manager.getEngineByName(firstPickEngine);
//nashorn compatability with graal
if (engine == null) if (engine == null)
{
engine = manager.getEngineByName(fallBackEngine); engine = manager.getEngineByName(fallBackEngine);
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
if (engine == null) bindings.put("polyglot.js.allowHostAccess", true);
throw new Exception("Cannot find Javascript script engine! Please contact Konloch."); bindings.put("polyglot.js.allowAllAccess", true);
bindings.put("polyglot.js.allowHostClassLookup", true);
bindings.put("activeContainer", activeContainer);
}
Reader reader = new FileReader(file); Reader reader = new FileReader(file);
engine.eval(reader); engine.eval(reader);
ScriptEngine finalEngine = engine; ScriptEngine finalEngine = engine;
return new Plugin()
Plugin plugin = new Plugin()
{ {
@Override @Override
public void execute(ArrayList<ClassNode> classNodeList) public void execute(ArrayList<ClassNode> classNodeList)
{ {
try try
{ {
//add the active container as a global variable to the JS script
finalEngine.put("activeContainer", activeContainer);
//invoke the JS function //invoke the JS function
((Invocable) finalEngine).invokeFunction("execute", classNodeList); ((Invocable) finalEngine).invokeFunction("execute", classNodeList);
} }
@ -74,5 +79,7 @@ public class JavascriptPluginLaunchStrategy implements PluginLaunchStrategy
} }
} }
}; };
plugin.setupJSContainer(finalEngine);
} }
} }