Is it possible in lua to execute a function from a string representing its name?
i.e: I have the string x = "foo"
, is it possible to do x()
?
在lua中是否可以从表示其名称的字符串中执行一个函数?我。e:我有字符串x = "foo",可以做x()吗?
If yes what is the syntax ?
如果是,语法是什么?
5 个解决方案
#1
33
To call a function in the global namespace (as mentioned by @THC4k) is easily done, and does not require loadstring()
.
要在全局名称空间中调用函数(如@THC4k所述)很容易完成,而且不需要loadstring()。
x='foo'
_G[x]() -- calls foo from the global namespace
You would need to use loadstring()
(or walk each table) if the function in another table, such as if x='math.sqrt'
.
如果另一个表中的函数,比如x='math.sqrt',则需要使用loadstring()(或遍历每个表)。
If loadstring()
is used you would want to not only append parenthesis with ellipse (...)
to allow for parameters, but also add return
to the front.
如果使用loadstring(),您不仅需要使用椭圆(…)附加圆括号来允许参数,还需要添加返回到前面。
x='math.sqrt'
print(assert(loadstring('return '..x..'(...)'))(25)) --> 5
or walk the tables:
或走的表:
function findfunction(x)
assert(type(x) == "string")
local f=_G
for v in x:gmatch("[^%.]+") do
if type(f) ~= "table" then
return nil, "looking for '"..v.."' expected table, not "..type(f)
end
f=f[v]
end
if type(f) == "function" then
return f
else
return nil, "expected function, not "..type(f)
end
end
x='math.sqrt'
print(assert(findfunction(x))(121)) -->11
#2
9
loadstring
is not the answer here. For starters you would need a return
in the string, and other details I won't go into.
loadstring不是这里的答案。对于初学者,你需要在字符串中返回,而其他的细节我不会进入。
THC4k has the right idea; if you have the function name in the variable x, then the call you want is
THC4k有正确的想法;如果在变量x中有函数名,那么需要的调用是。
_G[x](arg1, arg2, ...)
#3
9
I frequently put a bunch of functions in a table:
我经常把一些函数放在一个表中:
functions = {
f1 = function(arg) print("function one: "..arg) end,
f2 = function(arg) print("function two: "..arg..arg) end,
...,
fn = function(arg) print("function N: argh") end,
}
Then you can use a string as an table index and run your function like this
然后,可以使用字符串作为表索引,并像这样运行函数。
print(functions["f1"]("blabla"))
print(functions["f2"]("blabla"))
This is the result:
这是由于:
function one: blabla
function two: blablablabla
I find this to be cleaner than using loadstring()
. If you don't want to create a special function table you can use _G['foo']
.
我发现这比使用loadstring()更干净。如果您不想创建一个特殊的函数表,可以使用_G['foo']。
#4
4
Names are not unique, there can be many functions names foo in different namespaces. But _G['foo']
is foo
in the global namespace.
名称不是唯一的,在不同的名称空间中可以有很多函数名foo。但是_G['foo']在全局名称空间中是foo。
#5
1
It sounds like you want to do an 'eval', which is supported in Lua like so:
听起来你想要做一个“eval”,它在Lua中得到支持:
assert(loadstring(x))()
You'll probably want to concatenate the "()" onto x first, though.
不过,您可能想先将“()”连接到x上。
#1
33
To call a function in the global namespace (as mentioned by @THC4k) is easily done, and does not require loadstring()
.
要在全局名称空间中调用函数(如@THC4k所述)很容易完成,而且不需要loadstring()。
x='foo'
_G[x]() -- calls foo from the global namespace
You would need to use loadstring()
(or walk each table) if the function in another table, such as if x='math.sqrt'
.
如果另一个表中的函数,比如x='math.sqrt',则需要使用loadstring()(或遍历每个表)。
If loadstring()
is used you would want to not only append parenthesis with ellipse (...)
to allow for parameters, but also add return
to the front.
如果使用loadstring(),您不仅需要使用椭圆(…)附加圆括号来允许参数,还需要添加返回到前面。
x='math.sqrt'
print(assert(loadstring('return '..x..'(...)'))(25)) --> 5
or walk the tables:
或走的表:
function findfunction(x)
assert(type(x) == "string")
local f=_G
for v in x:gmatch("[^%.]+") do
if type(f) ~= "table" then
return nil, "looking for '"..v.."' expected table, not "..type(f)
end
f=f[v]
end
if type(f) == "function" then
return f
else
return nil, "expected function, not "..type(f)
end
end
x='math.sqrt'
print(assert(findfunction(x))(121)) -->11
#2
9
loadstring
is not the answer here. For starters you would need a return
in the string, and other details I won't go into.
loadstring不是这里的答案。对于初学者,你需要在字符串中返回,而其他的细节我不会进入。
THC4k has the right idea; if you have the function name in the variable x, then the call you want is
THC4k有正确的想法;如果在变量x中有函数名,那么需要的调用是。
_G[x](arg1, arg2, ...)
#3
9
I frequently put a bunch of functions in a table:
我经常把一些函数放在一个表中:
functions = {
f1 = function(arg) print("function one: "..arg) end,
f2 = function(arg) print("function two: "..arg..arg) end,
...,
fn = function(arg) print("function N: argh") end,
}
Then you can use a string as an table index and run your function like this
然后,可以使用字符串作为表索引,并像这样运行函数。
print(functions["f1"]("blabla"))
print(functions["f2"]("blabla"))
This is the result:
这是由于:
function one: blabla
function two: blablablabla
I find this to be cleaner than using loadstring()
. If you don't want to create a special function table you can use _G['foo']
.
我发现这比使用loadstring()更干净。如果您不想创建一个特殊的函数表,可以使用_G['foo']。
#4
4
Names are not unique, there can be many functions names foo in different namespaces. But _G['foo']
is foo
in the global namespace.
名称不是唯一的,在不同的名称空间中可以有很多函数名foo。但是_G['foo']在全局名称空间中是foo。
#5
1
It sounds like you want to do an 'eval', which is supported in Lua like so:
听起来你想要做一个“eval”,它在Lua中得到支持:
assert(loadstring(x))()
You'll probably want to concatenate the "()" onto x first, though.
不过,您可能想先将“()”连接到x上。