summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/LuaBindRef.cpp
blob: f6ff8a132890856d9b7a5933f5759574c9a1dcab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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)
	{
	}

}