Files
integration/luprex/syscpp/lpx-table.cpp
2020-11-15 16:49:42 -05:00

38 lines
930 B
C++

#include "lpx-table.hpp"
// Clear the table. Removes metatable and all key-value pairs.
int lpx_table_clear(lua_State *L) {
LpxArg tab;
LpxStackManager LSM(L, tab);
luaL_checktype(L, tab, LUA_TTABLE);
// Clear the metatable.
lua_pushnil(L);
lua_setmetatable(L, tab);
// Clear the elements.
lua_pushnil(L);
while (lua_next(L, tab) != 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);
}
return LSM.retval();
}
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) {
LpxStackManager::reg(L, "table", "clear", LpxArgCheck<1, lpx_table_clear>);
LpxStackManager::reg(L, "table", "coerce", LpxArgCheck<1, lpx_table_coerce>);
}