lua-users home
lua-l archive

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


Hi

I'm a new lua user, and I'm trying to understand how to manipulate
userdata in lua.

I've read the lua manual, the draft lua-book, and MANY posts on this
list, but I'm still confused. I'll try to explain what I'd like to
obtain with a little example.

On the C-side, i have a user type and some functions to manipulate it

typedef struct {
   some suff;
} stuffT;

stuffT *newstuff();   /* malloc */
void deletestuff(stuffT *);   /* free */

and an operator (add)

stuffT *addstuff(stuffT *a, stuffT *b);

or maybe

void addstuff(stuffT *result, stuffT *a, stuffT *b);

on the lua-side, I'd like to have this working;

1) have some globals stuffs (say a, b, and c) pre-initialized. (OK, I've
managed to get this done with newtag, pushusertag, etc)

2) a = b+c; (OK. I have built a wrapper around addstuff, and set the tag
metod for "add")

3) local d; d=a+b+c
this last point is what puzzles me. I think that what really happens
should be something like this
temp1 = newstuff()
temp = a+b
temp2 = newstuff()
temp2 = temp1+c
d = temp2
deletestuff(temp1) (maybe automatically, by gc)

Sadly I cannot get this to work. Do someone have a (as small as
possible) piece of code that solves this problem? It would be VERY
clarifying.

Thanks

Oscar Lazzarino

PS. Please forgive my horrible english, I hope it is correct enough to
be understanded.