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 @@ -1315,8 +1315,8 @@ private static String firstLine(String s) {

/**
* Finds where a function is used as a value rather than called: a class-table binding the payload repeats
* ({@code slots}), or a store it cannot reach ({@code captures}). A callback adapter passes its target to xpcall by
* name each time it runs, so it reaches the new body without either.
* ({@code slots}), or a store it cannot reach ({@code captures}). A callback adapter passes its target and its error
* handler to xpcall by name each time it runs, so it reaches their new bodies without either.
*/
private static void collectFunctionValues(LuaCompilationUnit model, Map<LuaFunction, List<String>> slots,
Map<LuaFunction, List<String>> captures) {
Expand All @@ -1328,9 +1328,9 @@ private static void collectFunctionValues(LuaCompilationUnit model, Map<LuaFunct
if (parent instanceof LuaAssignment a && a.getRight() == ref
&& a.getLeft() instanceof LuaExprFieldAccess fa && fa.getReceiver() instanceof LuaExprVarAccess table) {
slots.computeIfAbsent(target, t -> new ArrayList<>()).add(table.getVar().getName() + "." + fa.getFieldName());
} else if (parent instanceof LuaExprlist args && args.indexOf(ref) == 0
} else if (parent instanceof LuaExprlist args && args.indexOf(ref) <= 1
&& args.getParent() instanceof LuaExprFunctionCallByName call && call.getFuncName().equals("xpcall")) {
// resolved by name on every call
// the function and the message handler, resolved by name on every call
} else if (parent instanceof LuaIf condition && condition.getCond() == ref) {
// a native fallback asks whether the runtime defines the native
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,32 @@ public void removedCallbackAdapterWarns() throws Exception {
&& w.endsWith("restart the map to drop it.")));
}

/**
* A callback adapter gives xpcall its error handler by name on every call, as it gives its target, so a change to
* the handler reloads: here the edit leaves ErrorHandling's error unreachable, and the handler, which called it,
* prints instead.
*/
@Test
public void callbackErrorHandlerReloads() throws Exception {
CU errorHandling = new CU("ErrorHandling", String.join("\n", "package ErrorHandling",
"public function error(string msg)", " skip", ""));
String[] base = {"package Main", "import ErrorHandling", "native register(code callback)",
"function callback()", " skip", "function check(int x)", " if x < 0", " error(\"negative\")",
"init", " register(function callback)", " check(1)", ""};
String[] edited = {"package Main", "import ErrorHandling", "native register(code callback)",
"function callback()", " skip", "init", " register(function callback)", ""};
LuaHotReload.Manifest before = compileLuaHotReloadManifest(false, HOT_RELOAD, errorHandling,
new CU("callbackErrorHandlerReloads", String.join("\n", base)));
LuaHotReload.Manifest after = compileLuaHotReloadManifest(false, HOT_RELOAD, errorHandling,
new CU("callbackErrorHandlerReloads", String.join("\n", edited)));
String handler = before.functions().keySet().stream().filter(n -> n.startsWith("__wurst_callback_error"))
.findFirst().orElseThrow();
assertFalse("the edit changes the handler", before.functions().get(handler).definition()
.equals(after.functions().get(handler).definition()));
LuaHotReload.Payload payload = LuaHotReload.payload(before, after, LuaHotReload.History.NONE, 1);
assertTrue(payload.functions().toString(), payload.functions().contains(handler));
}

@Test
public void removedMethodWarnsUnlessItsSlotIsRebound() throws Exception {
LuaHotReload.Function method = new LuaHotReload.Function("f", "Test:f()void", "function f() end",
Expand Down
Loading