-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.lua
245 lines (227 loc) · 6.63 KB
/
serialize.lua
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
-- serialize.lua -- Serialization system for Lua
-- Copyright (C) 2015 Patrick J. Donnelly ([email protected])
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
local _EMPTY = {}
local assert = assert;
local error = error;
local getfenv = getfenv or function() return _EMPTY end;
local getmetatable = getmetatable;
local ipairs = ipairs;
local pairs = pairs;
local pcall = pcall;
local rawset = rawset;
local setmetatable = setmetatable;
local tostring = tostring;
local type = type;
local strdump = string.dump;
local huge = math.huge;
local debug = select(2, pcall(require, "debug")) or {}
local getupvalue = debug.getupvalue or function() return nil end;
local upvalueid = debug.upvalueid or function() return {} end;
local table = require "table"
local concat = table.concat;
module("serialize");
local core_objects;
local REVERSE_T = {};
local MAX_SIZE = {};
local SIZE = {};
local UPVALUES = {};
local function serialize_value(values, v, l)
local tv = type(v);
local serialized;
if tv == "string" then
serialized = ("%q"):format(v);
elseif tv == "number" or tv == "boolean" then
serialized = tostring(v);
elseif tv == "table" then
serialized = "\n"..l.." = {}";
local s = {};
local prefix = "\n"..l.."[";
for vkey, vval in pairs(v) do
s[#s+1] = prefix;
s[#s+1] = values[vkey];
s[#s+1] = "] = ";
s[#s+1] = values[vval];
end
local mt = getmetatable(v);
if mt then
s[#s+1] = "\nsetmetatable(";
s[#s+1] = l;
s[#s+1] = ", ";
s[#s+1] = values[mt];
s[#s+1] = ")";
end
return serialized, concat(s);
elseif core_objects[v] then
serialized = core_objects[v];
elseif tv == "function" then
local b, dumped = pcall(strdump, v);
if not b then -- unknown C function
serialized = ("%q"):format(tostring(v));
else
serialized = ("\n"..l.." = loadstring(%q)"):format(dumped);
if getupvalue(v, 1) ~= nil then
local c = {"\n__close(", l};
local s;
for i = 1, huge do
local name, val = getupvalue(v, i);
if name == nil then
break;
end
local vupv, upv;
c[#c+1] = ", ";
if upvalue then -- Is debug.upvalue available?
upv = upvalueid(v, i);
vupv = values[UPVALUES][upv];
end
if vupv then
s = s or {};
s[#s+1] = "\nupvaluejoin(";
s[#s+1] = l;
s[#s+1] = ", ";
s[#s+1] = i;
s[#s+1] = ", ";
s[#s+1] = vupv[1];
s[#s+1] = ", ";
s[#s+1] = vupv[2];
s[#s+1] = ")";
c[#c+1] = "nil";
else
values[UPVALUES][upv] = {l, i};
c[#c+1] = val == nil and "nil" or values[val];
end
end
c[#c+1] = ")";
if s then
for _,v in ipairs(s) do
c[#c+1] = v;
end
end
local env = getfenv(v);
if env then
c[#c+1] = "\nsetfenv(";
c[#c+1] = l;
c[#c+1] = ", ";
c[#c+1] = values[env];
c[#c+1] = ")";
end
return serialized, concat(c);
end
return serialized;
end
else -- thread/unknown C function or userdata
serialized = ("%q"):format(tostring(v));
end
return "\n"..l.." = "..serialized;
end
local values_m = {
__index = function(t, k)
if type(k) == "number" then
if k ~= k then -- nan
return "0/0";
else
t[k] = k;
end
else
local r = t[REVERSE_T];
local l = #r+1;
r[l] = ""; -- placeholder
t[k] = "t["..l.."]";
local before, after = serialize_value(t, k, t[k]);
r[l] = before;
r[#r+1] = after;
end
return t[k];
end,
__newindex = function(t, k, v)
t[SIZE] = t[SIZE] + 1;
if t[SIZE] >= t[MAX_SIZE] then
error("table has too many entries");
else
rawset(t, k, v);
end
end,
__call = function(t, value)
local v = t[value];
local r = t[REVERSE_T];
r[#r+1] = "\nreturn "..v.." end)()\n"
return concat(r);
end,
};
--
-- Call this when all modified _G functions are ready
--
local function get_library (lib, prefix, seen)
seen[lib] = true;
for k, v in pairs(lib) do
if type(k) == "string" then
local t = type(v);
if t == "function" or t == "userdata" or t == "thread" then
core_objects[v] = prefix..k;
elseif t == "table" and not seen[v] then
get_library(v, prefix..k..".", seen);
else
end
end
end
end
function loadlibs(_G)
core_objects = {};
get_library(_G, "", {});
end
--
-- function "dump"
-- parameters:
-- t = a table to serialize
-- max = max raw values that can be serialized
--
function dump (t, max)
assert(type(t) == "table", "t must be a table");
assert(type(max) == "number" or max == nil, "max must be a number");
if core_objects == nil then
loadlibs(getfenv(0));
end
local values = {
-- Closure code and starting table t
[REVERSE_T] = {[[
local select = select;
local loadstring = loadstring;
local setfenv = setfenv or function() end;
local setmetatable = setmetatable;
local setupvalue = debug.setupvalue;
local upvaluejoin = debug.upvaluejoin or function () end;
local function __close(f, ...)
for i = 1, select("#", ...) do
setupvalue(f, i, (select(i, ...)));
end
end
return (function()
local t = {}]]},
[MAX_SIZE] = max or 1e4, -- 10 000 default
[SIZE] = 0,
[UPVALUES] = {},
[dump] = "serialize.dump", -- so it can't serialize itself
[loadlibs] = "serialize.loadlibs",
[1/0] = "1/0", -- math.huge (infinity)
};
setmetatable(values, values_m);
return values(t);
end
return _M;