Serialize: Restore forward compatibility

This commit is contained in:
Lars Mueller 2022-08-17 18:48:55 +02:00 committed by SmallJoker
parent 6c24dc4e23
commit 7486f184c3

@ -70,6 +70,9 @@ local function serialize(value, write)
local type_ = type(object) local type_ = type(object)
-- Object must appear more than once. If it is a string, the reference has to be shorter than the string. -- Object must appear more than once. If it is a string, the reference has to be shorter than the string.
if count >= 2 and (type_ ~= "string" or #reference + 5 < #object) then if count >= 2 and (type_ ~= "string" or #reference + 5 < #object) then
if refnum == 1 then
write"local _={};" -- initialize reference table
end
write"_[" write"_["
write(reference) write(reference)
write("]=") write("]=")
@ -106,8 +109,16 @@ local function serialize(value, write)
end end
local type_ = type(value) local type_ = type(value)
if type_ == "number" then if type_ == "number" then
if value ~= value then -- nan
return write"0/0"
elseif value == math_huge then
return write"1/0"
elseif value == -math_huge then
return write"-1/0"
else
return write(string_format("%.17g", value)) return write(string_format("%.17g", value))
end end
end
-- Reference types: table, function and string -- Reference types: table, function and string
local ref = references[value] local ref = references[value]
if ref then if ref then
@ -190,8 +201,6 @@ end
local function dummy_func() end local function dummy_func() end
local nan = (0/0)^1 -- +nan
function core.deserialize(str, safe) function core.deserialize(str, safe)
-- Backwards compatibility -- Backwards compatibility
if str == nil then if str == nil then
@ -206,8 +215,8 @@ function core.deserialize(str, safe)
local func, err = loadstring(str) local func, err = loadstring(str)
if not func then return nil, err end if not func then return nil, err end
-- math.huge is serialized to inf, NaNs are serialized to nan by Lua -- math.huge was serialized to inf and NaNs to nan by Lua in Minetest 5.6, so we have to support this here
local env = {inf = math_huge, nan = nan, _ = {}} local env = {inf = math_huge, nan = 0/0}
if safe then if safe then
env.loadstring = dummy_func env.loadstring = dummy_func
else else