简体   繁体   English

在对字符串中的函数进行求值后,将参数传递给lua函数

[英]Passing argument to lua function after evaluating function in string

I have functions process and matrix . 我有函数processmatrix The following code works 以下代码有效

process(matrix({{2,4,6},{8,10,12},{14,16,20}}))

However the following doesn't work. 但是以下不起作用。

n='matrix({{2,4,6},{8,10,12},{14,16,20}})'
process(n)

It throws some error. 它会引发一些错误。 The reason is obvious that process takes n as string rather than the output of the function matrix . 原因很明显,进程将n作为string而不是函数matrix的输出。 So the basic difficulty involved here is about evaluating string from variable n and then give it as argument to the function process . 因此,这里所涉及的基本困难是关于evaluating从变量字符串n ,然后把它作为参数传递给函数process Here loadstring function is of no use as matrix is local function and can't be referred from loadstring . 这里的loadstring函数没有用,因为matrix是local函数,不能从loadstring Is there any work around for this? 这有什么工作吗? I hope that I have clearly stated the problem here. 我希望我在这里明确说明了这个问题。 It is about evaluating (or unloading) string and then passing it as argument to another function. 它是关于评估(或卸载)字符串然后将其作为参数传递给另一个函数。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

as matrix is local function 因为matrix是局部函数

Lua takes local declarations seriously . Lua 认真对待 local声明。 If a variable is declared local , it can only be accessed by code which is statically within the local scope of that variable. 如果变量被声明为local ,则只能通过静态在该变量的本地范围内的代码来访问它。 Strings which you later turn into code are not statically in the local scope and therefore cannot access local variables. 您稍后转换为代码的字符串在本地范围内不是静态的,因此无法访问local变量。

Now, with Lua 5.2+, you can provide load with a second parameter, a table which represents the global environment against which that Lua chunk will be built. 现在,使用Lua 5.2+,您可以为load提供第二个参数,该表表示将构建Lua块的全局环境。 If that table contains a matrix value, then the loaded string can access it. 如果该表包含matrix值,则加载的字符串可以访问它。 For Lua 5.1, you'd have to use setfenv on the function returned to load to accomplish a similar effect. 对于Lua 5.1,您必须在返回的函数上使用setfenvload以实现类似的效果。 The Lua 5.2+ method would look like this: Lua 5.2+方法看起来像这样:

local env = {matrix = matrix}
local func = load("return matrix({{2,4,6},{8,10,12},{14,16,20}})", nil, "t", env)
process(func())

Note the following: 请注意以下事项:

  • You must create an explicit table which is the global environment. 您必须创建一个显式表,这是一个全局环境。 There's nothing you can pass that says "make my locals available"; 没有什么可以通过的说“让我的当地人可用”; you have to put every local you'd like to access there. 你必须把你想去的每个地方都放在那里。 Which is, generally speaking, why you pass these things as parameters or just make them globals. 一般来说,这就是为什么将这些东西作为参数传递或者只是将它们作为全局变量。

  • You explicitly need the "return " there if you want to get the results of the call to matrix . 如果要获取对matrix的调用结果,则明确需要"return "

  • You have to call the function. 你必须调用该函数。 Functions are values in Lua, so you can freely pass them around. 函数是Lua中的值,因此您可以自由地传递它们。 But if you want to pass the results of a function to another function, you have to actually call it. 但是如果你想将函数的结果传递给另一个函数,你必须实际调用它。

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

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