3 store
= function (f
, item
)
5 persistence
.write(f
, item
, 0);
12 load
= function (path
)
13 local f
, e
= loadfile(path
);
22 write = function (f
, item
, level
)
24 persistence
.writers
[t
](f
, item
, level
);
27 writeIndent
= function (f
, level
)
34 ["nil"] = function (f
, item
, level
)
37 ["number"] = function (f
, item
, level
)
38 f
:write(tostring(item
));
40 ["string"] = function (f
, item
, level
)
41 f
:write(string.format("%q", item
));
43 ["boolean"] = function (f
, item
, level
)
50 ["table"] = function (f
, item
, level
)
53 -- First we test to see if it's a trivial flat layout
57 for k
, v
in ipairs(item
) do
58 if k
~= idx
then isflat
= false break end
64 for k
, v
in pairs(item
) do
65 if type(k
) ~= "number" or k
< 1 or k
> #item
then isflat
= false end
66 table.insert(order
, k
)
69 if #order
~= #item
then isflat
= false end
72 -- We're flat! Special case.
73 --f:write("--[[isflat]]")
74 persistence
.writeIndent(f
, level
+1);
76 for k
, v
in ipairs(item
) do
81 persistence
.write(f
, v
, level
+1)
84 --f:write(string.format("--[[notflat %d/%d]]", #order, #item))
85 table.sort(order
, function (a
, b
)
86 if type(a
) == type(b
) then return a
< b
end
87 return type(a
) < type(b
)
91 for _
, v
in pairs(order
) do
92 if not first
then f
:write(",\n") end
94 persistence
.writeIndent(f
, level
+1);
96 if type(v
) == "string" and v
:match("[a-z]+") then
100 persistence
.write(f
, v
, level
+1);
105 persistence
.write(f
, item
[v
], level
+1);
108 persistence
.writeIndent(f
, level
);
112 ["function"] = function (f
, item
, level
)
113 -- Does only work for "normal" functions, not those
114 -- with upvalues or c functions
115 local dInfo
= debug
.getinfo(item
, "uS");
116 if dInfo
.nups
> 0 then
117 f
:write("nil -- functions with upvalue not supported\n");
118 elseif dInfo
.what
~= "Lua" then
119 f
:write("nil -- function is not a lua function\n");
121 local r
, s
= pcall(string.dump
,item
);
123 f
:write(string.format("loadstring(%q)", s
));
125 f
:write("nil -- function could not be dumped\n");
129 ["thread"] = function (f
, item
, level
)
130 f
:write("nil --thread\n");
132 ["userdata"] = function (f
, item
, level
)
133 f
:write("nil --userdata\n");