lua-users home
lua-l archive

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


When I do a lua_tostring or lua_tonumber, do these values get poped off of the stack? Cause when I pass values from lua to 'C' using functions. I always use index 1 to retreive the values and it always returns the correct one. for example:

[LUA]
doit(21, 34, "I was here")

[C]
static int doit(lua_State *L)
{
int a, b;
char *text;

a=(int)lua_tonumber(L, 1);
b=(int)lua_tonumber(L, 2);
text=(char *)lua_tostring(L, 3);

printf("A=%d,  B=%d,  Text=%s\n", a, b, text);
//prints out:  A=21,  B=34,  Text=I was here   <------------
}

so 21 gets pushed into index #1 and 34 into #2 and "I was here" into #3.
So the top is at 3 (or -1??) and the bottom is at #1 right??
So a table would be pushed onto the stack at index #4 (after doit() is called).

From: Diego Fernandes Nehab <diego@tecgraf.puc-rio.br>
Reply-To: lua-l@tecgraf.puc-rio.br
To: Multiple recipients of list <lua-l@tecgraf.puc-rio.br>
Subject: RE: Lua Tables from 'C'
Date: Mon, 15 Oct 2001 14:22:31 -0200 (BRST)

> I don't understand. Don't values get pushen onto the bottom of the stack?? > Or are tables different? In that case wouldn't the table be at -1?? When I > call 'C' functions from lua with numbers as parameters, I can find them on
> the bottom of the stack.  This is confusing.

The stack grows  up with positive numbers. New values  are pushed on the
top!

If there are 3 elements in the stack,  the bottom is at 1, the middle is
at 2 and the top  is at 3. If you push a new elemement,  it will be at 4
and the others will keep their indexes unchanged.

You can also index elements relative  to the top, using negative numbers
as indexes. If there  are 3 elements in the stack, the  bottom is at -3,
the middle is at  -2 and the top is at -1. If  you push a new elemement,
it will  be at -1  and the  other indexes will  be decreased by  one, to
reflect their new distance to the top.

So, positive indexes are absolute, negative indexes are relative to the
top. The stack grows up, new elements at the top. That's about it.

[]s,
Diego.



_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp