lua-users home
lua-l archive

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


Hi Falko,

I once had a similar decision problem while implementing an image data type
in Lua. I believe the simpler solution to your problem is to stick to
usertag carrying pointers to your C structure. That is,
don't push lua tables from C to Lua. Instead, stick to C pointers to your C
structures, passed to lua as usertag objects. You should define the gettable and
settable methods for the new vector tag. Your getglobal method would push a
usertag object into the c2lua stack, carrying the pointer to the appropriate C structure.

Here are the new actions for the function calls:

localvec = newVector(10, 10, 10)
-- calls lua_pushusertag to push a pointer to a C structure containing the
-- vector (10, 10, 10), as an usertag object with the vector tag, into the c2lua stack

localvec = GlobalVec
-- calls lua_pushusertag to push the pointer to the C structure corresponding to the C -- global variable "GlobalVec", as an usertag object with the vector tag, into the c2lua stack

localnum = GlobalVec.x
-- lua will call getglobal for "GlobalVec" and your method will do the same as it did in the -- line above. Then, lua will call the gettable tagmethod for the vector tag, passing -- your usertag vector object. your tagmethod should then push the number value of
-- "x" into the c2lua stack

GlobalVec.x = localnum
-- lua will call getglobal for "GlobalVec" and your method will do the same as it did in the -- previous examples. Then, lua will call the settable tagmethod for the vector tag, passing -- your usertag vector object and the value of localnum. your tagmethod should then update
-- the field corresponding to "x" in the C structure

You might want to implement the gc tag method for your vectors, to avoid memory leaks.

Also, does lua_endblock() flush the c2lua stack?  I think it does, I just
want to make sure.

Yes it does.

Hope it helps,
regards,
Diego.