setfenv with tail calls

  • From: Maik Nijhuis <manyac@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 26 Jan 2015 14:48:36 +0100

Dear Mike,

The following chunk shows different behavior in luajit (2.0.3 and 2.1) and
PUC lua:

local setfenv, getfenv, print = setfenv, getfenv, print

local f1 = setfenv(function() return setfenv(1, { "xxx" }) end, { 1 })

local f2 = setfenv(function() return f1() end, { 2 })

local f3 = setfenv(function() return f2() end, { 3 })


print(getfenv(f1)[1], getfenv(f2)[1], getfenv(f3)[1], getfenv()[1])

Luajit outputs "1 2 3 xxx" where PUC lua outputs "xxx 2 3 nil"

When I remove the tail call in f1, using 'return (setfenv(1, {"xxx"}))',
luajit also outputs "xxx 2 3 nil".

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.

Could you tell me if this behavior is desired?


Other related posts: