Norbyte / ositools

Advanced scripting and mod support for Divinity Original Sin 2
MIT License
382 stars 30 forks source link

JSON exports ignore custom pairs, ipairs metamethods #104

Open PinewoodPip opened 1 year ago

PinewoodPip commented 1 year ago

Extender Version

v57, v58

Game Version

v3.6.117.3735

Bug Summary

The JSON serialization currently ignores any custom implementations of __pairs, __ipairs for tables. As a result, the JSON produced is not the one the user would expect.

The following code snippet demostrates the issue:

local m = {
    __pairs = function(self)
        -- Return nothing
        return function() return nil, nil end, self, nil
    end
}

local mytbl = {A = 1, B = 2, C = 3}
setmetatable(mytbl, m)

print("printing keys and values")
for k,v in pairs(mytbl) do
    print(k, v) -- Does not execute even once
end

Ext.IO.SaveFile("test", Ext.DumpExport(mytbl)) -- Will have A, B, C

The __pairs metamethod in the snippet is setup to never return anything, yet the JSON file created at the end will contain all values of the table.

A possible use case where this would interfere would be serializing a custom data structure with iterator metamethods implemented. Any internal keys related to the data structure's implementation would end up in the JSON even if __pairs, __ipairs had been overwritten to skip them.

The issue likely stems from this method using next() directly: image

Links

No response