lua-users home
lua-l archive

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


By the way, the form of function call I use now, to ensure setfenv()
is called after func() returns, will 'eat' all return values from
func() except first one.

  return func(), setfenv(func, e)

Is there other way to call setfenv() after func(), and still return
all what func() returns?

I assume following would give unnecessary overhead on table creation:

  local res = { func() }
  setfenv(func, e)
  return unpack(res)

I thought this would work, but it reduces results to one return value too:

  return setfenv(func, e) or func()

In 5.1 (looks like) is possible to use '...' for such purpose:

  eat_first = function(_, ...)
    return ...
  end

  in_context_of = function(t)
   assert_is_table(t)
   return function(func)
     assert_is_function(func)
     local e = getfenv(func)
     setfenv(func, t)
     return eat_first(setfenv(func, e), func())
   end
  end

But I'm still using 5.0.2, and can't move to 5.1 right now. Is there a
cheap solution for 5.0.2?

Alexander.