[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: New Lua C API Tutorial
- From: Patrick Rapin <toupie300@...>
- Date: Mon, 31 Oct 2011 11:48:41 +0100
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
const Point p = player->move(xoffset, yoffset);
lua_pushinteger(ls, p.x);
lua_pushinteger(ls, p.y);
return 2;
}
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.
Lua API is designed so that it is useless to pop arguments from the
stack: the return value already tells how many results there are
inside the stack.
The interpreter will automatically perform the equivalent of
lua_settop(0) after the call.
Worse: in some situations, it can yield to weird bugs.
If an argument is retrieved with lua_tostring, flushing the stack
could make the garbage collector to delete the content of the string,
yielding the const char* argument pointing to invalid memory!