This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tserialize.lua
375 lines (351 loc) · 10.5 KB
/
tserialize.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
--------------------------------------------------------------------------------
--- Serialize arbitrary Lua data to Lua code
-- @module lua-nucleo.tserialize
-- This file is a part of lua-nucleo library
-- @copyright lua-nucleo authors (see file `COPYRIGHT` for the license)
--------------------------------------------------------------------------------
-- Serializes arbitrary lua tables to lua code
-- that can be loaded back via loadstring()
-- Functions, threads, userdata are not supported
-- Metatables are ignored
-- Usage:
-- str = tserialize(explist) --> to serialize data
-- =(loadstring(str)()) --> to load it back
local tserialize
do
local pairs, type, ipairs, tostring, select
= pairs, type, ipairs, tostring, select
local table_concat, table_remove = table.concat, table.remove
local string_format, string_match = string.format, string.match
local lua51_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
}
-- convert numbers into loadable string, including inf, -inf and nan
local number_to_string
local serialize_number
do
local t =
{
[tostring(1/0)] = "1/0";
[tostring(-1/0)] = "-1/0";
[tostring(0/0)] = "0/0";
}
number_to_string = function(number)
-- no argument checking - called very often
local text = tostring(number)
return t[text] or text
end
serialize_number = function(number)
-- no argument checking - called very often
local text = ("%.17g"):format(number)
-- on the same platform tostring() and string.format()
-- return the same results for 1/0, -1/0, 0/0
-- so we don't need separate substitution table
return t[text] or text
end
end
local cur_buf
local cat = function(v)
cur_buf[#cur_buf + 1] = v
end
local function explode_rec(t, add, vis, added)
local t_type = type(t)
if t_type == "table" then
if not (added[t] or vis[t]) then
vis[t] = true
for k,v in pairs(t) do
explode_rec(k, add, vis, added)
explode_rec(v, add, vis, added)
end
else
if not added[t] and vis[t] then
add[#add+1] = t
added[t] = { name = "_["..#add.."]", num = #add}
end
end
end
end
local parse_rec
do
local started
local function impl(t, added, rec_info)
local t_type = type(t)
local rec = false
if t_type == "table" then
if not added[t]or not started then
started = true
for k, v in pairs(t) do
if impl(k, added, rec_info) or impl(v, added, rec_info) then
rec = true
if type(k) == "table" then
rec_info[k] = true
end
if type(v) == "table" then
rec_info[v] = true
end
end
end
else
return true
end
end
return rec
end
parse_rec = function(t, added, rec_info)
started = false
rec_info[t] = true
impl(t, added, rec_info)
end
end
local function recursive_proceed(t, added, rec_info, after, started)
local t_type = type(t)
if t_type == "table" then
if not started or not added[t] then
local started = true
cat("{")
-- Serialize numeric indices
local next_i = 0
for i, v in ipairs(t) do
next_i = i
if not (rec_info[i] or rec_info[v]) then
if i ~= 1 then cat(",") end
recursive_proceed(v, added, rec_info, after, started)
else
next_i = i - 1
break
end
end
next_i = next_i + 1
-- Serialize hash part
-- Skipping comma only at first element if there is no numeric part.
local comma = (next_i > 1) and "," or ""
for k, v in pairs(t) do
local k_type = type(k)
if not (rec_info[k] or rec_info[v]) then
--that means, if the value does not contain a recursive link
-- to the table itself
--and the index does not contain a recursive link...
if k_type == "string" then
cat(comma)
comma = ","
--check if we can use the short notation
-- eg {a=3,b=5} istead of {["a"]=3,["b"]=5}
if
not lua51_keywords[k] and string_match(k, "^[%a_][%a%d_]*$")
then
cat(k); cat("=")
else
cat(string_format("[%q]=", k))
end
recursive_proceed(v, added, rec_info, after, started)
elseif
k_type ~= "number" or -- non-string non-number
k >= next_i or k < 1 or -- integer key in hash part of the table
k % 1 ~= 0 -- non-integral key.
then
cat(comma)
comma = ","
cat("[")
recursive_proceed(k, added, rec_info, after, started)
cat("]")
cat("=")
recursive_proceed(v, added, rec_info, after, started)
end
else
after[#after + 1] = {k,v}
end
end
cat("}")
else -- already visited!
cat(added[t].name)
end
elseif t_type == "string" then
cat(string_format("%q", t))
elseif t_type == "number" then
cat(serialize_number(t))
elseif t_type == "boolean" then
cat(tostring(t))
elseif t == nil then
cat("nil")
else
return nil
end
return true
end
local function recursive_proceed_simple(t, added)
local t_type = type(t)
if t_type == "table" then
if not added[t] then
cat("{")
-- Serialize numeric indices
local next_i = 0
for i, v in ipairs(t) do
next_i = i
if i ~= 1 then cat(",") end
recursive_proceed_simple(v, added)
end
next_i = next_i + 1
-- Serialize hash part
-- Skipping comma only at first element if there is no numeric part.
local comma = (next_i > 1) and "," or ""
for k, v in pairs(t) do
local k_type = type(k)
if k_type == "string" then
cat(comma)
comma = ","
--check if we can use the short notation
-- eg {a=3,b=5} istead of {["a"]=3,["b"]=5}
if
not lua51_keywords[k] and string_match(k, "^[%a_][%a%d_]*$")
then
cat(k); cat("=")
else
cat(string_format("[%q]=", k))
end
recursive_proceed_simple(v, added)
elseif
k_type ~= "number" or -- non-string non-number
k >= next_i or k < 1 or -- integer key in hash part of the table
k % 1 ~= 0 -- non-integral key.
then
cat(comma)
comma = ","
cat("[")
recursive_proceed_simple(k, added)
cat("]")
cat("=")
recursive_proceed_simple(v, added)
end
end
cat("}")
else -- already visited!
cat(added[t].name)
end
elseif t_type == "string" then
cat(string_format("%q", t))
elseif t_type == "number" then
cat(serialize_number(t))
elseif t_type == "boolean" then
cat(tostring(t))
elseif t == nil then
cat("nil")
else
return nil
end
return true
end
local afterwork = function(k, v, buf, name, added)
cur_buf = buf
cat(" ")
cat(name)
cat("[")
local after = buf.afterwork
if not recursive_proceed_simple(k, added) then
return false
end
cat("]=")
if not recursive_proceed_simple(v, added) then
return false
end
cat(" ")
return true
end
tserialize = function (...)
--===================================--
--===========THE MAIN PART===========--
--===================================--
--PREPARATORY WORK: LOCATE THE RECURSIVE AND SHARED PARTS--
local narg = select("#", ...)
local visited = {}
-- table, containing recursive parts of our variables
local additional_vars = { }
local added = {}
for i = 1, narg do
local v = select(i, ...)
explode_rec(v, additional_vars, visited, added) -- discover recursive subtables
end
visited = nil -- no more needed
local nadd = #additional_vars
--SERIALIZE ADDITIONAL FIRST--
local rec_info = {}
for i = 1, nadd do
local v = additional_vars[i]
parse_rec(v, added, rec_info)
end
local buf = {}
for i = 1, nadd do
local v = additional_vars[i]
buf[i] = {afterwork = {}}
local after = buf[i].afterwork
cur_buf = buf[i]
if not recursive_proceed(v, added, rec_info, after) then
return nil, "Unserializable data in parameter #" .. i
end
end
rec_info = {}
for i = 1, nadd do
local v = additional_vars[i]
buf[i].afterstart = #buf[i]
for j = 1, #(buf[i].afterwork) do
if not afterwork(
buf[i].afterwork[j][1],
buf[i].afterwork[j][2],
buf[i],
added[v].name,
added
)
then
return nil, "Unserializable data in parameter #" .. i
end
end
end
--SERIALIZE GIVEN VARS--
for i = 1, narg do
local v = select(i, ...)
buf[i + nadd] = {}
cur_buf = buf[i + nadd]
if not recursive_proceed_simple(v, added) then
return nil, "Unserializable data in parameter #" .. i
end
end
--DECLARE ADDITIONAL VARS--
local prevbuf = {}
for v, inf in pairs(added) do
prevbuf[ #prevbuf + 1] =
"[" .. inf.num .. "]=" .. table_concat(buf[inf.num], "", 1, buf[inf.num].afterstart)..";"
end
--CONCAT PARTS--
for i = 1, nadd do
buf[i] = table_concat(buf[i], "", buf[i].afterstart + 1)
end
for i = nadd + 1, nadd+narg do
buf[i] = table_concat(buf[i])
end
--RETURN THE RESULT--
if nadd == 0 then
return "return " .. table_concat(buf,",")
else
local rez = {
"do local _={";
table_concat(prevbuf, " ");
'}';
table_concat(buf, " ", 1, nadd);
" return ";
table_concat(buf, ",", nadd+1);
" end";
}
return table_concat(rez)
end
end
end
return
{
tserialize = tserialize;
}