35 lines
845 B
C++
35 lines
845 B
C++
#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>);
|
|
}
|