Replace NashornScriptEngine with implicit GraalJSScriptEngine

GraalJSScriptEngine implements Invocable, which is why casting to it works.
However, this is just a quick and ugly fix to make it compile.
A better solution would be to cast it once, immediately after `eval`,
and from then on only handle it as Invocable.

Scripts still need to be fixed. They are still using Rhino and Nashorn-specific
ways of importing packages.
Usages of "importPackage" and "Packages" need to be replaced with
the Graal specific "Java.type".
This commit is contained in:
P0nk
2021-04-17 15:08:45 +02:00
parent e42fb27459
commit a18a1cb8ce
13 changed files with 284 additions and 364 deletions

View File

@@ -23,15 +23,16 @@ package scripting.map;
import client.MapleCharacter;
import client.MapleClient;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
import scripting.AbstractScriptManager;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractScriptManager;
import tools.FilePrinter;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
public class MapScriptManager extends AbstractScriptManager {
@@ -41,12 +42,12 @@ public class MapScriptManager extends AbstractScriptManager {
return instance;
}
private Map<String, NashornScriptEngine> scripts = new HashMap<>();
private ScriptEngineFactory sef;
private Map<String, Invocable> scripts = new HashMap<>();
private final ScriptEngineFactory sef;
private MapScriptManager() {
ScriptEngineManager sem = new ScriptEngineManager();
sef = sem.getEngineByName("javascript").getFactory();
sef = sem.getEngineByName("graal.js").getFactory();
}
public void reloadScripts() {
@@ -64,7 +65,7 @@ public class MapScriptManager extends AbstractScriptManager {
}
}
NashornScriptEngine iv = scripts.get(mapScriptPath);
Invocable iv = scripts.get(mapScriptPath);
if (iv != null) {
try {
iv.invokeFunction("start", new MapScriptMethods(c));
@@ -75,13 +76,13 @@ public class MapScriptManager extends AbstractScriptManager {
}
try {
iv = getScriptEngine("map/" + mapScriptPath + ".js");
iv = (Invocable) getScriptEngine("map/" + mapScriptPath + ".js");
if (iv == null) {
return false;
}
scripts.put(mapScriptPath, iv);
iv.invokeFunction("start", new MapScriptMethods(c));
((Invocable) iv).invokeFunction("start", new MapScriptMethods(c));
return true;
} catch (final UndeclaredThrowableException | ScriptException ute) {
FilePrinter.printError(FilePrinter.MAP_SCRIPT + mapScriptPath + ".txt", ute);