-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathursaliblua.lua
More file actions
executable file
·164 lines (142 loc) · 3.78 KB
/
ursaliblua.lua
File metadata and controls
executable file
·164 lines (142 loc) · 3.78 KB
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
ursaliblua = {}
local keywords = {
'break', 'do', 'end', 'else',
'elseif', 'function', 'if', 'local',
'nil', 'not', 'or', 'repeat',
'return', 'then', 'until', 'while', 'in'
}
for _, v in ipairs(keywords) do
keywords[v] = true
end
local persistence
persistence =
{
dump = function(item)
local stix = {}
function stix:write(txt)
table.insert(self, txt)
for i = #self - 1, 1, -1 do
if string.len(self[i]) > string.len(self[i + 1]) then break end
self[i] = self[i] .. table.remove(self, i + 1)
end
end
persistence.write(stix, item, 0)
return table.concat(stix)
end;
save = function (filename, item)
local f = io.open(filename, "wb")
assert(f)
persistence.write(f, item, 0);
f:write("\n");
f:close()
end;
load = function (data)
local f = loadstring("return " .. data);
assert(f)
return f();
end;
write = function (f, item, level)
local t = type(item);
persistence.writers[t](f, item, level);
end;
writeIndent = function (f, level)
for i = 1, level do
f:write("\t");
end;
end;
writers = {
["nil"] = function (f, item, level)
f:write("nil");
end;
["number"] = function (f, item, level)
f:write(tostring(item));
end;
["string"] = function (f, item, level)
f:write(string.format("%q", item));
end;
["boolean"] = function (f, item, level)
if item then
f:write("true");
else
f:write("false");
end
end;
["table"] = function (f, item, level)
f:write("{\n");
local first = true
local hit = {}
persistence.writeIndent(f, level+1);
for k, v in ipairs(item) do
hit[k] = true
if not first then
f:write(", ")
end
persistence.write(f, v, level+1)
first = false
end
local order = {}
for k, v in pairs(item) do
if not hit[k] then table.insert(order, k) end
end
table.sort(order, function (a, b)
if type(a) == type(b) then
if type(a) == "table" then
return false
else
return a < b
end
end
return type(a) < type(b)
end)
for _, v in pairs(order) do
if not first then f:write(",\n") end
first = false
persistence.writeIndent(f, level+1);
if type(v) == "string" and v:match("^[a-zA-Z_][a-zA-Z0-9_]*$") and not keywords[v] then
f:write(v)
else
f:write("[");
persistence.write(f, v, level+1);
f:write("]");
end
f:write(" = ");
persistence.write(f, item[v], level+1);
end
f:write("\n")
persistence.writeIndent(f, level);
f:write("}");
end;
["function"] = function (f, item, level)
-- Does only work for "normal" functions, not those
-- with upvalues or c functions
local dInfo = debug.getinfo(item, "uS");
if dInfo.nups > 0 then
f:write("nil -- functions with upvalue not supported\n");
elseif dInfo.what ~= "Lua" then
f:write("nil -- function is not a lua function\n");
else
local r, s = pcall(string.dump,item);
if r then
f:write(string.format("loadstring(%q)", s));
else
f:write("nil -- function could not be dumped\n");
end
end
end;
["thread"] = function (f, item, level)
f:write("nil --thread\n");
end;
["userdata"] = function (f, item, level)
f:write("nil --userdata\n");
end;
}
}
ursaliblua.persistence = persistence
function ursaliblua.return_pack(...)
local rpx = {...}
rpx.n = select('#', ...)
return rpx
end
function ursaliblua.return_unpack(item)
return unpack(item, 1, item.n)
end