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>
<version>0.2.1</version>
</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>
<build>

View File

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