1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef RZ_INCLUDE_LUA_HELPER_INLINE
18 #error "don't include directly, include lua_helper.h instead"
23 // CLuaStackChecker //
24 //////////////////////
26 //================================================================================
27 inline CLuaStackChecker::CLuaStackChecker(CLuaState
*state
, int numWantedResults
)
31 _FinalWantedSize
= state
->getTop() + numWantedResults
;
39 //================================================================================
40 inline void CLuaState::checkIndex(int index
)
42 // NB : more restrictive test that in the documentation there, because
43 // we don't expose the check stack function
44 nlassert( (index
!=0 && abs(index
) <= getTop())
45 || index
== LUA_REGISTRYINDEX
46 || index
== LUA_GLOBALSINDEX
50 //================================================================================
51 inline int CLuaState::getTop()
53 return lua_gettop(_State
);
56 //================================================================================
57 inline void CLuaState::setTop(int index
)
59 // if index is strictly negative, this is a "pop". ensure that the user don't pop more than allowed
64 // if index is positive and is more than the current top
65 else if ( index
>getTop() )
67 // must ensure that we have enough stack space
68 nlverify( lua_checkstack(_State
, index
-getTop()) );
72 lua_settop(_State
, index
);
75 //================================================================================
76 inline void CLuaState::pushValue(int index
)
79 lua_pushvalue(_State
, index
);
82 //================================================================================
83 inline void CLuaState::remove(int index
)
86 lua_remove(_State
, index
);
89 //================================================================================
90 inline void CLuaState::insert(int index
)
93 lua_insert(_State
, index
);
96 //================================================================================
97 inline void CLuaState::replace(int index
)
100 lua_replace(_State
, index
);
103 //================================================================================
104 inline void CLuaState::pop(int numElem
/* = 1 */)
106 nlassert(numElem
<= getTop());
107 lua_pop(_State
, numElem
);
110 //================================================================================
111 inline int CLuaState::type(int index
/* = -1 */)
114 return lua_type(_State
, index
);
117 //================================================================================
118 inline const char *CLuaState::getTypename(int type
)
120 return lua_typename(_State
, type
);
123 //================================================================================
124 inline bool CLuaState::isNil(int index
)
127 return lua_isnil(_State
, index
) != 0;
130 //================================================================================
131 inline bool CLuaState::isBoolean(int index
)
134 return lua_isboolean(_State
, index
) != 0;
137 //================================================================================
138 inline bool CLuaState::isNumber(int index
)
141 return lua_isnumber(_State
, index
) != 0;
144 //================================================================================
145 inline bool CLuaState::isString(int index
)
148 return lua_isstring(_State
, index
) != 0;
151 //================================================================================
152 inline bool CLuaState::isTable(int index
)
155 return lua_istable(_State
, index
) != 0;
158 //================================================================================
159 inline bool CLuaState::isFunction(int index
)
162 return lua_isfunction(_State
, index
) != 0;
165 //================================================================================
166 inline bool CLuaState::isCFunction(int index
)
169 return lua_iscfunction(_State
, index
) != 0;
172 //================================================================================
173 inline bool CLuaState::isUserData(int index
)
176 return lua_isuserdata(_State
, index
) != 0;
179 //================================================================================
180 inline bool CLuaState::isLightUserData(int index
)
183 return lua_islightuserdata(_State
, index
) != 0;
186 //================================================================================
187 inline bool CLuaState::toBoolean(int index
)
190 return lua_toboolean(_State
, index
) != 0;
193 //================================================================================
194 inline lua_Number
CLuaState::toNumber(int index
)
197 return lua_tonumber(_State
, index
);
200 //================================================================================
201 inline const char *CLuaState::toString(int index
)
204 return lua_tostring(_State
, index
);
207 //================================================================================
208 inline void CLuaState::toString(int index
, std::string
&str
)
211 const char *pc
= lua_tostring(_State
, index
);
218 //================================================================================
219 inline size_t CLuaState::strlen(int index
)
222 return lua_strlen(_State
, index
);
225 //================================================================================
226 inline lua_CFunction
CLuaState::toCFunction(int index
)
229 return lua_tocfunction(_State
, index
);
232 //================================================================================
233 inline void *CLuaState::toUserData(int index
)
236 return lua_touserdata(_State
, index
);
239 //================================================================================
240 inline const void *CLuaState::toPointer(int index
)
243 return lua_topointer(_State
, index
);
247 //================================================================================
248 inline void CLuaState::push(bool value
)
250 nlverify( lua_checkstack(_State
, 1) );
251 lua_pushboolean(_State
, (int) value
);
254 //================================================================================
255 inline void CLuaState::push(lua_Number value
)
257 nlverify( lua_checkstack(_State
, 1) );
258 lua_pushnumber(_State
, value
);
261 //================================================================================
262 inline void CLuaState::push(const char *str
)
264 nlverify( lua_checkstack(_State
, 1) );
265 lua_pushstring(_State
, str
);
269 //================================================================================
270 inline void CLuaState::push(const char *str
, int length
)
272 nlverify( lua_checkstack(_State
, 1) );
273 lua_pushlstring(_State
, str
, length
);
276 //================================================================================
277 inline void CLuaState::push(const std::string
&str
)
279 nlverify( lua_checkstack(_State
, 1) );
283 //================================================================================
284 inline void CLuaState::pushNil()
286 nlverify( lua_checkstack(_State
, 1) );
290 //================================================================================
291 inline void CLuaState::push(lua_CFunction f
)
293 nlverify( lua_checkstack(_State
, 1) );
294 lua_pushcfunction(_State
, f
);
297 //================================================================================
298 inline void CLuaState::pushLightUserData(void *ptr
)
300 nlverify( lua_checkstack(_State
, 1) );
301 lua_pushlightuserdata(_State
, ptr
);
304 //================================================================================
305 inline bool CLuaState::equal(int index1
, int index2
)
309 return lua_equal(_State
, index1
, index2
) != 0;
312 //================================================================================
313 inline bool CLuaState::getMetaTable(int index
)
316 return lua_getmetatable(_State
, index
) != 0;
319 //================================================================================
320 inline bool CLuaState::setMetaTable(int index
)
323 return lua_setmetatable(_State
, index
) != 0;
326 //================================================================================
327 inline bool CLuaState::rawEqual(int index1
, int index2
)
331 return lua_rawequal(_State
, index1
, index2
) != 0;
334 //================================================================================
335 inline bool CLuaState::lessThan(int index1
, int index2
)
339 return lua_lessthan(_State
, index1
, index2
) != 0;
343 //================================================================================
344 inline void CLuaState::concat(int numElem
)
346 nlassert(numElem
<= getTop());
347 lua_concat(_State
, numElem
);
350 //================================================================================
351 inline void CLuaState::getTable(int index
)
354 nlassert(isTable(index
) || isUserData(index
));
355 lua_gettable(_State
, index
);
358 //================================================================================
359 inline void CLuaState::rawGet(int index
)
362 lua_rawget(_State
, index
);
365 //================================================================================
366 inline void CLuaState::setTable(int index
)
369 nlassert(getTop() >= 2);
370 nlassert(isTable(index
));
371 lua_settable(_State
, index
);
374 //================================================================================
375 inline void CLuaState::rawSet(int index
)
378 lua_rawset(_State
, index
);
381 //================================================================================
382 inline bool CLuaState::next(int index
)
384 //H_AUTO(Lua_CLuaState_next)
386 return lua_next(_State
, index
) != 0;
389 //================================================================================
390 inline void CLuaState::rawSetI(int index
, int n
)
392 //H_AUTO(Lua_CLuaState_rawSetI)
394 lua_rawseti(_State
, index
, n
);
397 //================================================================================
398 inline void CLuaState::rawGetI(int index
, int n
)
401 lua_rawgeti(_State
, index
, n
);
404 //================================================================================
405 inline void CLuaState::call(int nargs
, int nresults
)
407 nlassert(getTop() >= nargs
);
408 lua_call(_State
, nargs
, nresults
);
411 //================================================================================
412 inline int CLuaState::pcall(int nargs
, int nresults
, int errfunc
)
414 return lua_pcall(_State
, nargs
, nresults
, errfunc
);
417 //================================================================================
418 inline void *CLuaState::newUserData(uint size
)
421 return lua_newuserdata(_State
, size
);