Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- --Copyright Notice--
- You may use, modifie, and distribute this
- code without the permission of the original
- developer
- However, you are not allowed to remove this
- copyright notice and you must give credit to the
- original developer
- --Intro--
- This script is designed to run as a stand alone
- script or as a lua library. This script's
- purpose is to allow lua code to run in a
- proteced environment.
- This environment will only allow lua code to
- use functions and variables defined by the user.
- Any changes in this environment will stay in the
- environment and will not effect any functions or
- variables from outside of the protected
- environment.
- --]]
- local fs = require "filesystem"
- local pse = {}
- --[[
- This function determins if sandbox is a valid
- sandbox
- --]]
- local function is_sandbox(sandbox)
- if not sandbox then
- return false
- elseif not type(sandbox) == "table" then
- return false
- elseif type(sandbox.code) == "nil" then
- return false
- elseif type(sandbox.args) == "nil" then
- return false
- end
- return true
- end
- --[[
- This function throws an error if the given
- sandbox is not actully a sandbox
- --]]
- local function sb_error(sandbox)
- if not is_sandbox(sandbox) then
- error("expected sandbox",3)
- end
- end
- --[[
- This next function will get anything a lua file
- will return when ran. This is a seperate function
- to make porting code to different systems easier
- --]]
- local function get_file(path)
- if not type(path) == "string" then
- error("expected string",3)
- end
- local file = fs.open(path,"r")
- if not file then
- error("file does not exists",3)
- end
- local data = ""
- local temp = ""
- while temp do
- temp = file:read(512)
- if temp then
- data = data..temp
- end
- end
- file:close()
- return data
- end
- --[[
- This function will create a new sanbox to work
- in. Each sandbox will contain all the code you
- put in it and it will also contain runtime
- arguments.
- --]]
- function pse.new_sandbox()
- local sandbox = {code={},args={}}
- return setmetatable({},{__index=sandbox})
- end
- function pse.newSandbox()
- return pse.new_sandbox()
- end
- --[[
- This function is for debugging the sandbox.
- --]]
- function pse.debug_sandbox(sandbox)
- sb_error(sandbox)
- print("Code:")
- for i,j in pairs(sandbox.code) do
- print(i)
- print(j)
- print()
- end
- print("Args:")
- for i=1,#sandbox.args do
- print(sandbox.args[i])
- end
- end
- function pse.debugSandbox(sandbox)
- return pse.debug_sandbox(sandbox)
- end
- --[[
- This will add any code to the sandbox you
- provide. This code can be either a function or a
- veriable
- --]]
- function pse.add_code(sandbox,name,code)
- sb_error(sandbox)
- if not (type(name) == "string") then
- error("expected string",2)
- elseif not code then
- error("expected code",2)
- end
- sandbox.code[name] = code
- return sandbox
- end
- function pse.addCode(sandbox,name,code)
- return pse.add_code(sandbox,name,code)
- end
- --[[
- This function will add runtime arguments to
- the given sandbox
- --]]
- function pse.add_argument(sandbox,arg)
- sb_error(sandbox)
- if not (type(arg) == "string") then
- error("expected string",2)
- end
- table.insert(sandbox.args,arg)
- return sandbox
- end
- function pse.addArgument(sandbox,arg)
- return pse.add_argument(sandbox,arg)
- end
- --[[
- This one is similar to the last one exept it
- adds anything the file at path returns to the
- sandbox
- --]]
- function pse.add_file(sandbox,name,path)
- sb_error(sandbox)
- if not (type(name) == "string") then
- error("expected string",2)
- end
- local program = load(get_file(path),"="..path,nil,sandbox.code)
- local result = table.pack(pcall(program))
- if not result[1] then
- error("error in file",2)
- end
- sandbox.code[name] = result[2]
- return sandbox
- end
- function pse.addFile(sandbox,name,path)
- return pse.add_file(sandbox,name,path)
- end
- --[[
- This function adds anything pre-defined in the
- global table. This could be any valid code. For
- example: print
- --]]
- function pse.add_global(sandbox,name)
- sb_error(sandbox)
- if not (type(name) == "string") then
- error("expected string",2)
- end
- sandbox.code[name] = _G[name]
- return sandbox
- end
- function pse.addGlobal(sandbox,name)
- return pse.add_global(sandbox,name)
- end
- --[[
- This function will run any string given to it
- as code withing the sandbox. It will also pass
- all the runtime arguments to the code.
- It will return false is the code had an error
- and will return all error data. Otherwise, the
- function will return what ever the code does
- --]]
- function pse.run_string(sandbox,name,data)
- sb_error(sandbox)
- if not (type(data) == "string") then
- error("expected string",2)
- elseif not (type(name) == "string") then
- error("expected string",2)
- end
- local program,reason = load(data,"="..name,nil,sandbox.code)
- if not program then
- return nil,reason
- end
- local status = table.pack(pcall(program,table.unpack(sandbox.args)))
- if status[1] then
- return table.unpack(status,2)
- end
- return table.unpack(status)
- end
- function pse.runString(sandbox,name,data)
- return pse.run_string(sandbox,name,data)
- end
- --[[
- This function is like the last but instead of
- treating a string like code, it will execute a
- file instead
- --]]
- function pse.run(sandbox,name,file)
- sb_error(sandbox)
- if not (type(name) == "string") then
- error("expected string",2)
- end
- local data = get_file(file)
- return pse.run_string(sandbox,name,data)
- end
- --[[
- Time to return all the functions needed for
- this library to work
- --]]
- return pse
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement