推一个Lua表 [英] Pushing a lua table
本文介绍了推一个Lua表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个在C LUA表,但我不知道如何该表推到堆栈的顶部,这样我可以把它传递给一个LUA功能。
有谁知道如何做到这一点?
这是我目前的code:
lua_createtable(州,libraries.size(),0);
INT table_index = lua_gettop(州);
的for(int i = 0; I< libraries.size();我++)
{
lua_pushstring(州,库[I] .c_str());
lua_rawseti(州,table_index,I + 1);
}lua_settable(州,-3);[PUSH其他的事情]
[来电功能]
解决方案
下面是一个简单的辅助函数来推动字符串表
无效l_pushtablestring(lua_State * L,字符*键,字符*值){
lua_pushstring(L,密钥);
lua_pushstring(L,值);
lua_settable(L,-3);
}
下面我用辅助函数来创建表并把它传递给函数
//创建一个Lua函数
luaL_loadstring(L,功能全名(T)打印(t.fname,t.lname)结束);
lua_pcall(L,0,0,0);//推功能到堆栈
lua_getglobal(L,全名);//创建在C的表(它会在堆栈的顶部)
lua_newtable(L);
l_pushtablestring(L,其中fname,约翰);
l_pushtablestring(L,LNAME,斯图尔特);//调用函数有一个参数
lua_pcall(L,1,0,0);
I have created a lua table in C, but i'm not sure how to push that table onto the top of a stack so I can pass it to a lua function.
Does anyone know how to do this?
This is my current code:
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 ]
解决方案
Here's a quick helper function to push strings to the table
void l_pushtablestring(lua_State* L , char* key , char* value) {
lua_pushstring(L, key);
lua_pushstring(L, value);
lua_settable(L, -3);
}
Here I use the helper function to create the table and pass it to a function
// create a lua function
luaL_loadstring(L, "function fullName(t) print(t.fname,t.lname) end");
lua_pcall(L, 0, 0, 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, 0);
这篇关于推一个Lua表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文