I would love it to be the same as pcall as in ok, err (ok boolean, err string).
From what I understand loadstring and dofile can be used, but I don't know how to keep the sandbox.
Here is the sandbox so far (not done):
local oTerm = term.current()
local term = { -- new term
write = function()
--# here, we actually handle the write. ANY call to write will come here, not the BIOS. Unlike term.redirect.
end,
setBackgroundColor = function() end,
setBackgroundColour = function() end,
setTextColor = function() end,
setTextColour = function() end,
getSize = function() end,
setCursorBlink = function() end,
getPos = function() end,
reposition = function() end
}
local protected = { --# the new programs environment (added to default so override anything you don't want usable)
term = term,
write = term.write --# "redirect any shortcuts."
}
setmetatable( protected.term, {__index = oTerm}) --# If we did not create a block/redirect for a term command in our new term object, then redirect the request to the original term.
local ok = os.run( protected, "test.lua" )
if not ok then
print("This program has crashed! \nPress any key to continue")
os.pullEvent("key")
elseif ok then
print("This program has finished.\nPress any key to continue")
os.pullEvent("key")
end
*apologies for identation, it looks fine in Notepad++ and Sublime Text :/If you know a better way of doing a sandbox then please let me know, this is my first attempt at one and I am not sure if there is a hidden workaround. it is essentially designed to redirect all term calls to my own functions, term.redirect is no good for my purpose as I am using multitasking and cannot figure out an elegant way of stopping programs altering other windows (getting off topic now).
So essentially, how do I call a program, with a sandbox like what explained above (redirects desired functions to my own (entire term library, print and write)) where I can also catch the error, much like a pcall on a function. I kind of know how to get a program into a function, but not with a sandbox.
local ok, err = pcal(func)
if not ok and err then
-- Good looking error screen explaining what went wrong
end
Thanks in advance.