首页 > 解决方案 > Lua:调用函数时获取传递的参数

问题描述

我想获取传递给特定函数的参数。

例如:

load("return 2+1")()

想要的输出:

return 2+1

阅读调试库后我不知道:(

标签: lua

解决方案


如果我正确理解您想要什么,请在调用之前覆盖加载函数:

local global_load = load
local function load (...)
    print (...) -- or use whatever debug tool to see the arguments.
    return global_load (...)
end

您可以通过这种方式重新定义任何函数:

local function verbose (func)
    return function (...)
        print (...) -- or use whatever debug tool to see the arguments.
        return func (...)
    end
end

local load = verbose (load)

print (load 'return 2 + 1' ())

推荐阅读