在Lua脚本中获取所有功能 [英] Getting all functions in a Lua script

查看:70
本文介绍了在Lua脚本中获取所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种在Lua脚本中获取所有功能的方法.该脚本已通过loadfile编译成函数.例如,我想获取下面脚本中定义的每个函数.

I'm trying to figure out a way to get all functions in a Lua script. This script has been compiled into a function through loadfile. For example, I'd want to get every function defined in the script below.

function example1()

end

local function example2()

end

local library = {}

function library:example3()

end

(function()
    -- Functions like this too.
end)

名称并不重要,我只是在寻找一种获取实际功能的方法,因此我可以在debug.getinfo中使用它们并获取信息(如它们在其中定义的行).我有LuaJIT,这更容易.这样的事情有可能吗?预先感谢.

The names aren't important, I'm just looking for a way to get the actual functions so I can use them in debug.getinfo and get information like the lines they were defined in. I have LuaJIT, if that makes this any easier. Is something like this even possible? Thanks in advance.

推荐答案

如果没有语法或字节码分析,这是不可能的,因为每个函数定义都是一个赋值(示例中的形式不同).请参见字节码检查器和相关的讨论此处.对于语法分析,您可以使用 metalua 或类似

This is not likely to be possible without syntax or bytecode analysis as each function definition is an assignment (just has different forms in your examples). See the bytecode inspector and a related discussion here. For the syntax analysis you can use metalua or something like lua-loose-parser. Keep in mind that even those tools won't give you the entire list of functions as some functions may be defined dynamically using loadstring (or similar methods).

如果您只能访问 loadfile 的结果,那么最好的选择是使用字节码分析器.

If you only have access to the result of loadfile, then you best bet is to use the bytecode analyzer.

这篇关于在Lua脚本中获取所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆