Moonshark/runner/lua/sandbox.lua

37 lines
708 B
Lua

-- sandbox.lua
function __execute(script_func, ctx, response)
-- Store context and response globally for function access
__ctx = ctx
__response = response
_G.ctx = ctx
-- Create a coroutine for script execution to handle early exits
local co = coroutine.create(function()
return script_func()
end)
local ok, result = coroutine.resume(co)
-- Clean up
__ctx = nil
__response = nil
if not ok then
-- Real error during script execution
error(result, 0)
end
-- Check if exit was requested
if result == "__EXIT__" then
return {nil, response}
end
return {result, response}
end
-- Exit sentinel using coroutine yield instead of error
function exit()
coroutine.yield("__EXIT__")
end