From b2f5a993d9b39639ca8157f7cda01b37d7a1f37c Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 1 May 2013 20:51:17 -0700 Subject: [PATCH] Actually define __ipairs on LuaAssFile --- aegisub/src/auto4_lua.h | 2 ++ aegisub/src/auto4_lua_assfile.cpp | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/aegisub/src/auto4_lua.h b/aegisub/src/auto4_lua.h index 7f79157d2..80ed7eca1 100644 --- a/aegisub/src/auto4_lua.h +++ b/aegisub/src/auto4_lua.h @@ -93,6 +93,8 @@ namespace Automation4 { void ObjectAppend(lua_State *L); void ObjectInsert(lua_State *L); void ObjectGarbageCollect(lua_State *L); + int ObjectIPairs(lua_State *L); + int IterNext(lua_State *L); int LuaParseKaraokeData(lua_State *L); diff --git a/aegisub/src/auto4_lua_assfile.cpp b/aegisub/src/auto4_lua_assfile.cpp index 1f2dd7d52..8315f93bf 100644 --- a/aegisub/src/auto4_lua_assfile.cpp +++ b/aegisub/src/auto4_lua_assfile.cpp @@ -512,6 +512,28 @@ namespace Automation4 { LOG_D("automation/lua") << "Garbage collected LuaAssFile"; } + int LuaAssFile::ObjectIPairs(lua_State *L) + { + lua_pushvalue(L, lua_upvalueindex(1)); // push 'this' as userdata + lua_pushcclosure(L, closure_wrapper<&LuaAssFile::IterNext>, 1); + lua_pushnil(L); + push_value(L, 0); + return 3; + } + + int LuaAssFile::IterNext(lua_State *L) + { + int i = luaL_checkint(L, 2); + if (i >= (int)lines.size()) { + lua_pushnil(L); + return 1; + } + + push_value(L, i + 1); + AssEntryToLua(L, lines[i]); + return 2; + } + int LuaAssFile::LuaParseKaraokeData(lua_State *L) { agi::scoped_ptr e(LuaToAssEntry(L)); @@ -619,6 +641,7 @@ namespace Automation4 { set_field(L, "__newindex", closure_wrapper_v<&LuaAssFile::ObjectIndexWrite>); set_field(L, "__len", closure_wrapper<&LuaAssFile::ObjectGetLen>); set_field(L, "__gc", closure_wrapper_v<&LuaAssFile::ObjectGarbageCollect>); + set_field(L, "__ipairs", closure_wrapper<&LuaAssFile::ObjectIPairs>); lua_setmetatable(L, -2); // register misc functions