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
3 changes: 2 additions & 1 deletion BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ Notes; finished-work narrative does not.
- Callback bodies now use source-callable ownership for native `code` callbacks as well as class closures. Stable
event arguments anchor distinct registrations, including qualified static calls such as `EventListener.add` and
`DamageEvent.addListener`. Independent, unique direct registrations added to existing package
init blocks run as complete source-owned units, once, without repeating previous setup. Instance receivers, computed/local operands,
init blocks, including immutable boolean build guards, run as complete source-owned units, once, without repeating
previous setup. Dynamic/effectful guards, loops, instance receivers, computed/local operands,
generic/duplicate registrations and existing live capture/state migration remain separate work. Test behavioral
effects of CF/ZD edits; payload acceptance and warning counts alone do not establish whole-commit coverage.
- An anonymous order group
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,88 @@
package de.peeeq.wurstscript.translation.imtranslation;

import de.peeeq.wurstscript.WurstOperator;
import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.types.WurstTypeBool;

import java.util.*;

/** Owns complete, independent callback registration statements before lowering can split their temporaries. */
final class HotReloadInitActions {
private HotReloadInitActions() { }

private record Guard(Expr condition, boolean thenBranch) { }
private record Unit(WStatement statement, List<Guard> guards) { }

static void translate(InitBlock block, ImTranslator translator, ImFunction init) {
Map<String, Integer> occurrences = new LinkedHashMap<>();
Map<FunctionCall, String> identities = new IdentityHashMap<>();
// Equal registrations in separate init blocks are ambiguous too. Keep such groups in the ordinary init.
// Equal registrations in separate blocks or branches are ambiguous too. Keep them in ordinary init code.
((WPackage) block.attrNearestPackage()).accept(new de.peeeq.wurstscript.ast.Element.DefaultVisitor() {
@Override public void visit(InitBlock candidate) {
for (WStatement statement : candidate.getBody()) {
if (!(statement instanceof FunctionCall call) || readsOutsideLocal(call)) continue;
for (Unit unit : units(candidate)) {
if (!(unit.statement() instanceof FunctionCall call) || readsOutsideLocal(call)) continue;
String key = ClosureTranslator.registrationIdentity(call, translator);
if (key != null) { identities.put(call, key); occurrences.merge(key, 1, Integer::sum); }
}
}
});
for (WStatement statement : block.getBody()) {
String key = statement instanceof FunctionCall call ? identities.get(call) : null;
for (Unit unit : units(block)) {
String key = unit.statement() instanceof FunctionCall call ? identities.get(call) : null;
if (key == null || occurrences.get(key) != 1) {
init.getBody().addAll(translator.translateStatements(init, List.of(statement)));
init.getBody().addAll(translate(unit, translator, init));
continue;
}
ImFunction action = JassIm.ImFunction(statement, "init_action_" + key, JassIm.ImTypeVars(), JassIm.ImVars(),
ImFunction action = JassIm.ImFunction(unit.statement(), "init_action_" + key, JassIm.ImTypeVars(), JassIm.ImVars(),
JassIm.ImVoid(), JassIm.ImVars(), JassIm.ImStmts(), List.of(FunctionFlagEnum.HOT_RELOAD_INIT_ACTION));
translator.imProg().getFunctions().add(action);
action.getBody().addAll(translator.translateStatements(action, List.of(statement)));
init.getBody().add(JassIm.ImFunctionCall(statement, action, JassIm.ImTypeArguments(), JassIm.ImExprs(), false, CallType.NORMAL));
action.getBody().addAll(translate(unit, translator, action));
init.getBody().add(JassIm.ImFunctionCall(unit.statement(), action, JassIm.ImTypeArguments(), JassIm.ImExprs(), false, CallType.NORMAL));
}
}

private static List<Unit> units(InitBlock block) {
List<Unit> result = new ArrayList<>();
collect(block.getBody(), List.of(), result);
return result;
}

private static void collect(WStatements statements, List<Guard> guards, List<Unit> result) {
for (WStatement statement : statements) {
if (statement instanceof StmtIf conditional && immutableGuard(conditional.getCond())) {
List<Guard> path = new ArrayList<>(guards);
path.add(new Guard(conditional.getCond(), true));
collect(conditional.getThenBlock(), List.copyOf(path), result);
path.set(path.size() - 1, new Guard(conditional.getCond(), false));
collect(conditional.getElseBlock(), List.copyOf(path), result);
} else {
result.add(new Unit(statement, guards));
}
}
}

private static List<ImStmt> translate(Unit unit, ImTranslator translator, ImFunction function) {
ImStmts body = JassIm.ImStmts(translator.translateStatements(function, List.of(unit.statement())));
// Every leaf keeps its original path. These guards have no effects and cannot change during init, so
// splitting them preserves order and lets later payloads add a registration without repeating its siblings.
for (int i = unit.guards().size() - 1; i >= 0; i--) {
Guard guard = unit.guards().get(i);
body = JassIm.ImStmts(JassIm.ImIf(guard.condition(), guard.condition().imTranslateExpr(translator, function),
guard.thenBranch() ? body : JassIm.ImStmts(), guard.thenBranch() ? JassIm.ImStmts() : body));
}
return body.removeAll();
}

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;
}

private static boolean readsOutsideLocal(FunctionCall call) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public List<String> getReasons() {
static Manifest install(LuaTranslator tr, Map<LuaFunction, ImFunction> reloadable, List<ImFunction> packageInits,
Set<ImFunction> entries, String compilerVersion) {
// Older scripts did not record every supported action kind; applying an unmarked unit would repeat setup.
compilerVersion += "+hotreload-init-actions-v2";
compilerVersion += "+hotreload-init-actions-v3";
Set<ImFunction> inits = Collections.newSetFromMap(new IdentityHashMap<>());
inits.addAll(packageInits);
Map<ImFunction, String> initNames = new IdentityHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public void hotReloadQualifiedStaticRegistrationsAreDeterministic() {
assertEquals(compileLuaHotReloadManifest(false, args, registrations, main), first);
}

@Test
public void hotReloadGuardedRegistrationsAreDeterministic() {
CU registrations = compilationUnit("Events.wurst", "package Events", "public interface Callback",
" function run() returns int", "Callback array saved", "public constant boolean PROD = false",
"public constant boolean ENABLED = true", "public class Registry",
" static function add(int event, Callback cb)", " saved[event] = cb");
CU main = compilationUnit("Main.wurst", "package Main", "import Events", "init",
" if not PROD and ENABLED", " Registry.add(2, () -> 20)", " Registry.add(3, () -> 30)",
" else", " Registry.add(4, () -> 40)");
RunArgs args = new RunArgs().with("-lua", "-luaHotReload", "-stacktraces");
var first = compileLuaHotReloadManifest(false, args, registrations, main);
assertEquals(3L, first.statements().stream().filter(statement -> statement.kind().equals("action")).count());
assertEquals(compileLuaHotReloadManifest(false, args, main, registrations), first);
assertEquals(compileLuaHotReloadManifest(false, args, registrations, main), first);
}

@Test
public void hotReloadUnreadSnapshotRootsAreDeterministic() {
CU values = compilationUnit("Values.wurst", "package Values", "public class Node", " public int value",
Expand Down
Loading
Loading