Lua - 反射 - 获取对象上的函数/字段列表?

我是 Lua 的新手,正在使用一个程序的 alpha 版本来处理 Lua 脚本。开发者没有回应,我需要获取一些可从 Lua 代码中访问的 C++ 对象提供的函数列表。

有没有什么简单的方法可以查看这些对象公开的字段和函数?

原文链接 https://stackoverflow.com/questions/2620377

点赞
stackoverflow用户32203
stackoverflow用户32203

在 Lua 中,可以使用以下方法查看对象的成员:

for key,value in pairs(o) do
    print("found member " .. key);
end

不幸的是,我不知道这是否适用于从 C++ 导入的对象。

2010-04-12 07:06:08
stackoverflow用户137317
stackoverflow用户137317

如果环境允许的话,查看导出的 C++ 对象的元表可能会有所帮助:

for key,value in pairs(getmetatable(o)) do
    print(key, value)
end
2010-04-12 22:34:05
stackoverflow用户1463446
stackoverflow用户1463446

打印所有的全局变量:

    -- globals.lua
    -- 显示所有的全局变量

    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,"")

来源: http://www.lua.org/cgi-bin/demo

输出:


    _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
2015-08-27 18:58:43