lua-users home
lua-l archive

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


On Oct 20, 2011, at 12:40 AM, Tony Finch wrote:

> The usual proposal to improve the first two is as follows, but I'm trying
> to find a more general solution.

What about something along these lines:

local function import( aName, ... )
  local aModule = require( aName )
  local aFunction = debug.getinfo( 2, 'f' ).func
  local _, anEnvironement = debug.getupvalue( aFunction, 1 )

  for anIndex = 1, select( '#', ... ) do
    local aName = select( anIndex, ... )
    anEnvironement[ aName ] = aModule[ aName ]
  end
end

import( 'math', 'min', 'max' )

print( min )
print( max )

> function: 0x10001af60
> function: 0x10001aee0