From 99cd354a16f4ec550ba3a2a50b40b0d95985c49d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Mon, 25 Mar 2024 20:49:00 +0100 Subject: [PATCH] Fix dialuges looping infinitely in some cases Namely, when no completion callback is specified, they use themselves, of all things, as their completion callback, because lua_gettop returns 0 and somehow Lua is ok with that stack index and thinks it refers to the function being executed. --- src/lua/LuaInterface.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/lua/LuaInterface.cpp b/src/lua/LuaInterface.cpp index e38992a15..4e8af47a9 100644 --- a/src/lua/LuaInterface.cpp +++ b/src/lua/LuaInterface.cpp @@ -50,7 +50,10 @@ static int beginMessageBox(lua_State *L) auto message = PickIfType(L, 2, String("Message")); auto large = PickIfType(L, 3, false); auto cb = std::make_shared(); // * Bind to main lua state (might be different from L). - cb->Assign(L, lua_gettop(L)); + if (lua_gettop(L)) + { + cb->Assign(L, lua_gettop(L)); + } new InformationMessage(title, message, large, { [cb]() { auto *lsi = GetLSI(); auto L = lsi->L; @@ -74,7 +77,10 @@ static int beginThrowError(lua_State *L) { auto errorMessage = PickIfType(L, 1, String("Error text")); auto cb = std::make_shared(); // * Bind to main lua state (might be different from L). - cb->Assign(L, lua_gettop(L)); + if (lua_gettop(L)) + { + cb->Assign(L, lua_gettop(L)); + } new ErrorMessage("Error", errorMessage, { [cb]() { auto *lsi = GetLSI(); auto L = lsi->L; @@ -101,7 +107,10 @@ static int beginInput(lua_State *L) auto text = PickIfType(L, 3, String("")); auto shadow = PickIfType(L, 4, String("")); auto cb = std::make_shared(); // * Bind to main lua state (might be different from L). - cb->Assign(L, lua_gettop(L)); + if (lua_gettop(L)) + { + cb->Assign(L, lua_gettop(L)); + } auto handle = [cb](std::optional input) { auto *lsi = GetLSI(); auto L = lsi->L; @@ -140,7 +149,10 @@ static int beginConfirm(lua_State *L) auto message = PickIfType(L, 2, String("Message")); auto buttonText = PickIfType(L, 3, String("Confirm")); auto cb = std::make_shared(); // * Bind to main lua state (might be different from L). - cb->Assign(L, lua_gettop(L)); + if (lua_gettop(L)) + { + cb->Assign(L, lua_gettop(L)); + } auto handle = [cb](int result) { auto *lsi = GetLSI(); auto L = lsi->L;