summaryrefslogtreecommitdiff
path: root/Source/3rdParty/Luax/luax_state.inl
blob: c9a95f6ffe2717f9ce6d3c67a2aacfbc16b638aa (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace Luax
{

	///
	/// עṤΪעinterface tableclass tabletype nameΪƿռϡע׶βԪȵNew
	/// õʱŻᡣ
	/// 
	template<typename T>
	void LuaxState::RegisterFactory()
	{
		lua_State* L = mState;
		LuaxState& state = *this;

		int top = lua_gettop(L); // namespace table
		assert(lua_istable(L, top));

		// 1) interface table
		lua_newtable(L);
		LuaxClass<T>::RegisterLuaxInterface(state);
		T::RegisterLuaxInterface(state);

		// interface table[__index] = interface table
		lua_pushvalue(L, -1);
		lua_setfield(L, -2, "__index");

		LuaxClass<T>::SetLuaxInterfaceTableRef(state, -1);

		lua_settop(L, top);

		// 2) class table
		lua_newtable(L);
		LuaxClass<T>::RegisterLuaxClass(state);
		LuaxClass<T>::RegisterLuaxFactoryClass(state);
		T::RegisterLuaxClass(state);

		// TǷûעķ
	#define _assertmethod(I, NAME) \
		GetField(I, NAME); \
		assert(IsType(-1, LUA_TFUNCTION)); \
		Pop();

		_assertmethod(-1, "New");

	#undef _assertmethod

		// .Extend()
		lua_pushvalue(state, -1); // class table
		LuaxClass<T>::PushLuaxInterfaceTable(state); // interface table
		lua_pushcclosure(state, LuaxClass<T>::l_ExtendFactory, 2);
		lua_setfield(state, -2, "Extend");

		// .GetInterfaceTable()
		LuaxClass<T>::PushLuaxInterfaceTable(state); // interface table
		lua_pushcclosure(state, LuaxClass<T>::l_GetInterfaceTable, 1);
		lua_setfield(state, -2, "GetInterfaceTable");

		LuaxClass<T>::SetLuaxClassTableRef(state, -1);

		cc8* type = T::GetLuaxFactoryName();
		SetField(top, type);

		// reset top
		lua_settop(L, top);

		// 
		T::RegisterLuaxPostprocess(state);
	}

	///
	/// Singletonֻһclass tableûinterface table
	///
	template<typename T>
	void LuaxState::RegisterSingleton()
	{
		lua_State* L = mState;
		LuaxState& state = *this;

		int top = lua_gettop(L); // namespace table
		assert(lua_istable(L, top));

		// class table.
		lua_newtable(L);
		LuaxClass<T>::RegisterLuaxClass(state);
		LuaxClass<T>::RegisterLuaxFactoryClass(state);
		T::RegisterLuaxClass(state);

		LuaxClass<T>::SetLuaxClassTableRef(state, -1);

		// class table__index__newindexָ
		lua_pushvalue(state, -1);
		lua_setfield(state, -2, "__index");
		lua_pushvalue(state, -1);
		lua_setfield(state, -2, "__newindex");

		cc8* type = T::GetLuaxSingletonName();
		SetField(top, type);

		// reset top
		lua_settop(L, top);

		// 
		T::RegisterLuaxPostprocess(state);
	}

	template<typename T>
	void LuaxState::SetField(int idx, cc8* key, T value)
	{
		if (IsTableOrUserdata(idx))
		{
			idx = AbsIndex(idx);
			this->Push(value);
			lua_setfield(mState, idx, key);
		}
	}

	template<typename T>
	T LuaxState::GetField(int idx, cc8* key, T value)
	{
		GetField(idx, key);
		T result = GetValue < T >(-1, value);
		this->Pop();

		return result;
	}

	template<typename T>
	T LuaxState::GetField(int idx, int key, T value)
	{
		GetField(idx, key);
		T result = GetValue < T >(-1, value);
		Pop();

		return result;
	}

	template<typename T>
	T* LuaxState::GetLuaUserdata(int idx)
	{
		void* p = nullptr;

		if (IsType(idx, LUA_TUSERDATA))
		{
			p = *(void**)lua_touserdata(mState, idx);
		}

		return static_cast<T*>(p);
	}

}