diff --git a/src/serpent.lua b/src/serpent.lua index 4628a74..e78d28f 100644 --- a/src/serpent.lua +++ b/src/serpent.lua @@ -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 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} @@ -92,7 +92,7 @@ local function s(t, opts) seen[t] = insref or spath local ok, res = pcall(string.dump, t) 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)) else return tag..safestr(t) end -- handle all other types end diff --git a/t/test.lua b/t/test.lua index f757df5..1a9016b 100644 --- a/t/test.lua +++ b/t/test.lua @@ -375,6 +375,22 @@ do 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.") if arg[1] == 'perf' then