Fix bluon:len

This commit is contained in:
Lars Mueller
2021-06-11 20:24:23 +02:00
parent e5ac7aa27b
commit a9c4e86b83

@ -1,5 +1,9 @@
--! experimental --! experimental
local bluon = getfenv(1) local bluon = getfenv(1)
local no_op = modlib.func.no_op
local write_float = modlib.binary.write_float
local metatable = {__index = function(_self, key) local metatable = {__index = function(_self, key)
return rawget(bluon, key) return rawget(bluon, key)
end} end}
@ -108,17 +112,17 @@ function len(self, object)
local _type = type(object) local _type = type(object)
if _type == "number" then if _type == "number" then
if object ~= object then if object ~= object then
stream:write(constant_nan) return 1
return
end end
if object % 1 == 0 then if object % 1 == 0 then
return 1 + uint_len(object > 0 and object or -object) return 1 + uint_len(object > 0 and object or -object)
end end
-- TODO ensure this check is proper -- HACK use write_float to get the length
if mantissa % 2^-23 > 0 then local bytes = 4
return 9 write_float(no_op, object, function(double)
end if double then bytes = 8 end
return 5 end)
return 1 + bytes
end end
local id = object_ids[object] local id = object_ids[object]
if id then if id then
@ -175,7 +179,6 @@ function write(self, object, stream)
byte(base + type_offset) byte(base + type_offset)
uint(type_offset, _uint) uint(type_offset, _uint)
end end
local write_float = modlib.binary.write_float
local function float_on_write(double) local function float_on_write(double)
byte(double and type_ranges.number or type_ranges.number_f32) byte(double and type_ranges.number or type_ranges.number_f32)
end end