Skip to content

Commit a1da120

Browse files
committed
Copy mw.dumpObject() from Scribunto
very helpfully for debugging Lua modules
1 parent 3e54e6f commit a1da120

File tree

1 file changed

+84
-1
lines changed
  • src/wikitextprocessor/lua

1 file changed

+84
-1
lines changed

src/wikitextprocessor/lua/mw.lua

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,90 @@ function mw.clone(v)
8383
end
8484

8585
function mw.dumpObject(obj)
86-
print("mw.dumpObject", obj)
86+
-- https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/184216759e22635bd25a0844e6f68979ecf7bc2a/includes/Engines/LuaCommon/lualib/mw.lua#L551
87+
local doneTable = {}
88+
local doneObj = {}
89+
local ct = {}
90+
local function sorter( a, b )
91+
local ta, tb = type( a ), type( b )
92+
if ta ~= tb then
93+
return ta < tb
94+
end
95+
if ta == 'string' or ta == 'number' then
96+
return a < b
97+
end
98+
if ta == 'boolean' then
99+
return tostring( a ) < tostring( b )
100+
end
101+
return false -- Incomparable
102+
end
103+
local function _dumpObject( object, indent, expandTable )
104+
local tp = type( object )
105+
if tp == 'number' or tp == 'nil' or tp == 'boolean' then
106+
return tostring( object )
107+
elseif tp == 'string' then
108+
return string.format( "%q", object )
109+
elseif tp == 'table' then
110+
if not doneObj[object] then
111+
local s = tostring( object )
112+
if string.sub(s, 1, 6) == 'table:' then -- this line changed
113+
ct[tp] = ( ct[tp] or 0 ) + 1
114+
doneObj[object] = 'table#' .. ct[tp]
115+
else
116+
doneObj[object] = s
117+
doneTable[object] = true
118+
end
119+
end
120+
if doneTable[object] or not expandTable then
121+
return doneObj[object]
122+
end
123+
doneTable[object] = true
124+
125+
local ret = { doneObj[object], ' {\n' }
126+
local mt = getmetatable( object )
127+
local indentString = " "
128+
if mt then
129+
ret[#ret + 1] = string.rep( indentString, indent + 2 )
130+
ret[#ret + 1] = 'metatable = '
131+
ret[#ret + 1] = _dumpObject( mt, indent + 2, false )
132+
ret[#ret + 1] = "\n"
133+
end
134+
135+
local doneKeys = {}
136+
for key, value in ipairs( object ) do
137+
doneKeys[key] = true
138+
ret[#ret + 1] = string.rep( indentString, indent + 2 )
139+
ret[#ret + 1] = _dumpObject( value, indent + 2, true )
140+
ret[#ret + 1] = ',\n'
141+
end
142+
local keys = {}
143+
for key in pairs( object ) do
144+
if not doneKeys[key] then
145+
keys[#keys + 1] = key
146+
end
147+
end
148+
table.sort( keys, sorter )
149+
for i = 1, #keys do
150+
local key = keys[i]
151+
ret[#ret + 1] = string.rep( indentString, indent + 2 )
152+
ret[#ret + 1] = '['
153+
ret[#ret + 1] = _dumpObject( key, indent + 3, false )
154+
ret[#ret + 1] = '] = '
155+
ret[#ret + 1] = _dumpObject( object[key], indent + 2, true )
156+
ret[#ret + 1] = ",\n"
157+
end
158+
ret[#ret + 1] = string.rep( indentString, indent )
159+
ret[#ret + 1] = '}'
160+
return table.concat( ret )
161+
else
162+
if not doneObj[object] then
163+
ct[tp] = ( ct[tp] or 0 ) + 1
164+
doneObj[object] = tostring( object ) .. '#' .. ct[tp]
165+
end
166+
return doneObj[object]
167+
end
168+
end
169+
return _dumpObject( obj, 0, true )
87170
end
88171

89172
function mw.incrementExpensiveFunctionCount()

0 commit comments

Comments
 (0)