Localize global fields

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

View File

@ -1,5 +1,6 @@
-- 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
local _ENV = {}
@ -61,8 +62,8 @@ local constants = {
[0] = "\2",
-- not possible as table entry as Lua doesn't allow +/-nan as table key
-- [0/0] = "\3",
[math.huge] = "\4",
[-math.huge] = "\5",
[math_huge] = "\4",
[-math_huge] = "\5",
[""] = "\20"
}
@ -297,18 +298,18 @@ function read(self, stream)
end
if type <= type_ranges.string then
local string = stream_read(uint(type - type_ranges.number))
table.insert(references, string)
table_insert(references, string)
return string
end
if type <= type_ranges.table then
type = type - type_ranges.string - 1
local tab = {}
table.insert(references, tab)
table_insert(references, tab)
if type == 0 then
return tab
end
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
tab[index] = _read(stream_read(1))
end

View File

@ -1,5 +1,5 @@
local 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, 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, string.match, setfenv, math.huge, loadfile, loadstring
local count_values = modlib.table.count_values
-- Build a table with the succeeding character from A-Z
@ -45,7 +45,7 @@ function serialize(object, write)
end
end
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
local function dump(value)
-- Primitive types
@ -60,7 +60,7 @@ function serialize(object, write)
end
local type_ = type(value)
if type_ == "number" then
return write(("%.17g"):format(value))
return write(string_format("%.17g", value))
end
-- Reference types: table and string
local ref = references[value]