From f0807fc44dde14531759306317611bab87c8fccf Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 19 Oct 2020 09:13:58 +0800 Subject: +gamelab proj --- Runtime/Scripting/luax_ref.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Runtime/Scripting/luax_ref.cpp (limited to 'Runtime/Scripting/luax_ref.cpp') diff --git a/Runtime/Scripting/luax_ref.cpp b/Runtime/Scripting/luax_ref.cpp new file mode 100644 index 0000000..d4be775 --- /dev/null +++ b/Runtime/Scripting/luax_ref.cpp @@ -0,0 +1,72 @@ +#include "luax_vm.h" +#include "luax_ref.h" + +namespace Luax +{ + + LuaxRef::LuaxRef(int mode) + : mRefID(LUA_NOREF) + , mMode(mode) + { + } + + LuaxRef::~LuaxRef() + { + } + + LuaxRef::operator bool() + { + return (mRefID != LUA_NOREF); + } + + bool LuaxRef::PushRef(LuaxState& state) + { + assert(mRefID != LUA_NOREF); + + LuaxVM* vm = state.GetVM(); + if (!vm) return false; + if (mMode == STRONG_REF) + { + LuaxRefTable& table = vm->GetStrongRefTable(); + table.PushRef(state, mRefID); + } + else if (mMode == WEAK_REF) + { + LuaxRefTable& table = vm->GetWeakRefTable(); + table.PushRef(state, mRefID); + } + else + { + state.PushNil(); + return false; + } + return true; + } + + void LuaxRef::SetRef(LuaxState& state, int idx) + { + LuaxVM* vm = state.GetVM(); + if (!vm) return; + if (mMode == STRONG_REF) + { + LuaxRefTable& table = vm->GetStrongRefTable(); + mRefID = table.Ref(state, idx); + } + else if (mMode == WEAK_REF) + { + LuaxRefTable& table = vm->GetWeakRefTable(); + mRefID = table.Ref(state, idx); + } + } + + LuaxStrongRef::LuaxStrongRef() + : LuaxRef(STRONG_REF) + { + } + + LuaxWeakRef::LuaxWeakRef() + : LuaxRef(WEAK_REF) + { + } + +} -- cgit v1.1-26-g67d0