diff options
author | chai <chaifix@163.com> | 2021-10-17 23:05:01 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-17 23:05:01 +0800 |
commit | 7c8c68d79343d04be382334c15a73d079450857c (patch) | |
tree | 9aaacc042f0b7eeb4123c07dcc5f49c14fd8026c /Runtime/LuaBind/LuaBindRef.cpp | |
parent | 6e73ca6ada8a41692809dae5db89c8db0675ce1e (diff) |
*misc
Diffstat (limited to 'Runtime/LuaBind/LuaBindRef.cpp')
-rw-r--r-- | Runtime/LuaBind/LuaBindRef.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Runtime/LuaBind/LuaBindRef.cpp b/Runtime/LuaBind/LuaBindRef.cpp new file mode 100644 index 0000000..00a65d0 --- /dev/null +++ b/Runtime/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) + { + } + +} |