lua-users home
lua-l archive

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


On Mon, Aug 14, 2006 at 05:09:53AM -0700, Vegetable wrote:
> Vegetable wrote:

c'est vraiment drole. :-)

> > 1) Sometimes calling my C-function from my lua script, the arguments given
> > in my script are 'lost' : i call tex_draw(1,2,3,4) and i receive in my C-
> > function (1,None,3,4).
> > 
> 
> I add some info :
> 
> the C-function is :
> static int tex_draw (lua_State *L)
> {
>     int n = lua_gettop(L);    /* number of arguments */
>     if (n!=4)
>     {
>         writeStack(L);
>         lua_pushstring(L, "mauvais nombre d'arguments passés à 'tex_draw'");
>         lua_error(L);
>     }
>     for (int i=1;i<=4;i++)
>         if (!lua_isnumber(L, i))

lua_isnumber() has side-effects, it will convert the argument to a
number if it isn't, and it can (so you can pass "5" and it will become a
number).

I find very useful for debugging to have a "stack print" utility like:

void print_stack(lua_State* L)
{
  for(int i = 1; i <= lua_gettop(L); i++) {
	  switch(lua_type(L,i))
	  {
		  case LUA_TNUMBER:
		     printf("#%d number %f\n", i, lua_tonumber(L,i);
			 break;

			 // ... you get the idea!

if called before you call lua_tonumber(), a function like this would
answer the question of what exactly IS on the stack when you get
called, not just what isn't there.

Cheers,
Sam