获取函数的 AST

发布于 2024-12-05 11:41:10 字数 270 浏览 8 评论 0原文

假设我有一个函数:

function hi()
    print('hi')
end

我希望能够将此函数作为参数并对其进行分析,例如列出其中的所有函数调用,查看它引用的所有变量等。

请注意,我是不是谈论分析源代码;我没有函数的源文本,我只有函数对象,即这是在运行时完成的。

我认为你这样做的方法是获取函数的 AST。但我不知道该怎么做。那么我该如何获取函数的 AST(或者如果有更好的方法则对其进行分析)?

Say I have a function:

function hi()
    print('hi')
end

I'd like to be able to take this function as an argument and perform analysis on it, such as listing all the function calls in it, seeing all the variables it references, etc.

Note that I'm not talking about analysing source code; I don't have the source text of the function, I just have the function object, i.e. this is being done at runtime.

The way I think you'd go about doing this is to get the AST of the function. But I don't know how to do that. So how would I go about getting the AST of a function (or otherwise analysing it if there's a better way)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的韩爷 2024-12-12 11:41:10

您可能想尝试我的 lbci,一个字节码检查器图书馆。

You may want to try my lbci, a bytecode inspector library.

情话已封尘 2024-12-12 11:41:10

我必须问。怎么没有源码呢?是否以某种方式被混淆了?

不管怎样,正如前面提到的,AST——尽管 Lua 中一开始就没有 AST,但不能通过任何运行时机制获得。

尽管在许多情况下很容易获得对源代码的访问,因为它被输入到嵌入或未嵌入的解释器中(当然除非它们只提供字节码......

I have to ask. Why don't you have the source? Was is obfuscated in some way?

Anyhow as mentioned, the AST - even though there really wasn't one to begin with in Lua, is not available via any runtime mechanism.

Though it is quite easy to obtain access to the source in many cases as it is being fed into the interpreter embedded or not (unless of course they only ship bytecode...

合约呢 2024-12-12 11:41:10

抽象语法树(几乎根据定义)是一个源代码级概念。

因此,您可能在非反射编译语言中寻找的是 "decompiler"

会这个工作?

http://luadec.luaforge.net/

Abstract Syntax Trees are (pretty much by definition) a source-level concept.

So what you are perhaps looking for in a non-reflective compiled language would be a "decompiler"

Would this work?

http://luadec.luaforge.net/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文