1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Add table.copy_with_metatables (#15754)

This commit is contained in:
Lars Müller 2025-03-14 11:52:42 +01:00 committed by GitHub
parent 8717c7bd00
commit 077828d0d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 63 additions and 10 deletions

View file

@ -457,18 +457,37 @@ do
end
end
--------------------------------------------------------------------------------
function table.copy(t, seen)
local n = {}
seen = seen or {}
seen[t] = n
for k, v in pairs(t) do
n[(type(k) == "table" and (seen[k] or table.copy(k, seen))) or k] =
(type(v) == "table" and (seen[v] or table.copy(v, seen))) or v
local function table_copy(value, preserve_metatables)
local seen = {}
local function copy(val)
if type(val) ~= "table" then
return val
end
local t = val
if seen[t] then
return seen[t]
end
local res = {}
seen[t] = res
for k, v in pairs(t) do
res[copy(k)] = copy(v)
end
if preserve_metatables then
setmetatable(res, getmetatable(t))
end
return res
end
return n
return copy(value)
end
function table.copy(value)
return table_copy(value, false)
end
function table.copy_with_metatables(value)
return table_copy(value, true)
end
function table.insert_all(t, other)
if table.move then -- LuaJIT