/usr/share/lua/5.1/luxio/serialise.lua is in lua-luxio 12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | -- Light Unix I/O for Lua
-- Copyright 2012 Rob Kendrick <rjek+luxio@rjek.com>
--
-- Distributed under the same terms as Lua itself (MIT).
--
-- Simple serialiser that tries to make things small, for use for IPC.
local type = type
local format = string.format
local tostring = tostring
local error = error
local concat = table.concat
local dump = string.dump
local function a(t, x)
t[#t+1] = x
end
local keywords = {
["and"] = true,
["break"] = true,
["do"] = true,
["else"] = true,
["elseif"] = true,
["end"] = true,
["false"] = true,
["for"] = true,
["function"] = true,
["if"] = true,
["in"] = true,
["local"] = true,
["nil"] = true,
["not"] = true,
["or"] = true,
["repeat"] = true,
["return"] = true,
["then"] = true,
["true"] = true,
["until"] = true,
["while"] = true
}
local function serialise(x)
local t = type(x)
if t == "string" then
return format("%q", x)
elseif t == "function" then
return format("loadstring(%q)", dump(x))
elseif t == "userdata" or t == "lightuserdata" then
error "Cannot serialise userdata or lightuserdata"
elseif t ~= "table" then
return tostring(x)
end
local r = { "{" }
-- first emit any sequence
local k = 1
repeat
local v = x[k]
if v ~= nil then
a(r, serialise(v))
a(r, ",")
end
k = k + 1
until v == nil
-- emit other numerical keys
for i, j in pairs(x) do
if type(i) == "number" and i >= k then
if r[#r] ~= "," then a(r, ",") end
a(r, format("[%d]=%s", i, serialise(j)))
a(r, ",")
end
end
-- emit non-numeric keys
for i, j in pairs(x) do
local key
local ti = type(i)
if ti ~= "number" then
if ti == "string" then
key = format("%q", i)
if not keywords[i] and i == format("%q", i):sub(2, -2) then
key = i
else
key = "[" .. key .. "]"
end
elseif ti == "function" then
key = "[loadstring(" .. format("%q", dump(i)) .. ")]"
elseif ti == "table" then
key = "[" .. serialise(i) .. "]"
else
error "unhandled key type"
end
a(r, format("%s=%s", key, serialise(j)))
a(r, ",")
end
end
if r[#r] == "," then r[#r] = nil end
a(r, "}")
return concat(r)
end
return { serialise = serialise }
|