lua-users home
lua-l archive

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


On Fri, 23 Sep 2005 15:04:37 +0100
David Given <dg@cowlark.com> wrote:

> 
> Well, the actual class isn't important --- it's the interfaces implemented by 
> the class that's important. Think Java; we have an object that implements 
> Vector and Synchronisable, therefore we need to generate bindings for those 
> particular methods, while the fact that this is actually an instance of 
> my.dodgy.app.Fnord is irrelevant. (Actually, our objects are probably going 
> to implement about four interfaces each.)

I have a situation similar to this. I've grown fond of the following technique
to call object methods. I think it's a big win once you get used to its
syntax:

        obj[intf.method]()

Here, the intf.method can be a userdata that points directly at your
underlying object system's method descriptor structure. When the __index
metamethod gets the userdata, all it has to do is prepare the arguments
and then do the method dispatch just as a call in your native implementation
would do. You don't have to do a string lookup at call-time (although this
isn't terrible in Lua because strings are automatically interned), plus
you have full immunity to namespace collision problems among interfaces.

Eric