lua-users home
lua-l archive

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

My framework creates userdata-objects and gives them to Lua.  To make sure
that I don't give the same object to Lua twice, I track them with weak
references (something that lua_pushuserdata() USED to do in 4.0 -- I'm not
arguing with the new behavior, but it is different ;-).  

Sometimes, I need to make sure that the object is not collected.
To do this, I need to convert the reference that I'm holding for the object
into a locked reference.  Currently, I'm doing something like this:

    // convert object->ref from a weak reference to a locked
    // reference.
    old_ref = object->ref;
    if (lua_getref(L,old_ref)) { // error: object was collected already }
    object->ref = lua_ref(L,1);

When I need to convert the locked reference back into a weak reference ...

    old_ref = object->ref;
    object->ref = lua_ref(L,0);

Is there a better way to do this?  Is this legal? (is it moral?)

I've looked at the code for lua_getref() in lapi.c, is it legal/moral
to just twiddle the registry/weak registry "by hand" to accomplish what
I need done?

It would be really cool if Lua had a 'USERHANDLE' type -- works like userdata
(i.e.: can have tag methods) but is NOT GC'ed (and thus no need to mark/sweep
them) the lifetime of the objects would be entirely handled by the host

Mike Cuddy (, MC312), Programmer, Daddy, Human.
Fen's Ende Software, Redwood City, CA, USA, Earth, Sol System, Milky Way.

       Now I lay me down to sleep / CVS, I pray, my code to keep.  
       If disks crash before I wake: / format, newfs, cvs up, make.

       Join CAUCE: The Coalition Against Unsolicited Commercial E-mail.