Added new 'type' function, other mods

This commit is contained in:
2021-02-07 15:35:31 -05:00
parent 076daaa3a2
commit 8c15e56fe9
6 changed files with 64 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
#include "luastack.hpp"
#include "util.hpp"
#include <iostream>
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
@@ -24,11 +25,10 @@ int LuaStack::collect_tagged_pointer(lua_State *L) {
if (p==0) {
luaL_error(L, "lua deleter function received a non-userdata");
}
if (p->ptr == 0) {
luaL_error(L, "lua object already deleted");
if (p->ptr != 0) {
p->del(p->ptr);
p->ptr = 0;
}
p->del(p->ptr);
p->ptr = 0;
return 0;
}
@@ -37,7 +37,7 @@ void LuaStack::register_all_userdata(lua_State *L) {
LuaStack LS(L, tab, lud);
auto regs = LuaFunctionReg::all();
for (const LuaFunctionReg *r : regs) {
const std::string &name = r->get_name();
const std::string &name = util::tolower(r->get_name());
lua_CFunction tag = r->get_func();
std::string mode = r->get_mode();
if (mode.find('t') != std::string::npos) { // Register type
@@ -209,4 +209,18 @@ void LuaStack::check_nret(int xnret, int otop, int nret) const {
luaL_error(L_, "expected %d return values", xnret);
}
}
LuaDefine(system_type, "f") {
LuaArg obj;
LuaRet tname;
LuaVar mt;
LuaStack LS(L, obj, tname, mt);
int type = LS.type(obj);
if (type == LUA_TUSERDATA) {
LS.getmetatable(mt, obj);
LS.getfield(tname, mt, "type");
} else {
LS.set(tname, lua_typename(L, type));
}
return LS.result();
}