1818
1919namespace UnLua
2020{
21- /* *
22- * Lua stack index wrapper
23- */
24- FLuaIndex::FLuaIndex (FLuaEnv* Env, int32 InIndex)
25- : Env(Env)
21+ FLuaIndex::FLuaIndex (lua_State* InL, int32 InIndex)
22+ : L(InL), Index(LowLevel::AbsIndex(InL, InIndex))
2623 {
27- if (InIndex < 0 && InIndex > LUA_REGISTRYINDEX)
28- {
29- const auto L = Env->GetMainState ();
30- int32 Top = lua_gettop (L);
31- Index = Top + InIndex + 1 ;
32- }
33- else
34- {
35- Index = InIndex;
36- }
3724 }
3825
39- /* *
40- * Generic Lua value
41- */
26+ FLuaIndex::FLuaIndex (FLuaEnv* Env, int32 Index)
27+ : FLuaIndex(Env->GetMainState (), Index)
28+ {
29+ }
30+
31+ FLuaValue::FLuaValue (lua_State* L, int32 Index)
32+ : FLuaIndex(L, Index), Type(lua_type(L, Index))
33+ {
34+ }
35+
36+ FLuaValue::FLuaValue (lua_State* L, int32 Index, int32 Type)
37+ : FLuaIndex(L, Index), Type(Type)
38+ {
39+ check (lua_type (L, Index) == Type);
40+ }
41+
4242 FLuaValue::FLuaValue (FLuaEnv* Env, int32 Index)
43- : FLuaIndex (Env, Index)
43+ : FLuaValue (Env-> GetMainState () , Index)
4444 {
45- const auto L = Env->GetMainState ();
46- Type = lua_type (L, Index);
4745 }
4846
4947 FLuaValue::FLuaValue (FLuaEnv* Env, int32 Index, int32 Type)
50- : FLuaIndex(Env, Index), Type(Type)
48+ : FLuaValue(Env->GetMainState (), Index, Type)
49+ {
50+ }
51+
52+ FLuaTable::FLuaTable (lua_State* L, int32 Index)
53+ : FLuaIndex(L, Index), PushedValues(0 )
54+ {
55+ }
56+
57+ FLuaTable::FLuaTable (lua_State* L, FLuaValue Value)
58+ : FLuaIndex(L, Value.GetIndex()), PushedValues(0 )
5159 {
52- const auto L = Env->GetMainState ();
53- check (lua_type (L, Index) == Type);
5460 }
5561
56- /* *
57- * Lua table wrapper
58- */
5962 FLuaTable::FLuaTable (FLuaEnv* Env, int32 Index)
60- : FLuaIndex (Env, Index ), PushedValues( 0 )
63+ : FLuaTable (Env-> GetMainState ( ), Index )
6164 {
62- const auto L = Env->GetMainState ();
6365 check (lua_type (L, Index) == LUA_TTABLE);
6466 }
6567
@@ -78,94 +80,81 @@ namespace UnLua
7880 {
7981 if (PushedValues)
8082 {
81- const auto L = Env->GetMainState ();
8283 lua_pop (L, PushedValues);
8384 PushedValues = 0 ;
8485 }
8586 }
8687
8788 int32 FLuaTable::Length () const
8889 {
89- const auto L = Env->GetMainState ();
9090 return lua_rawlen (L, Index);
9191 }
9292
9393 FLuaValue FLuaTable::operator [](int32 i) const
9494 {
95- const auto L = Env->GetMainState ();
9695 lua_pushinteger (L, i);
9796 lua_gettable (L, Index);
9897 ++PushedValues;
99- return FLuaValue (Env , -1 );
98+ return FLuaValue (L , -1 );
10099 }
101100
102101 FLuaValue FLuaTable::operator [](int64 i) const
103102 {
104- const auto L = Env->GetMainState ();
105103 lua_pushinteger (L, i);
106104 lua_gettable (L, Index);
107105 ++PushedValues;
108- return FLuaValue (Env , -1 );
106+ return FLuaValue (L , -1 );
109107 }
110108
111109 FLuaValue FLuaTable::operator [](double d) const
112110 {
113- const auto L = Env->GetMainState ();
114111 lua_pushnumber (L, d);
115- int32 Type = lua_gettable (L, Index);
112+ lua_gettable (L, Index);
116113 ++PushedValues;
117- return FLuaValue (Env , -1 );
114+ return FLuaValue (L , -1 );
118115 }
119116
120- FLuaValue FLuaTable::operator [](const char * s) const
117+ FLuaValue FLuaTable::operator [](const char * s) const
121118 {
122- const auto L = Env->GetMainState ();
123119 lua_pushstring (L, s);
124- int32 Type = lua_gettable (L, Index);
120+ lua_gettable (L, Index);
125121 ++PushedValues;
126- return FLuaValue (Env , -1 );
122+ return FLuaValue (L , -1 );
127123 }
128124
129- FLuaValue FLuaTable::operator [](const void * p) const
125+ FLuaValue FLuaTable::operator [](const void * p) const
130126 {
131- const auto L = Env->GetMainState ();
132127 lua_pushlightuserdata (L, (void *)p);
133- int32 Type = lua_gettable (L, Index);
128+ lua_gettable (L, Index);
134129 ++PushedValues;
135- return FLuaValue (Env , -1 );
130+ return FLuaValue (L , -1 );
136131 }
137132
138133 FLuaValue FLuaTable::operator [](FLuaIndex StackIndex) const
139134 {
140- const auto L = Env->GetMainState ();
141135 lua_pushvalue (L, StackIndex.GetIndex ());
142- int32 Type = lua_gettable (L, Index);
136+ lua_gettable (L, Index);
143137 ++PushedValues;
144- return FLuaValue (Env , -1 );
138+ return FLuaValue (L , -1 );
145139 }
146140
147141 FLuaValue FLuaTable::operator [](FLuaValue Key) const
148142 {
149- const auto L = Env->GetMainState ();
150143 lua_pushvalue (L, Key.GetIndex ());
151144 int32 Type = lua_gettable (L, Index);
152145 ++PushedValues;
153- return FLuaValue (Env , Type);
146+ return FLuaValue (L , Type);
154147 }
155148
156- /* *
157- * Lua function wrapper
158- */
159- FLuaFunction::FLuaFunction (FLuaEnv* Env, const char *GlobalFuncName)
160- : Env(Env), FunctionRef(LUA_REFNIL)
149+ FLuaFunction::FLuaFunction (lua_State* L, const char * GlobalFuncName)
150+ : L(L), FunctionRef(LUA_REFNIL)
161151 {
162152 if (!GlobalFuncName)
163153 {
164154 return ;
165155 }
166156
167157 // find global function and create a reference for the function
168- const auto L = Env->GetMainState ();
169158 int32 Type = lua_getglobal (L, GlobalFuncName);
170159 if (Type == LUA_TFUNCTION)
171160 {
@@ -178,16 +167,15 @@ namespace UnLua
178167 }
179168 }
180169
181- FLuaFunction::FLuaFunction (FLuaEnv* Env , const char * GlobalTableName, const char * FuncName)
182- : Env(Env ), FunctionRef(LUA_REFNIL)
170+ FLuaFunction::FLuaFunction (lua_State* L , const char * GlobalTableName, const char * FuncName)
171+ : L(L ), FunctionRef(LUA_REFNIL)
183172 {
184173 if (!GlobalTableName || !FuncName)
185174 {
186175 return ;
187176 }
188177
189178 // find a function in a global table and create a reference for the function
190- const auto L = Env->GetMainState ();
191179 int32 Type = lua_getglobal (L, GlobalTableName);
192180 if (Type == LUA_TTABLE)
193181 {
@@ -210,11 +198,10 @@ namespace UnLua
210198 }
211199 }
212200
213- FLuaFunction::FLuaFunction (FLuaEnv* Env , FLuaTable Table, const char * FuncName)
214- : Env(Env ), FunctionRef(LUA_REFNIL)
201+ FLuaFunction::FLuaFunction (lua_State* L , FLuaTable Table, const char * FuncName)
202+ : L(L ), FunctionRef(LUA_REFNIL)
215203 {
216204 // find a function in a table and create a reference for the function
217- const auto L = Env->GetMainState ();
218205 lua_pushstring (L, FuncName);
219206 int32 Type = lua_gettable (L, Table.GetIndex ());
220207 if (Type == LUA_TFUNCTION)
@@ -228,48 +215,68 @@ namespace UnLua
228215 }
229216 }
230217
231- FLuaFunction::FLuaFunction (FLuaEnv* Env , FLuaValue Value)
232- : Env(Env ), FunctionRef(LUA_REFNIL)
218+ FLuaFunction::FLuaFunction (lua_State* L , FLuaValue Value)
219+ : L(L ), FunctionRef(LUA_REFNIL)
233220 {
234221 // create a reference for the Generic Lua value if it's a function
235222 int32 Type = Value.GetType ();
236223 if (Type == LUA_TFUNCTION)
237224 {
238- const auto L = Env->GetMainState ();
239225 lua_pushvalue (L, Value.GetIndex ());
240226 FunctionRef = luaL_ref (L, LUA_REGISTRYINDEX);
241227 }
242228 }
243229
230+ FLuaFunction::FLuaFunction (FLuaEnv* Env, const char * GlobalFuncName)
231+ : FLuaFunction(Env->GetMainState (), GlobalFuncName)
232+ {
233+ }
234+
235+ FLuaFunction::FLuaFunction (FLuaEnv* Env, const char * GlobalTableName, const char * FuncName)
236+ : FLuaFunction(Env->GetMainState (), GlobalTableName, FuncName)
237+ {
238+ }
239+
240+ FLuaFunction::FLuaFunction (FLuaEnv* Env, FLuaTable Table, const char * FuncName)
241+ : FLuaFunction(Env->GetMainState (), Table, FuncName)
242+ {
243+ }
244+
245+ FLuaFunction::FLuaFunction (FLuaEnv* Env, FLuaValue Value)
246+ : FLuaFunction(Env->GetMainState (), Value)
247+ {
248+ }
249+
244250 FLuaFunction::~FLuaFunction ()
245251 {
246252 // releases reference for the function
247253 if (FunctionRef != LUA_REFNIL)
248254 {
249- const auto L = Env->GetMainState ();
250255 luaL_unref (L, LUA_REGISTRYINDEX, FunctionRef);
251256 }
252257 }
253258
254- /* *
255- * Lua function return values
256- */
257- FLuaRetValues::FLuaRetValues (FLuaEnv* Env, int32 NumResults)
258- : Env(Env), bValid(NumResults > -1 )
259+ FLuaRetValues::FLuaRetValues (lua_State* L, int32 NumResults)
260+ : L(L), bValid(NumResults > -1 )
259261 {
260262 if (NumResults > 0 )
261263 {
262264 Values.Reserve (NumResults);
263265 for (int32 i = 0 ; i < NumResults; ++i)
264266 {
265- Values.Add (FLuaValue (Env , i - NumResults));
267+ Values.Add (FLuaValue (L , i - NumResults));
266268 }
267269 }
268270 }
269271
272+ FLuaRetValues::FLuaRetValues (FLuaEnv* Env, int32 NumResults)
273+ : L(Env->GetMainState ()), bValid(NumResults > -1 )
274+ {
275+ }
276+
270277 // move constructor, and disable copy constructor
271- FLuaRetValues::FLuaRetValues (FLuaRetValues && Src)
272- : Env (Src.Env ), Values(MoveTemp(Src.Values)), bValid(Src.bValid)
278+ FLuaRetValues::FLuaRetValues (FLuaRetValues&& Src)
279+ : L (Src.L ), Values(MoveTemp(Src.Values)), bValid(Src.bValid)
273280 {
274281 check (true );
275282 }
@@ -283,7 +290,6 @@ namespace UnLua
283290 {
284291 if (Values.Num () > 0 )
285292 {
286- const auto L = Env->GetMainState ();
287293 lua_pop (L, Values.Num ());
288294 Values.Empty ();
289295 }
0 commit comments