lua-users home
lua-l archive

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


On Wednesday 02 February 2005 16.31, Initial-N wrote:
> Thanks, Ashwin, 
> It works!!! 
> After getting the global_objects table, I need to push a string 
(effecttype), then do a lua_gettable(L, -2) in order to get the 
effecttype table.
> Therefore, entrie sequence is:
>  
> lua_getglobal(L, "global_objects");
> lua_pushstring(L, "effecttype");
> lua_gettable(L, -2);
> lua_pushstring(L, "water");
> lua_gettable(L, -2);
>  
> One thing I have not yet clear is, what exactly are happening in the 
stack when calling the sequence above.

I don't know exactly what Lua does, but assuming it works like your 
average stack based machine:

> lua_getglobal(L, "global_objects");

-1: table global_objects

> lua_pushstring(L, "effecttype");

-2: table global_objects
-1: string "effecttype"

> lua_gettable(L, -2);

(The operation pops the two arguments and pushes the result.)

-1: table "effecttype"

> lua_pushstring(L, "water");

-2: table "effecttype"
-1: string "water"

> lua_gettable(L, -2);

("Same procedure as every year, James!")

-1: <whatever is at index "water" in table "effecttype">


Then again, my own VM (the one in EEL) doesn't have a stack in the 
usual sense (the closest match would be the linked list of closures 
that forms when functions call each other), so what do I know!? :-D


> I know calling lua_getglobal give me a table which live in the lua
> global environment, but I wonder if  the entire table STRUCTURE is
> push on the top of the stack, or any magic tag is being pushed so
> that the stack is able to give me the next table when do the
> pushstring and gettable.

I believe the Lua stack (like EEL's "registers") consists of so called 
"tagged values" - that is, something like this:

typedef struct
{
 int type;
 union
 {
  int integer;
  double real;
  TOBJECT *objref;
  ...
 } v;
} TVALUE;

So, when you "get" an object as a result (or pass one, for that 
matter), it's actually just a value set to {type = TYPE_OBJECT, 
v.objref = <address of object>} - that is, a wrapped pointer to an 
instance.


//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
|  Free/Open Source audio engine for games and multimedia.  |
| MIDI, modular synthesis, real time effects, scripting,... |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---