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
if not recount then
local count = counts[val] local count = counts[val]
counts[val] = (count or 0) + 1 counts[val] = (count or 0) + 1
local mt = getmetatable(val) end
if type_ == "table" then local mt = (not recount) and (type_ == "table" or type_ == "userdata") and getmetatable(val)
if not count then if mt and core.known_metatables[mt] then
type_lookup[val] = core.known_metatables[mt]
count_values(val, true)
elseif type_ == "table" then
for k, v in pairs(val) do for k, v in pairs(val) do
count_values(k) count_values(k)
count_values(v) count_values(v)
end end
if mt then elseif type_ ~= "string" then
type_lookup[val] = core.known_metatables[mt]
end
end
elseif type_ ~= "string" and type_ ~= "function" and not is_itemstack(val) 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