From c4f6310c2d2aab3f1bbb8ceeae22a392c8161373 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 27 Jan 2010 20:47:17 +0000 Subject: [PATCH] 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 --- ag.cc | 40 ++++++++++++++++++++++++++++++++++++++++ ag.h | 1 + 2 files changed, 41 insertions(+) diff --git a/ag.cc b/ag.cc index 9a9f103..ae9d429 100644 --- a/ag.cc +++ b/ag.cc @@ -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); diff --git a/ag.h b/ag.h index 6636e88..778f94f 100644 --- a/ag.h +++ b/ag.h @@ -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);