tracking module integrity when module composition changes

This commit is contained in:
gnembon committed 2021-04-29 02:17:02 -03:00
1 parent f8c3de21d3
commit 06c9aea326
7 files changed
+47 -26

No files matched your search

@@ -78,8 +78,8 @@ public class ScriptCommand
// not that useful in commandline, more so in external scripts, so skipping here // not that useful in commandline, more so in external scripts, so skipping here
if (eventPrefix != null) scarpetMatches.addAll(CarpetEventServer.Event.publicEvents(null).stream(). if (eventPrefix != null) scarpetMatches.addAll(CarpetEventServer.Event.publicEvents(null).stream().
filter(e -> e.name.startsWith(eventPrefix)).map(s -> "__on_"+s.name+"(").collect(Collectors.toList())); filter(e -> e.name.startsWith(eventPrefix)).map(s -> "__on_"+s.name+"(").collect(Collectors.toList()));
scarpetMatches.addAll(host.globaFunctionNames(host.main, s -> s.startsWith(prefix)).map(s -> s+"(").collect(Collectors.toList())); scarpetMatches.addAll(host.globalFunctionNames(host.main, s -> s.startsWith(prefix)).map(s -> s+"(").collect(Collectors.toList()));
scarpetMatches.addAll(host.globaVariableNames(host.main, s -> s.startsWith(prefix)).collect(Collectors.toList())); scarpetMatches.addAll(host.globalVariableNames(host.main, s -> s.startsWith(prefix)).collect(Collectors.toList()));
return scarpetMatches; return scarpetMatches;
} }
@@ -332,7 +332,7 @@ public class ScriptCommand
private static Collection<String> suggestFunctionCalls(CommandContext<ServerCommandSource> c) throws CommandSyntaxException private static Collection<String> suggestFunctionCalls(CommandContext<ServerCommandSource> c) throws CommandSyntaxException
{ {
CarpetScriptHost host = getHost(c); CarpetScriptHost host = getHost(c);
return host.globaFunctionNames(host.main, s -> !s.startsWith("_")).sorted().collect(Collectors.toList()); return host.globalFunctionNames(host.main, s -> !s.startsWith("_")).sorted().collect(Collectors.toList());
} }
private static int listEvents(ServerCommandSource source) private static int listEvents(ServerCommandSource source)
{ {
@@ -358,7 +358,7 @@ public class ScriptCommand
ServerCommandSource source = context.getSource(); ServerCommandSource source = context.getSource();
Messenger.m(source, "lb Stored functions"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":")); Messenger.m(source, "lb Stored functions"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":"));
host.globaFunctionNames(host.main, (str) -> all || !str.startsWith("__")).sorted().forEach( (s) -> { host.globalFunctionNames(host.main, (str) -> all || !str.startsWith("__")).sorted().forEach( (s) -> {
FunctionValue fun = host.getFunction(s); FunctionValue fun = host.getFunction(s);
if (fun == null) if (fun == null)
{ {
@@ -379,7 +379,7 @@ public class ScriptCommand
//Messenger.m(source, "w "+code); //Messenger.m(source, "w "+code);
Messenger.m(source, "w "); Messenger.m(source, "w ");
Messenger.m(source, "lb Global variables"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":")); Messenger.m(source, "lb Global variables"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":"));
host.globaVariableNames(host.main, (s) -> s.startsWith("global_")).sorted().forEach( (s) -> { host.globalVariableNames(host.main, (s) -> s.startsWith("global_")).sorted().forEach( (s) -> {
LazyValue variable = host.getGlobalVariable(s); LazyValue variable = host.getGlobalVariable(s);
if (variable == null) if (variable == null)
{ {
@@ -498,7 +498,7 @@ public class CarpetScriptHost extends ScriptHost
boolean hasTypeSupport = appConfig.getOrDefault(StringValue.of("legacy_command_type_support"), Value.FALSE).getBoolean(); boolean hasTypeSupport = appConfig.getOrDefault(StringValue.of("legacy_command_type_support"), Value.FALSE).getBoolean();
for (String function : globaFunctionNames(main, s -> !s.startsWith("_")).sorted().collect(Collectors.toList())) for (String function : globalFunctionNames(main, s -> !s.startsWith("_")).sorted().collect(Collectors.toList()))
{ {
if (hasTypeSupport) if (hasTypeSupport)
{ {
@@ -5,6 +5,7 @@ import carpet.script.bundled.BundledModule;
import carpet.CarpetServer; import carpet.CarpetServer;
import carpet.script.bundled.FileModule; import carpet.script.bundled.FileModule;
import carpet.script.bundled.Module; import carpet.script.bundled.Module;
import carpet.script.exception.IntegrityException;
import carpet.script.exception.InvalidCallbackException; import carpet.script.exception.InvalidCallbackException;
import carpet.script.value.FunctionValue; import carpet.script.value.FunctionValue;
import carpet.script.value.Value; import carpet.script.value.Value;
@@ -403,7 +404,7 @@ public class CarpetScriptServer
{ {
host.callUDF(BlockPos.ORIGIN, source.withLevel(CarpetSettings.runPermissionLevel), udf, argv); host.callUDF(BlockPos.ORIGIN, source.withLevel(CarpetSettings.runPermissionLevel), udf, argv);
} }
catch (NullPointerException | InvalidCallbackException npe) catch (NullPointerException | InvalidCallbackException | IntegrityException npe)
{ {
if (reportFails) return -1; if (reportFails) return -1;
continue; continue;
@@ -16,6 +16,7 @@ import carpet.script.exception.BreakStatement;
import carpet.script.exception.ContinueStatement; import carpet.script.exception.ContinueStatement;
import carpet.script.exception.ExitStatement; import carpet.script.exception.ExitStatement;
import carpet.script.exception.ExpressionException; import carpet.script.exception.ExpressionException;
import carpet.script.exception.IntegrityException;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ResolvedException; import carpet.script.exception.ResolvedException;
import carpet.script.exception.ReturnStatement; import carpet.script.exception.ReturnStatement;
@@ -267,6 +268,8 @@ public class Expression
{ {
if (exc instanceof ExitStatement) if (exc instanceof ExitStatement)
return exc; return exc;
if (exc instanceof IntegrityException)
return exc;
if (exc instanceof InternalExpressionException) if (exc instanceof InternalExpressionException)
return ((InternalExpressionException) exc).promote(c, e, token); return ((InternalExpressionException) exc).promote(c, e, token);
if (exc instanceof ArithmeticException) if (exc instanceof ArithmeticException)
+26 -18
View File
@@ -2,6 +2,7 @@ package carpet.script;
import carpet.script.bundled.Module; import carpet.script.bundled.Module;
import carpet.script.exception.ExpressionException; import carpet.script.exception.ExpressionException;
import carpet.script.exception.IntegrityException;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.language.Arithmetic; import carpet.script.language.Arithmetic;
import carpet.script.value.FunctionValue; import carpet.script.value.FunctionValue;
@@ -181,8 +182,8 @@ public abstract class ScriptHost
throw new InternalExpressionException("Cannot import from module that is not imported"); throw new InternalExpressionException("Cannot import from module that is not imported");
} }
return Stream.concat( return Stream.concat(
globaVariableNames(source, s -> s.startsWith("global_")), globalVariableNames(source, s -> s.startsWith("global_")),
globaFunctionNames(source, s -> true) globalFunctionNames(source, s -> true)
).distinct().sorted(); ).distinct().sorted();
} }
@@ -205,7 +206,7 @@ public abstract class ScriptHost
} }
private FunctionValue getFunction(Module module, String name) private FunctionValue getFunction(Module module, String name)
{ {
ModuleData local = moduleData.get(module); ModuleData local = getModuleData(module);
FunctionValue ret = local.globalFunctions.get(name); // most uses would be from local scope anyways FunctionValue ret = local.globalFunctions.get(name); // most uses would be from local scope anyways
if (ret != null) return ret; if (ret != null) return ret;
ModuleData target = local.functionImports.get(name); ModuleData target = local.functionImports.get(name);
@@ -239,7 +240,7 @@ public abstract class ScriptHost
public LazyValue getGlobalVariable(String name) { return getGlobalVariable(main, name); } public LazyValue getGlobalVariable(String name) { return getGlobalVariable(main, name); }
public LazyValue getGlobalVariable(Module module, String name) public LazyValue getGlobalVariable(Module module, String name)
{ {
ModuleData local = moduleData.get(module); ModuleData local = getModuleData(module);
LazyValue ret = local.globalVariables.get(name); // most uses would be from local scope anyways LazyValue ret = local.globalVariables.get(name); // most uses would be from local scope anyways
if (ret != null) return ret; if (ret != null) return ret;
ModuleData target = local.globalsImports.get(name); ModuleData target = local.globalsImports.get(name);
@@ -272,55 +273,62 @@ public abstract class ScriptHost
public void delFunctionWithPrefix(Module module, String prefix) public void delFunctionWithPrefix(Module module, String prefix)
{ {
ModuleData data = moduleData.get(module); ModuleData data = getModuleData(module);
data.globalFunctions.entrySet().removeIf(e -> e.getKey().startsWith(prefix)); data.globalFunctions.entrySet().removeIf(e -> e.getKey().startsWith(prefix));
data.functionImports.entrySet().removeIf(e -> e.getKey().startsWith(prefix)); data.functionImports.entrySet().removeIf(e -> e.getKey().startsWith(prefix));
} }
public void delFunction(Module module, String funName) public void delFunction(Module module, String funName)
{ {
ModuleData data = moduleData.get(module); ModuleData data = getModuleData(module);
data.globalFunctions.remove(funName); data.globalFunctions.remove(funName);
data.functionImports.remove(funName); data.functionImports.remove(funName);
} }
public void delGlobalVariableWithPrefix(Module module, String prefix) public void delGlobalVariableWithPrefix(Module module, String prefix)
{ {
ModuleData data = moduleData.get(module); ModuleData data = getModuleData(module);
data.globalVariables.entrySet().removeIf(e -> e.getKey().startsWith(prefix)); data.globalVariables.entrySet().removeIf(e -> e.getKey().startsWith(prefix));
data.globalsImports.entrySet().removeIf(e -> e.getKey().startsWith(prefix)); data.globalsImports.entrySet().removeIf(e -> e.getKey().startsWith(prefix));
} }
public void delGlobalVariable(Module module, String varName) public void delGlobalVariable(Module module, String varName)
{ {
ModuleData data = moduleData.get(module); ModuleData data = getModuleData(module);
data.globalFunctions.remove(varName); data.globalFunctions.remove(varName);
data.functionImports.remove(varName); data.functionImports.remove(varName);
} }
private ModuleData getModuleData(Module module)
{
ModuleData data = moduleData.get(module);
if (data == null) throw new IntegrityException("Module structure changed for the app. Did you reload the app with tasks running?");
return data;
}
public void addUserDefinedFunction(Context ctx, Module module, String name, FunctionValue fun) public void addUserDefinedFunction(Context ctx, Module module, String name, FunctionValue fun)
{ {
moduleData.get(module).globalFunctions.put(name, fun); getModuleData(module).globalFunctions.put(name, fun);
} }
public void setGlobalVariable(Module module, String name, LazyValue lv) public void setGlobalVariable(Module module, String name, LazyValue lv)
{ {
moduleData.get(module).globalVariables.put(name, lv); getModuleData(module).globalVariables.put(name, lv);
} }
public Stream<String> globaVariableNames(Module module, Predicate<String> predicate) public Stream<String> globalVariableNames(Module module, Predicate<String> predicate)
{ {
return Stream.concat(Stream.concat( return Stream.concat(Stream.concat(
moduleData.get(module).globalVariables.keySet().stream(), getModuleData(module).globalVariables.keySet().stream(),
moduleData.get(module).globalsImports.keySet().stream() getModuleData(module).globalsImports.keySet().stream()
), moduleData.get(module).futureImports.keySet().stream().filter(s -> s.startsWith("global_"))).filter(predicate); ), getModuleData(module).futureImports.keySet().stream().filter(s -> s.startsWith("global_"))).filter(predicate);
} }
public Stream<String> globaFunctionNames(Module module, Predicate<String> predicate) public Stream<String> globalFunctionNames(Module module, Predicate<String> predicate)
{ {
return Stream.concat(Stream.concat( return Stream.concat(Stream.concat(
moduleData.get(module).globalFunctions.keySet().stream(), getModuleData(module).globalFunctions.keySet().stream(),
moduleData.get(module).functionImports.keySet().stream() getModuleData(module).functionImports.keySet().stream()
),moduleData.get(module).futureImports.keySet().stream().filter(s -> !s.startsWith("global_"))).filter(predicate); ),getModuleData(module).futureImports.keySet().stream().filter(s -> !s.startsWith("global_"))).filter(predicate);
} }
public ScriptHost retrieveForExecution(String /*Nullable*/ user) public ScriptHost retrieveForExecution(String /*Nullable*/ user)
@@ -0,0 +1,9 @@
package carpet.script.exception;
public class IntegrityException extends RuntimeException
{
public IntegrityException(String message)
{
super(message);
}
}
@@ -414,7 +414,7 @@ public class Sys {
List<Value> values = new ArrayList<>(); List<Value> values = new ArrayList<>();
if (prefix.startsWith("global")) if (prefix.startsWith("global"))
{ {
c.host.globaVariableNames(expression.module, (s) -> s.startsWith(prefix)).forEach(s -> values.add(new StringValue(s))); c.host.globalVariableNames(expression.module, (s) -> s.startsWith(prefix)).forEach(s -> values.add(new StringValue(s)));
} }
else else
{ {