[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: New Lua C API Tutorial
- From: Tom N Harris <telliamed@...>
- Date: Mon, 31 Oct 2011 20:30:22 -0400
On 10/31/2011 06:48 AM, Patrick Rapin wrote:
In the "Functions" chapter, you give the following example :
int move_player (lua_Stack * ls)
{
const int xoffset = lua_tointeger(ls, 1);
const int yoffset = lua_tointeger(ls, 2);
lua_settop(ls, 0); //<-- I am talking about this line
... snip ...
You say it is good practice to maintain a nice clean stack and
recommend to use lua_settop(0). Well certainly not in such a
situation.
Not to mention that the function is assuming the stack has 2 valid
values on it. I think the properly defensive function would begin:
lua_settop(ls, 2); // expect two arguments
const int xoffset = lua_tointeger(ls, 1);
const int yoffset = lua_tointeger(ls, 2);
As you say, Lua will clean up the stack afterwards, so the function need
not pop the consumed values. And of course, if the arguments are
mandatory then you should also be checking for nil.
--
- tom
telliamed@whoopdedo.org