|
发表于 2010-8-21 11:16:00
|
显示全部楼层
Re:C++ 如何获得LUA函数返回的表中的数据
void test()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
if(luaL_dofile(L, "test.lua"))
luaL_error(L, "%s", lua_tostring(L, -1));
lua_getglobal(L, "foo");
lua_pcall(L, 0, 1, 0);
lua_pushstring(L, "name");
lua_gettable(L, -2);
printf("name = %s,\n", lua_tostring(L, -1));
lua_pop(L, 1);
lua_pushstring(L, "age");
lua_gettable(L, -2);
printf("age = %d\n", lua_tonumber(L, -1));
lua_pop(L, 1);
lua_pop(L, 1);
lua_close(L);
}
--test.lua
function foo()
t = {
["name"] = '小明',
["age"] = 24
};
return t;
end |
|