cleanup undef

This commit is contained in:
gnembon committed 2019-11-22 00:23:50 -04:00
1 parent 24c1ee8d3d
commit 6413c17657
5 files changed
+63 -25

No files matched your search

@@ -479,6 +479,7 @@ public class CarpetEventServer
return Event.byName.get(event).handler.addEventCallDirect(host, function); return Event.byName.get(event).handler.addEventCallDirect(host, function);
} }
public boolean removeEvent(String event, String funName) public boolean removeEvent(String event, String funName)
{ {
@@ -489,6 +490,11 @@ public class CarpetEventServer
return true; return true;
} }
public void removeEventDirectly(String event, ScriptHost host, String funName)
{
Event.byName.get(event).handler.removeEventCall(host.getName(), funName);
}
public void removeAllHostEvents(String hostName) public void removeAllHostEvents(String hostName)
{ {
Event.byName.values().forEach((e) -> e.handler.removeAllCalls(hostName)); Event.byName.values().forEach((e) -> e.handler.removeAllCalls(hostName));
@@ -61,6 +61,20 @@ public class CarpetScriptHost extends ScriptHost
} }
} }
@Override
public void delFunction(String funName)
{
super.delFunction(funName);
// mcarpet
if (funName.startsWith("__on_"))
{
// this is nasty, we have the host and function, yet we add it via names, but hey - works for now
String event = funName.replaceFirst("__on_","");
if (CarpetEventServer.Event.byName.containsKey(event))
scriptServer.events.removeEventDirectly(event, this, funName);
}
}
public CarpetScriptHost retrieveForExecution(ServerCommandSource source) public CarpetScriptHost retrieveForExecution(ServerCommandSource source)
{ {
if (!perUser) if (!perUser)
+1 -15
View File
@@ -18,7 +18,7 @@ public class Context
static final int LOCALIZATION = 8; static final int LOCALIZATION = 8;
static final int LVALUE = 9; static final int LVALUE = 9;
private Map<String, LazyValue> variables = new HashMap<>(); public Map<String, LazyValue> variables = new HashMap<>();
protected ScriptHost host; protected ScriptHost host;
@@ -56,20 +56,6 @@ public class Context
void delVariable(String variable) void delVariable(String variable)
{ {
if (variable.startsWith("global_"))
{
host.globalVariables.remove(variable);
return;
}
variables.remove(variable);
}
void clearAll(String variable)
{
if (variable.startsWith("global_"))
{
host.globalVariables.remove(variable);
return;
}
variables.remove(variable); variables.remove(variable);
} }
+32 -10
View File
@@ -2864,26 +2864,48 @@ public class Expression implements Cloneable
addLazyFunction("undef", 1, (c, t, lv) -> addLazyFunction("undef", 1, (c, t, lv) ->
{ {
String varname = lv.get(0).evalValue(c).getString(); Value remove = lv.get(0).evalValue(c);
if (varname.startsWith("_")) if (remove instanceof FunctionValue)
throw new InternalExpressionException("Cannot replace local built-in variables, i.e. those that start with '_'");
if (varname.endsWith("*"))
{ {
c.host.delFunction(remove.getString());
return (cc, tt) -> Value.NULL;
}
String varname = remove.getString();
boolean isPrefix = varname.endsWith("*");
if (isPrefix)
varname = varname.replaceAll("\\*+$", ""); varname = varname.replaceAll("\\*+$", "");
if (isPrefix)
{
for (String key: c.host.globalFunctions.keySet()) for (String key: c.host.globalFunctions.keySet())
{ {
if (key.startsWith(varname)) c.host.globalFunctions.remove(key); if (key.startsWith(varname)) c.host.delFunction(key);
} }
for (String key: c.host.globalVariables.keySet()) if (varname.startsWith("global_"))
{ {
if (key.startsWith(varname)) c.host.globalVariables.remove(key); for (String key : c.host.globalVariables.keySet())
{
if (key.startsWith(varname)) c.host.delGlobalVariable(key);
}
}
if (!varname.startsWith("_"))
{
for (String key : c.variables.keySet())
{
if (key.startsWith(varname)) c.delVariable(key);
}
} }
c.clearAll(varname);
} }
else else
{ {
c.host.globalFunctions.remove(varname); c.host.delFunction(varname);
c.delVariable(varname); if (varname.startsWith("global_"))
{
c.host.delGlobalVariable(varname);
}
if (!varname.startsWith("_"))
{
c.delVariable(varname);
}
} }
return (cc, tt) -> Value.NULL; return (cc, tt) -> Value.NULL;
}); });
@@ -46,6 +46,16 @@ public class ScriptHost
globalFunctions.put(funName, function); globalFunctions.put(funName, function);
} }
public void delFunction(String funName)
{
globalFunctions.remove(funName);
}
public void delGlobalVariable(String varName)
{
globalFunctions.remove(varName);
}
public ScriptHost retrieveForExecution(String /*Nullable*/ user) public ScriptHost retrieveForExecution(String /*Nullable*/ user)
{ {
if (!perUser) if (!perUser)