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 @@ -13,6 +13,7 @@
import de.peeeq.wurstscript.types.*;
import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.utils.Utils;
import org.eclipse.jdt.annotation.Nullable;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -352,15 +353,30 @@ private String hotReloadCallSites() {
Element argument = e;
for (Element parent = e.getParent(); parent != null; argument = parent, parent = parent.getParent()) {
if (!(parent instanceof Arguments args) || !(parent.getParent() instanceof FunctionCall call)) continue;
if (!(argument instanceof ExprClosure) || args.stream().allMatch(value -> value instanceof ExprClosure)) continue;
String key = registrationIdentity(call, tr);
if (!(argument instanceof ExprClosure)) continue;
String localReceiver = localReceiverName(call);
if (localReceiver == null && args.stream().allMatch(value -> value instanceof ExprClosure)) continue;
String key = registrationIdentity(call, tr, localReceiver);
if (key != null) sites.append("_site").append(key).append("_argument").append(args.indexOf(argument));
}
return sites.toString();
}

static String registrationIdentity(FunctionCall call, ImTranslator tr) {
if (call instanceof ExprMemberMethod member) {
return registrationIdentity(call, tr, null);
}

/** A local receiver can name a callback site, but its setup cannot be replayed as an init action. */
private static @Nullable String localReceiverName(FunctionCall call) {
if (call instanceof ExprMemberMethodDot member && member.getLeft() instanceof ExprVarAccess access
&& access.attrNameDef() instanceof LocalVarDef local && local.attrIsConstant()) {
return local.getName();
}
return null;
}

private static String registrationIdentity(FunctionCall call, ImTranslator tr, @Nullable String localReceiver) {
if (localReceiver == null && call instanceof ExprMemberMethod member) {
// A static qualifier names the resolved declaration, not a runtime receiver. Dynamic receivers and
// generic bindings need their own identities; do not guess those from the spelling of the qualifier.
if (!(member instanceof ExprMemberMethodDot)
Expand All @@ -372,6 +388,9 @@ static String registrationIdentity(FunctionCall call, ImTranslator tr) {
}
if (!call.getTypeArgs().isEmpty() || !tr.getFuncFor(call.attrFuncDef()).getTypeVariables().isEmpty()) return null;
StringBuilder key = new StringBuilder(hotReloadOwnerIdentity(call.attrFuncDef(), tr));
if (localReceiver != null) {
key.append("receiver:").append(localReceiver.length()).append(':').append(localReceiver);
}
boolean callback = false;
for (int i = 0; i < call.getArgs().size(); i++) {
Expr value = call.getArgs().get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,34 @@ public void qualifiedStaticCallbackSitesKeepCapturedValuesAcrossInsertions() {
.lines(qualifiedRegistrations(eventCallbacks(false)));
}

@Test
public void callbacksOnDistinctLocalReceiversKeepTheirLiveCaptures() {
String[] baseline = {
"package Test", "native hotReload()", "native testSuccess()", "native testFail(string message)",
"interface Callback", " function run() returns int",
"class Button", " Callback callback",
"function Button.onClick(Callback action)", " this.callback = action",
"Button mainButton", "Button alternateButton",
"function setup(int seed)",
" let main = new Button()", " let alternate = new Button()",
" mainButton = main", " alternateButton = alternate",
" main.onClick() ->\n return seed + 1",
"init",
" setup(10)",
" hotReload()",
" if mainButton.callback.run() != 12",
" testFail(\"existing button lost its callback or captures\")",
" setup(20)",
" if mainButton.callback.run() != 22 or alternateButton.callback.run() != 120",
" testFail(\"new buttons did not get their own callbacks\")",
" testSuccess()"
};
String[] edited = baseline.clone();
edited[17] = " alternate.onClick() ->\n return seed + 100\n"
+ " main.onClick() ->\n return seed + 2";
test().testLua(true).executeProg().luaHotReload(edited).lines(baseline);
}

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