Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 在Lua脚本中获取所有函数_Function_Lua - Fatal编程技术网

Function 在Lua脚本中获取所有函数

Function 在Lua脚本中获取所有函数,function,lua,Function,Lua,我正试图找出一种方法来获取Lua脚本中的所有函数。此脚本已通过loadfile编译成函数。例如,我想获得下面脚本中定义的每个函数 function example1() end local function example2() end local library = {} function library:example3() end (function() -- Functions like this too. end) 名称并不重要,我只是在寻找一种获取实际函数的方

我正试图找出一种方法来获取Lua脚本中的所有函数。此脚本已通过loadfile编译成函数。例如,我想获得下面脚本中定义的每个函数

function example1()

end

local function example2()

end

local library = {}

function library:example3()

end

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

名称并不重要,我只是在寻找一种获取实际函数的方法,这样我就可以在debug.getinfo中使用它们,并获取它们在中定义的行之类的信息。我有LuaJIT,如果这能让事情变得更容易的话。这样的事情可能吗?提前感谢。

我猜该文件将其函数声明为全局函数,否则将很容易跟踪返回的内容

如果是这种情况,您可以使用泛型for循环遍历所有全局项,并且只从中获取函数:

allFuncs = {}

for key, item in pairs(_G) do
    if type(item) == "function" then
        allFuncs[#allFuncs + 1] = item
    end
end
\u G
是包含所有全局变量的表)

然后您将拥有一个列表(
allFuncs
),其中包含所有声明的函数,但请注意,它还将包含默认函数,如
setmetatable
xpcall

很容易修改代码以避免发生这种情况,但仅将其用于测试/学习:

function allFuncs()
    local funcsTab = {}
    for key, item in pairs(_G) do
        if type(item) == "function" then
            funcsTab[#funcsTab + 1] = item
        end
    end
    return funcsTab
end

defaultFuncs = allFuncs()

--then you load your file: other functions get declared
--we create another table containg the default + the new functions

myFuncs = allFuncs()

--then you subtract the first table from the second

for i = 1, #myFuncs do
    for o = 1, #defaultFuncs do
        if myFuncs[i] == defaultFuncs[o] then
            table.remove(myFuncs, i)
        end
    end
end
如果您的文件不返回任何内容并将其函数声明为全局函数,则会出现这种情况


如果文件将它们声明为本地,然后返回一个包含它们的表,只需使用第一段代码替换返回的表。

我猜文件将其函数声明为全局函数,或者很容易跟踪返回的内容

如果是这种情况,您可以使用泛型for循环遍历所有全局项,并且只从中获取函数:

allFuncs = {}

for key, item in pairs(_G) do
    if type(item) == "function" then
        allFuncs[#allFuncs + 1] = item
    end
end
\u G
是包含所有全局变量的表)

然后您将拥有一个列表(
allFuncs
),其中包含所有声明的函数,但请注意,它还将包含默认函数,如
setmetatable
xpcall

很容易修改代码以避免发生这种情况,但仅将其用于测试/学习:

function allFuncs()
    local funcsTab = {}
    for key, item in pairs(_G) do
        if type(item) == "function" then
            funcsTab[#funcsTab + 1] = item
        end
    end
    return funcsTab
end

defaultFuncs = allFuncs()

--then you load your file: other functions get declared
--we create another table containg the default + the new functions

myFuncs = allFuncs()

--then you subtract the first table from the second

for i = 1, #myFuncs do
    for o = 1, #defaultFuncs do
        if myFuncs[i] == defaultFuncs[o] then
            table.remove(myFuncs, i)
        end
    end
end
如果您的文件不返回任何内容并将其函数声明为全局函数,则会出现这种情况


如果文件将它们声明为本地,然后返回一个包含它们的表,只需使用第一段代码替换返回的表。

如果没有语法或字节码分析,这是不可能的,因为每个函数定义都是赋值(在示例中只是有不同的形式)。请参阅和相关的讨论。对于语法分析,您可以使用或类似的方法。请记住,即使这些工具也不会提供完整的函数列表,因为有些函数可能会使用
loadstring
(或类似方法)动态定义


如果您只能访问加载文件的结果,那么最好使用字节码分析器。

如果没有语法或字节码分析,这是不可能的,因为每个函数定义都是一个赋值(只是在示例中有不同的形式)。请参阅和相关的讨论。对于语法分析,您可以使用或类似的方法。请记住,即使这些工具也不会提供完整的函数列表,因为有些函数可能会使用
loadstring
(或类似方法)动态定义


如果您只能访问加载文件的结果,那么最好使用字节码分析器。

这可以使用LuaJIT中的
jit.attach

您可以通过以下方式将回调附加到许多编译器事件:
jit.attach
。可以调用回调函数:

  • 当函数已编译为字节码(“bc”)
  • 跟踪记录开始或停止时(“跟踪”)
  • 记录跟踪时(“记录”)
  • 或者当跟踪通过侧面出口(“texit”)退出时


这可以使用LuaJIT中的
jit.attach

您可以通过以下方式将回调附加到许多编译器事件:
jit.attach
。可以调用回调函数:

  • 当函数已编译为字节码(“bc”)
  • 跟踪记录开始或停止时(“跟踪”)
  • 记录跟踪时(“记录”)
  • 或者当跟踪通过侧面出口(“texit”)退出时


脚本将是动态的(它来自其他地方,由用户创建),我需要在脚本中定义每个函数,而不是每个全局函数。不过,谢谢你!好的,但是脚本如何声明函数呢?它是否将它们声明为
local
,然后在表中返回它们?或者它只是将它们声明为全局函数,而不返回任何内容?如果它确实返回一个表,您可以使用上面的代码用返回的表替换
\G
。脚本将是动态的(它来自其他地方,由用户创建),我需要获取该脚本中定义的每个函数,而不是每个全局函数。不过,谢谢你!好的,但是脚本如何声明函数呢?它是否将它们声明为
local
,然后在表中返回它们?或者它只是将它们声明为全局函数,而不返回任何内容?如果它确实返回了一个表,您可以使用上面的代码将
\G
替换为返回的表。