lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 1 November 2011 11:30, Tom N Harris <telliamed@whoopdedo.org> wrote:
> 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.
>

That is also rather incorrect usage
You ensure the arguments are there by using the luaL_check* functions.
the code above should be:

const int xoffset = luaL_checkint ( L , 1 );
const int yoffset = luaL_checkint ( L , 2 );

const Point p = player->move(xoffset, yoffset);

lua_pushinteger ( L , p.x );
lua_pushinteger ( L , p.y );
return 2;