Exclude deprecated things from dir()

This commit is contained in:
Celtic Minstrel 2022-06-04 23:00:04 -04:00 committed by Celtic Minstrel
parent d104a81269
commit 1af084515f
2 changed files with 34 additions and 10 deletions

View File

@ -45,20 +45,23 @@ function wesnoth.deprecate_api(elem_name, replacement_name, level, version, elem
wesnoth.deprecated_message(elem_name, level, version, message)
end
end
return setmetatable({}, {
return setmetatable({__deprecated = true}, {
__index = show_msg,
__newindex = show_msg,
__call = show_msg,
__metatable = "removed API",
})
elseif type(elem) == "function" or getmetatable(elem) == "function" then
return function(...)
if not msg_shown then
msg_shown = true
wesnoth.deprecated_message(elem_name, level, version, message)
end
return elem(...)
end
return setmetatable({__deprecated = true}, {
__call = function(self, ...)
if not msg_shown then
msg_shown = true
wesnoth.deprecated_message(elem_name, level, version, message)
end
return elem(...)
end,
__metatable = "function"
})
elseif type(elem) == "table" then
-- Don't clobber the old metatable.
local old_mt = getmetatable(elem) or {}
@ -86,7 +89,7 @@ function wesnoth.deprecate_api(elem_name, replacement_name, level, version, elem
end
elem[key] = val
end
return setmetatable({}, mt)
return setmetatable({__deprecated = true}, mt)
else
wesnoth.log('warn', "Attempted to deprecate something that is not a table or function: " ..
elem_name .. " -> " .. replacement_name .. ", which is " .. tostring(elem))

View File

@ -517,6 +517,7 @@ static void dir_meta_helper(lua_State* L, std::vector<std::string>& keys)
std::copy(dir_keys.begin(), dir_keys.end(), std::back_inserter(keys));
break;
}
lua_pop(L, 1);
}
/**
@ -525,6 +526,8 @@ static void dir_meta_helper(lua_State* L, std::vector<std::string>& keys)
* - For a table, all keys defined in the table
* - Any keys accessible through the metatable chain (if __index on the metatable is a table)
* - The output of the __dir metafunction
* - Filtering out any keys beginning with two underscores
* - Filtering out any keys for which object[key].__deprecated exists and is true
* The list is then sorted alphabetically and formatted into columns.
* - Arg 1: Any object
* - Arg 3: (optional) Function to use for output; defaults to _G.print
@ -570,7 +573,25 @@ static int intf_object_dir(lua_State* L)
}
// Sort and remove any duplicates
std::sort(keys.begin(), keys.end());
keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
auto final = std::unique(keys.begin(), keys.end());
final = std::remove_if(keys.begin(), final, [L](const std::string& key) {
if(key.compare(0, 2, "__") == 0) {
return true;
}
auto type = lua_getfield(L, 1, key.c_str());
if(type == LUA_TTABLE) {
lua_pushliteral(L, "__deprecated");
if(lua_rawget(L, -2) == LUA_TBOOLEAN) {
auto deprecated = luaW_toboolean(L, -1);
lua_pop(L, 2);
return deprecated;
}
lua_pop(L, 1);
}
lua_pop(L, 1);
return false;
});
keys.erase(final, keys.end());
size_t max_len = std::accumulate(keys.begin(), keys.end(), 0, [](size_t max, const std::string& next) {
return std::max(max, next.size());
});