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

@@ -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)
{
@@ -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;
@@ -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)
+26 -18
View File
@@ -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<String> globaVariableNames(Module module, Predicate<String> predicate)
public Stream<String> globalVariableNames(Module module, Predicate<String> 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<String> globaFunctionNames(Module module, Predicate<String> predicate)
public Stream<String> globalFunctionNames(Module module, Predicate<String> 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)
@@ -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<>();
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
{