lua-users home
lua-l archive

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


> Please, consider the following toy code:
> 
> [...]

This is the kind of use that deserves the use of the debug library,
because you explicitly want to break the usual visibility rules.

Note that, for this particular use, you do not need the entire
apparatus of Rhozenko's code. (In particular, you do not need the part
you had not understood.)  A simpler 'setfenv' will do:

-- not tested!
setfenv = function(f, t)
  for up = 1, math.huge do
    local name = debug.getupvalue(f, up)
    if name == '_ENV' then
      debug.setupvalue(f, up, t)
      return
    elseif name == nil then
      return
    end
  end
end

-- Roberto