lua-users home
lua-l archive

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

2008/3/31, Mike Pall <>:
> Jérôme Vuarand wrote:
>  > I'm very surprised by the question, and the rest of the thread. In
>  > some of my modules I use full userdata as the environment to other
>  > full userdata, I never ran into an error specifying that the
>  > environment has to be a table.
> If you had turned on assertions with LUA_USE_APICHECK, the
>   api_check(L, ttistable(L->top - 1));
> in lua_setfenv() would have caught it.

Indeed, it does assert.

>  > More specifically, my userdata A is a pointer to a C structure, and
>  > when I __index one of its field, it returns a userdata B pointing to
>  > the field directly. To avoid the A structure to be collected while I
>  > have a pointer to its field (B), I make A the environment of B. It
>  > seems to work perfectly fine.
> The GC doesn't care which kind of object is referenced from the
>  u->env pointer (as long as it's a collectable object). You were
>  just lucky. But lua_getfenv() will gladly push any environment
>  object on the stack and tag it as a table (ouch).

So this means that having a userdata as environment works (as a
collection prevention as in my example above), as long as you don't
try to access it once set ?

It would be nice to have lua_setfenv check the type in a future
version. Alternatively (and I'd favor that alternative), it would be
nice to allow userdata as environment of other userdata. In the
meantime I'll try other methods to store my owner<->owned