forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.lua
More file actions
158 lines (148 loc) · 5.04 KB
/
Copy pathdebug.lua
File metadata and controls
158 lines (148 loc) · 5.04 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
local indent_string = " "
local table = require "table"
local dt = require "darktable"
local debug = require "debug"
local introspect_internal
local introspect_body
local M = {debug = false, max_depth = 10, known = {[_G] = "_G", [_ENV] = "_ENV" }}
local depth = 0;
local function get_userdata_type(object)
if type(object) ~= "userdata" then return end
local metatable = getmetatable(object);
if not metatable then return end
return metatable.__luaA_TypeName
end
local function header(object,indent,name,obj_expand_mode)
if name == nil then name = "" end
local obj_type = type(object)
local complement =""
local user_type = get_userdata_type(object);
if user_type then
complement = complement..","..user_type
end
if obj_type ~= "number" then
name = tostring(name)
end
return indent..name.." ("..obj_type..complement..")"
end
local function key(object)
local user_type = get_userdata_type(object);
if not user_type then
return object
end
if user_type == "dt_lua_image_t" then
return "dt_lua_image_t"..object.id
elseif user_type == "dt_lua_film_t" then
return "dt_lua_film_t"..object.id
end
return object
end
local function introspect_metatable(object,indent,name,known,ancestors)
if not M.debug then return "" end
if name == nil then name = "(unknown)" end
local metatable = getmetatable(object);
if metatable == nil then return indent.."(no metatable)\n" end
local result = indent..name..".metatable"
table.insert(ancestors,name)
result = result..introspect_body(metatable,indent..indent_string,"metatable",known,ancestors)
table.remove(ancestors);
return result
end
local function introspect_uservalue(object,indent,name,known,ancestors)
if not M.debug then return "" end
if name == nil then name = "(unknown)" end
local uservalue = debug.getuservalue(object);
if uservalue == nil then return indent.."(no uservalue)\n" end
local result = indent..name..".uservalue"
table.insert(ancestors,name)
result = result..introspect_body(uservalue,indent..indent_string,"uservalue",known,ancestors)
table.remove(ancestors);
return result
end
introspect_body = function (object,indent,name,known,ancestors)
if name == nil then name = "(unknown)" end
local result = ""
if #indent > M.max_depth*#indent_string then
return "max depth\n"
end
local obj_expand_mode = type(object)
-- simple types
if obj_expand_mode == "nil" then
return "\n"
elseif obj_expand_mode == "number" then
return " : "..object.."\n"
elseif obj_expand_mode == "string" then
return " : \""..object.."\"\n"
elseif obj_expand_mode == "boolean" then
return " : "..tostring(object).."\n"
end
-- complex types
if known[key(object)] then
return ": "..known[key(object)].."\n";
end
known[key(object)]= table.concat(ancestors,".").."."..name;
if obj_expand_mode == "table" then
if next(object) == nil then return " : empty\n" end
result = " :\n"
for k,v in pairs(object) do
table.insert(ancestors,name)
result = result..introspect_internal(v,indent..indent_string,k,known,ancestors)
table.remove(ancestors);
end
result = result..introspect_metatable(object,indent..indent_string,name,known,ancestors)
result = result..introspect_uservalue(object,indent..indent_string,name,known,ancestors)
return result;
elseif obj_expand_mode == "function" then
return "\n"
elseif obj_expand_mode == "thread" then
return "\n"
elseif obj_expand_mode == "userdata" then
local metatable = getmetatable(object);
if metatable and metatable.__tostring then
result = result.. " : "..tostring(object).."\n"
else
result = result.. " :\n"
end
if metatable and metatable.__pairs then
for k,v in pairs(object) do
table.insert(ancestors,name)
result = result..introspect_internal(v,indent..indent_string,k,known,ancestors)
table.remove(ancestors);
end
end
result = result..introspect_metatable(object,indent..indent_string,name,known,ancestors)
result = result..introspect_uservalue(object,indent..indent_string,name,known,ancestors)
return result
end
error("unknown type of object")
end
introspect_internal = function(object,indent,name,known,ancestors)
local result = header(object,indent,name)
return result..introspect_body(object,indent,name,known,ancestors)
end
function M.dump(object,name,orig_known)
if name == nil or name == "" then
name = "toplevel"
end
if orig_known == nil then
orig_known = M.known
end
local known = {}
for k,v in pairs(orig_known) do
known[key(k)] = v
end
if(key(object)) then
known[key(object)] = nil -- always document the object that was actually asked
end
return introspect_internal(object,"",name,known,{})
end
function M.type(object)
if type(object) ~= "userdata" then return type(object) end
local metatable = getmetatable(object);
if not metatable or not metatable.__luaA_TypeName then return "userdata" end
return metatable.__luaA_TypeName
end
dt.debug = M
return M
--
-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua