From f2fc9e7517a7947f3e2f508c9a28b1d399c82042 Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Sun, 19 Jan 2014 21:40:03 -0800 Subject: [PATCH] Improved Lua 5.2 support for serialized functions. --- src/serpent.lua | 4 ++-- t/test.lua | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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