我想使用一些调试器函数显示Lua代码的所有变量,但是我无法获得表的exacte大小,而且我也无法检索某些表的元素(值,名称等)。
以下代码说明了我的所作所为:
local name, value;
local NIL = {} -- to represent nil variables
local locals = {}
local a = 9
local i = 1
local tab ={1,2,3,4,4,4}
local tab2 = {22,22,22,22}
local tab4 = {'X', 'Y','Z'}
while( true ) do
name, value = debug.getlocal( 1, i )
if ( name == nil ) then break end
locals[ name ] = value == nil and NIL or value
i = i + 1
end
for k, v in pairs( locals ) do
if(type(v) ~= "table") then -- if it's a simple variable
print( k, v )
else
print("tabel : "..k) -- if it's a table, display it's element
print("size : "..#k)
-- I want to diplay all the elements of the table !!
for x, y in pairs(v) do
print(y)
end
end
end