尝试使用嵌套表调用Lua中的函数
发布时间:2020-12-14 21:47:11 所属栏目:大数据 来源:网络整理
导读:我正在尝试创建一个名为Dfetch()的C函数,该函数在Lua中注册为fetch().我正在寻找分层,以便我可以将dog.beagle.fetch()称为Lua的函数.它只是有助于更好地组织代码.下面是我的,但它不是调用我的C函数.如果我只是执行全局而不是子表,则调用C函数.我是Lua的新手,
我正在尝试创建一个名为Dfetch()的C函数,该函数在Lua中注册为fetch().我正在寻找分层,以便我可以将dog.beagle.fetch()称为Lua的函数.它只是有助于更好地组织代码.下面是我的,但它不是调用我的C函数.如果我只是执行全局而不是子表,则调用C函数.我是Lua的新手,所以我想我只是把桌子弄错了.
void myregister(lua_State *L,const char *libname,const char *sublibname,const luaL_Reg *l) { luaL_newmetatable(L,libname); lua_newtable(L); luaL_setfuncs(L,l,0); lua_pushvalue(L,-1); if(sublibname != NULL) { lua_newtable(L); lua_setfield(L,-2,sublibname); } lua_setglobal(L,libname); } luaL_Reg fidofuncModule[] = { {"fetch",Dfetch},{NULL,NULL}}; 在我的main()中,我调用以下内容: lua_State * execContext = luaL_newstate(); //adding lua basic library luaL_openlibs(execContext); myregister(execContext,"dog","beagle",fidofuncModule); strcpy(buff,"dog.beagle.fetch();"); errorcode = luaL_loadbuffer(execContext,buff,strlen(buff),"line"); errorcode = lua_pcall(execContext,0); if (errorcode) { printf("%s",lua_tostring(execContext,-1)); lua_pop(execContext,1); /* pop error message from the stack */ } //cleaning house lua_close(execContext); 蒂姆,谢谢 解决方法void myregister(lua_State *L,const luaL_Reg *lib) { // create 'libname' table lua_newtable(L); // no sublib: just import our library functions directly into lib and we're done if (sublibname == NULL) { luaL_setfuncs(L,lib,0); } // sublib: create a table for it,import functions to it,add to parent lib else { lua_newtable(L); luaL_setfuncs(L,0); lua_setfield(L,sublibname); } lua_setglobal(L,libname); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |