lua-users home
lua-l archive

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

On 21/01/2010 11:29, M Joonas Pihlaja wrote:
>> 	in _G:pushenv( env ) do ... end
> You left out the stack unwinding with popenv().  Of course you need to 
> pop it on errors as well, so every pushenv() ought to be called inside 
> a pcall().  The required error handling makes your approach less 
> appealing than what your example might suggest.

Ah, but you don't! pushenv() would be implemented like this:

function pushenv(env)
  local newenv = {}
  setmetatable(newenv, { __index = env })
  return newenv

Note that the new environment contains a pointer to the old one, but not
vice versa.

On exit, nothing needs popping, because the old environment has not been
modified. ensures that the new environment is only used
inside the lexical block. When the block is exited, through whatever
mechanism, the new environment becomes unused and will be safely garbage

David Given