lua-users home
lua-l archive

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


Peter Shook wrote:
> 
> But with lua_settop you get a behavior that is the same as that of Lua
> functions without changing the API.  And how does the argument handling
> function 'luaL_check_type' stop working in this example?
> 
> static int luaB_eventtable (lua_State *L) {
>   luaL_check_type(L, 1, LUA_TTABLE);
>   if (lua_isnull(L, 2))
>     lua_geteventtable(L, 1);
>   else {
>     lua_settop(L, 2);
>     luaL_check_type(L, 2, LUA_TTABLE);
>     lua_seteventtable(L, 1);
>   }
>   return 1;
> }

This function does _not_ behave like a Lua function[1]: eventtable(x)
gives a different result than eventtable(x,nil).  You want lua_settop as
the first statement of the function.  luaL_check_type will then break if
used with LUA_TNONE as would lua_isnull or all the luaL_opt_xxx functions.

Ciao, ET.


[1] unless you implement it with varargs (function eventable(x, ...))
which would be counterintuitive.