我在C中创建了一个Lua表,但我不确定如何将该表推到堆栈的顶部,这样我就可以将它传递给Lua函数了.
有谁知道如何做到这一点?
这是我目前的代码:
lua_createtable(state,libraries.size(),0); int table_index = lua_gettop(state); for (int i = 0; i < libraries.size(); i++) { lua_pushstring(state,libraries[i].c_str()); lua_rawseti(state,table_index,i + 1); } lua_settable(state,-3); [ Push other things ] [ Call function ]
解决方法
这是一个快速帮助函数,用于将字符串推送到表中
void l_pushtablestring(lua_State* L,char* key,char* value) { lua_pushstring(L,key); lua_pushstring(L,value); lua_settable(L,-3); }
在这里,我使用辅助函数来创建表并将其传递给函数
// create a lua function luaL_loadstring(L,"function fullName(t) print(t.fname,t.lname) end"); lua_pcall(L,0); // push the function to the stack lua_getglobal(L,"fullName"); // create a table in c (it will be at the top of the stack) lua_newtable(L); l_pushtablestring(L,"fname","john"); l_pushtablestring(L,"lname","stewart"); // call the function with one argument lua_pcall(L,1,0);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。