[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: default table in C functions
- From: Peter Cawley <lua@...>
- Date: Wed, 5 Jan 2011 14:38:46 +0000
On Wed, Jan 5, 2011 at 2:32 PM, jseb <gmane2010@finiderire.com> wrote:
> Hello,
>
> For some time, i couldn't accessing datas from a table i send to a C
> function.
>
> I was accessing table members like this:
>
> char *params[]={"rotx","roty","rotz","posx","posy","posz",
> "md2_id","current_anim","current_frame",0};
> int nb_params=(sizeof(params)/sizeof(char*))-1;
> for (int i=0; params[i]; i++) {
> lua_getfield(L,1,params[i]);
> }
>
> You can see that the extracted parameters are all pushed (they are put into
> C vars after the loop. And yes, i check if there's enough room on the stack
> before doing it).
>
> The problem was that nothing was retrieved.
> So i've dumped the lua stack, at the very beginning of my C function.
>
> ** STACK DUMP (top: 2) **
> type [table] unknown @ 1 <- ??? unknown table
> type [table] unknown @ 2 <- my table
>
> Seeing that, i just changed the stack index in "lua_getfield" to "2".
> Ok, it works.
>
> Now, my question is : what is exactly this table ? Does it contain
> interesting things, or raise it no point for an average programmer like me ?
>
> I tried a google search with "c environment lua table" (and some minor
> variations), with no luck.
>
> Thank you.
It is there because somebody put it there. If you've set up your C
function as a __call metamethod on something, then you get that
something as the 1st argument. If you're calling your C function as
MyFunctions:f(...), then MyFunctions will be the 1st argument. If
you're using some arcane binding library, then it may inject extra
parameters.