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

Things that we don't consider worth fixing at this moment.
ness056
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sun Jul 24, 2022 3:47 pm
Contact:

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

Post 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.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14403
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post by Rseding91 »

Thanks for the report however in this situation you are welcome to use your own implementation of deepcopy.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Won't fix.”