Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This was made by chucky4523:AKA in rl Pedro Brooks:
- local needssandboxing = function(i) end
- sandbox = {}
- sandbox.cache = {}
- sandbox.mt = {
- __index = function(self, k)
- local original = sandbox.cache[self]
- local v = original[k]
- return sandbox.any(v)
- end,
- __newindex = function(self, k, v)
- local original = sandbox.cache[self]
- original[k] = unsandbox.any(v)
- end
- }
- --sandbox any object
- function sandbox.any(a)
- if sandbox.cache[a] then
- -- already sandboxed
- return a
- elseif type(a) == "function" then
- return sandbox.func(a)
- elseif type(a) == "table" then
- return sandbox.table(a)
- elseif needssandboxing(a) then
- return sandbox.object(a)
- else
- --doesn't need sandboxing
- return value
- end
- end
- --sandbox instances and events
- function sandbox.object(o)
- local sandboxed = setmetatable({}, sandbox.mt)
- sandbox.cache[sandboxed] = o
- return sandboxed
- end
- --sandbox a function
- function sandbox.func(f)
- local sandboxed = function(...)
- return sandbox(f(unsandbox(...)))
- end
- sandbox.cache[sandboxed] = f
- return sandboxed
- end
- function sandbox.table(t)
- local sandboxed = {}
- for k, v in pairs(t) do
- --by sandboxing every key and every value
- sandboxed[sandbox.any(k)] = sandbox.any(v)
- end
- return sandboxed
- end
- unsandbox = {}
- --unsandbox any objects
- unsandbox.any = function(a)
- if sandbox.cache[a] then
- --if we have it cached, return it
- return sandbox.cache[a]
- elseif type(a) == "function" then
- return unsandbox.func(a)
- elseif type(a) == "table"
- return unsandbox.table(a)
- else
- return a
- end
- end
- unsandbox.table = function(t)
- local unsandboxed = {}
- for k, v in pairs(t) do
- --by unsandboxing every key and every value
- unsandboxed[unsandbox.any(k)] = unsandbox.any(v)
- end
- return unsandboxed
- end
- unsandbox.func = function(f)
- local raw = function(...)
- return unsandbox(f(sandbox(...)))
- end
- sandbox.cache[f] = raw
- return raw
- end
- local callable_mt = {
- __call = function(self, first, ...)
- if select('#', ...) == 0 then
- return self.any(first)
- else
- return self.any(first), self(...)
- end
- end
- }
- setmetatable(sandbox, callable_mt)
- setmetatable(unsandbox, callable_mt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement