Advertisement
Pinkishu

with

Nov 30th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.61 KB | None | 0 0
  1. function with(scope)
  2.   local cEnv = {}  
  3.   local oEnv = getfenv(2)
  4.   local cEnvMT = {}
  5.   local outT = {}
  6.   local outMT = {}
  7.  
  8.   outMT.__index = function(t,k)
  9.     return oEnv[k]
  10.   end
  11.  
  12.   outMT.__newindex = function(t,k,v)
  13.     oEnv[k]=v
  14.   end
  15.  
  16.   outMT.__call = function(...)
  17.     setfenv(2,oEnv)
  18.     oEnv = nil
  19.   end
  20.   setmetatable(outT,outMT)
  21.  
  22.   cEnvMT.__index = function(t,k)
  23.     if k == "out" then
  24.       return outT
  25.     else
  26.       return scope[k] or outT[k]
  27.     end
  28.   end
  29.  
  30.   cEnvMT.__newindex = function(t,k,v)
  31.     scope[k]=v
  32.   end
  33.  
  34.   setmetatable(cEnv,cEnvMT)  
  35.   setfenv(2,cEnv)
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement