简体   繁体   English

如何将 function 转换为 Lua 中的字符串?

[英]how to convert a function to string in Lua?

How to output the contents of a function?怎么output的内容一个function?

function a()
print("Hello,World")
end
print(func_toString(a))

I hope the result of func_toString(a) could be我希望func_toString(a)的结果可以是

function a()
print("Hello,World")
end

or just要不就

print("Hello,World")

It is assumed that the source code is executed directly without precompiling or embedding.假设源代码直接执行,无需预编译或嵌入。 How to do this?这个怎么做?

This is not possible from within the running Lua program.这在正在运行的 Lua 程序中是不可能的。

Lua provides the debug library to inspect functions and variable. Lua 提供debug库来检查函数和变量。 This allows you to obtain the source (the path or string where the function is defined) and the line range on which the function is defined.这允许您获取源(定义 function 的路径或字符串)和定义 function 的行范围。 In the vast majority of cases this might actually be enough: Just find the first occurrence of function(...) on the first line and the first occurrence of end on the last line, then string.sub the relevant portion.在绝大多数情况下,这实际上可能就足够了:只需在第一行找到第一次出现的function(...)并在最后一行找到第一次出现的end ,然后string.sub相关部分。 This is however rather error prone;然而,这很容易出错; consider eg考虑例如

function a() function b() end end
a()
func_toString(a)

since both functions are on the same line, you can't distinguish them - the debug library only provides you line info, nothing more.因为这两个函数都在同一行,所以你无法区分它们——调试库只为你提供行信息,仅此而已。 You could try to distinguish them by their signature since hacks exist to obtain the method signature, but that would fail here as well since both functions have the same signature.您可以尝试通过它们的签名来区分它们,因为存在获取方法签名的黑客攻击,但这也会失败,因为这两个函数具有相同的签名。 You could try gsub ing them out based on their names, but remember that functions can be anonymous in Lua.您可以尝试gsub根据它们的名称将它们取出,但请记住,函数在 Lua 中可以是匿名的。

Lua also provides the string.dump function to obtain the bytecode of a function. Lua还提供了string.dump function来获取function的字节码。 I highly doubt that this is of any use to you;我非常怀疑这对您有什么用处; theoretically you could decompile it to get back a "Lua" representation of what the function does, but it would hardly be recognizable or readable.从理论上讲,您可以对其进行反编译以获取 function 所做的“Lua”表示,但它几乎无法识别或读取。

well, it's not completely impossible, the code in some cases can be read from a lua file, for example:好吧,这并非完全不可能,在某些情况下,代码可以从 lua 文件中读取,例如:

function a()
    print("Hello,World")
end

local function get_source_code(f)
    local t = debug.getinfo (f)
    if t.linedefined < 0 then print("source",t.source); return end
    local name = t.source:gsub("^@","")
    local i = 0
    local text = {}
    for line in io.lines(name) do
     i=i+1
     if i >= t.linedefined then text[#text+1] = line end
     if i >= t.lastlinedefined then break end
    end
    return table.concat(text,"\n") 
  
end

print( get_source_code(a) )

maybe that will be enough.也许这就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM