Lua: Fix error when calling wesnoth.print_attributes on a GUI2 widget

This not only fixes the error in the GUI2 widget __dir metamethod,
but also makes the system swallow any errors in said metamethod.
A Lua warning is output if such an error arises.
This commit is contained in:
Celtic Minstrel 2024-01-06 12:03:03 -05:00
parent 3e3913da82
commit 219be073a3
3 changed files with 12 additions and 6 deletions

View file

@ -517,8 +517,11 @@ static void dir_meta_helper(lua_State* L, std::vector<std::string>& keys)
case LUA_TFUNCTION:
lua_pushvalue(L, 1);
lua_push(L, keys);
lua_call(L, 2, 1);
keys = lua_check<std::vector<std::string>>(L, -1);
if(lua_pcall(L, 2, 1, 0) == LUA_OK) {
keys = lua_check<std::vector<std::string>>(L, -1);
} else {
lua_warning(L, "wesnoth.print_attributes: __dir metamethod raised an error", false);
}
break;
case LUA_TTABLE:
auto dir_keys = lua_check<std::vector<std::string>>(L, -1);
@ -576,8 +579,11 @@ static int impl_get_dir_suffix(lua_State*L)
* This function does the actual work of grabbing all the attribute names.
* It's a separate function so that it can be used by tab-completion as well.
*/
static std::vector<std::string> luaW_get_attributes(lua_State* L, int idx)
std::vector<std::string> luaW_get_attributes(lua_State* L, int idx)
{
if(idx < 0 && idx >= -lua_gettop(L)) {
idx = lua_absindex(L, idx);
}
std::vector<std::string> keys;
if(lua_istable(L, idx)) {
// Walk the metatable chain (as long as __index is a table)...

View file

@ -144,3 +144,5 @@ private:
static lua_kernel_base*& get_lua_kernel_base_ptr(lua_State *L);
std::vector<std::tuple<std::string, std::string>> registered_widget_definitions_;
};
std::vector<std::string> luaW_get_attributes(lua_State* L, int idx);

View file

@ -609,10 +609,8 @@ int impl_widget_dir(lua_State* L)
}
}
// Add the gui.widget methods
luaW_getglobal(L, "dir");
luaW_getglobal(L, "gui", "widget");
lua_call(L, 1, 1);
auto methods = lua_check<std::vector<std::string>>(L, -1);
auto methods = luaW_get_attributes(L, -1);
keys.insert(keys.end(), methods.begin(), methods.end());
lua_push(L, keys);
return 1;