From 14614a72d3fb6faf5c47ccbafadbe82a1bb65482 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Thu, 27 Jul 2023 16:38:01 -0400 Subject: [PATCH] lcpu: Remove TestObject entirely --- lua/autorun/lcpu_load.lua | 15 --------- native/projects/lcpu/src/LcpuGlobals.cpp | 42 ------------------------ native/projects/lcpu/src/LuaHelpers.hpp | 41 +---------------------- native/projects/lcpu/src/LuaObject.hpp | 12 ++----- 4 files changed, 4 insertions(+), 106 deletions(-) diff --git a/lua/autorun/lcpu_load.lua b/lua/autorun/lcpu_load.lua index 910b2fe..33d1665 100644 --- a/lua/autorun/lcpu_load.lua +++ b/lua/autorun/lcpu_load.lua @@ -10,21 +10,6 @@ if SERVER then return end ---[[ - testobj = LCPUNative.CreateTest() - print(testobj:Test()) - print(testobj.Variable) - - testobj.MemberVariable = 32.9 - print(testobj.MemberVariable) - - testobj.Variable = 32.1 -- this should fial - testobj.Test = nil; - - print(testobj.Variable) - - print(testobj.Name) -]] -- rapid iteration requires rapid solutions --[[ diff --git a/native/projects/lcpu/src/LcpuGlobals.cpp b/native/projects/lcpu/src/LcpuGlobals.cpp index 0b95e69..29448b9 100644 --- a/native/projects/lcpu/src/LcpuGlobals.cpp +++ b/native/projects/lcpu/src/LcpuGlobals.cpp @@ -1,43 +1,9 @@ #include "LcpuGlobals.hpp" #include "GarrysMod/Lua/Interface.h" -#include "GarrysMod/Lua/LuaBase.h" #include "LuaCpu.hpp" #include "LuaDevice.hpp" -#include "LuaHelpers.hpp" -#include "LuaObject.hpp" - -/// test for the "new" lua object system -struct TestLuaObject : public lcpu::lua::LuaObject { - constexpr static const char* Name() { return "TestLuaObject"; } - - static void RegisterClass(GarrysMod::Lua::ILuaBase* LUA) { - RegisterClassStart(LUA); - - // Register methods. Maybe later I'll do some crazy template stuff; for now this is pretty barebones. - RegisterMethod("Test", Test); - RegisterGetter("Variable", [](GarrysMod::Lua::ILuaBase* LUA) { LUA->PushNumber(32.6); }); - RegisterGetter("MemberVariable", [](GarrysMod::Lua::ILuaBase* LUA) { - auto self = TestLuaObject::FromLua(LUA, 1); - LUA->PushNumber(self->n); - }); - RegisterSetter("MemberVariable", [](GarrysMod::Lua::ILuaBase* LUA) { - // The value of a setter is placed at the top of the stack by LuaObject - auto self = TestLuaObject::FromLua(LUA, 1); - self->n = LUA->GetNumber(-1); - }); - } - - LUA_MEMBER_FUNCTION(Test); - double n; -}; - -LUA_MEMBER_FUNCTION_IMPLEMENT(TestLuaObject, Test) { - LUA->PushString("hi :)"); - return 1; -} - LUA_FUNCTION(LCPUNative_CreateCPU) { LUA->CheckType(1, GarrysMod::Lua::Type::Number); auto memorySize = static_cast(LUA->GetNumber(1)); @@ -59,15 +25,9 @@ LUA_FUNCTION(LCPUNative_CreateDevice) { return 1; } - LUA_FUNCTION(LCPUNative_CreateTest) { - TestLuaObject::Create(LUA); - return 1; -} - void GlobalsBind(GarrysMod::Lua::ILuaBase* LUA) { LuaCpu::RegisterClass(LUA); LuaDevice::RegisterClass(LUA); - TestLuaObject::RegisterClass(LUA); // clang-format off LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB); @@ -77,8 +37,6 @@ void GlobalsBind(GarrysMod::Lua::ILuaBase* LUA) { LUA_SET_C_FUNCTION_NAME(LCPUNative_CreateCPU, "CreateCPU"); LUA_SET_C_FUNCTION_NAME(LCPUNative_CreateDevice, "CreateDevice"); - - LUA_SET_C_FUNCTION_NAME(LCPUNative_CreateTest, "CreateTest"); LUA->SetField(-2, "LCPUNative"); LUA->Pop(); // clang-format on diff --git a/native/projects/lcpu/src/LuaHelpers.hpp b/native/projects/lcpu/src/LuaHelpers.hpp index 5629e08..d61d36f 100644 --- a/native/projects/lcpu/src/LuaHelpers.hpp +++ b/native/projects/lcpu/src/LuaHelpers.hpp @@ -4,7 +4,6 @@ #include #include -#include #include "GarrysMod/Lua/LuaBase.h" @@ -29,44 +28,6 @@ return LUA->GetUserType(stackPos, T::__lua_typeid); \ } -// This class binding package always implements the __gc metamethod -// to free any C++ object bound to Lua. - -// Declare required binding variables. -#define LUA_CLASS_BIND_VARIABLES(ACCESS_LEVEL) \ - public: \ - static int __lua_typeid; \ - ACCESS_LEVEL: \ - LUA_MEMBER_FUNCTION(__gc); - -// Implement required binding variables (typically in a .cpp file). -#define LUA_CLASS_BIND_VARIABLES_IMPLEMENT(T) \ - int T::__lua_typeid = 0; \ - LUA_MEMBER_FUNCTION_IMPLEMENT(T, __gc) { \ - LUA->CheckType(1, T::__lua_typeid); \ - auto self = LUA->GetUserType(1, T::__lua_typeid); \ - if(self != nullptr) { /* GetUserType returns nullptr on failure */ \ - lucore::LogInfo("GCing {} object @ {:p}", #T, static_cast(self)); \ - delete self; \ - } \ - return 0; \ - } - -// Begin the Bind() method of a class. This just sets up boilerplate -// and required things to setup a class. -#define LUA_CLASS_BIND_BEGIN(T) \ - T::__lua_typeid = LUA->CreateMetaTable(#T); \ - LUA->PushSpecial(GarrysMod::Lua::SPECIAL_REG); \ - LUA->PushNumber(T::__lua_typeid); \ - LUA->SetField(-2, #T "__typeid"); \ - LUA->Pop(); /* pop registry */ \ - LUA->Push(-1); \ - LUA->SetField(-2, "__index"); \ - LUA_SET_C_FUNCTION(__gc) - -// End the Bind() method. -#define LUA_CLASS_BIND_END() LUA->Pop(); - // Set a C function as a field. #define LUA_SET_C_FUNCTION(name) \ LUA->PushCFunction(name); \ @@ -88,5 +49,5 @@ namespace lcpu::lua { return {}; } } - + } // namespace lcpu::lua diff --git a/native/projects/lcpu/src/LuaObject.hpp b/native/projects/lcpu/src/LuaObject.hpp index dceee83..4c8599f 100644 --- a/native/projects/lcpu/src/LuaObject.hpp +++ b/native/projects/lcpu/src/LuaObject.hpp @@ -1,6 +1,6 @@ #pragma once -//#include +#include #include "GarrysMod/Lua/Interface.h" #include "GarrysMod/Lua/LuaBase.h" @@ -16,14 +16,8 @@ namespace lcpu::lua { using ILuaFunc = int (*)(GarrysMod::Lua::ILuaBase*); using ILuaVoidFunc = void (*)(GarrysMod::Lua::ILuaBase*); - //using CFuncStd = std::function; - //using ILuaFuncStd = std::function; - //using ILuaVoidFuncStd = std::function; - /// Register a C++ method. - static void RegisterMethod(const std::string& name, CFunc func) { - methods()[name] = func; - } + static void RegisterMethod(const std::string& name, CFunc func) { methods()[name] = func; } /// Register a getter for a value to be read. static void RegisterGetter(const std::string& name, ILuaVoidFunc func) { getters()[name] = func; } @@ -65,7 +59,7 @@ namespace lcpu::lua { // clang-format on } - /// Register a metafunction. + /// Register a metafunction. /// Note that the following metafunctions are reserved by the implementation /// of this object, and should not be overwritten: ///