From bfa5a8df1eaf893ca642b845dd7c2a2a3c13fe11 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Sat, 25 May 2013 16:31:48 -0700 Subject: [PATCH] Use lua_for_each more places --- aegisub/src/auto4_lua.cpp | 15 +++++++-------- aegisub/src/auto4_lua_dialog.cpp | 23 +++-------------------- aegisub/src/auto4_lua_utils.h | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index b6368a332..68bb8cc99 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -802,13 +802,12 @@ namespace Automation4 { std::set sel; entryIter it = c->ass->Line.begin(); int last_idx = 1; - lua_pushnil(L); - while (lua_next(L, -2)) { + lua_for_each(L, [&] { if (lua_isnumber(L, -1)) { int cur = lua_tointeger(L, -1); if (cur < 1 || cur > (int)c->ass->Line.size()) { wxLogError("Selected row %d is out of bounds (must be 1-%u)", cur, c->ass->Line.size()); - break; + throw LuaForEachBreak(); } advance(it, cur - last_idx); @@ -816,7 +815,7 @@ namespace Automation4 { AssDialogue *diag = dynamic_cast(&*it); if (!diag) { wxLogError("Selected row %d is not a dialogue line", cur); - break; + throw LuaForEachBreak(); } sel.insert(diag); @@ -824,17 +823,17 @@ namespace Automation4 { if (!active_line || active_idx == cur) active_line = diag; } - lua_pop(L, 1); - } + }); AssDialogue *new_active = c->selectionController->GetActiveLine(); if (active_line && (active_idx > 0 || !sel.count(new_active))) new_active = active_line; c->selectionController->SetSelectionAndActive(sel, new_active); } + else + lua_pop(L, 1); - stackcheck.check_stack(1); - lua_pop(L, 1); + stackcheck.check_stack(0); } catch (agi::UserCancelException const&) { subsobj->Cancel(); diff --git a/aegisub/src/auto4_lua_dialog.cpp b/aegisub/src/auto4_lua_dialog.cpp index 6107c4c2a..6b87ea8c3 100644 --- a/aegisub/src/auto4_lua_dialog.cpp +++ b/aegisub/src/auto4_lua_dialog.cpp @@ -36,6 +36,7 @@ #include "auto4_lua.h" +#include "auto4_lua_utils.h" #include "ass_style.h" #include "colour_button.h" #include "compat.h" @@ -43,8 +44,6 @@ #include "utils.h" #include "validators.h" -#include - #include #include #include @@ -65,9 +64,6 @@ #include #include -// These must be after the headers above. -#include - namespace { inline void get_if_right_type(lua_State *L, std::string &def) { if (lua_isstring(L, -1)) @@ -101,16 +97,6 @@ namespace { return get_field(L, name, std::string()); } - template - void lua_for_each(lua_State *L, Func&& func) { - lua_pushnil(L); // initial key - while (lua_next(L, -2)) { - func(); - lua_pop(L, 1); // pop value, leave key - } - lua_pop(L, 1); // pop key - } - template void read_string_array(lua_State *L, T &cont) { lua_for_each(L, [&] { @@ -438,9 +424,7 @@ namespace Automation4 { luaL_error(L, "Cannot create config dialog from something non-table"); // Ok, so there is a table with controls - lua_pushvalue(L, 1); - lua_pushnil(L); // initial key - while (lua_next(L, -2)) { + lua_for_each(L, [&] { if (!lua_istable(L, -1)) luaL_error(L, "bad control table entry"); @@ -475,8 +459,7 @@ namespace Automation4 { luaL_error(L, "bad control table entry"); controls.push_back(ctl); - lua_pop(L, 1); - } + }); if (include_buttons && lua_istable(L, 2)) { lua_pushvalue(L, 2); diff --git a/aegisub/src/auto4_lua_utils.h b/aegisub/src/auto4_lua_utils.h index 0ed36ab7c..9a860313d 100644 --- a/aegisub/src/auto4_lua_utils.h +++ b/aegisub/src/auto4_lua_utils.h @@ -85,6 +85,24 @@ inline std::string get_global_string(lua_State *L, const char *name) { return ret; } +struct LuaForEachBreak {}; + +template +void lua_for_each(lua_State *L, Func&& func) { + lua_pushnil(L); // initial key + while (lua_next(L, -2)) { + try { + func(); + } + catch (LuaForEachBreak) { + lua_pop(L, 1); + break; + } + lua_pop(L, 1); // pop value, leave key + } + lua_pop(L, 1); // pop table +} + #ifdef _DEBUG struct LuaStackcheck { lua_State *L;