-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_state.nut
245 lines (211 loc) · 4.15 KB
/
serialize_state.nut
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
try {
local objs_reg={maxid=0,refs={}}
complex_types <- {
["table"] = null,
["array"] = null,
["class"] = null,
["instance"] = null,
["weakref"] = null,
}
function build_refs(t)
{
if(t == ::getroottable())
return;
local otype = ::type(t);
if(otype in complex_types)
{
if(!(t in objs_reg.refs)) {
objs_reg.refs[t] <- objs_reg.maxid++;
iterateobject(t,function(o,i,val)
{
build_refs(val);
build_refs(i);
})
}
}
}
function getvalue(v)
{
switch(::type(v))
{
case "table":
case "array":
case "class":
case "instance":
return objs_reg.refs[v].tostring();
case "integer":
case "float":
return v;
case "bool":
return v.tostring();
case "string":
return v;
case "null":
return "null";
default:
return pack_type(::type(v));
}
}
local packed_types={
["null"]="n",
["string"]="s",
["integer"]="i",
["float"]="f",
["userdata"]="u",
["function"]="fn",
["table"]="t",
["array"]="a",
["generator"]="g",
["thread"]="h",
["instance"]="x",
["class"]="y",
["bool"]="b",
["weakref"]="w"
}
function pack_type(type)
{
if(type in packed_types)return packed_types[type]
return type
}
function iterateobject(obj,func)
{
local ty = ::type(obj);
if(ty == "instance") {
try { //TRY TO USE _nexti
foreach(idx,val in obj)
{
func(obj,idx,val);
}
}
catch(e) {
foreach(idx,val in obj.getclass())
{
func(obj,idx,obj[idx]);
}
}
}
else if(ty == "weakref") {
func(obj,"@ref",obj.ref());
}
else {
foreach(idx,val in obj)
{
func(obj,idx,val);
}
}
}
function build_tree()
{
foreach(i,o in objs_reg.refs)
{
beginelement("o");
attribute("type",(i==::getroottable()?"r":pack_type(::type(i))));
local _typeof = typeof i;
if(_typeof != ::type(i)) {
attribute("typeof",_typeof);
}
attribute("ref",o.tostring());
if(i != ::getroottable()){
iterateobject(i,function (obj,idx,val) {
if(::type(val) == "function")
return;
beginelement("e");
emitvalue("kt","kv",idx);
emitvalue("vt","v",obj[idx]);
endelement("e");
})
}
endelement("o");
}
}
function evaluate_watch(locals,id,expression)
{
local func_src="return function ("
local params=[];
params.append(locals["this"])
local first=1;
foreach(i,v in locals) {
if(i!="this" && i[0] != '@'){ //foreach iterators start with @
if(!first){
func_src=func_src+","
}
first=null
params.append(v)
func_src=func_src+i
}
}
func_src=func_src+"){\n"
func_src=func_src+"return ("+expression+")\n}"
try {
local func=::compilestring(func_src);
return {status="ok" , val=func().acall(params)};
}
catch(e)
{
return {status="error"}
}
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
function emitvalue(type_attrib,value_attrib,val)
{
attribute(type_attrib,pack_type(::type(val)));
attribute(value_attrib,getvalue(val).tostring());
}
local stack=[]
local level=3;
local si;
//try {
//ENUMERATE THE STACK WATCHES
while(si=::getstackinfos(level))
{
stack.append(si);
level++;
}
//EVALUATE ALL WATCHES
objs_reg.refs[::getroottable()] <- objs_reg.maxid++;
foreach(i,val in stack)
{
foreach(i,l in val.locals)
build_refs(l);
}
beginelement("objs");
build_tree();
endelement("objs");
beginelement("calls");
foreach(i,val in stack)
{
beginelement("call");
attribute("fnc",val.func);
attribute("src",val.src);
attribute("line",val.line.tostring());
foreach(i,v in val.locals)
{
beginelement("l");
attribute("name",getvalue(i).tostring());
emitvalue("type","val",v);
endelement("l");
}
if("watches" in val) {
foreach(i,v in val.watches)
{
beginelement("w");
attribute("id",i.tostring());
attribute("exp",v.exp);
attribute("status",v.status);
if(v.status!="error") {
emitvalue("type","val",v.val);
}
endelement("w");
}
}
endelement("call");
}
endelement("calls");
objs_reg = null;
stack = null;
if("collectgarbage" in ::getroottable()) ::collectgarbage();
}catch(e)
{
::print("ERROR " + e +"\n");
}