diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/LuaHotReload.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/LuaHotReload.java index 924cdfe16..69f6c06d8 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/LuaHotReload.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/LuaHotReload.java @@ -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> slots, Map> captures) { @@ -1328,9 +1328,9 @@ private static void collectFunctionValues(LuaCompilationUnit model, Map 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 { diff --git a/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/LuaHotReloadTests.java b/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/LuaHotReloadTests.java index d0e7e3c70..dab2d4666 100644 --- a/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/LuaHotReloadTests.java +++ b/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/LuaHotReloadTests.java @@ -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",