Parameter rename

This commit is contained in:
Lars Mueller 2021-07-07 17:35:25 +02:00
parent b5e7f331c4
commit 392959e50c

@ -81,20 +81,20 @@ local function uint_type(uint)
end end
local valid_types = modlib.table.set{"nil", "boolean", "number", "string"} local valid_types = modlib.table.set{"nil", "boolean", "number", "string"}
function is_valid(self, object) function is_valid(self, value)
local _type = type(object) local _type = type(value)
if valid_types[_type] then if valid_types[_type] then
return true return true
end end
if _type == "table" then if _type == "table" then
for key, value in pairs(object) do for key, value in pairs(value) do
if not (is_valid(self, key) and is_valid(self, value)) then if not (is_valid(self, key) and is_valid(self, value)) then
return false return false
end end
end end
return true return true
end end
return self.aux_is_valid(object) return self.aux_is_valid(value)
end end
local function uint_len(uint) local function uint_len(uint)
@ -105,69 +105,69 @@ local function is_map_key(key, list_len)
return type(key) ~= "number" or (key < 1 or key > list_len or key % 1 ~= 0) return type(key) ~= "number" or (key < 1 or key > list_len or key % 1 ~= 0)
end end
function len(self, object) function len(self, value)
if object == nil then if value == nil then
return 0 return 0
end end
if constants[object] then if constants[value] then
return 1 return 1
end end
local object_ids = {} local object_ids = {}
local current_id = 0 local current_id = 0
local _type = type(object) local _type = type(value)
if _type == "number" then if _type == "number" then
if object ~= object then if value ~= value then
return 1 return 1
end end
if object % 1 == 0 then if value % 1 == 0 then
return 1 + uint_len(object > 0 and object or -object) return 1 + uint_len(value > 0 and value or -value)
end end
-- HACK use write_float to get the length -- HACK use write_float to get the length
local bytes = 4 local bytes = 4
write_float(no_op, object, function(double) write_float(no_op, value, function(double)
if double then bytes = 8 end if double then bytes = 8 end
end) end)
return 1 + bytes return 1 + bytes
end end
local id = object_ids[object] local id = object_ids[value]
if id then if id then
return 1 + uint_len(id) return 1 + uint_len(id)
end end
current_id = current_id + 1 current_id = current_id + 1
object_ids[object] = current_id object_ids[value] = current_id
if _type == "string" then if _type == "string" then
local object_len = object:len() local object_len = value:len()
return 1 + uint_len(object_len) + object_len return 1 + uint_len(object_len) + object_len
end end
if _type == "table" then if _type == "table" then
if next(object) == nil then if next(value) == nil then
-- empty {} table -- empty {} table
return 1 return 1
end end
local list_len = #object local list_len = #value
local kv_len = 0 local kv_len = 0
for key, _ in pairs(object) do for key, _ in pairs(value) do
if is_map_key(key, list_len) then if is_map_key(key, list_len) then
kv_len = kv_len + 1 kv_len = kv_len + 1
end end
end end
local table_len = 1 + uint_len(list_len) + uint_len(kv_len) local table_len = 1 + uint_len(list_len) + uint_len(kv_len)
for index = 1, list_len do for index = 1, list_len do
table_len = table_len + self:len(object[index]) table_len = table_len + self:len(value[index])
end end
for key, value in pairs(object) do for key, value in pairs(value) do
if is_map_key(key, list_len) then if is_map_key(key, list_len) then
table_len = table_len + self:len(key) + self:len(value) table_len = table_len + self:len(key) + self:len(value)
end end
end end
return kv_len + table_len return kv_len + table_len
end end
return self.aux_len(object) return self.aux_len(value)
end end
--: stream any object implementing :write(text) --: stream any object implementing :write(text)
function write(self, object, stream) function write(self, value, stream)
if object == nil then if value == nil then
return return
end end
local object_ids = {} local object_ids = {}
@ -191,49 +191,49 @@ function write(self, object, stream)
write_float(byte, number, float_on_write) write_float(byte, number, float_on_write)
end end
local aux_write = self.aux_write local aux_write = self.aux_write
local function _write(object) local function _write(value)
local constant = constants[object] local constant = constants[value]
if constant then if constant then
stream:write(constant) stream:write(constant)
return return
end end
local _type = type(object) local _type = type(value)
if _type == "number" then if _type == "number" then
if object ~= object then if value ~= value then
stream:write(constant_nan) stream:write(constant_nan)
return return
end end
if object % 1 == 0 then if value % 1 == 0 then
uint_with_type(object > 0 and type_ranges.number_constant or type_ranges.number_negative, object > 0 and object or -object) uint_with_type(value > 0 and type_ranges.number_constant or type_ranges.number_negative, value > 0 and value or -value)
return return
end end
float(object) float(value)
return return
end end
local id = object_ids[object] local id = object_ids[value]
if id then if id then
uint_with_type(type_ranges.table, id) uint_with_type(type_ranges.table, id)
return return
end end
if _type == "string" then if _type == "string" then
local len = object:len() local len = value:len()
current_id = current_id + 1 current_id = current_id + 1
object_ids[object] = current_id object_ids[value] = current_id
uint_with_type(type_ranges.number, len) uint_with_type(type_ranges.number, len)
stream:write(object) stream:write(value)
return return
end end
if _type == "table" then if _type == "table" then
current_id = current_id + 1 current_id = current_id + 1
object_ids[object] = current_id object_ids[value] = current_id
if next(object) == nil then if next(value) == nil then
-- empty {} table -- empty {} table
byte(type_ranges.string + 1) byte(type_ranges.string + 1)
return return
end end
local list_len = #object local list_len = #value
local kv_len = 0 local kv_len = 0
for key, _ in pairs(object) do for key, _ in pairs(value) do
if is_map_key(key, list_len) then if is_map_key(key, list_len) then
kv_len = kv_len + 1 kv_len = kv_len + 1
end end
@ -244,9 +244,9 @@ function write(self, object, stream)
uint(list_len_sig, list_len) uint(list_len_sig, list_len)
uint(kv_len_sig, kv_len) uint(kv_len_sig, kv_len)
for index = 1, list_len do for index = 1, list_len do
_write(object[index]) _write(value[index])
end end
for key, value in pairs(object) do for key, value in pairs(value) do
if is_map_key(key, list_len) then if is_map_key(key, list_len) then
_write(key) _write(key)
_write(value) _write(value)
@ -254,9 +254,9 @@ function write(self, object, stream)
end end
return return
end end
aux_write(object, object_ids) aux_write(value, object_ids)
end end
_write(object) _write(value)
end end
local constants_flipped = modlib.table.flip(constants) local constants_flipped = modlib.table.flip(constants)