diff options
author | chai <chaifix@163.com> | 2021-10-18 19:56:41 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-18 19:56:41 +0800 |
commit | 45328cbadd8a946c19a77301f218efbf650e2f28 (patch) | |
tree | 8ec4f3a9737b2cbb9744f8347a56783743be2a4c /Runtime/Lua/LuaBind/LuaBindRef.cpp | |
parent | b5702ece4c2cf751c252e76fb885a7ec41ccabe8 (diff) |
*misc
Diffstat (limited to 'Runtime/Lua/LuaBind/LuaBindRef.cpp')
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindRef.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBindRef.cpp b/Runtime/Lua/LuaBind/LuaBindRef.cpp new file mode 100644 index 0000000..00a65d0 --- /dev/null +++ b/Runtime/Lua/LuaBind/LuaBindRef.cpp @@ -0,0 +1,72 @@ +#include "LuaBindVM.h" +#include "LuaBindRef.h" + +namespace LuaBind +{ + + Ref::Ref(int mode) + : mRefID(LUA_NOREF) + , mMode(mode) + { + } + + Ref::~Ref() + { + } + + Ref::operator bool() + { + return (mRefID != LUA_NOREF); + } + + bool Ref::PushRef(State& state) + { + assert(mRefID != LUA_NOREF); + + VM* vm = state.GetVM(); + if (!vm) return false; + if (mMode == STRONG_REF) + { + RefTable& table = vm->GetStrongRefTable(); + table.PushRef(state, mRefID); + } + else if (mMode == WEAK_REF) + { + RefTable& table = vm->GetWeakRefTable(); + table.PushRef(state, mRefID); + } + else + { + state.PushNil(); + return false; + } + return true; + } + + void Ref::SetRef(State& state, int idx) + { + VM* vm = state.GetVM(); + if (!vm) return; + if (mMode == STRONG_REF) + { + RefTable& table = vm->GetStrongRefTable(); + mRefID = table.Ref(state, idx); + } + else if (mMode == WEAK_REF) + { + RefTable& table = vm->GetWeakRefTable(); + mRefID = table.Ref(state, idx); + } + } + + StrongRef::StrongRef() + : Ref(STRONG_REF) + { + } + + WeakRef::WeakRef() + : Ref(WEAK_REF) + { + } + +} |