lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Thu, Feb 6, 2014 at 2:45 AM, Rena <hyperhacker@gmail.com> wrote:
Unfortunately it doesn't provide the function's address anymore (maybe some code used that to uniquely identify functions or perform black magic) since there's no "rawtostring" function.

You can get function's address by temporarily disabling __tostring option in functions' metatable ;-)

local meta
local doc = {}

local function get_func_info(func)
    meta.__tostring = nil
    local addr = tostring(func):match'%X(%x+)%X*$'
    meta.__tostring = get_func_info
    local info, params = debug.getinfo(func, 'u'), {}
    for i = 1, info.nparams do
        params[i] = debug.getlocal(func, i)
    end
    if info.isvararg then params[#params+1] = '...' end
    return 'function@'..addr..'('..table.concat(params, ', ')..')'..(doc[func] or '')
end

meta = debug.getmetatable(get_func_info) or {}
meta.__tostring = debug.setmetatable(get_func_info, meta)

local x = 1
local function f(a, b, c, ...) return x end
doc[f] = " -> number"

print(f)
print(print)

-- output with vanilla Lua
function@004132C0(a, b, c, ...) -> number
function@10004570(...)

-- output with LuaJIT
function@00999218(a, b, c, ...) -> number
function@30(...)