From 06c9aea3263cbddb7174ff8ea60281c6f25ced59 Mon Sep 17 00:00:00 2001 From: gnembon Date: Thu, 29 Apr 2021 02:17:02 -0300 Subject: [PATCH] tracking module integrity when module composition changes --- .../java/carpet/commands/ScriptCommand.java | 10 ++--- .../java/carpet/script/CarpetScriptHost.java | 2 +- .../carpet/script/CarpetScriptServer.java | 3 +- src/main/java/carpet/script/Expression.java | 3 ++ src/main/java/carpet/script/ScriptHost.java | 44 +++++++++++-------- .../script/exception/IntegrityException.java | 9 ++++ src/main/java/carpet/script/language/Sys.java | 2 +- 7 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 src/main/java/carpet/script/exception/IntegrityException.java diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index bb83e056..9746211e 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -78,8 +78,8 @@ public class ScriptCommand // not that useful in commandline, more so in external scripts, so skipping here if (eventPrefix != null) scarpetMatches.addAll(CarpetEventServer.Event.publicEvents(null).stream(). 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.globaVariableNames(host.main, s -> s.startsWith(prefix)).collect(Collectors.toList())); + scarpetMatches.addAll(host.globalFunctionNames(host.main, s -> s.startsWith(prefix)).map(s -> s+"(").collect(Collectors.toList())); + scarpetMatches.addAll(host.globalVariableNames(host.main, s -> s.startsWith(prefix)).collect(Collectors.toList())); return scarpetMatches; } @@ -332,7 +332,7 @@ public class ScriptCommand private static Collection suggestFunctionCalls(CommandContext c) throws CommandSyntaxException { 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) { @@ -358,7 +358,7 @@ public class ScriptCommand ServerCommandSource source = context.getSource(); 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); if (fun == null) { @@ -379,7 +379,7 @@ public class ScriptCommand //Messenger.m(source, "w "+code); Messenger.m(source, "w "); 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); if (variable == null) { diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index 3fb3bc54..3f002753 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -498,7 +498,7 @@ public class CarpetScriptHost extends ScriptHost 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) { diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index 01048258..d15c7144 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -5,6 +5,7 @@ import carpet.script.bundled.BundledModule; import carpet.CarpetServer; import carpet.script.bundled.FileModule; import carpet.script.bundled.Module; +import carpet.script.exception.IntegrityException; import carpet.script.exception.InvalidCallbackException; import carpet.script.value.FunctionValue; import carpet.script.value.Value; @@ -403,7 +404,7 @@ public class CarpetScriptServer { host.callUDF(BlockPos.ORIGIN, source.withLevel(CarpetSettings.runPermissionLevel), udf, argv); } - catch (NullPointerException | InvalidCallbackException npe) + catch (NullPointerException | InvalidCallbackException | IntegrityException npe) { if (reportFails) return -1; continue; diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 3da49f79..d0ccf197 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -16,6 +16,7 @@ import carpet.script.exception.BreakStatement; import carpet.script.exception.ContinueStatement; import carpet.script.exception.ExitStatement; import carpet.script.exception.ExpressionException; +import carpet.script.exception.IntegrityException; import carpet.script.exception.InternalExpressionException; import carpet.script.exception.ResolvedException; import carpet.script.exception.ReturnStatement; @@ -267,6 +268,8 @@ public class Expression { if (exc instanceof ExitStatement) return exc; + if (exc instanceof IntegrityException) + return exc; if (exc instanceof InternalExpressionException) return ((InternalExpressionException) exc).promote(c, e, token); if (exc instanceof ArithmeticException) diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index 93b7dc35..389b8e62 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -2,6 +2,7 @@ package carpet.script; import carpet.script.bundled.Module; import carpet.script.exception.ExpressionException; +import carpet.script.exception.IntegrityException; import carpet.script.exception.InternalExpressionException; import carpet.script.language.Arithmetic; import carpet.script.value.FunctionValue; @@ -181,8 +182,8 @@ public abstract class ScriptHost throw new InternalExpressionException("Cannot import from module that is not imported"); } return Stream.concat( - globaVariableNames(source, s -> s.startsWith("global_")), - globaFunctionNames(source, s -> true) + globalVariableNames(source, s -> s.startsWith("global_")), + globalFunctionNames(source, s -> true) ).distinct().sorted(); } @@ -205,7 +206,7 @@ public abstract class ScriptHost } 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 if (ret != null) return ret; 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(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 if (ret != null) return ret; ModuleData target = local.globalsImports.get(name); @@ -272,55 +273,62 @@ public abstract class ScriptHost 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.functionImports.entrySet().removeIf(e -> e.getKey().startsWith(prefix)); } public void delFunction(Module module, String funName) { - ModuleData data = moduleData.get(module); + ModuleData data = getModuleData(module); data.globalFunctions.remove(funName); data.functionImports.remove(funName); } 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.globalsImports.entrySet().removeIf(e -> e.getKey().startsWith(prefix)); } public void delGlobalVariable(Module module, String varName) { - ModuleData data = moduleData.get(module); + ModuleData data = getModuleData(module); data.globalFunctions.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) { - moduleData.get(module).globalFunctions.put(name, fun); + getModuleData(module).globalFunctions.put(name, fun); } public void setGlobalVariable(Module module, String name, LazyValue lv) { - moduleData.get(module).globalVariables.put(name, lv); + getModuleData(module).globalVariables.put(name, lv); } - public Stream globaVariableNames(Module module, Predicate predicate) + public Stream globalVariableNames(Module module, Predicate predicate) { return Stream.concat(Stream.concat( - moduleData.get(module).globalVariables.keySet().stream(), - moduleData.get(module).globalsImports.keySet().stream() - ), moduleData.get(module).futureImports.keySet().stream().filter(s -> s.startsWith("global_"))).filter(predicate); + getModuleData(module).globalVariables.keySet().stream(), + getModuleData(module).globalsImports.keySet().stream() + ), getModuleData(module).futureImports.keySet().stream().filter(s -> s.startsWith("global_"))).filter(predicate); } - public Stream globaFunctionNames(Module module, Predicate predicate) + public Stream globalFunctionNames(Module module, Predicate predicate) { return Stream.concat(Stream.concat( - moduleData.get(module).globalFunctions.keySet().stream(), - moduleData.get(module).functionImports.keySet().stream() - ),moduleData.get(module).futureImports.keySet().stream().filter(s -> !s.startsWith("global_"))).filter(predicate); + getModuleData(module).globalFunctions.keySet().stream(), + getModuleData(module).functionImports.keySet().stream() + ),getModuleData(module).futureImports.keySet().stream().filter(s -> !s.startsWith("global_"))).filter(predicate); } public ScriptHost retrieveForExecution(String /*Nullable*/ user) diff --git a/src/main/java/carpet/script/exception/IntegrityException.java b/src/main/java/carpet/script/exception/IntegrityException.java new file mode 100644 index 00000000..262c57f9 --- /dev/null +++ b/src/main/java/carpet/script/exception/IntegrityException.java @@ -0,0 +1,9 @@ +package carpet.script.exception; + +public class IntegrityException extends RuntimeException +{ + public IntegrityException(String message) + { + super(message); + } +} diff --git a/src/main/java/carpet/script/language/Sys.java b/src/main/java/carpet/script/language/Sys.java index c9481537..9840e801 100644 --- a/src/main/java/carpet/script/language/Sys.java +++ b/src/main/java/carpet/script/language/Sys.java @@ -414,7 +414,7 @@ public class Sys { List values = new ArrayList<>(); 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 {