lua-users home
lua-l archive

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


A while back, I implemented most class functionality in my implementation of
lua.  Since I was learning lua at the time, I used tolua as my model, but
made some changes.  At any rate, I have class member variables in lua,

myClassInstance.memberVar = someValue
someVar = myClassInstance.memberVar

works fine (where myClassInstance is actually a userdata pointer to a class
instance in my c++ program (and memberVar is a member variable within it)).
I did this by using the get and settable tag methods in a similar way to
tolua (the get and settable methods retrieve a table containing all the
functions that define the class, and apply those functions to the class

I'm having a problem with member functions however.  What happens is when
lua reads the line:

returnvalue = myClassInstance.memfunc(param1, param2...)

it calls the gettable tag method for myClassInstance with the index memfunc.
How I deal with this is as follows:
1. the gettable tag method for myClassInstance is called
2. this tag method gets the table that contains all the get functions for
3. the function corresponding to memfunc is retrieved from the myClass
table, then pushed on the stack.
4. lua calls the function in this manner: function(param1, param2 ...)

The problem is that now myClassInstance (which is a userdata pointing
towards the class instance in my c++ code) is no longer on the lua stack, so
the function cannot access the c++ instance (through the userdata pointer).
If I manually call the function in step 3 (instead of pushing it back on the
stack), none of the class method's parameters get passed.  Basically I need
#4 to read:

4. lua calls the function in this manner: function(this, param1, param2...)

How can I do this?  Can I somehow push the myClassInstance back onto the
stack so that when lua calls the function all is well?  

Note: I'm still using lua4.0 alpha (for internal reasons) but will be moving
to lua4.0 final in January.  If that makes a difference, let me know.

This email seems convoluted.  I hope you understand.  If not, let me know
and I'll try to clarify.