Updated to avoid calling metamethods when metatostring set to false (fixes #29).

This commit is contained in:
Paul Kulchenko 2018-03-18 20:53:36 -07:00
parent a0e2a889b1
commit 33b12f1cb4

View File

@ -1,4 +1,4 @@
local n, v = "serpent", "0.30" -- (C) 2012-17 Paul Kulchenko; MIT License
local n, v = "serpent", "0.301" -- (C) 2012-17 Paul Kulchenko; MIT License
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}
local badtype = {thread = true, userdata = true, cdata = true}
@ -51,10 +51,10 @@ local function s(t, opts)
sref[#sref+1] = spath..space..'='..space..seen[t]
return tag..'nil'..comment('ref', level) end
-- protect from those cases where __tostring may fail
if type(mt) == 'table' then
if type(mt) == 'table' and metatostring ~= false then
local to, tr = pcall(function() return mt.__tostring(t) end)
local so, sr = pcall(function() return mt.__serialize(t) end)
if (opts.metatostring ~= false and to or so) then -- knows how to serialize itself
if (to or so) then -- knows how to serialize itself
seen[t] = insref or spath
t = so and sr or tr
ttype = type(t)