C向Lua函数传递table参数请参考:C向Lua函数传递table参数
-
传递纯数组参数
test.lua
mytest = nil
mytest = require "iTest"
if not mytest then
print("Failed to require iTest!")
else
print("Succend to require iTest!");
end
function TestFunc()
local testtabl = {4,8,9,7,1,0,3,2,6,5} --需要传递的table
print("Func_maxvalue:",mytest.getmaxvalue(10,testtabl)) --调用C函数
end
test.c
int LuaGetMaxValue(lua_State* L)
{
int maxvalue = 0;
int count = lua_tonumber(L,1);
for (int i = 0; i < count; ++i)
{
lua_pushnumber(L, i+1);
lua_gettable(L, -2);
int tempnum = lua_tointeger(L, -1);
if (tempnum > maxvalue)
{
maxvalue = tempnum;
}
printf("table[%d]=%d\n",i,tempnum);
lua_pop(L, 1);
}
lua_pop(L, 2);
printf("stack size = %d\n", lua_gettop(L));
lua_pushnumber(L,maxvalue);
return 1;
}
static const luaL_Reg TestLua[] =
{
{"getmaxvalue",LuaGetMaxValue},
{ NULL, NULL }
};
int Luaopen_TestLua(lua_State* L)
{
if (L == NULL)
{
return 1;
}
luaL_checkversion(L);
luaL_newlib(L,TestLua);
return 1;
}
/*******************************************************************************/
lua_State *L = luaL_newstate(); //返回一個指向棧頂的指針
if(L == nullptr)
{
return;
}
//获取相应lua库的使用权
luaopen_base(L);
luaopen_table(L);
luaopen_package(L);
luaopen_io(L);
luaopen_string(L);
luaL_requiref(L, "iTest", Luaopen_TestLua, 1);
luaL_openlibs(L);
if(luaL_dofile(L,"Test.lua")) //luaL_dofile宏封装了luaL_Loadfile和lua_pcall
{
const char * error = lua_tostring(L, -1);
printf(error);
return;
}
lua_getglobal(L,"TestFunc");
lua_pcall(L,0,0,0);
lua_close(L);
/*******************************************************************************/
输出
-
传递复杂数组
test.lua
mytest = nil
mytest = require "iTest"
if not mytest then
print("Failed to require iTest!")
else
print("Succend to require iTest!");
end
local tTest = --需要传递的table
{
gdp = 1234,
info = "this is test about exchange table data!",
task = {12, 23, 34, 45},
}
function TestFunc()
mytest.testtable(tTest) --调用C函数
end
test.c
int LuaTestTable(lua_State* L)
{
printf("stack size = %d\n", lua_gettop(L)); //打印栈中元素的个数
lua_pushstring(L, "gdp"); //将gdp字符串压入栈顶
//根据栈顶的key获取table中的value,将key(这里的“gdp”)移除,再将value压入栈顶
lua_gettable(L, 1);
printf("%s\n", lua_tostring(L, -1)); //取栈顶元素(注意这里的整型值都是string类型)
lua_pop(L, 1); //取完之后清理栈顶
printf("stack size = %d\n", lua_gettop(L)); //打印栈中元素的个数
lua_pushstring(L, "info"); //同上
lua_gettable(L, 1);
printf("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
printf("stack size = %d\n", lua_gettop(L));
lua_pushstring(L, "task"); //这里的value值是一个table哦,没关系栈操作都是一样的
lua_gettable(L, 1);
for (int i = 0; i < 4; ++i)
{
lua_pushnumber(L, i+1);
lua_gettable(L, -2);
printf("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_pop(L, 1);
printf("stack size = %d\n", lua_gettop(L)); //到这里tTest表依然在栈底,但不影响后面的操作。
//------华丽的分割线------------//
//到这里table数据的解析就结束了,以下内容是c API给lua返回table数据
lua_newtable(L);//要给lua脚本返回一个table类型,先要new一个,压入栈顶
lua_pushnumber(L, 1); //将key先压入栈
lua_pushstring(L, "table2lua"); //再将value压入栈
lua_settable(L, -3);//settable将操作-2,-1编号的键值对,设置到table中,并把key-value从栈中移除
lua_pushstring(L, "key"); //同上
lua_newtable(L); //这里有个子table
lua_pushstring(L, "capi");
//这里的value类型使用lua_CFunction类型,可用做c API调用,函数实现请参看附录1
lua_pushcfunction(L, LuaSayHello);
lua_settable(L, -3);
lua_pushnumber(L, 2);
lua_pushnumber(L, 10086);
lua_settable(L, -3);
lua_settable(L, -3); //这个从这里“lua_pushstring(L, "key"); //同上”开始匹配的
printf("stack size = %d\n", lua_gettop(L));
return 1; //返回栈顶1个元素
}
static const luaL_Reg TestLua[] =
{
{"testtable",LuaTestTable},
{ NULL, NULL }
};
int Luaopen_TestLua(lua_State* L)
{
if (L == NULL)
{
return 1;
}
luaL_checkversion(L);
luaL_newlib(L,TestLua);
return 1;
}
/*******************************************************************************/
lua_State *L = luaL_newstate(); //返回一個指向棧頂的指針
if(L == nullptr)
{
return;
}
//获取相应lua库的使用权
luaopen_base(L);
luaopen_table(L);
luaopen_package(L);
luaopen_io(L);
luaopen_string(L);
luaL_requiref(L, "iTest", Luaopen_TestLua, 1);
luaL_openlibs(L);
if(luaL_dofile(L,"Test.lua")) //luaL_dofile宏封装了luaL_Loadfile和lua_pcall
{
const char * error = lua_tostring(L, -1);
printf(error);
return;
}
lua_getglobal(L,"TestFunc");
lua_pcall(L,0,0,0);
lua_close(L);
/*******************************************************************************/
输出