Created
April 21, 2017 22:15
-
-
Save marcogravbrot/9994f450b5aca67e492ef026f061132a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function runCode(code) | |
local environment = { | |
-- | |
-- global functions | |
-- | |
print = print, | |
tostring = tostring, | |
tonumber = tonumber, | |
type = type, | |
pairs = pairs, | |
error = error, | |
ipairs = ipairs, | |
next = next, | |
unpack = unpack, | |
_VERSION = _VERSION, | |
select = select, | |
-- | |
-- global libraries/tables | |
-- | |
math = math, | |
string = string, | |
table = table, | |
-- | |
-- global variables | |
-- | |
ping = "pong!", | |
--message = message, | |
} | |
local func, err = loadstring("return ".. code, "eval", "t", environment) | |
if err and (not func) then -- return <code> doesn't work, let's try without return | |
func, err = loadstring(code, "eval", "t", environment) | |
if err and (not func) then | |
return false, err | |
end | |
local ret = {func()} | |
if ret and #ret > 0 then | |
return true, ret | |
end | |
return true | |
end | |
local ret = {func()} | |
if ret and #ret > 0 then | |
return true, ret | |
end | |
return true | |
end | |
-- | |
-- Example code, showcases a use case with multiple returns | |
-- | |
local ok, ret = runCode("return 1, 2, 3") | |
if ok and ret then | |
print(unpack(ret)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kek