2020-11-27 13:21:07 -05:00
|
|
|
#include "table.hpp"
|
2020-12-05 18:57:53 -05:00
|
|
|
#include "source.hpp"
|
2020-11-27 13:21:07 -05:00
|
|
|
|
|
|
|
|
// Clear the table. Removes metatable and all key-value pairs.
|
2020-12-05 18:57:53 -05:00
|
|
|
LuaDefineGlobalMethod(table_clear) {
|
2020-11-27 13:21:07 -05:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 18:57:53 -05:00
|
|
|
LuaDefineGlobalMethod(table_coerce) {
|
2020-11-27 13:21:07 -05:00
|
|
|
if (!lua_istable(L, -1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|