[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: returning userdata
- From: Enno Rehling <enno@...>
- Date: Sun, 18 May 2003 20:45:11 +0200
Let's say I've got a C struct like this:
typedef struct data {
struct data * next;
int x;
} data;
I want to write a "next" handler in teh "index" metatable that will return a
userdata object. However, I've figured out from the manual and
experimentation that I can't just do a lua_pushlightuserdata(L, d->next),
because light userdata doesn't have a metatable, and if I write
print(a.next.x), lua complains that I'm using an index function of a.next,
which is light userdata. How do I make my "next" handler return non-light
userdata? I don't want to create a new userdata object, because what I want
to return is the actual object that's behind the ->next in C.
One way I thought of to solve that wasn't nice: I could to wrap all my
objects in ynother layer of data, and make userdata objects that contain
data* pointers (which I could then easily copy). I'd have something like
typedef struct lua_data {
data * cdata;
} lua_data;
and would write something like:
((lua_data*)lua_newuserdata(L, sizeof(lua_data)))->cdata = a->cdata->next;
but really, that's pretty ugly, and a lot of 4-byte mallocs.
Enno.