Allow limited access to unsafepairs/unsafenext
This commit is contained in:
@@ -313,7 +313,7 @@ static void auxsort (lua_State *L, int tab, int l, int u) {
|
||||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
int table_sortedpairs(lua_State *L, bool *unsortable) {
|
||||
int table_getpairs(lua_State *L, bool sort, bool *unsortable) {
|
||||
lua_checkstack(L, 40);
|
||||
LuaArg tab;
|
||||
LuaVar key, value;
|
||||
@@ -328,7 +328,10 @@ int table_sortedpairs(lua_State *L, bool *unsortable) {
|
||||
lua_pushnil(L);
|
||||
int total = 0;
|
||||
while (lua_next(L, tab.index()) != 0) {
|
||||
total += 1;
|
||||
int ktype = lua_type(L, -2);
|
||||
if (ktype == LUA_TNUMBER || ktype == LUA_TSTRING || ktype == LUA_TBOOLEAN) {
|
||||
total += 1;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
// Create the table, store the initial 1.
|
||||
@@ -350,7 +353,9 @@ int table_sortedpairs(lua_State *L, bool *unsortable) {
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
auxsort(L, pairs.index(), 1, total);
|
||||
if (sort) {
|
||||
auxsort(L, pairs.index(), 1, total);
|
||||
}
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
@@ -362,7 +367,7 @@ int table_sortedpairs(lua_State *L, bool *unsortable) {
|
||||
//
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
||||
static int nextsortedpair(lua_State *L) {
|
||||
LuaDefine(table_nextpair, "c") {
|
||||
if (lua_gettop(L) < 2) {
|
||||
luaL_error(L, "Not enough arguments to nextpair");
|
||||
}
|
||||
@@ -387,18 +392,37 @@ static int nextsortedpair(lua_State *L) {
|
||||
//
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
||||
LuaDefine(table_unsafenext, "c") {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
||||
if (lua_next(L, 1))
|
||||
return 2;
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
LuaDefine(table_unsafepairs, "c") {
|
||||
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
|
||||
lua_pushcfunction(L, table_unsafenext); /* will return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnil(L);
|
||||
return 3;
|
||||
}
|
||||
|
||||
LuaDefine(table_pairs, "f") {
|
||||
LuaArg tab;
|
||||
LuaRet closure, rtab, key;
|
||||
LuaStack LS(L, tab, closure, rtab, key);
|
||||
lua_pushvalue(L, tab.index());
|
||||
bool unsortable;
|
||||
table_sortedpairs(L, &unsortable);
|
||||
table_getpairs(L, true, &unsortable);
|
||||
if (unsortable) {
|
||||
luaL_error(L, "Cannot iterate over a table with unsortable keys");
|
||||
}
|
||||
lua_replace(L, rtab.index());
|
||||
LS.set(closure, nextsortedpair);
|
||||
LS.set(closure, table_nextpair);
|
||||
LS.set(key, LuaNil);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user