Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ private static String hotReloadArgument(Expr value) {
if (value instanceof ExprStringVal literal) return "string:" + literal.getValS();
if (value instanceof ExprBoolVal literal) return "bool:" + literal.getValB();
if (value instanceof ExprNull) return "null";
GlobalVarDef global = registrationConstant(value);
GlobalVarDef global = hotReloadConstant(value);
if (global != null) {
StringBuilder name = new StringBuilder("global:");
String segment = global.getName(); name.append(segment.length()).append(':').append(segment);
Expand All @@ -446,7 +446,7 @@ private static String hotReloadArgument(Expr value) {
return null;
}

private static @Nullable GlobalVarDef registrationConstant(Expr value) {
static @Nullable GlobalVarDef hotReloadConstant(Expr value) {
if (value instanceof ExprVarAccess access && access.attrNameDef() instanceof GlobalVarDef global
&& global.attrIsConstant()) {
return global;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ private static List<ImStmt> translate(Unit unit, ImTranslator translator, ImFunc

private static boolean immutableGuard(Expr expression) {
if (expression instanceof ExprBoolVal) return true;
if (expression instanceof ExprVarAccess access && access.attrNameDef() instanceof GlobalVarDef global) {
return global.attrIsConstant() && !global.attrIsDynamicClassMember() && access.attrTyp() instanceof WurstTypeBool;
}
if (expression instanceof ExprUnary unary && unary.getOpU() == WurstOperator.NOT) return immutableGuard(unary.getRight());
if (expression instanceof ExprBinary binary && (binary.getOp() == WurstOperator.AND || binary.getOp() == WurstOperator.OR)) {
return immutableGuard(binary.getLeft()) && immutableGuard(binary.getRight());
}
return false;
GlobalVarDef constant = ClosureTranslator.hotReloadConstant(expression);
return constant != null && !constant.attrIsDynamicClassMember() && expression.attrTyp() instanceof WurstTypeBool;
}

private static boolean readsOutsideLocal(FunctionCall call) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,31 @@ public void constantGuardedRegistrationsRunOnceAndKeepStartupOrder(boolean enabl
.lines(guardedRegistrations(false, enabled, qualified, true));
}

@Test(dataProvider = "guardedRegistrationCases")
public void qualifiedConstantGuardsReplayOnlyNewRegistrations(boolean enabled, boolean qualified) {
test().testLua(true).executeProg().luaHotReload(qualifiedConstantGuards(true, enabled, qualified))
.lines(qualifiedConstantGuards(false, enabled, qualified));
}

private static String[] qualifiedConstantGuards(boolean edited, boolean enabled, boolean qualified) {
return java.util.Arrays.stream(guardedRegistrations(edited, enabled, qualified, true))
.map(line -> line.replace("constant boolean isProd", "class Flags\n static constant boolean isProd")
.replace("constant boolean FEATURE", " static constant boolean FEATURE")
.replace("not isProd and FEATURE", "not Flags.isProd and Flags.FEATURE"))
.toArray(String[]::new);
}

@Test
public void qualifiedMutableAndInstanceGuardsRemainOrdinaryInit() {
String[] mutable = guardedFixture(true, "Flags.FEATURE", "register(1, () -> 10)");
mutable[0] = "package Main\nclass Flags\n static boolean FEATURE = true";
assertFalse(manifest(mutable).statements().stream().anyMatch(statement -> statement.kind().equals("action")));

String[] instance = guardedFixture(true, "flags.feature", "register(1, () -> 10)");
instance[0] = "package Main\nclass Flags\n let feature = true\nconstant flags = new Flags()";
assertFalse(manifest(instance).statements().stream().anyMatch(statement -> statement.kind().equals("action")));
}

@Test(dataProvider = "guardedRegistrationCases")
public void constantGuardedRegistrationsKeepNormalBackendSemantics(boolean enabled, boolean qualified) {
test().testLua(true).luaOnly(false).executeProg().lines(guardedRegistrations(true, enabled, qualified, false));
Expand Down
Loading