sandbox.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. --A function that creates new sandboxed global environment.
  2. local bit = require("bit")
  3. local _LuaBCHeader = string.char(0x1B).."LJ"
  4. return function(parent)
  5. local GLOB = {
  6. assert=assert,
  7. error=error,
  8. ipairs=ipairs,
  9. pairs=pairs,
  10. next=next,
  11. pcall=pcall,
  12. select=select,
  13. tonumber=tonumber,
  14. tostring=tostring,
  15. type=type,
  16. unpack=unpack,
  17. _VERSION=_VERSION,
  18. xpcall=xpcall,
  19. setmetatable=setmetatable,
  20. getmetatable=getmetatable,
  21. rawget = rawget,
  22. rawset = rawset,
  23. rawequal = rawequal,
  24. string={
  25. byte=string.byte,
  26. char=string.char,
  27. find=string.find,
  28. format=string.format,
  29. gmatch=string.gmatch,
  30. gsub=string.gsub,
  31. len=string.len,
  32. lower=string.lower,
  33. match=string.match,
  34. rep=string.rep,
  35. reverse=string.reverse,
  36. sub=string.sub,
  37. upper=string.upper
  38. },
  39. table={
  40. insert=table.insert,
  41. maxn=table.maxn,
  42. remove=table.remove,
  43. sort=table.sort,
  44. concat=table.concat
  45. },
  46. math={
  47. abs=math.abs,
  48. acos=math.acos,
  49. asin=math.asin,
  50. atan=math.atan,
  51. atan2=math.atan2,
  52. ceil=math.ceil,
  53. cos=math.cos,
  54. cosh=math.cosh,
  55. deg=math.deg,
  56. exp=math.exp,
  57. floor=math.floor,
  58. fmod=math.fmod,
  59. frexp=math.frexp,
  60. huge=math.huge,
  61. ldexp=math.ldexp,
  62. log=math.log,
  63. log10=math.log10,
  64. max=math.max,
  65. min=math.min,
  66. modf=math.modf,
  67. pi=math.pi,
  68. pow=math.pow,
  69. rad=math.rad,
  70. random=love.math.random, --Replaced with love.math versions
  71. randomseed=function(s) if s then love.math.setRandomSeed(s) else return love.math.getRandomSeed() end end,
  72. sin=math.sin,
  73. sinh=math.sinh,
  74. sqrt=math.sqrt,
  75. tan=math.tan,
  76. tanh=math.tanh,
  77. noise = love.math.noise, --LOVE releated apis
  78. b64enc = function(rawstr) return love.data.encode("string","base64",rawstr) end, --Will be replaced by love.math ones in love 0.11
  79. b64dec = function(rawstr) return love.data.decode("string","base64",rawstr) end,
  80. hexenc = function(rawstr) return love.data.encode("string","hex",rawstr) end,
  81. hexdec = function(rawstr) return love.data.decode("string","hex",rawstr) end,
  82. compress = function(rawstr,format,lvl) return love.data.compress("string",format or "lz4",rawstr,lvl or -1) end,
  83. decompress = function(rawstr,format) return love.data.decompress("string",format or "lz4",rawstr) end,
  84. isConvex = love.math.isConvex,
  85. triangulate = love.math.triangulate,
  86. randomNormal = love.math.randomNormal
  87. },
  88. coroutine={
  89. create = coroutine.create,
  90. resume = coroutine.resume,
  91. yield = coroutine.yield,
  92. status = coroutine.status
  93. },
  94. os={
  95. time=os.time,
  96. clock=os.clock,
  97. date=os.date
  98. },
  99. bit={
  100. cast=bit.cast,
  101. bnot=bit.bnot,
  102. band=bit.band,
  103. bor=bit.bor,
  104. bxor=bit.bxor,
  105. lshift=bit.lshift,
  106. rshift=bit.rshift,
  107. arshift=bit.arshift,
  108. tobit=bit.tobit,
  109. tohex=bit.tohex,
  110. rol=bit.rol,
  111. ror=bit.ror,
  112. bswap=bit.swap
  113. }
  114. }
  115. GLOB.getfenv = function(f)
  116. if type(f) ~= "function" then return error("bad argument #1 to 'getfenv' (function expected, got "..type(f)) end
  117. local ok, env = pcall(getfenv,f)
  118. if not ok then return error(env) end
  119. if env.love == love then env = {} end --Protection
  120. return env
  121. end
  122. GLOB.setfenv = function(f,env)
  123. if type(f) ~= "function" then return error("bad argument #1 to 'setfenv' (function expected, got "..type(f)) end
  124. if type(env) ~= "table" then return error("bad argument #2 to 'setfenv' (table expected, got "..type(env)) end
  125. local oldenv = getfenv(f)
  126. if oldenv.love == love then return end --Trying to make a crash ! evil.
  127. local ok, err = pcall(setfenv,f,env)
  128. if not ok then return error(err) end
  129. end
  130. GLOB.loadstring = function(data,chunkname)
  131. if data:sub(1,3) == _LuaBCHeader then return error("LOADING BYTECODE IS NOT ALLOWED, YOU HACKER !") end
  132. if chunkname and type(chunkname) ~= "string" then return error("Chunk name must be a string or a nil, provided: "..type(chunkname)) end
  133. local chunk, err = loadstring(data,chunkname)
  134. if not chunk then return nil, err end
  135. setfenv(chunk,GLOB)
  136. return chunk
  137. end
  138. GLOB.load = function(iter,chunkname)
  139. if type(iter) ~= "string" then return error("Iterator must be a function, provided: "..type(iter)) end
  140. if chunkname and type(chunkname) ~= "string" then return error("Chunk name must be a string or a nil, provided: "..type(chunkname)) end
  141. local firstline = iter()
  142. if firstline:sub(1,3) == _LuaBCHeader then return error("LOADING BYTECODE IS NOT ALLOWED, YOU HACKER !") end
  143. local newiter = function()
  144. if firstline then
  145. local l = firstline
  146. firstline = nil
  147. return l
  148. end
  149. return iter()
  150. end
  151. return load(newiter,chunkname)
  152. end
  153. GLOB.coroutine.sethook = function(co,...)
  154. --DEPRICATED--
  155. --Coroutine hooks are useless because of LuaJIT
  156. --[[if type(co) ~= "thread" then return error("bad argument #1 (thread expected, got "..type(co)..")") end
  157. local ok, err = pcall(debug.sethook,co,...)
  158. if not ok then return error(err) end
  159. return err]]
  160. end
  161. GLOB.coroutine.running = function()
  162. local curco = coroutine.running()
  163. if parent and parent.co and curco == parent.co then return end
  164. return curco
  165. end
  166. GLOB._G=GLOB --Mirror Mirror
  167. return GLOB
  168. end