Maik Nijhuis wrote: > One the one hand, one would expect that luajit behaves equally as PUC lua, > so it is a bug. On the other hand, tail calls are part of the language, so > I can imagine that the behavior of setfenv is implementation dependent. The Lua interpreter has inconsistent behavior wrt. tail calls to C functions. It doesn't really do tail calls to them -- under the hood it's a regular call followed by a return. But LuaJIT does: it always replaces the current stack frame for a tail call, no matter whether you call a Lua or a C function. > Could you tell me if this behavior is desired? Well, strictly speaking, LuaJIT is closer to the language spec. The real problem with getfenv() is that the 'level' argument is relative to frame that getfenv() is executing in. But that depends on whether the VM is doing a true tail call to getfenv() (LuaJIT) or a call+return (Lua). IMHO 'return getfenv(level)' makes the reader of the code wonder which frame the level actually corresponds to. That's why I'd recommend to force a regular call (with parentheses). That avoids the ambiguity and then both VMs behave the same, too. --Mike