summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/LuaBindRef.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-11-15 09:59:32 +0800
committerchai <chaifix@163.com>2020-11-15 09:59:32 +0800
commitd2e4b2839bc7ce874370ff4c52dcfdadf666ff52 (patch)
treeb83e34c2d66cf80b94af5c9a420b6c9c206c138c /Runtime/Scripting/LuaBindRef.cpp
parent64f89347883d519b202711a11d2ea175bd80be37 (diff)
+lua binding
Diffstat (limited to 'Runtime/Scripting/LuaBindRef.cpp')
-rw-r--r--Runtime/Scripting/LuaBindRef.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/Runtime/Scripting/LuaBindRef.cpp b/Runtime/Scripting/LuaBindRef.cpp
new file mode 100644
index 0000000..f6ff8a1
--- /dev/null
+++ b/Runtime/Scripting/LuaBindRef.cpp
@@ -0,0 +1,72 @@
+#include "LuaBindVM.h"
+#include "LuaBindRef.h"
+
+namespace LuaBind
+{
+
+ LuaBindRef::LuaBindRef(int mode)
+ : mRefID(LUA_NOREF)
+ , mMode(mode)
+ {
+ }
+
+ LuaBindRef::~LuaBindRef()
+ {
+ }
+
+ LuaBindRef::operator bool()
+ {
+ return (mRefID != LUA_NOREF);
+ }
+
+ bool LuaBindRef::PushRef(LuaBindState& state)
+ {
+ assert(mRefID != LUA_NOREF);
+
+ LuaBindVM* vm = state.GetVM();
+ if (!vm) return false;
+ if (mMode == STRONG_REF)
+ {
+ LuaBindRefTable& table = vm->GetStrongRefTable();
+ table.PushRef(state, mRefID);
+ }
+ else if (mMode == WEAK_REF)
+ {
+ LuaBindRefTable& table = vm->GetWeakRefTable();
+ table.PushRef(state, mRefID);
+ }
+ else
+ {
+ state.PushNil();
+ return false;
+ }
+ return true;
+ }
+
+ void LuaBindRef::SetRef(LuaBindState& state, int idx)
+ {
+ LuaBindVM* vm = state.GetVM();
+ if (!vm) return;
+ if (mMode == STRONG_REF)
+ {
+ LuaBindRefTable& table = vm->GetStrongRefTable();
+ mRefID = table.Ref(state, idx);
+ }
+ else if (mMode == WEAK_REF)
+ {
+ LuaBindRefTable& table = vm->GetWeakRefTable();
+ mRefID = table.Ref(state, idx);
+ }
+ }
+
+ LuaBindStrongRef::LuaBindStrongRef()
+ : LuaBindRef(STRONG_REF)
+ {
+ }
+
+ LuaBindWeakRef::LuaBindWeakRef()
+ : LuaBindRef(WEAK_REF)
+ {
+ }
+
+}