lua-users home
lua-l archive

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


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