MindenCucc

DixBox

May 13th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. -- DixBox
  2. -- a simple Lua sandbox, mainly for CC
  3.  
  4. --==========[COPY-PASTE DIX PLUGIN DESCRIPTOR]==========--
  5.  
  6.   local versionString="0.1"
  7.   local pluginName="sandbox"
  8.   local pluginAuthor="MarcusD"
  9.   local isColorNeeded=false
  10.  
  11.   dixmod(pluginName, pluginAuthor, versionString, isColorNeeded)
  12.  
  13. --==========[COPY-PASTE DIX PLUGIN DESCRIPTOR]==========--
  14.  
  15. function new(func, sandenv, ignore, fenv)
  16.   if not sandenv then sandenv = {} end
  17.   if not ignore then ignore = {} end
  18.   if not fenv then fenv = getfenv() end
  19.  
  20.   local sandenv_meta = {}
  21.   function sandenv_meta:__index(k)
  22.     local thing = rawget(sandenv, k)
  23.     if ignore[k] then return thing end
  24.     return thing ~= nil and thing or fenv[k]
  25.   end
  26.   function sandenv_meta:__newindex(k,v)
  27.     rawset(sandenv, k, v)
  28.   end
  29.   setmetatable(sandenv, sandenv_meta)
  30.  
  31.   local cor = nil
  32.  
  33.   if type(func) == "function" then
  34.     setfenv(func, sandenv)
  35.     cor = coroutine.create(func)
  36.   end
  37.  
  38.   return
  39.   {
  40.     getsand = function()
  41.       return sandenv
  42.     end,
  43.    
  44.     getsand_meta = function()
  45.       return sandenv_meta
  46.     end,
  47.    
  48.     reset_meta = function()
  49.       return setmetatable(sandenv, sandenv_meta)
  50.     end,
  51.    
  52.     setsand = function(wat)
  53.       if type(wat) ~= "table" then error("Not a table") end
  54.       sandenv = wat
  55.     end,
  56.    
  57.     setsand_meta = function(wat)
  58.       if type(wat) ~= "table" then error("Not a table") end
  59.       sandenv_meta = wat
  60.     end,
  61.    
  62.     setignore = function(k, b)
  63.       if type(k) ~= "string" or type(b) ~= "boolean" then error("Expected string, boolean") end
  64.       if k == "" or k:match("[^%s]*") ~= k then error("Key must be non-empty, nor containing whitespace") end
  65.       ignore[k] = b
  66.     end,
  67.    
  68.     getignore = function(k)
  69.       if type(k) ~= "string" then error("Expected string") end
  70.       if k == "" or k:match("[^%s]*") ~= k then error("Key must be non-empty, nor containing whitespace") end
  71.       return ignore[k]
  72.     end,
  73.    
  74.     getfuncenv = function()
  75.       return fenv
  76.     end,
  77.    
  78.     getroutine = function()
  79.       return cor
  80.     end,
  81.    
  82.     run = function(...)
  83.       return coroutine.resume(cor, ...)
  84.     end,
  85.    
  86.     unyield = function(...)
  87.       return coroutine.resume(cor, coroutine.yield(...))
  88.     end
  89.   }
  90.  
  91. end
Advertisement
Add Comment
Please, Sign In to add comment