lua-users home
lua-l archive

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


This seems obvious, but I can't find the right info.  I have a C
module that exports a 'class' to Lua that has some methods and fields.
 (It's an image library, for context).

The first thing I did was newmetatable, pushvalue, setfield(-2,
"__index"), register(L, NULL, methods).  Basically, by the book.  That
worked great for the methods:

img = GetImage()
img:resize(x, y)
img:makepretty() -- whatever

But in order to get access to the width and height, I needed to use
getters ("img:width()", as a call) which are annoying.  I want
"img.width" to work.

Currently, I instead wrote my own index method, set that in __index,
and basically emulate register's behavior by iterating over my method
array by hand.  Then, if no function matches, I fall back on a series
of if-else strcmp tests for known field names.  This seems hackish.

Alternatively, I could return a table and hang my userdata off that,
but it seems prone to accidents.

Am I missing something obvious in the documentation?  Everything I've
found only talks about adding methods.