lua-users home
lua-l archive

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


> -----Original Message-----
> From: Brian Hook [mailto:hook_l@pyrogon.com]
> Sent: Thursday, March 06, 2003 5:03 PM
> To: Multiple recipients of list
> Subject: Re: 5.0 changes question
> 
> 
> Just to be clear:
> 
> >and,
> >
> >lua_pushvalue(L, LUA_GLOBALSINDEX);
> >
> >behaves like:
> >
> >lua_getglobals(L);
> 

lua_getglobals is not in the manual for Lua 5 beta.

> so you're saying that wherever I called lua_getglobals(L), I can now 
> start calling lua_pushvalue(L, LUA_GLOBALSINDEX) instead?
> 
> After reading everyone's responses, I'm feeling a little flustered -- 
> how in the world are everyone learning this stuff?  Reading the 

By reading section 4.13 in the manual for Lua 5 beta. It's called "Manipulating Global Variables". 

LUA_GLOBALSINDEX is just a special stack index used for manipulating global values from C.

Usually in order to fetch a table value you would do this

<put table on stack>
<put key on stack>
lua_gettable(L, -2); // leaves a value from the 'table' on stack

In Lua 5 for global values you can replace the above sequence with the following one

<put key on stack>
lua_gettable(L, LUA_GLOBALSINDEX); // leaves a value from the 'global table' on stack

Same concept applies to setting global values. Also if you just wanted to have that table, found at index LUA_GLOBALSINDEX, on top of stack you just say 

lua_pushvalue(L, LUA_GLOBALSINDEX);

and that table will be at stack position -1 (in addition to being at LUA_GLOBALSINDEX).

> implementation code?  I can't find any docs, the mailing list 
> archives are a bit, er, fractured (especially as 5 went from alpha to 
> beta, and now some changes to final), there isn't much sample source, 
> yet somehow everyone else here has clued in on how this stuff works 
> and I haven't.  
> 
> Granted, I haven't taken the time to just read all the Lua source 
> code, but I was hoping that was a last resort =|
> 
> Brian
> 
> 
> 
>