Page 1 of 1

[1.1.109] table.deepcopy in lualib/util.lua is wrong.

Posted: Mon Aug 05, 2024 1:00 am
by ness056
Hello there,

the function table.deepcopy defined in data/core/lualib/util.lua that allow to recursively copy a table does not work in the really particular case where the table is a metatable that modifies either the __index or the __pairs metamethods. In my case, I have a table with which I need to do particular things when an index is modified (like some sort of hook). To do it I use the __newindex metamethod to trigger the hook and I store the value in another hidden table (like in this). To make the table look like a normal table, I also modified the __index and __pairs metamethods to point to the hidden table. This makes the deepcopy function copy the hidden table and not the actual table.

I solved this problem in my mod by overriding the function and modifying the loop by this to ignore the metamethods:

Code: Select all

for index, value in next, object, nil do
    rawset(new_table, _copy(index), _copy(value))
end
It is a really small issue and I did solve it in my mod so I would understand if you don't change the function, but since I found it, I might as well report it.

Re: [1.1.109] table.deepcopy in lualib/util.lua is wrong.

Posted: Thu Oct 10, 2024 4:54 pm
by Rseding91
Thanks for the report however in this situation you are welcome to use your own implementation of deepcopy.