Class methods in lua

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Class methods in lua

Falko Poiker
Hi,

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,
where:

myClassInstance.memberVar = someValue
and
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
instance).

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
myClass.
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.
Falko


Reply | Threaded
Open this post in threaded view
|

Re: Class methods in lua

Luiz Henrique de Figueiredo
>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
>myClass.
>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 the gettable tag method is called for the expression
"myClassInstance.memfunc", not for the whole thing. In other words, it
does not know you're calling a function yet.

One way to do this for the gettable tag method to return the function
corresponding to memfunc as userdata and then to setup a "function" tag method
for the tag of this userdata. In this "function" tag method, you'll have
the arguments as required and you just cast the userdata back to whatever it
was in C or C++.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Class methods in lua

Daniel Silverstone
In reply to this post by Falko Poiker
On Mon, Dec 18, 2000 at 05:23:46PM -0800, Falko Poiker wrote:
> 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?  

1. You can make the function you return a closure including the userdata
information.
2. Call the function as myClassInstance:membfunc(param1,param2) which adds
the implicit 'self' parameter to the parameter list (IIRC)

Regards,

Daniel


Reply | Threaded
Open this post in threaded view
|

RE: Class methods in lua

Falko Poiker
In reply to this post by Falko Poiker
> 1. You can make the function you return a closure including 
> the userdata information.
Right on.  That's exactly what I needed.  Now I also understand
the reason for cclosures.

Thanks Daniel and lhf for the various solutions!
Falko