Localize global fields

This commit is contained in:
Lars Mueller 2021-07-06 23:43:03 +02:00
parent 60ce6f3e53
commit 60dd1be82c
2 changed files with 11 additions and 10 deletions

@ -1,5 +1,6 @@
-- Localize globals -- Localize globals
local assert, error, ipairs, math, modlib, next, pairs, setmetatable, string, table, type, unpack = assert, error, ipairs, math, modlib, next, pairs, setmetatable, string, table, type, unpack local assert, error, ipairs, math_floor, math_huge, modlib, next, pairs, setmetatable, string, table_insert, type, unpack
= assert, error, ipairs, math.floor, math.huge, modlib, next, pairs, setmetatable, string, table.insert, type, unpack
-- Set environment -- Set environment
local _ENV = {} local _ENV = {}
@ -61,8 +62,8 @@ local constants = {
[0] = "\2", [0] = "\2",
-- not possible as table entry as Lua doesn't allow +/-nan as table key -- not possible as table entry as Lua doesn't allow +/-nan as table key
-- [0/0] = "\3", -- [0/0] = "\3",
[math.huge] = "\4", [math_huge] = "\4",
[-math.huge] = "\5", [-math_huge] = "\5",
[""] = "\20" [""] = "\20"
} }
@ -297,18 +298,18 @@ function read(self, stream)
end end
if type <= type_ranges.string then if type <= type_ranges.string then
local string = stream_read(uint(type - type_ranges.number)) local string = stream_read(uint(type - type_ranges.number))
table.insert(references, string) table_insert(references, string)
return string return string
end end
if type <= type_ranges.table then if type <= type_ranges.table then
type = type - type_ranges.string - 1 type = type - type_ranges.string - 1
local tab = {} local tab = {}
table.insert(references, tab) table_insert(references, tab)
if type == 0 then if type == 0 then
return tab return tab
end end
local list_len = uint(type % 5) local list_len = uint(type % 5)
local kv_len = uint(math.floor(type / 5)) local kv_len = uint(math_floor(type / 5))
for index = 1, list_len do for index = 1, list_len do
tab[index] = _read(stream_read(1)) tab[index] = _read(stream_read(1))
end end

@ -1,5 +1,5 @@
local assert, next, pairs, pcall, error, type, table_insert, table_concat, string_format, setfenv, math_huge, loadfile, loadstring local assert, next, pairs, pcall, error, type, table_insert, table_concat, string_format, string_match, setfenv, math_huge, loadfile, loadstring
= assert, next, pairs, pcall, error, type, table.insert, table.concat, string.format, setfenv, math.huge, loadfile, loadstring = assert, next, pairs, pcall, error, type, table.insert, table.concat, string.format, string.match, setfenv, math.huge, loadfile, loadstring
local count_values = modlib.table.count_values local count_values = modlib.table.count_values
-- Build a table with the succeeding character from A-Z -- Build a table with the succeeding character from A-Z
@ -45,7 +45,7 @@ function serialize(object, write)
end end
end end
local function is_short_key(key) local function is_short_key(key)
return not references[key] and type(key) == "string" and key:match"^[%a_][%a%d_]*$" return not references[key] and type(key) == "string" and string_match(key, "^[%a_][%a%d_]*$")
end end
local function dump(value) local function dump(value)
-- Primitive types -- Primitive types
@ -60,7 +60,7 @@ function serialize(object, write)
end end
local type_ = type(value) local type_ = type(value)
if type_ == "number" then if type_ == "number" then
return write(("%.17g"):format(value)) return write(string_format("%.17g", value))
end end
-- Reference types: table and string -- Reference types: table and string
local ref = references[value] local ref = references[value]