lcpu: more cleanup. this is the last one I promise, I need sleep

This commit is contained in:
Lily Tsuru 2023-07-27 07:21:04 -04:00
parent 1b347eecc8
commit 4538cd182d
1 changed files with 15 additions and 6 deletions

View File

@ -89,6 +89,8 @@ namespace lcpu::lua {
} }
} }
/// A CRTP-based class which allows binding C++ to Lua, in a
/// fairly sensible manner.
template <class TImpl> template <class TImpl>
struct LuaObject { struct LuaObject {
using CFunc = GarrysMod::Lua::CFunc; using CFunc = GarrysMod::Lua::CFunc;
@ -128,7 +130,8 @@ namespace lcpu::lua {
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_REG); LUA->PushSpecial(GarrysMod::Lua::SPECIAL_REG);
LUA->PushNumber(__lua_typeid); LUA->PushNumber(__lua_typeid);
LUA->SetField(-2, typeid_name.c_str()); LUA->SetField(-2, typeid_name.c_str());
LUA->Pop(); /* pop registry */ LUA->Pop(); // pop registry
// add in required metamethods
LUA_SET_C_FUNCTION(__gc) LUA_SET_C_FUNCTION(__gc)
LUA_SET_C_FUNCTION(__index) LUA_SET_C_FUNCTION(__index)
LUA_SET_C_FUNCTION(__newindex) LUA_SET_C_FUNCTION(__newindex)
@ -158,6 +161,10 @@ namespace lcpu::lua {
lua->ReferenceFree(tableReference); lua->ReferenceFree(tableReference);
} }
/// The LUA interface used to create this class.
GarrysMod::Lua::ILuaBase* lua;
private: private:
// base metamethods // base metamethods
LUA_MEMBER_FUNCTION(__gc) LUA_MEMBER_FUNCTION(__gc)
@ -184,7 +191,6 @@ namespace lcpu::lua {
// instance stuff // instance stuff
int tableReference { -1 }; int tableReference { -1 };
GarrysMod::Lua::ILuaBase* lua;
}; };
template <class TImpl> template <class TImpl>
@ -235,15 +241,18 @@ namespace lcpu::lua {
if(LUA->GetType(2) == GarrysMod::Lua::Type::String) { if(LUA->GetType(2) == GarrysMod::Lua::Type::String) {
auto key = GetLuaString(LUA, 2); auto key = GetLuaString(LUA, 2);
if(methods().find(key) != methods().end()) { // don't allow overwriting methods
if(methods().find(key) != methods().end())
return 0; return 0;
}
if(getters().find(key) != getters().end() && setters().find(key) == setters().end()) { // or read-only values
if(getters().find(key) != getters().end() && setters().find(key) == setters().end())
return 0; return 0;
}
if(setters().find(key) != setters().end()) { if(setters().find(key) != setters().end()) {
// for ergonomic sake only I kind of want to make this like
// LUA->Push(3) so that -1 (top of stack) is the value
// a bit cleaner. idk
setters()[key](LUA); setters()[key](LUA);
return 0; return 0;
} }