Software /
code /
prosody
Annotate
util/serialization.lua @ 9168:29de7ad20250
spec: Correct order of arguments to asserts in configmanager tests
assert.foo(expect, got, ...)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 11 Aug 2018 19:59:19 +0200 |
parent | 9060:69bc3144c2b7 |
child | 9341:009dad867e52 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1135
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2222
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2222
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
4 -- Copyright (C) 2018 Kim Alvefur |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5021
diff
changeset
|
5 -- |
758 | 6 -- This project is MIT/X11 licensed. Please see the |
7 -- COPYING file in the source package for more information. | |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 -- |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
10 local getmetatable = getmetatable; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
11 local next, type = next, type; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
12 local s_format = string.format; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
13 local s_gsub = string.gsub; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
14 local s_rep = string.rep; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
15 local s_char = string.char; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
16 local s_match = string.match; |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local t_concat = table.concat; |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
3736
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
19 local pcall = pcall; |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
3745
diff
changeset
|
20 local envload = require"util.envload".envload; |
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
3745
diff
changeset
|
21 |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
22 local pos_inf, neg_inf = math.huge, -math.huge; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
23 local m_log = math.log; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
24 local m_log10 = math.log10 or function (n) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
25 return m_log(n, 10); |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 end |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
27 local m_floor = math.floor; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
28 -- luacheck: ignore 143/math |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
29 local m_type = math.type or function (n) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
30 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
31 end; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
32 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
33 local char_to_hex = {}; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
34 for i = 0,255 do |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
35 char_to_hex[s_char(i)] = s_format("%02x", i); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
36 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
37 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
38 local function to_hex(s) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
39 return (s_gsub(s, ".", char_to_hex)); |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 end |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
41 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
42 local function fatal_error(obj, why) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
43 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
44 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
45 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
46 local function default_fallback(x, why) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
47 return s_format("nil --[[%s: %s]]", type(x), why or "fail"); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
48 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
49 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
50 local string_escapes = { |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
51 ['\a'] = [[\a]]; ['\b'] = [[\b]]; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
52 ['\f'] = [[\f]]; ['\n'] = [[\n]]; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
53 ['\r'] = [[\r]]; ['\t'] = [[\t]]; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
54 ['\v'] = [[\v]]; ['\\'] = [[\\]]; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
55 ['\"'] = [[\"]]; ['\''] = [[\']]; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
56 } |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
57 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
58 for i = 0, 255 do |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
59 local c = s_char(i); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
60 if not string_escapes[c] then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
61 string_escapes[c] = s_format("\\%03d", i); |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
65 local default_keywords = { |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
66 ["do"] = true; ["and"] = true; ["else"] = true; ["break"] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
67 ["if"] = true; ["end"] = true; ["goto"] = true; ["false"] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
68 ["in"] = true; ["for"] = true; ["then"] = true; ["local"] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
69 ["or"] = true; ["nil"] = true; ["true"] = true; ["until"] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
70 ["elseif"] = true; ["function"] = true; ["not"] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
71 ["repeat"] = true; ["return"] = true; ["while"] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
72 }; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
73 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
74 local function new(opt) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
75 if type(opt) ~= "table" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
76 opt = { preset = opt }; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
77 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
78 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
79 local types = { |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
80 table = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
81 string = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
82 number = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
83 boolean = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
84 ["nil"] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
85 }; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
86 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
87 -- presets |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
88 if opt.preset == "debug" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
89 opt.preset = "oneline"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
90 opt.freeze = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
91 opt.fatal = false; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
92 opt.fallback = default_fallback; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
93 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
94 if opt.preset == "oneline" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
95 opt.indentwith = opt.indentwith or ""; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
96 opt.itemstart = opt.itemstart or " "; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
97 opt.itemlast = opt.itemlast or ""; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
98 opt.tend = opt.tend or " }"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
99 elseif opt.preset == "compact" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
100 opt.indentwith = opt.indentwith or ""; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
101 opt.itemstart = opt.itemstart or ""; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
102 opt.itemlast = opt.itemlast or ""; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
103 opt.equals = opt.equals or "="; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
104 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
105 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
106 local fallback = opt.fatal and fatal_error or opt.fallback or default_fallback; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
107 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
108 local function ser(v) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
109 return (types[type(v)] or fallback)(v); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
110 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
111 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
112 local keywords = opt.keywords or default_keywords; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
113 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
114 -- indented |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
115 local indentwith = opt.indentwith or "\t"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
116 local itemstart = opt.itemstart or "\n"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
117 local itemsep = opt.itemsep or ";"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
118 local itemlast = opt.itemlast or ";\n"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
119 local tstart = opt.tstart or "{"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
120 local tend = opt.tend or "}"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
121 local kstart = opt.kstart or "["; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
122 local kend = opt.kend or "]"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
123 local equals = opt.equals or " = "; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
124 local unquoted = opt.unquoted == nil and "^[%a_][%w_]*$" or opt.unquoted; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
125 local hex = opt.hex; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
126 local freeze = opt.freeze; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
127 local precision = opt.precision or 10; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
128 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
129 -- serialize one table, recursively |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
130 -- t - table being serialized |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
131 -- o - array where tokens are added, concatenate to get final result |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
132 -- - also used to detect cycles |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
133 -- l - position in o of where to insert next token |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
134 -- d - depth, used for indentation |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
135 local function serialize_table(t, o, l, d) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
136 if o[t] or d > 127 then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
137 o[l], l = fallback(t, "recursion"), l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
138 return l; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
139 end |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
141 o[t] = true; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
142 if freeze then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
143 -- opportunity to do pre-serialization |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
144 local mt = getmetatable(t); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
145 local fr = (freeze ~= true and freeze[mt]); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
146 local mf = mt and mt.__freeze; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
147 local tag; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
148 if type(fr) == "string" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
149 tag = fr; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
150 fr = mf; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
151 elseif mt then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
152 tag = mt.__type; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
153 end |
9060
69bc3144c2b7
util.serialization: Tighten up type checks
Kim Alvefur <zash@zash.se>
parents:
9008
diff
changeset
|
154 if type(fr) == "function" then |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
155 t = fr(t); |
9060
69bc3144c2b7
util.serialization: Tighten up type checks
Kim Alvefur <zash@zash.se>
parents:
9008
diff
changeset
|
156 if type(tag) == "string" then |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
157 o[l], l = tag, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
158 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
159 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
160 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
161 o[l], l = tstart, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
162 local indent = s_rep(indentwith, d); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
163 local numkey = 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
164 local ktyp, vtyp; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
165 for k,v in next,t do |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
166 o[l], l = itemstart, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
167 o[l], l = indent, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
168 ktyp, vtyp = type(k), type(v); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
169 if k == numkey then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
170 -- next index in array part |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
171 -- assuming that these are found in order |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
172 numkey = numkey + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
173 elseif unquoted and ktyp == "string" and |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
174 not keywords[k] and s_match(k, unquoted) then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
175 -- unquoted keys |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
176 o[l], l = k, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
177 o[l], l = equals, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
178 else |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
179 -- quoted keys |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
180 o[l], l = kstart, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
181 if ktyp == "table" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
182 l = serialize_table(k, o, l, d+1); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
183 else |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
184 o[l], l = ser(k), l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
185 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
186 -- = |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
187 o[l], o[l+1], l = kend, equals, l + 2; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
188 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
189 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
190 -- the value |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
191 if vtyp == "table" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
192 l = serialize_table(v, o, l, d+1); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
193 else |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
194 o[l], l = ser(v), l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
195 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
196 -- last item? |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
197 if next(t, k) ~= nil then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
198 o[l], l = itemsep, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
199 else |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
200 o[l], l = itemlast, l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
201 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
202 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
203 if next(t) ~= nil then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
204 o[l], l = s_rep(indentwith, d-1), l + 1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
205 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
206 o[l], l = tend, l +1; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
207 return l; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
208 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
209 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
210 function types.table(t) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
211 local o = {}; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
212 serialize_table(t, o, 1, 1); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
213 return t_concat(o); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
214 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
215 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
216 local function serialize_string(s) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
217 return '"' .. s_gsub(s, "[%z\1-\31\"\'\\\127-\255]", string_escapes) .. '"'; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
218 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
219 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
220 if hex then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
221 function types.string(s) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
222 local esc = serialize_string(s); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
223 if #esc > (#s*2+2+#hex) then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
224 return hex .. '"' .. to_hex(s) .. '"'; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
225 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
226 return esc; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
227 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
228 else |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
229 types.string = serialize_string; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
230 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
231 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
232 function types.number(t) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
233 if m_type(t) == "integer" then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
234 return s_format("%d", t); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
235 elseif t == pos_inf then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
236 return "(1/0)"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
237 elseif t == neg_inf then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
238 return "(-1/0)"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
239 elseif t ~= t then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
240 return "(0/0)"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
241 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
242 local log = m_floor(m_log10(t)); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
243 if log > precision then |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
244 return s_format("%.18e", t); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
245 else |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
246 return s_format("%.18g", t); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
247 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
248 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
249 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
250 -- Are these faster than tostring? |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
251 types["nil"] = function() |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
252 return "nil"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
253 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
254 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
255 function types.boolean(t) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
256 return t and "true" or "false"; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
257 end |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
258 |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
259 return ser; |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
260 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
261 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
262 local function deserialize(str) |
3736
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
263 if type(str) ~= "string" then return nil; end |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
264 str = "return "..str; |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
3745
diff
changeset
|
265 local f, err = envload(str, "@data", {}); |
3736
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
266 if not f then return nil, err; end |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
267 local success, ret = pcall(f); |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
268 if not success then return nil, ret; end |
73399dd525e8
util.serialization: Implemented deserialize().
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
269 return ret; |
546
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
270 end |
1e65f64dfabf
Added module util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
271 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
272 return { |
9008
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
273 new = new; |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
274 serialize = function (x, opt) |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
275 return new(opt)(x); |
ae3c52419ec1
util.serialization: Rewritte for performance and flexibility
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
276 end; |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
277 deserialize = deserialize; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
6670
diff
changeset
|
278 }; |