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

@@ -26,7 +26,6 @@ import client.Skill;
import client.SkillFactory;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import constants.net.ServerConstants;
import net.server.audit.LockCollector;
import net.server.audit.locks.*;
import net.server.audit.locks.factory.MonitoredReadLockFactory;
@@ -52,6 +51,7 @@ import server.maps.MapleReactor;
import tools.MaplePacketCreator;
import tools.Pair;
import javax.script.Invocable;
import javax.script.ScriptException;
import java.awt.*;
import java.util.List;
@@ -217,7 +217,7 @@ public class EventInstanceManager {
public Object invokeScriptFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
if (!disposed) {
return em.getIv().invokeFunction(name, args);
return ((Invocable) em.getIv()).invokeFunction(name, args);
} else {
return null;
}
@@ -557,31 +557,25 @@ public class EventInstanceManager {
}
public void monsterKilled(MapleCharacter chr, final MapleMonster mob) {
try {
int inc;
if (ServerConstants.JAVA_8) {
inc = (int)invokeScriptFunction("monsterValue", EventInstanceManager.this, mob.getId());
} else {
inc = ((Double) invokeScriptFunction("monsterValue", EventInstanceManager.this, mob.getId())).intValue();
}
if (inc != 0) {
Integer kc = killCount.get(chr);
if (kc == null) {
kc = inc;
} else {
kc += inc;
}
killCount.put(chr, kc);
if (expedition != null){
expedition.monsterKilled(chr, mob);
}
}
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
try {
final int inc = (int) invokeScriptFunction("monsterValue", EventInstanceManager.this, mob.getId());
if (inc != 0) {
Integer kc = killCount.get(chr);
if (kc == null) {
kc = inc;
} else {
kc += inc;
}
killCount.put(chr, kc);
if (expedition != null) {
expedition.monsterKilled(chr, mob);
}
}
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public int getKillCount(MapleCharacter chr) {
Integer kc = killCount.get(chr);
@@ -886,31 +880,25 @@ public class EventInstanceManager {
public MapleMonster getMonster(int mid) {
return(MapleLifeFactory.getMonster(mid));
}
private List<Integer> convertToIntegerArray(List<Object> list) {
List<Integer> intList = new ArrayList<>();
if (ServerConstants.JAVA_8) {
for (Object d: list) {
intList.add((Integer) d);
private List<Integer> convertToIntegerList(List<Object> objects) {
List<Integer> intList = new ArrayList<>();
for (Object object : objects) {
intList.add((Integer) object);
}
} else {
for (Object d: list) {
intList.add(((Double) d).intValue());
}
}
return intList;
return intList;
}
public void setEventClearStageExp(List<Object> gain) {
onMapClearExp.clear();
onMapClearExp.addAll(convertToIntegerArray(gain));
onMapClearExp.addAll(convertToIntegerList(gain));
}
public void setEventClearStageMeso(List<Object> gain) {
onMapClearMeso.clear();
onMapClearMeso.addAll(convertToIntegerArray(gain));
onMapClearMeso.addAll(convertToIntegerList(gain));
}
public Integer getClearStageExp(int stage) { //stage counts from ONE.
@@ -940,7 +928,7 @@ public class EventInstanceManager {
}
public final void setExclusiveItems(List<Object> items) {
List<Integer> exclusive = convertToIntegerArray(items);
List<Integer> exclusive = convertToIntegerList(items);
wL.lock();
try {
@@ -968,8 +956,8 @@ public class EventInstanceManager {
if(eventLevel <= 0 || eventLevel > YamlConfig.config.server.MAX_EVENT_LEVELS) return;
eventLevel--; //event level starts from 1
List<Integer> rewardIds = convertToIntegerArray(rwds);
List<Integer> rewardQtys = convertToIntegerArray(qtys);
List<Integer> rewardIds = convertToIntegerList(rwds);
List<Integer> rewardQtys = convertToIntegerList(qtys);
//rewardsSet and rewardsQty hold temporary values
wL.lock();

View File

@@ -24,10 +24,6 @@ package scripting.event;
import client.MapleCharacter;
import config.YamlConfig;
import constants.game.GameConstants;
import constants.net.ServerConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.api.scripting.ScriptUtils;
import net.server.Server;
import net.server.audit.LockCollector;
import net.server.audit.locks.MonitoredLockType;
@@ -48,6 +44,8 @@ import server.maps.MapleMap;
import server.quest.MapleQuest;
import tools.exceptions.EventInstanceInProgressException;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.*;
import java.util.concurrent.Semaphore;
@@ -62,7 +60,7 @@ import java.util.logging.Logger;
* @author Ronan
*/
public class EventManager {
private NashornScriptEngine iv;
private ScriptEngine iv;
private Channel cserv;
private World wserv;
private Server server;
@@ -85,7 +83,7 @@ public class EventManager {
private static final int maxLobbys = 8; // an event manager holds up to this amount of concurrent lobbys
public EventManager(Channel cserv, NashornScriptEngine iv, String name) {
public EventManager(Channel cserv, ScriptEngine iv, String name) {
this.server = Server.getInstance();
this.iv = iv;
this.cserv = cserv;
@@ -104,7 +102,7 @@ public class EventManager {
ess.dispose();
try {
iv.invokeFunction("cancelSchedule", (Object) null);
((Invocable) iv).invokeFunction("cancelSchedule", (Object) null);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
@@ -151,20 +149,14 @@ public class EventManager {
queueLock = queueLock.dispose();
startLock = startLock.dispose();
}
private List<Integer> convertToIntegerArray(List<Object> list) {
private List<Integer> convertToIntegerList(List<Object> objects) {
List<Integer> intList = new ArrayList<>();
if (ServerConstants.JAVA_8) {
for (Object d: list) {
intList.add((Integer) d);
}
} else {
for (Object d: list) {
intList.add(((Double) d).intValue());
}
for (Object object : objects) {
intList.add((Integer) object);
}
return intList;
}
@@ -174,18 +166,8 @@ public class EventManager {
private List<Integer> getLobbyRange() {
try {
if (!ServerConstants.JAVA_8) {
return convertToIntegerArray((List<Object>)iv.invokeFunction("setLobbyRange", (Object) null));
} else { // java 8 support here thanks to MedicOP
ScriptObjectMirror object = (ScriptObjectMirror) iv.invokeFunction("setLobbyRange", (Object) null);
int[] to = object.to(int[].class);
List<Integer> list = new ArrayList<>();
for (int i : to) {
list.add(i);
}
return list;
}
List<Object> objects = (List<Object>) ((Invocable) iv).invokeFunction("setLobbyRange", (Object) null);
return convertToIntegerList(objects);
} catch (ScriptException | NoSuchMethodException ex) { // they didn't define a lobby range
List<Integer> defaultRange = new ArrayList<>();
defaultRange.add(0);
@@ -202,7 +184,7 @@ public class EventManager {
public EventScheduledFuture schedule(final String methodName, final EventInstanceManager eim, long delay) {
Runnable r = () -> {
try {
iv.invokeFunction(methodName, eim);
((Invocable) iv).invokeFunction(methodName, eim);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -217,7 +199,7 @@ public class EventManager {
public EventScheduledFuture scheduleAtTimestamp(final String methodName, long timestamp) {
Runnable r = () -> {
try {
iv.invokeFunction(methodName, (Object) null);
((Invocable) iv).invokeFunction(methodName, (Object) null);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -235,7 +217,7 @@ public class EventManager {
return cserv;
}
public NashornScriptEngine getIv() {
public ScriptEngine getIv() {
return iv;
}
@@ -386,7 +368,7 @@ public class EventManager {
}
private EventInstanceManager createInstance(String name, Object... args) throws ScriptException, NoSuchMethodException {
return (EventInstanceManager) iv.invokeFunction(name, args);
return (EventInstanceManager) ((Invocable) iv).invokeFunction(name, args);
}
private void registerEventInstance(String eventName, int lobbyId) {
@@ -707,8 +689,8 @@ public class EventManager {
}
registerEventInstance(eim.getName(), lobbyId);
eim.setLeader(leader);
iv.invokeFunction("setup", eim);
((Invocable) iv).invokeFunction("setup", eim);
eim.setProperty("leader", ldr);
eim.startEvent();
@@ -735,17 +717,10 @@ public class EventManager {
return(new ArrayList<>());
}
try {
Object p = iv.invokeFunction("getEligibleParty", party.getPartyMembersOnline());
Object p = ((Invocable) iv).invokeFunction("getEligibleParty", party.getPartyMembersOnline());
if(p != null) {
List<MaplePartyCharacter> lmpc;
if(ServerConstants.JAVA_8) {
lmpc = new ArrayList<>(((Map<String, MaplePartyCharacter>)(ScriptUtils.convert(p, Map.class))).values());
} else {
lmpc = new ArrayList<>((List<MaplePartyCharacter>) p);
}
final List<MaplePartyCharacter> lmpc = new ArrayList<>((List<MaplePartyCharacter>) p);
party.setEligibleMembers(lmpc);
return lmpc;
}
@@ -758,7 +733,7 @@ public class EventManager {
public void clearPQ(EventInstanceManager eim) {
try {
iv.invokeFunction("clearPQ", eim);
((Invocable) iv).invokeFunction("clearPQ", eim);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -766,7 +741,7 @@ public class EventManager {
public void clearPQ(EventInstanceManager eim, MapleMap toMap) {
try {
iv.invokeFunction("clearPQ", eim, toMap);
((Invocable) iv).invokeFunction("clearPQ", eim, toMap);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}

View File

@@ -21,19 +21,19 @@
*/
package scripting.event;
import java.util.concurrent.ConcurrentHashMap;
import net.server.channel.Channel;
import scripting.AbstractScriptManager;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import net.server.channel.Channel;
import scripting.AbstractScriptManager;
/**
*
* @author Matze
@@ -42,11 +42,11 @@ public class EventScriptManager extends AbstractScriptManager {
private class EventEntry {
public EventEntry(NashornScriptEngine iv, EventManager em) {
public EventEntry(ScriptEngine iv, EventManager em) {
this.iv = iv;
this.em = em;
}
public NashornScriptEngine iv;
public ScriptEngine iv;
public EventManager em;
}
@@ -58,7 +58,7 @@ public class EventScriptManager extends AbstractScriptManager {
super();
for (String script : scripts) {
if (!script.equals("")) {
NashornScriptEngine iv = getScriptEngine("event/" + script + ".js");
ScriptEngine iv = getScriptEngine("event/" + script + ".js");
events.put(script, new EventEntry(iv, new EventManager(cserv, iv, script)));
}
}
@@ -83,7 +83,7 @@ public class EventScriptManager extends AbstractScriptManager {
for (EventEntry entry : events.values()) {
try {
entry.iv.put("em", entry.em);
entry.iv.invokeFunction("init", (Object) null);
((Invocable) entry.iv).invokeFunction("init", (Object) null);
} catch (Exception ex) {
Logger.getLogger(EventScriptManager.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error on script: " + entry.em.getName());
@@ -102,7 +102,7 @@ public class EventScriptManager extends AbstractScriptManager {
Channel cserv = eventEntries.iterator().next().getValue().em.getChannelServer();
for (Entry<String, EventEntry> entry : eventEntries) {
String script = entry.getKey();
NashornScriptEngine iv = getScriptEngine("event/" + script + ".js");
ScriptEngine iv = getScriptEngine("event/" + script + ".js");
events.put(script, new EventEntry(iv, new EventManager(cserv, iv, script)));
}
}