mirror of
https://github.com/appgurueu/modlib.git
synced 2024-08-19 21:04:13 +02:00
Bluon: Clean up number writing
This commit is contained in:
parent
aa327126bf
commit
eb0a55af58
19
bluon.lua
19
bluon.lua
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user