我想从一个C程序调用一个Lua脚本,该脚本需要()s lyaml模块,Lua绑定LibYAML.
我从源代码编译了Lua 5.2,并且我修改了模块以使它与Lua 5.2一起工作.它可以在github找到.
Lua脚本如下,它适用于Lua 5.1和5.2:
-- foo.lua require('lyaml') function hello () res = lyaml.load("a: 4\n") return res.a end -- then calling hello() it works like a charm print( hello() ) -> 4
问题
我编写了一个C程序,它应该在Programming in Lua,Chapter 25和Lua 5.2 Reference Manual之后从脚本中调用hello().
C程序如下:
/* foo.c */ #include <lua.h> #include <lualib.h> #include <lauxlib.h> int main(void) { double z; lua_State *L = luaL_newstate(); luaL_openlibs(L); if (luaL_dofile(L,"foo.lua")) luaL_error(L,"error running script: %s",lua_tostring(L,-1)); lua_getglobal(L,"hello"); if (lua_pcall(L,1,0) != 0) luaL_error(L,"error calling hello: %s",-1)); if (!lua_isnumber(L,-1)) luaL_error(L,"result must be number"); z = lua_tonumber(L,-1); lua_pop(L,1); lua_close(L); return 0; }
我编译发行:
gcc -Wall -o foo foo.c -ldl -lm -llua
然后在运行foo时,我在运行时收到以下错误:
PANIC: unprotected error in call tu Lua API ( error running script: error loading module 'lyaml' from file '/path/to/lyaml.so': /path/to/lyaml.so: undefined symbol: lua_gettop) Aborted
所以我尝试从C程序加载lyaml,在luaL_openlibs()调用后添加以下行:
luaL_requiref(L,"lyaml",luaopen_package,1);
重新编译后,错误变为:
PANIC: unprotected error in call tu Lua API ( error running script: hello.lua:4: attempt to index global 'lyaml' (a nil value)) Aborted
所以我想,没有lyaml符号,并且require()调用以某种方式失败.
通过阅读luaL_requiref()文档,我认为modname将通过其调用设置glb标志设置为true:
void luaL_requiref (lua_State *L,const char *modname,lua_CFunction openf,int glb);
Calls function
openf
with stringmodname
as an argument and sets the call result inpackage.loaded[modname]
,as if that function has been called throughrequire
.If glb is true,also stores the result into global
modname
.
Leaves a copy of that result on the stack.
我试图在Lua脚本中注释require()调用,结果是一样的.
题
我做错了什么?我忘了做某事吗?
编辑
我用其替代品破解了模块更新已弃用(已删除)的函数/类型,如下所示:
lua_strlen() -> luaL_len() luaL_reg -> luaL_Reg luaL_getn() -> luaL_len()
然而Lua脚本使用lyaml工作所以我认为问题不是我的黑客.
我用Lua 5.1尝试了原始的lyaml模块.结果是一样的,所以我确定问题不是我的黑客.
UPDATE
根据Doug Currie的回答,添加以下行,C程序与Lua 5.1完美配合.我仍然在5.2中得到相同的错误.
lyaml = require('lyaml')
解决方法
foo.lua应该从
lyaml = require('lyaml')