1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

minor refactor

This commit is contained in:
y5nw 2025-02-22 21:22:14 +01:00
parent 5f56d14bd4
commit e713d5291d

View file

@ -26,29 +26,29 @@ local function prepare_objects(value)
-- Early return for nil; tables can't contain nil -- Early return for nil; tables can't contain nil
return counts, type_lookup return counts, type_lookup
end end
local function count_values(val) local function count_values(val, recount)
local type_ = type(val) local type_ = type(val)
if type_ == "boolean" or type_ == "number" then if type_ == "boolean" or type_ == "number" then
return return
end end
local count = counts[val] if not recount then
counts[val] = (count or 0) + 1 local count = counts[val]
local mt = getmetatable(val) counts[val] = (count or 0) + 1
if type_ == "table" then end
if not count then local mt = (not recount) and (type_ == "table" or type_ == "userdata") and getmetatable(val)
for k, v in pairs(val) do if mt and core.known_metatables[mt] then
count_values(k) type_lookup[val] = core.known_metatables[mt]
count_values(v) count_values(val, true)
end elseif type_ == "table" then
if mt then for k, v in pairs(val) do
type_lookup[val] = core.known_metatables[mt] count_values(k)
end count_values(v)
end end
elseif type_ ~= "string" and type_ ~= "function" and not is_itemstack(val) then elseif type_ ~= "string" then
error("unsupported type: " .. type_) error("unsupported type: " .. type_)
end end
end end
count_values(value, {}) count_values(value, false)
return counts, type_lookup return counts, type_lookup
end end