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

@@ -22,39 +22,35 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package scripting;
import client.MapleClient;
import tools.FilePrinter;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.script.*;
import constants.net.ServerConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import tools.FilePrinter;
/**
*
* @author Matze
*/
public abstract class AbstractScriptManager {
private ScriptEngineFactory sef;
private final ScriptEngineFactory sef;
protected AbstractScriptManager() {
sef = new ScriptEngineManager().getEngineByName("javascript").getFactory();
sef = new ScriptEngineManager().getEngineByName("graal.js").getFactory();
}
protected NashornScriptEngine getScriptEngine(String path) {
protected ScriptEngine getScriptEngine(String path) {
path = "scripts/" + path;
File scriptFile = new File(path);
if (!scriptFile.exists()) {
return null;
}
NashornScriptEngine engine = (NashornScriptEngine) sef.getScriptEngine();
ScriptEngine engine = sef.getScriptEngine();
try (FileReader fr = new FileReader(scriptFile)) {
if (ServerConstants.JAVA_8){
engine.eval("load('nashorn:mozilla_compat.js');" + System.lineSeparator());
}
engine.eval(fr);
} catch (final ScriptException | IOException t) {
FilePrinter.printError(FilePrinter.INVOCABLE + path.substring(12), t, path);
@@ -64,8 +60,8 @@ public abstract class AbstractScriptManager {
return engine;
}
protected NashornScriptEngine getScriptEngine(String path, MapleClient c) {
NashornScriptEngine engine = c.getScriptEngine("scripts/" + path);
protected ScriptEngine getScriptEngine(String path, MapleClient c) {
ScriptEngine engine = c.getScriptEngine("scripts/" + path);
if (engine == null) {
engine = getScriptEngine(path);
c.setScriptEngine(path, engine);