Improved Lua 5.2 support for serialized functions.

This commit is contained in:
Paul Kulchenko 2014-01-19 21:40:03 -08:00
parent 8070d1edbb
commit f2fc9e7517
2 changed files with 18 additions and 2 deletions

View File

@ -1,4 +1,4 @@
local n, v = "serpent", 0.27 -- (C) 2012-13 Paul Kulchenko; MIT License local n, v = "serpent", 0.271 -- (C) 2012-13 Paul Kulchenko; MIT License
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer" 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 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} local badtype = {thread = true, userdata = true, cdata = true}
@ -92,7 +92,7 @@ local function s(t, opts)
seen[t] = insref or spath seen[t] = insref or spath
local ok, res = pcall(string.dump, t) local ok, res = pcall(string.dump, t)
local func = ok and ((opts.nocode and "function() --[[..skipped..]] end" or local func = ok and ((opts.nocode and "function() --[[..skipped..]] end" or
"loadstring("..safestr(res)..",'@serialized')")..comment(t, level)) "((loadstring or load)("..safestr(res)..",'@serialized'))")..comment(t, level))
return tag..(func or globerr(t, level)) return tag..(func or globerr(t, level))
else return tag..safestr(t) end -- handle all other types else return tag..safestr(t) end -- handle all other types
end end

View File

@ -375,6 +375,22 @@ do
end end
end end
do -- test for Lua 5.2 compiled without loadstring
local a = {function() return 1 end}
local load, loadstring = _G.load, _G.loadstring
local f = assert(loadstring('load = loadstring or load; loadstring = nil; return '..serpent.line(a)),
"serializing table with function as a value (1/2): failed")
local _a = f()
assert(_a[1]() == a[1](), "deserialization of function value without loadstring (1/2): failed")
_G.load, _G.loadstring = load, loadstring
local f = assert(loadstring('return '..serpent.line(a)),
"serializing table with function as a value (2/2): failed")
local _a = f()
assert(_a[1]() == a[1](), "deserialization of function value without loadstring (2/2): failed")
end
print("All tests passed.") print("All tests passed.")
if arg[1] == 'perf' then if arg[1] == 'perf' then