printf(func) || print function definition

Discussion in 'Mods & Lua Functions' started by Xeraphus, Jan 11, 2017.

  1. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,723
    made this while learning debug library for fun
    always wondered how to print function definition

    usage: printf(Game.convertIpToString)
    [​IMG]

    code:
    Code:
    function printf(func)
        local info = debug.getinfo(func)
        if (info.what == "C") then
            return print("Input may not be a C function.")
        end
        local count = 0
        local startL, endL = info.linedefined, info.lastlinedefined
        for line in io.lines(info.short_src) do
            count = count + 1
            if (count >= startL) and (count <= endL) then
                -- Fix large indentations by replacing all tab characters with 4 spaces
                local text = line:gsub('%\t', '    ')
                print(text)
               
                -- Break loop once last line is printed
                if (count == endl) then
                    break
                end
            end
        end
    end
     

Share This Page