Bluon: Clean up number writing

This commit is contained in:
Lars Mueller 2022-07-02 17:41:11 +02:00
parent aa327126bf
commit eb0a55af58

@ -9,7 +9,8 @@ setfenv(1, _ENV)
--! experimental --! experimental
local no_op = modlib.func.no_op local no_op = modlib.func.no_op
local write_float = modlib.binary.write_float local fround = modlib.math.fround
local write_single, write_double = modlib.binary.write_single, modlib.binary.write_double
local metatable = {__index = _ENV} local metatable = {__index = _ENV}
@ -122,11 +123,8 @@ function len(self, value)
if value % 1 == 0 then if value % 1 == 0 then
return 1 + uint_len(value > 0 and value or -value) return 1 + uint_len(value > 0 and value or -value)
end end
-- HACK use write_float to get the length
local bytes = 4 local bytes = 4
write_float(no_op, value, function(double) if fround(value) ~= value then bytes = 8 end
if double then bytes = 8 end
end)
return 1 + bytes return 1 + bytes
end end
local id = object_ids[value] local id = object_ids[value]
@ -184,11 +182,14 @@ function write(self, value, stream)
byte(base + type_offset) byte(base + type_offset)
uint(type_offset, _uint) uint(type_offset, _uint)
end end
local function float_on_write(double)
byte(double and type_ranges.number or type_ranges.number_f32)
end
local function float(number) local function float(number)
write_float(byte, number, float_on_write) if fround(number) == number then
byte(type_ranges.number_f32)
write_single(byte, number)
else
byte(type_ranges.number)
write_double(byte, number)
end
end end
local aux_write = self.aux_write local aux_write = self.aux_write
local function _write(value) local function _write(value)