lua-users home
lua-l archive

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


I did originally do that, but then the receiving code I would pass this larger userdata object to would call lua_rawlen() and then the game was over :(  I need these 2 userdata to be bound through collection but entirely separate otherwise (I make this fun).

Let's say lua_setuservalue() allowed joining 2 userdata and not just a userdata-table pair:  Say I'm associating the "public" A userdata with the "private" B userdata.  A becomes referenced from somewhere on the Lua side, like maybe _G.A = <userdata>.  B is never referenced from the Lua side, it's only known to the GC as something not to be collected until A becomes unreferenced -- correct?


On Mon, Dec 2, 2013 at 12:41 AM, Sean Conner <sean@conman.org> wrote:
It was thus said that the Great Sir Pogsalot once stated:
> Currently you can only associate a lua table with a userdata through
> lua_setuservalue() -- I wish you could also associate userdata.
>
> Not sure if this isn't already possible with some other function, but what
> I think I need is to be able to associate one userdata with another for
> collection -- or just a bit of memory to be collected when the one
> Lua-exposed userdata become unreferenced?
>
> I have a project where I create userdata from a third-party lib -- I cannot
> change its structure, I must expose it to Lua as userdata (that's it).  I
> want to keep private information about that userdata but I don't want to
> keep track of its deallocation from C, I just want to associate it with the
> userdata I expose to Lua and have the private data available through
> getuservalue() and I want it deallocated by the GC when the exposed
> userdata becomes unreferenced.
>
> To me, it would feel less 'to the point' to contain that userdata within a
> table to then associate with the original userdata that's created for me.
>  It would feel less readable and sort of ugly to lua_getuservalue() and
> then fetch the first sequence index for the userdata I need containing my
> privates (tee hee).
>
> Is this an acceptable suggestion or am I off my rocker?

  What's wrong with this approach?

        struct myfoo
        {
          struct foo base;
          int        private1;
          int        private2;
        };

        /* ... */

        struct myfoo *p = lua_newuserdata(sizeof(struct myfoo));
        foo_init(&p->base);
        p->private1 = 1;
        p->private2 = 2;

        /* ... */

  That is, wrap a larger structure around the structure from the third party
lib and store your private data there?

  -spc