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 @@ -354,9 +354,9 @@ private String hotReloadCallSites() {
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)) continue;
String localReceiver = localReceiverName(call);
if (localReceiver == null && args.stream().allMatch(value -> value instanceof ExprClosure)) continue;
String key = registrationIdentity(call, tr, localReceiver);
String receiver = callbackReceiverIdentity(call);
if (receiver == null && args.stream().allMatch(value -> value instanceof ExprClosure)) continue;
String key = registrationIdentity(call, tr, receiver);
if (key != null) sites.append("_site").append(key).append("_argument").append(args.indexOf(argument));
}
return sites.toString();
Expand All @@ -366,17 +366,33 @@ static String registrationIdentity(FunctionCall call, ImTranslator tr) {
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
/** Name the source site only. The receiver and its accessor are evaluated by the original setup. */
private @Nullable String callbackReceiverIdentity(FunctionCall call) {
if (!(call instanceof ExprMemberMethodDot member)) return null;
if (member.getLeft() instanceof ExprMemberMethodDot accessor) {
String local = immutableLocalName(accessor.getLeft());
if (local != null && accessor.getArgs().isEmpty() && accessor.getTypeArgs().isEmpty()
&& accessor.getLeft().attrTyp() instanceof WurstTypeNamedScope scope
&& scope.getTypeParameters().isEmpty()
&& accessor.attrFuncDef() instanceof FuncDef method && method.attrIsDynamicClassMember()
&& method.getParameters().isEmpty() && tr.getFuncFor(method).getTypeVariables().isEmpty()) {
return local + ":accessor:" + hotReloadOwnerIdentity(method, tr);
}
return null;
}
return immutableLocalName(member.getLeft());
}

private static @Nullable String immutableLocalName(Expr receiver) {
if (receiver 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) {
private static String registrationIdentity(FunctionCall call, ImTranslator tr, @Nullable String receiver) {
if (receiver == 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 @@ -388,8 +404,8 @@ private 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);
if (receiver != null) {
key.append("receiver:").append(receiver.length()).append(':').append(receiver);
}
boolean callback = false;
for (int i = 0; i < call.getArgs().size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,62 @@ public void callbacksOnDistinctLocalReceiversKeepTheirLiveCaptures() {
test().testLua(true).executeProg().luaHotReload(edited).lines(baseline);
}

@Test
public void callbacksOnDistinctAccessorResultsKeepTheirLiveCaptures() {
String source = """
package Test
native hotReload()
native testSuccess()
native testFail(string message)
interface Callback
function run() returns int
class Button
Callback callback
int getterCalls
class Panel
let primary = new Button()
let secondary = new Button()
function getPrimary() returns Button
getterCalls++
return primary
function getSecondary() returns Button
getterCalls++
return secondary
function Button.onClick(Callback action)
this.callback = action
Panel shown
Panel other
function setup(int seed)
let panel = new Panel()
let alternate = new Panel()
shown = panel
other = alternate
%s
init
setup(10)
hotReload()
if shown.primary.callback.run() != 12 or getterCalls != 1
testFail("reload did not preserve the live callback or ran an accessor")
setup(20)
if shown.primary.callback.run() != 22 or shown.secondary.callback.run() != 120 or other.primary.callback.run() != 220
testFail("setup did not install separate callbacks")
testSuccess()
""";
String existing = """
panel.getPrimary().onClick() ->
return seed + %d
""";
String added = """
panel.getSecondary().onClick() ->
return seed + 100
alternate.getPrimary().onClick() ->
return seed + 200
""";
String[] baseline = source.formatted(existing.formatted(1)).lines().toArray(String[]::new);
String[] edited = source.formatted(added + existing.formatted(2)).lines().toArray(String[]::new);
test().testLua(true).executeProg().luaHotReload(edited).lines(baseline);
}

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