Added ability to process __tostring results that may be non-string values.

This commit is contained in:
Paul Kulchenko 2013-01-15 11:53:07 -08:00
parent bb73826310
commit 16c5599903
3 changed files with 13 additions and 2 deletions

View File

@ -130,6 +130,9 @@ See LICENSE file.
## History
### v0.22 (Jan 15 2013)
- Added ability to process __tostring results that may be non-string values.
### v0.21 (Jan 08 2013)
- Added `keyallow` and `valtypeignore` options (thanks to Jess Telford).
- Renamed `ignore` to `valignore`.

View File

@ -1,4 +1,4 @@
local n, v = "serpent", 0.21 -- (C) 2012 Paul Kulchenko; MIT License
local n, v = "serpent", 0.22 -- (C) 2012 Paul Kulchenko; MIT License
local c, d = "Paul Kulchenko", "Serializer and pretty printer of Lua data types"
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}
@ -58,7 +58,7 @@ local function s(t, opts)
if level >= maxl then return tag..'{}'..comment('max', level) end
seen[t] = insref or spath -- set path to use as reference
if getmetatable(t) and getmetatable(t).__tostring
then return tag..safestr(tostring(t))..comment("meta", level) end
then return tag..val2str(tostring(t),nil,indent,false,nil,nil,level+1)..comment("meta", level) end
if next(t) == nil then return tag..'{}'..comment(t, level) end -- table empty
local maxn, o, out = #t, {}, {}
for key = 1, maxn do table.insert(o, key) end

View File

@ -134,6 +134,14 @@ do
assert(loadstring(serpent.dump(a, {sparse = false, nocode = true, comment = true})),
"metatable with __tostring serialized with a comment: failed")
local shadow = {x = 11, y = 12}
mt.__index = function(t, f) return shadow[f] end
mt.__tostring = function(t) return {t[1], x = 1, y=t.y} end
local _a = assert(loadstring(serpent.dump(a, {sparse = false, nocode = true, comment = 1})))()
assert(_a.y == 12, "metatable with __tostring and __index 1: failed")
assert(_a[1] == 'a', "metatable with __tostring and __index 2: failed")
assert(_a.x == 1, "metatable with __tostring and __index 3: failed")
end
print("All tests passed.")