Lua - Reflection - Get list of functions/fields on an object?

前端 未结 4 1206
北恋
北恋 2020-12-23 16:57

I\'m new to Lua and dealing with Lua as a scripting language in an alpha release of a program. The developer is unresponsive and I need to get a list of functions provided b

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 17:47

    Print all the globals:

        -- globals.lua
        -- show all global variables
    
        local seen={}
    
        function dump(t,i)
            seen[t]=true
            local s={}
            local n=0
            for k in pairs(t) do
                n=n+1 s[n]=k
            end
            table.sort(s)
            for k,v in ipairs(s) do
                print(i,v)
                v=t[v]
                if type(v)=="table" and not seen[v] then
                    dump(v,i.."\t")
                end
            end
        end
    
        dump(_G,"")
    

    source: http://www.lua.org/cgi-bin/demo

    Output:

    
        _G
        _VERSION
        assert
        bit32
            arshift
            band
            bnot
            bor
            btest
            bxor
            extract
            lrotate
            lshift
            replace
            rrotate
            rshift
        collectgarbage
        coroutine
            create
            isyieldable
            resume
            running
            status
            wrap
            yield
        debug
            gethook
            getinfo
            getlocal
            getmetatable
            getupvalue
            getuservalue
            sethook
            setlocal
            setmetatable
            setupvalue
            setuservalue
            traceback
            upvalueid
            upvaluejoin
        dump
        error
        getmetatable
        io
            write
        ipairs
        load
        math
            abs
            acos
            asin
            atan
            atan2
            ceil
            cos
            cosh
            deg
            exp
            floor
            fmod
            frexp
            huge
            ldexp
            log
            log10
            max
            maxinteger
            min
            mininteger
            modf
            pi
            pow
            rad
            random
            randomseed
            sin
            sinh
            sqrt
            tan
            tanh
            tointeger
            type
            ult
        next
        os
            clock
            date
            difftime
            exit
            setlocale
            time
        pairs
        pcall
        print
        rawequal
        rawget
        rawlen
        rawset
        select
        setmetatable
        string
            byte
            char
            dump
            find
            format
            gmatch
            gsub
            len
            lower
            match
            pack
            packsize
            rep
            reverse
            sub
            unpack
            upper
        table
            concat
            insert
            move
            pack
            remove
            sort
            unpack
        tonumber
        tostring
        type
        utf8
            char
            charpattern
            codepoint
            codes
            len
            offset
        xpcall
    
    

提交回复
热议问题