Make subs.delete accept a table of rows to delete

Lua has a limit to how many arguments can be pushed onto the stack, so
when deleting very large numbers of lines at once unpacking a table can
fail.
This commit is contained in:
Thomas Goyne 2014-04-15 20:47:59 -07:00
parent c685ae4aea
commit 162e119afa

View file

@ -448,15 +448,26 @@ namespace Automation4 {
// get number of items to delete // get number of items to delete
int itemcount = lua_gettop(L); int itemcount = lua_gettop(L);
std::vector<int> ids; if (itemcount == 0) return;
ids.reserve(itemcount);
std::vector<int> ids;
if (itemcount == 1 && lua_istable(L, 1)) {
lua_pushvalue(L, 1);
lua_for_each(L, [&] {
int n = luaL_checkint(L, -1);
luaL_argcheck(L, n > 0 && n <= (int)lines.size(), 1, "Out of range line index");
ids.push_back(n - 1);
});
}
else {
ids.reserve(itemcount);
while (itemcount > 0) { while (itemcount > 0) {
int n = luaL_checkint(L, itemcount); int n = luaL_checkint(L, itemcount);
luaL_argcheck(L, n > 0 && n <= (int)lines.size(), itemcount, "Out of range line index"); luaL_argcheck(L, n > 0 && n <= (int)lines.size(), itemcount, "Out of range line index");
ids.push_back(n - 1); ids.push_back(n - 1);
--itemcount; --itemcount;
} }
}
sort(ids.begin(), ids.end()); sort(ids.begin(), ids.end());