Created
January 28, 2019 21:51
-
-
Save edubart/45baaeca03e9415f7908698512ab610f to your computer and use it in GitHub Desktop.
Generate lua functions from lua environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function get_args(fun) | |
local args = {} | |
local hook = debug.gethook() | |
local argHook = function( ... ) | |
local info = debug.getinfo(3) | |
if 'pcall' ~= info.name then return end | |
for i=1,100 do | |
local name, value = debug.getlocal(2, i) | |
if name == nil or name:match('%(') then | |
break | |
end | |
table.insert(args,name) | |
end | |
debug.sethook(hook) | |
error('') | |
end | |
debug.sethook(argHook, "c") | |
pcall(fun) | |
return args | |
end | |
function generate() | |
local functions = {} | |
local parsed = {} | |
local function trace(t, prefix) | |
if parsed[t] then return end | |
parsed[t] = true | |
for name,v in pairs(t) do | |
if type(name) == 'string' then | |
if type(v) == 'function' then | |
local info = debug.getinfo(v) | |
local args = get_args(v) | |
local fname = string.format('%s%s(%s)', prefix, name, table.concat(args, ',')) | |
table.insert(functions, fname) | |
elseif type(v) == 'table' then | |
trace(v, prefix .. name .. '.') | |
end | |
end | |
end | |
end | |
trace(_G, '') | |
table.sort(functions) | |
return functions | |
end | |
local functions = generate() | |
for i,name in ipairs(functions) do | |
print(name) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment