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,8 +431,8 @@ 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";
if (value instanceof ExprVarAccess access && access.attrNameDef() instanceof GlobalVarDef global
&& global.attrIsConstant()) {
GlobalVarDef global = registrationConstant(value);
if (global != null) {
StringBuilder name = new StringBuilder("global:");
String segment = global.getName(); name.append(segment.length()).append(':').append(segment);
for (Element scope = global.getParent(); scope != null; scope = scope.getParent()) {
Expand All @@ -446,6 +446,21 @@ private static String hotReloadArgument(Expr value) {
return null;
}

private static @Nullable GlobalVarDef registrationConstant(Expr value) {
if (value instanceof ExprVarAccess access && access.attrNameDef() instanceof GlobalVarDef global
&& global.attrIsConstant()) {
return global;
}
if (value instanceof ExprMemberVarDot access
&& access.getLeft().attrTyp() instanceof WurstTypeNamedScope qualifier && qualifier.isStaticRef()
&& qualifier.getDef() instanceof ClassDef owner && owner.getTypeParameters().isEmpty()
&& access.attrNameDef() instanceof GlobalVarDef global && global.attrIsConstant()
&& !global.attrIsDynamicClassMember() && global.attrNearestStructureDef() == owner) {
return global;
}
return null;
}

private String makeFuncName(FuncDef superClass) {
String name = superClass.getName();
// LuaDispatchPreparation reads a semantic name back out of the implementation's name: what
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,30 @@ public void qualifiedStaticRegistrationsRunOnceWithoutRepeatingOldSetup() {
.lines(qualifiedRegistrations(initEventCallbacks(false)));
}

@Test
public void qualifiedEventConstantsKeepRegistrationsDistinct() {
test().testLua(true).executeProg().luaHotReload(qualifiedEventConstants(true))
.lines(qualifiedEventConstants(false));
}

private static String[] qualifiedEventConstants(boolean edited) {
return java.util.Arrays.stream(initEventCallbacks(edited))
.map(line -> line.equals("package Test")
? line + "\nclass EventIds\n static constant FIRST = 1\n static constant SECOND = 2"
: line.replace("register(1,", "register(EventIds.FIRST,")
.replace("register(2,", "register(EventIds.SECOND,"))
.toArray(String[]::new);
}

@Test
public void qualifiedInstanceEventConstantsRemainOrdinaryInit() {
String[] source = {"package Test", "class EventIds", " let eventId = 1", "constant ids = new EventIds()",
"interface Callback", " function run() returns int", "Callback array saved",
"function register(int event, Callback cb)", " saved[event] = cb",
"init", " register(ids.eventId, () -> 10)"};
assertFalse(manifest(source).statements().stream().anyMatch(statement -> statement.kind().equals("action")));
}

@Test
public void qualifiedStaticNativeRegistrationsKeepEditedBodies() {
test().testLua(true).executeProg().luaHotReload(qualifiedRegistrations(nativeEventCallbacks(true)))
Expand Down
Loading