lcpu: Remove TestObject entirely

This commit is contained in:
Lily Tsuru 2023-07-27 16:38:01 -04:00
parent b5595cc996
commit 14614a72d3
4 changed files with 4 additions and 106 deletions

View File

@ -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
--[[

View File

@ -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<TestLuaObject> {
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<u32>(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

View File

@ -4,7 +4,6 @@
#include <GarrysMod/Lua/Interface.h>
#include <lucore/Logger.hpp>
#include <unordered_map>
#include "GarrysMod/Lua/LuaBase.h"
@ -29,44 +28,6 @@
return LUA->GetUserType<T>(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<T>(1, T::__lua_typeid); \
if(self != nullptr) { /* GetUserType returns nullptr on failure */ \
lucore::LogInfo("GCing {} object @ {:p}", #T, static_cast<void*>(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

View File

@ -1,6 +1,6 @@
#pragma once
//#include <functional>
#include <unordered_map>
#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<int(lua_State*)>;
//using ILuaFuncStd = std::function<int(GarrysMod::Lua::ILuaBase*)>;
//using ILuaVoidFuncStd = std::function<void(GarrysMod::Lua::ILuaBase*)>;
/// 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:
///