lua-users home
lua-l archive

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


Hello all,

    This is a clean version:

-- begin code

function catchglobal( _function )
 local tab = {}
 local meta = {}
 local catched = {}
 local func_env = getfenv(_function);
 function meta:__index( index )
   local c = catched
   local e = func_env
   c[index] = (c[index] or 0) + 1;
   return e[index];
 end
 function meta:__newindex( index, value )
   local c = catched
   local e = func_env;
   c[index] = (c[index] or 0) + 1;
   e[index] = value;
 end
 setmetatable(tab,meta);
 return function(...)
  local cat = catched;
  local env = tab;
  local func = _function;
  local oldenv = func_env;
  setfenv(func,env);
  local result = { func(unpack(arg)) }
  setfenv(func,oldenv);
  return cat, unpack(result); -- or return the right side of result = above
 end
end

-- end code

                                The God's Peace,

                                                                Leandro.