Lots of work on LuaStack

This commit is contained in:
2020-11-27 13:21:07 -05:00
parent f690fb147b
commit 455ba5df87
19 changed files with 715 additions and 527 deletions

34
luprex/syscpp/table.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include "table.hpp"
// Clear the table. Removes metatable and all key-value pairs.
int lpx_table_clear(lua_State *L) {
LuaArg tab;
LuaStack LS(L, tab);
LS.checktype(tab, LUA_TTABLE);
LS.clearmetatable(tab);
lua_pushnil(L);
while (lua_next(L, tab.index()) != 0) {
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());
}
return LS.result();
}
int lpx_table_coerce(lua_State *L) {
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
}
return 1;
}
void luaopen_lpx_table (lua_State *L) {
LuaStack::reg(L, "table", "clear", LuaArgCheck<1, lpx_table_clear>);
LuaStack::reg(L, "table", "coerce", LuaArgCheck<1, lpx_table_coerce>);
}