CatchGlobal "clean"

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

CatchGlobal "clean"

Leandro Candido
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.