[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lua-l
Subject:    RE: Lua Tables from 'C'
From:       Diego Fernandes Nehab <diego () tecgraf ! puc-rio ! br>
Date:       2001-10-15 18:50:49
Message-ID: Pine.LNX.4.33.0110151647230.1135-100000 () localhost ! localdomain
[Download RAW message or body]

Hi,

> 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).

Your  example is  correct. I  am not  sure if  you understood  that each
function is called  with a "private" stack,  so that even if  you do not
pop your stuff  off inside doit(), a  new call to doit()  will still use
the same indexes.

Some functions pop values from the stack, others do not. Check the manual.
After a while, it starts making sense that some functions should pop and
others should not.

[]s,
Diego.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic