Advertisement
Derek1017

Sandbox

Feb 23rd, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. -- TODO: return true if instance or event
  2. local needssandboxing = function(i) end
  3. sandbox = {}
  4.  
  5. -- a map of sandboxed items to their original counterparts. TODO: weak keys/values?
  6. sandbox.cache = {}
  7.  
  8. sandbox.mt = {
  9. __index = function(self, k)
  10. local original = sandbox.cache[self]
  11.  
  12. -- todo: add logic here to filter property/method reading
  13.  
  14. local v = original[k]
  15.  
  16. return sandbox.any(v)
  17. end,
  18. __newindex = function(self, k, v)
  19. local original = sandbox.cache[self]
  20.  
  21. -- todo: add logic here to filter property writing
  22.  
  23. original[k] = unsandbox.any(v)
  24. end
  25. }
  26. --sandbox any object
  27. function sandbox.any(a)
  28. if sandbox.cache[a] then
  29. -- already sandboxed
  30. return a
  31. elseif type(a) == "function" then
  32. return sandbox.func(a)
  33. elseif type(a) == "table" then
  34. return sandbox.table(a)
  35. elseif needssandboxing(a) then
  36. return sandbox.object(a)
  37. else
  38. --doesn't need sandboxing
  39. return value
  40. end
  41. end
  42. --sandbox instances and events
  43. function sandbox.object(o)
  44. local sandboxed = setmetatable({}, sandbox.mt)
  45. sandbox.cache[sandboxed] = o
  46. return sandboxed
  47. end
  48. --sandbox a function
  49. function sandbox.func(f)
  50. local sandboxed = function(...)
  51. return sandbox(f(unsandbox(...)))
  52. end
  53. sandbox.cache[sandboxed] = f
  54. return sandboxed
  55. end
  56. --sandbox a table. TODO: prevent crash on recursive tables.
  57. function sandbox.table(t)
  58. local sandboxed = {}
  59. for k, v in pairs(t) do
  60. --by sandboxing every key and every value
  61. sandboxed[sandbox.any(k)] = sandbox.any(v)
  62. end
  63. return sandboxed
  64. end
  65. unsandbox = {}
  66. --unsandbox any objects
  67. unsandbox.any = function(a)
  68. if sandbox.cache[a] then
  69. --if we have it cached, return it
  70. return sandbox.cache[a]
  71. elseif type(a) == "function" then
  72. return unsandbox.func(a)
  73. elseif type(a) == "table"
  74. return unsandbox.table(a)
  75. else
  76. return a
  77. end
  78. end
  79. --unsandbox a table. TODO: prevent crash on recursive tables.
  80. unsandbox.table = function(t)
  81. local unsandboxed = {}
  82. for k, v in pairs(t) do
  83. --by unsandboxing every key and every value
  84. unsandboxed[unsandbox.any(k)] = unsandbox.any(v)
  85. end
  86. return unsandboxed
  87. end
  88. --unsandbox a function (sandboxed -> sandboxed), such as one passed to an event handler, making it (raw -> raw)
  89. unsandbox.func = function(f)
  90. local raw = function(...)
  91. return unsandbox(f(sandbox(...)))
  92. end
  93. sandbox.cache[f] = raw
  94. return raw
  95. end
  96.  
  97. -- make sandbox and unsandbox function acting on tuples
  98. local callable_mt = {
  99. __call = function(self, first, ...)
  100. if select('#', ...) == 0 then
  101. return self.any(first)
  102. else
  103. return self.any(first), self(...)
  104. end
  105. end
  106. }
  107.  
  108. setmetatable(sandbox, callable_mt)
  109. setmetatable(unsandbox, callable_mt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement