lua-users home
lua-l archive

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

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.

> 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).

> How should the restriction manifest itself ? Is that something
> specific to the Lua version of setfenv (as opposed to the C function
> lua_setfenv) ? Is that something from Lua 5.0 (I'm using 5.1) ?

Nope, it's been there since Lua 5.1 was released. Userdatas had no
environment in Lua 5.0.