From 4538cd182d05052f118f770a0e2f6344d41ce61b Mon Sep 17 00:00:00 2001 From: modeco80 Date: Thu, 27 Jul 2023 07:21:04 -0400 Subject: [PATCH] lcpu: more cleanup. this is the last one I promise, I need sleep --- native/projects/lcpu/src/LuaHelpers.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/native/projects/lcpu/src/LuaHelpers.hpp b/native/projects/lcpu/src/LuaHelpers.hpp index 78f1fd8..a8229fb 100644 --- a/native/projects/lcpu/src/LuaHelpers.hpp +++ b/native/projects/lcpu/src/LuaHelpers.hpp @@ -89,6 +89,8 @@ namespace lcpu::lua { } } + /// A CRTP-based class which allows binding C++ to Lua, in a + /// fairly sensible manner. template struct LuaObject { using CFunc = GarrysMod::Lua::CFunc; @@ -128,7 +130,8 @@ namespace lcpu::lua { LUA->PushSpecial(GarrysMod::Lua::SPECIAL_REG); LUA->PushNumber(__lua_typeid); 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(__index) LUA_SET_C_FUNCTION(__newindex) @@ -158,6 +161,10 @@ namespace lcpu::lua { lua->ReferenceFree(tableReference); } + + /// The LUA interface used to create this class. + GarrysMod::Lua::ILuaBase* lua; + private: // base metamethods LUA_MEMBER_FUNCTION(__gc) @@ -184,7 +191,6 @@ namespace lcpu::lua { // instance stuff int tableReference { -1 }; - GarrysMod::Lua::ILuaBase* lua; }; template @@ -235,15 +241,18 @@ namespace lcpu::lua { if(LUA->GetType(2) == GarrysMod::Lua::Type::String) { auto key = GetLuaString(LUA, 2); - if(methods().find(key) != methods().end()) { + // don't allow overwriting methods + if(methods().find(key) != methods().end()) 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; - } 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); return 0; }