From dba5bccfa0e353a9e69fb098211504813a68e0ae Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Fri, 9 Jul 2021 13:52:03 -0400 Subject: [PATCH] A variety of small fixes --- luprex/core/cpp/luastack.cpp | 2 +- luprex/core/cpp/table.cpp | 44 ++++-------------------------------- luprex/core/lua/inspect.lua | 3 --- luprex/core/lua/ut-table.lua | 7 ++++++ 4 files changed, 12 insertions(+), 44 deletions(-) diff --git a/luprex/core/cpp/luastack.cpp b/luprex/core/cpp/luastack.cpp index 9c6e3529..1f19f064 100644 --- a/luprex/core/cpp/luastack.cpp +++ b/luprex/core/cpp/luastack.cpp @@ -253,7 +253,7 @@ void LuaStack::cleartable(LuaSlot tab) const { lua_pop(L_, 1); // Pop the old value. lua_pushvalue(L_, -1); // Clone the key lua_pushnil(L_); // Push the new value. - lua_settable(L_, tab.index()); + lua_rawset(L_, tab.index()); } } diff --git a/luprex/core/cpp/table.cpp b/luprex/core/cpp/table.cpp index eafc72ef..6508d045 100644 --- a/luprex/core/cpp/table.cpp +++ b/luprex/core/cpp/table.cpp @@ -119,13 +119,7 @@ LuaDefine(table_empty, "c") { LuaDefine(table_count, "c") { luaL_checktype(L, -1, LUA_TTABLE); - lua_pushnil(L); - lua_Integer total = 0; - while (lua_next(L, -2) != 0) { - total += 1; - lua_pop(L, 1); - } - lua_pop(L, 1); + int total = lua_nkeys(L, -1); lua_pushinteger(L, total); return 1; } @@ -367,7 +361,7 @@ int table_getpairs(lua_State *L, bool sort, bool *unsortable) { // ///////////////////////////////////////////////////////////// -LuaDefine(table_nextpair, "c") { +LuaDefine(table_nextsortedpair, "c") { if (lua_gettop(L) < 2) { luaL_error(L, "Not enough arguments to nextpair"); } @@ -386,32 +380,7 @@ LuaDefine(table_nextpair, "c") { } } -///////////////////////////////////////////////////////////// -// -// Replace the lua 'pairs' and 'next' iterators. -// -///////////////////////////////////////////////////////////// - -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") { +LuaDefine(table_sortedpairs, "c") { LuaArg tab; LuaRet closure, rtab, key; LuaStack LS(L, tab, closure, rtab, key); @@ -422,16 +391,11 @@ LuaDefine(table_pairs, "f") { luaL_error(L, "Cannot iterate over a table with unsortable keys"); } lua_replace(L, rtab.index()); - LS.set(closure, table_nextpair); + LS.set(closure, table_nextsortedpair); LS.set(key, LuaNil); return LS.result(); } -LuaDefine(table_next, "f") { - luaL_error(L, "The 'next' iterator is not allowed in this version of lua"); - return 0; -} - LuaDefine(table_genlt, "f") { LuaArg o1,o2; LuaRet lt; diff --git a/luprex/core/lua/inspect.lua b/luprex/core/lua/inspect.lua index f65730fc..218cf53f 100644 --- a/luprex/core/lua/inspect.lua +++ b/luprex/core/lua/inspect.lua @@ -35,9 +35,6 @@ local tostring = tostring inspect.KEY = {} inspect.METATABLE = {} -local function rawpairs(t) - return table.unsafepairs(t) -end -- Apostrophizes the string if it has quotes, but not aphostrophes -- Otherwise, it returns a regular quoted string diff --git a/luprex/core/lua/ut-table.lua b/luprex/core/lua/ut-table.lua index 1ea3ec4f..b91d70cf 100644 --- a/luprex/core/lua/ut-table.lua +++ b/luprex/core/lua/ut-table.lua @@ -1,6 +1,13 @@ makeclass("unittests") +function cmt() + for i=1,100000 do + local t = {1,2,3,4,5,a=1,b=2,c=3,d=4,e=5} + table.clear(t) + end +end + function unittests.tables() -- check table.count assert(table.count({}) == 0)