added next() and pairs() top-level Lua functions for traversing tables easily

git-svn-id: svn://anubis/anaglym/trunk@226 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-01-27 20:47:17 +00:00
parent a7a21a6370
commit c4f6310c2d
2 changed files with 41 additions and 0 deletions

40
ag.cc
View File

@ -45,6 +45,7 @@ namespace ag
{ "loadModel", loadModel },
{ "loadModelStatic", loadModelStatic },
{ "loadTexture", loadTexture },
{ "next", next },
{ "print", print },
{ "println", println },
{ "registerEventHandler", registerEventHandler },
@ -73,6 +74,18 @@ namespace ag
{ NULL, NULL }
};
luaL_register(L, "ag", functions);
luaL_loadstring(L,
"next = ag.next\n"
"pairs = function(table)\n"
" return next, table, nil\n"
"end\n"
"ag.createAMotor = function()\n"
"end\n"
);
int s = lua_pcall(L, 0, 0, 0);
}
static void print_val(lua_State * L, int index)
@ -139,6 +152,33 @@ namespace ag
return ret;
}
int next(lua_State * L)
{
int argc = lua_gettop(L);
if (argc == 1 || argc == 2)
{
if (argc == 1)
{
lua_pushnil(L);
}
else
{
lua_pushvalue(L, 2);
}
if (lua_next(L, 1) == 0)
{
lua_pushnil(L);
return 1;
}
return 2;
}
else
{
lua_pushnil(L);
return 1;
}
}
static void createLuaObject(lua_State * L, int id)
{
lua_newtable(L);

1
ag.h
View File

@ -24,6 +24,7 @@ namespace ag
int loadModel(lua_State * L);
int loadModelStatic(lua_State * L);
int loadTexture(lua_State * L);
int next(lua_State * L);
int print(lua_State * L);
int println(lua_State * L);
int registerEventHandler(lua_State * L);