diff options
author | chai <chaifix@163.com> | 2021-10-22 23:59:54 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-22 23:59:54 +0800 |
commit | 4dafefe46a72ba47468b13d011f8299055081b0f (patch) | |
tree | 2a85835ec4d74fecf3815397b384cefe39d31f56 /Runtime/Lua/LuaBind/LuaBindRef.cpp | |
parent | 1f18d2afec632aa9361079ca3bcb5a7f2d73db3a (diff) |
*LuaBind
Diffstat (limited to 'Runtime/Lua/LuaBind/LuaBindRef.cpp')
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindRef.cpp | 72 |
1 files changed, 47 insertions, 25 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBindRef.cpp b/Runtime/Lua/LuaBind/LuaBindRef.cpp index 676525a..b2f8e1c 100644 --- a/Runtime/Lua/LuaBind/LuaBindRef.cpp +++ b/Runtime/Lua/LuaBind/LuaBindRef.cpp @@ -4,16 +4,16 @@ namespace LuaBind { - Ref::Ref(RefMode mode) + Ref::Ref(LuaBind::VM* vm, RefMode mode) : mRefID(LUA_NOREF) , mMode(mode) + , mOwner(vm) { } Ref::~Ref() { // 自动释放引用 - } Ref::operator bool() @@ -21,11 +21,49 @@ namespace LuaBind return (mRefID != LUA_NOREF); } - bool Ref::PushRef(State& state) + void Ref::UnRef() + { + if (mRefID == LUA_NOREF) + return; + VM* vm = mOwner; + if (!vm) return; + if (mMode == STRONG_REF) + { + RefTable& table = vm->GetStrongRefTable(); + table.UnRef(mRefID); + mRefID = LUA_NOREF; + } + else if (mMode == WEAK_REF) + { + RefTable& table = vm->GetWeakRefTable(); + table.UnRef(mRefID); + mRefID = LUA_NOREF; + } + } + + void Ref::SetRef(LuaBind::State& state, int idx) + { + assert(state.GetVM() == mOwner); + + VM* vm = mOwner; + 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); + } + } + + bool Ref::PushRef(LuaBind::State& state) { assert(mRefID != LUA_NOREF); - VM* vm = state.GetVM(); + VM* vm = mOwner; if (!vm) return false; if (mMode == STRONG_REF) { @@ -39,35 +77,19 @@ namespace LuaBind } else { - state.PushNil(); + 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) + StrongRef::StrongRef(LuaBind::VM* vm) + : Ref(vm, STRONG_REF) { } - WeakRef::WeakRef() - : Ref(WEAK_REF) + WeakRef::WeakRef(LuaBind::VM* vm) + : Ref(vm, WEAK_REF) { } |