Lua:加载文件后获取全局函数失败 [英] Lua: getting global function failing after loading file
问题描述
我正在尝试在名为test2.lua的lua文件中调用一个函数 这是test2.lua的内容:
I'm attempting to call a function inside of a lua file called test2.lua This is the contents of test2.lua:
function abc(path)
t = {}
table.insert(t, "a")
return t
end
如您所见,它只需要一个输入并返回一个字符串.
As you can see it takes a single input and returns a string.
这是我的C代码.很简单但是,为了调用该函数,我的调用getglobal无效.lua_getglobal表示在我测试该函数时,它不是函数...这是什么原因? abc不应该是可在源文件内部返回的全局函数吗?为什么然后只为这个全局值找到零呢?
Here is my C code. It's pretty simple. However my call getglobal in order to call that function does not work... lua_getglobal says it isn't a function when I test it... Any reason why this is? Shouldn't abc be a global function returnable inside of the source file? Why then does it only find nil for this global?
L = lua_open();
luaL_openlibs(L);
luaL_loadfile(L, "src/test2.lua");
lua_getglobal(L, "abc");
lua_pushstring(L, "coollll");
int error = 0;
if ((error = lua_pcall(L, 1, 1, 0)) == 0)
{
std::cout << "cool";
}
无论使用loadfile还是dofile,调用lua_getglobal都会导致我的程序破坏控制权...知道为什么吗?
calling lua_getglobal is causing my program to break control regardless of using loadfile or dofile... any idea why?
推荐答案
函数luaL_loadfile()
读取,解析并编译命名的Lua文件.它不执行任何内容.这对您来说很重要,因为语句function abc(path)
... end
在执行之前没有可见的作用.您使用过的function
关键字等同于编写
The function luaL_loadfile()
reads, parses, and compiles the named Lua file. It does not execute any of its content. This is important in your case because the statement function abc(path)
...end
has no visible effect until it is executed. The function
keyword as you've used it is equivalent to writing
abc = function(path)
t = {}
table.insert(t, "a")
return t
end
在这种形式下,很明显,在执行代码之前,实际上没有为名为abc
的变量分配值.
In this form, it is clearer that the variable named abc
is not actually assigned a value until the code executes.
luaL_loadfile()
返回时,它已将Luan堆栈顶部的一个匿名函数推入了,这是编译文件的结果.您需要调用它,而lua_pcall()
可以解决问题.用以下内容替换对luaL_loadfile()
的引用:
When luaL_loadfile()
returns, it has pushed an anonymous function on the top of the Lua stack that is the result of compiling your file. You need to call it, and lua_pcall()
will do the trick. Replace your reference to luaL_loadfile()
with this:
if (luaL_loadfile(L, "src/test2.lua") || lua_pcall(L, 0, LUA_MULTRET, 0)) {
// do something with the error
}
这时,test2.lua已经执行,它定义的任何功能或修改的其他全局变量都可用.
At this point, test2.lua has been executed and any functions it defined or other global variables it modified are available.
这是一个足够常见的习惯用法,提供了功能luaL_dofile()
来按名称加载和调用文件.
This is a common enough idiom, that the function luaL_dofile()
is provided to load and call a file by name.
所呈现的代码中还有第二个更微妙的问题.函数abc()
使用名为t
的变量,但是您应该知道,所使用的t
是全局变量.您可能打算在abc()
的顶部写local t = {}
.
There is a second, more subtle issue in your code as presented. The function abc()
uses a variable named t
, but you should be aware that t
as used is a global variable. You probably meant to write local t = {}
at the top of abc()
.
这篇关于Lua:加载文件后获取全局函数失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!