Comparison

util/debug.lua @ 6054:7a5ddbaf758d

Merge 0.9->0.10
author Matthew Wild <mwild1@gmail.com>
date Wed, 02 Apr 2014 17:41:38 +0100
parent 5787:9a22586f67eb
child 6777:5de6b93d0190
comparison
equal deleted inserted replaced
6053:2f93a04564b2 6054:7a5ddbaf758d
22 location = _("yellow"); 22 location = _("yellow");
23 }; 23 };
24 end 24 end
25 module("debugx", package.seeall); 25 module("debugx", package.seeall);
26 26
27 function get_locals_table(level) 27 function get_locals_table(thread, level)
28 level = level + 1; -- Skip this function itself
29 local locals = {}; 28 local locals = {};
30 for local_num = 1, math.huge do 29 for local_num = 1, math.huge do
31 local name, value = debug.getlocal(level, local_num); 30 local name, value;
31 if thread then
32 name, value = debug.getlocal(thread, level, local_num);
33 else
34 name, value = debug.getlocal(level+1, local_num);
35 end
32 if not name then break; end 36 if not name then break; end
33 table.insert(locals, { name = name, value = value }); 37 table.insert(locals, { name = name, value = value });
34 end 38 end
35 return locals; 39 return locals;
36 end 40 end
86 function get_traceback_table(thread, start_level) 90 function get_traceback_table(thread, start_level)
87 local levels = {}; 91 local levels = {};
88 for level = start_level, math.huge do 92 for level = start_level, math.huge do
89 local info; 93 local info;
90 if thread then 94 if thread then
91 info = debug.getinfo(thread, level+1); 95 info = debug.getinfo(thread, level);
92 else 96 else
93 info = debug.getinfo(level+1); 97 info = debug.getinfo(level+1);
94 end 98 end
95 if not info then break; end 99 if not info then break; end
96 100
97 levels[(level-start_level)+1] = { 101 levels[(level-start_level)+1] = {
98 level = level; 102 level = level;
99 info = info; 103 info = info;
100 locals = get_locals_table(level+1); 104 locals = get_locals_table(thread, level+(thread and 0 or 1));
101 upvalues = get_upvalues_table(info.func); 105 upvalues = get_upvalues_table(info.func);
102 }; 106 };
103 end 107 end
104 return levels; 108 return levels;
105 end 109 end
106 110
107 function traceback(...) 111 function traceback(...)
108 local ok, ret = pcall(_traceback, ...); 112 local ok, ret = pcall(_traceback, ...);
132 thread, message, level = coroutine.running(), thread, message; 136 thread, message, level = coroutine.running(), thread, message;
133 elseif type(thread) ~= "thread" then 137 elseif type(thread) ~= "thread" then
134 return nil; -- debug.traceback() does this 138 return nil; -- debug.traceback() does this
135 end 139 end
136 140
137 level = level or 1; 141 level = level or 0;
138 142
139 message = message and (message.."\n") or ""; 143 message = message and (message.."\n") or "";
140 144
141 -- +3 counts for this function, and the pcall() and wrapper above us 145 -- +3 counts for this function, and the pcall() and wrapper above us, the +1... I don't know.
142 local levels = get_traceback_table(thread, level+3); 146 local levels = get_traceback_table(thread, level+(thread == nil and 4 or 0));
143 147
144 local last_source_desc; 148 local last_source_desc;
145 149
146 local lines = {}; 150 local lines = {};
147 for nlevel, level in ipairs(levels) do 151 for nlevel, level in ipairs(levels) do
148 local info = level.info; 152 local info = level.info;
149 local line = "..."; 153 local line = "...";
150 local func_type = info.namewhat.." "; 154 local func_type = info.namewhat.." ";
169 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc)); 173 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
170 end 174 end
171 nlevel = nlevel-1; 175 nlevel = nlevel-1;
172 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line); 176 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
173 local npadding = (" "):rep(#tostring(nlevel)); 177 local npadding = (" "):rep(#tostring(nlevel));
174 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding); 178 if level.locals then
175 if locals_str then 179 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
176 table.insert(lines, "\t "..npadding.."Locals: "..locals_str); 180 if locals_str then
181 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
182 end
177 end 183 end
178 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding); 184 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
179 if upvalues_str then 185 if upvalues_str then
180 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str); 186 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
181 end 187 end