2020-11-13 18:17:47 -05:00
|
|
|
#include "lpx-table.hpp"
|
|
|
|
|
|
|
|
|
|
// Clear the table. Removes metatable and all key-value pairs.
|
|
|
|
|
int lpx_table_clear(lua_State *L) {
|
2020-11-15 16:49:42 -05:00
|
|
|
LpxArg tab;
|
|
|
|
|
LpxStackManager LSM(L, tab);
|
|
|
|
|
|
2020-11-13 18:17:47 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2020-11-15 16:49:42 -05:00
|
|
|
|
|
|
|
|
return LSM.retval();
|
2020-11-13 18:17:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int lpx_table_coerce(lua_State *L) {
|
|
|
|
|
if (!lua_istable(L, -1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 16:49:42 -05:00
|
|
|
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>);
|
2020-11-13 18:17:47 -05:00
|
|
|
}
|