diff options
author | Lars Mueller <appgurulars@gmx.de> | 2022-08-17 18:48:55 +0200 |
---|---|---|
committer | SmallJoker <mk939@ymail.com> | 2022-09-11 19:27:18 +0200 |
commit | 7486f184c3c800d462cf783a0f10289dcf9ebec6 (patch) | |
tree | a1c3f91585045c4ed7750f43d5eb601db381aed2 /builtin/common/serialize.lua | |
parent | 6c24dc4e23892a4fee4cf670e5999e7b643a2265 (diff) | |
download | minetest-7486f184c3c800d462cf783a0f10289dcf9ebec6.tar.xz |
Serialize: Restore forward compatibility
Diffstat (limited to 'builtin/common/serialize.lua')
-rw-r--r-- | builtin/common/serialize.lua | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/builtin/common/serialize.lua b/builtin/common/serialize.lua index 06eb181e0..afa63b362 100644 --- a/builtin/common/serialize.lua +++ b/builtin/common/serialize.lua @@ -70,6 +70,9 @@ local function serialize(value, write) local type_ = type(object) -- 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 refnum == 1 then + write"local _={};" -- initialize reference table + end write"_[" write(reference) write("]=") @@ -106,7 +109,15 @@ local function serialize(value, write) end local type_ = type(value) if type_ == "number" then - return write(string_format("%.17g", value)) + 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)) + end end -- Reference types: table, function and string local ref = references[value] @@ -190,8 +201,6 @@ end local function dummy_func() end -local nan = (0/0)^1 -- +nan - function core.deserialize(str, safe) -- Backwards compatibility if str == nil then @@ -206,8 +215,8 @@ function core.deserialize(str, safe) local func, err = loadstring(str) if not func then return nil, err end - -- math.huge is serialized to inf, NaNs are serialized to nan by Lua - local env = {inf = math_huge, nan = nan, _ = {}} + -- 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 = 0/0} if safe then env.loadstring = dummy_func else |