* luci/libd/core: Add serialize_data() and restore_data() to luci.util, use the new functions to permit the generation of bytecode for most data types. Translation fixes.
This commit is contained in:
parent
7384e0e6b7
commit
1ff791f765
1 changed files with 75 additions and 13 deletions
|
@ -75,7 +75,7 @@ end
|
||||||
--- Test whether the given object is an instance of the given class.
|
--- Test whether the given object is an instance of the given class.
|
||||||
-- @param object Object instance
|
-- @param object Object instance
|
||||||
-- @param class Class object to test against
|
-- @param class Class object to test against
|
||||||
-- @return Boolean indicating wheather the object is an instance
|
-- @return Boolean indicating whether the object is an instance
|
||||||
-- @see class
|
-- @see class
|
||||||
-- @see clone
|
-- @see clone
|
||||||
function instanceof(object, class)
|
function instanceof(object, class)
|
||||||
|
@ -162,7 +162,7 @@ end
|
||||||
|
|
||||||
--- Write given object to stderr.
|
--- Write given object to stderr.
|
||||||
-- @param obj Value to write to stderr
|
-- @param obj Value to write to stderr
|
||||||
-- @return Boolean indicating wheather the write operation was successful
|
-- @return Boolean indicating whether the write operation was successful
|
||||||
function perror(obj)
|
function perror(obj)
|
||||||
return io.stderr:write(tostring(obj) .. "\n")
|
return io.stderr:write(tostring(obj) .. "\n")
|
||||||
end
|
end
|
||||||
|
@ -209,12 +209,12 @@ end
|
||||||
--- Splits given string on a defined seperator sequence and return a table
|
--- Splits given string on a defined seperator sequence and return a table
|
||||||
-- containing the resulting substrings. The optional max parameter specifies
|
-- containing the resulting substrings. The optional max parameter specifies
|
||||||
-- the number of bytes to process, regardless of the actual length of the given
|
-- the number of bytes to process, regardless of the actual length of the given
|
||||||
-- string. The optional last parameter, regex, sepcifies wheather the separator
|
-- string. The optional last parameter, regex, sepcifies whether the separator
|
||||||
-- sequence is interpreted as regular expression.
|
-- sequence is interpreted as regular expression.
|
||||||
-- @param str String value containing the data to split up
|
-- @param str String value containing the data to split up
|
||||||
-- @param pat String with separator pattern (optional, defaults to "\n")
|
-- @param pat String with separator pattern (optional, defaults to "\n")
|
||||||
-- @param max Num of bytes to process (optional, default is string length)
|
-- @param max Num of bytes to process (optional, default is string length)
|
||||||
-- @param regexp Boolean indicating wheather to interprete the separator
|
-- @param regexp Boolean indicating whether to interprete the separator
|
||||||
-- pattern as regular expression (optional, default is false)
|
-- pattern as regular expression (optional, default is false)
|
||||||
-- @return Table containing the resulting substrings
|
-- @return Table containing the resulting substrings
|
||||||
function split(str, pat, max, regex)
|
function split(str, pat, max, regex)
|
||||||
|
@ -335,7 +335,7 @@ end
|
||||||
--- Checks whether the given table contains the given value.
|
--- Checks whether the given table contains the given value.
|
||||||
-- @param table Table value
|
-- @param table Table value
|
||||||
-- @param value Value to search within the given table
|
-- @param value Value to search within the given table
|
||||||
-- @return Boolean indicating wheather the given value occurs within table
|
-- @return Boolean indicating whether the given value occurs within table
|
||||||
function contains(table, value)
|
function contains(table, value)
|
||||||
for k, v in pairs(table) do
|
for k, v in pairs(table) do
|
||||||
if value == v then
|
if value == v then
|
||||||
|
@ -358,7 +358,7 @@ end
|
||||||
|
|
||||||
--- Clones the given object and return it's copy.
|
--- Clones the given object and return it's copy.
|
||||||
-- @param object Table value to clone
|
-- @param object Table value to clone
|
||||||
-- @param deep Boolean indicating wheather to do recursive cloning
|
-- @param deep Boolean indicating whether to do recursive cloning
|
||||||
-- @return Cloned table value
|
-- @return Cloned table value
|
||||||
function clone(object, deep)
|
function clone(object, deep)
|
||||||
local copy = {}
|
local copy = {}
|
||||||
|
@ -375,18 +375,80 @@ function clone(object, deep)
|
||||||
return copy
|
return copy
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Test whether the given table value is a numerically indexed table.
|
||||||
|
function _is_numeric_table(t)
|
||||||
|
local k = pairs(t)(t)
|
||||||
|
return ( tonumber(k) ~= nil )
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Serialize the contents of a table value.
|
||||||
|
function _serialize_table(t)
|
||||||
|
local data = ""
|
||||||
|
if _is_numeric_table(t) then
|
||||||
|
for i, v in ipairs(t) do
|
||||||
|
v = serialize_data(v)
|
||||||
|
data = data .. ( #data > 0 and ", " or "" ) .. v
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
k = serialize_data(k)
|
||||||
|
v = serialize_data(v)
|
||||||
|
data = data .. ( #data > 0 and "; " or "" ) ..
|
||||||
|
'[' .. k .. '] = ' .. v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return data
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Recursively serialize given data to lua code, suitable for restoring
|
||||||
|
-- with loadstring().
|
||||||
|
-- @param val Value containing the data to serialize
|
||||||
|
-- @return String value containing the serialized code
|
||||||
|
-- @see restore_data
|
||||||
|
-- @see get_bytecode
|
||||||
|
function serialize_data(val)
|
||||||
|
if val == nil then
|
||||||
|
return "nil"
|
||||||
|
elseif type(val) == "number" then
|
||||||
|
return tostring(val)
|
||||||
|
elseif type(val) == "string" then
|
||||||
|
val = val:gsub("\\", "\\\\")
|
||||||
|
:gsub("\r", "\\r")
|
||||||
|
:gsub("\n", "\\n")
|
||||||
|
:gsub('"','\\"')
|
||||||
|
return '"' .. val .. '"'
|
||||||
|
elseif type(val) == "table" then
|
||||||
|
return "{ " .. _serialize_table(val) .. " }"
|
||||||
|
else
|
||||||
|
return '"[unhandled data type:' .. type(val) .. ']"'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Restore data previously serialized with serialize_data().
|
||||||
|
-- @param str String containing the data to restore
|
||||||
|
-- @return Value containing the restored data structure
|
||||||
|
-- @see serialize_data
|
||||||
|
-- @see get_bytecode
|
||||||
|
function restore_data(str)
|
||||||
|
return loadstring("return " .. str)()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Byte code manipulation routines
|
-- Byte code manipulation routines
|
||||||
--
|
--
|
||||||
|
|
||||||
--- Return the current runtime bytecode of the given function. The byte code
|
--- Return the current runtime bytecode of the given data. The byte code
|
||||||
-- will be stripped before it is returned.
|
-- will be stripped before it is returned if the given value is a function.
|
||||||
-- @param f Function value to return as bytecode
|
-- @param val Function value to return as bytecode
|
||||||
-- @return String value containing the bytecode of the given function
|
-- @return String value containing the bytecode of the given function
|
||||||
function get_bytecode(f)
|
function get_bytecode(val)
|
||||||
local d = string.dump(f)
|
if type(val) == "function" then
|
||||||
return d and strip_bytecode(d)
|
local code = string.dump(val)
|
||||||
|
return code and strip_bytecode(code)
|
||||||
|
else
|
||||||
|
return string.dump( loadstring( "return " .. serialize_data(val) ) )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Strips unnescessary lua bytecode from given string. Information like line
|
--- Strips unnescessary lua bytecode from given string. Information like line
|
||||||
|
|
Loading…
Reference in a new issue