-------------------------------------------------------------------------------
-- Coroutine safe xpcall and pcall versions
--
-- Encapsulates the protected calls with a coroutine based loop, so errors can
-- be dealed without the usual Lua 5.x pcall/xpcall issues with coroutines
-- yielding inside the call to pcall or xpcall.
--
-- Authors: Roberto Ierusalimschy and Andre Carregal
-- Contributors: Thomas Harning Jr., Ignacio Burgue�o, F�bio Mascarenhas
--
-- Copyright 2005 - Kepler Project (www.keplerproject.org)
--
-- $Id: coxpcall.lua,v 1.13 2008/05/19 19:20:02 mascarenhas Exp $
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Implements xpcall with coroutines
-------------------------------------------------------------------------------
local performResume, handleReturnValue
local oldpcall, oldxpcall = pcall, xpcall
function handleReturnValue(err, co, status, ...)
if not status then
return false, err(debug.traceback(co, (...)), ...)
end
if coroutine.status(co) == 'suspended' then
return performResume(err, co, coroutine.yield(...))
else
return true, ...
end
end
function performResume(err, co, ...)
return handleReturnValue(err, co, coroutine.resume(co, ...))
end
function coxpcall(f, err, ...)
local res, co = oldpcall(coroutine.create, f)
if not res then
local params = {...}
local newf = function() return f(unpack(params)) end
co = coroutine.create(newf)
end
return performResume(err, co, ...)
end
-------------------------------------------------------------------------------
-- Implements pcall with coroutines
-------------------------------------------------------------------------------
local function id(trace, ...)
return ...
end
function copcall(f, ...)
return coxpcall(f, id, ...)
end
local _lib
if package.loaded.core then
_lib = true
else
_lib = false
require "core"
end
_exports = {}
local _main = function()
local object = object
local array = array
local number = number
local string = base_string
local symbol = symbol
local exception = exception
local hash = hash
local regex = regex
local _self = object
local _type = type
local _error = error
local _tostring = tostring
local brat_function = brat_function
local _lifted_call = _lifted_call
local _rawget = rawget
local _table = table
local _lifted = {}
setfenv(1, {})
local _lifted_strings = {
symbol:new('opt'),
symbol:new('number'),
}
local _temp1
local _temp2
local _m_vim
if vim then
_m_vim = vim
else
_m_vim = _self["vim"]
end
if object._is_callable(_m_vim) then
_temp2 = _m_vim(_self)
elseif _m_vim then
_temp2 = _m_vim
elseif _self.no_undermethod then
_temp2 = _self:no_undermethod(string:new('vim'))
else
_error(exception:name_error("vim"))
end
_m_vim = nil
if export then
_temp1 = export(_self, _temp2)
else
local _m__self_export = _self.export
if object._is_callable(_m__self_export) then
_temp1 = _m__self_export(_self, _temp2)
elseif _m__self_export ~= nil then
_error(exception:argument_error('function', 0, 0))
elseif _self.no_undermethod then
_temp1 = _self:no_undermethod(string:new('export'), _temp2)
else
_error(exception:method_error(_self, 'export'))
end
_m__self_export = nil
end
local _temp3
local _m_vim
if vim then
_m_vim = vim
else
_m_vim = _self["vim"]
end
if object._is_callable(_m_vim) then
_temp2 = _m_vim(_self)
elseif _m_vim then
_temp2 = _m_vim
elseif _self.no_undermethod then
_temp2 = _self:no_undermethod(string:new('vim'))
else
_error(exception:name_error("vim"))
end
_m_vim = nil
local _temp4 = _lifted_strings[1]
if _type(_temp2) == 'number' then
_temp2 = number:new(_temp2)
elseif object._is_callable(_temp2) then
_temp2 = brat_function:new(_temp2)
end
local _m__temp2_get = _temp2.get
if object._is_callable(_m__temp2_get) then
_temp3 = _m__temp2_get(_temp2, _temp4)
elseif _m__temp2_get ~= nil then
_error(exception:argument_error('function', 0, 0))
elseif _temp2.no_undermethod then
_temp3 = _temp2:no_undermethod(string:new('get'), _temp4)
else
_error(exception:method_error(_temp2, 'get'))
end
_m__temp2_get = nil
_temp4 = _lifted_strings[2]
local _temp5
local _m__true
if _true then
_m__true = _true
else
_m__true = _self["_true"]
end
if object._is_callable(_m__true) then
_temp5 = _m__true(_self)
elseif _m__true then
_temp5 = _m__true
elseif _self.no_undermethod then
_temp5 = _self:no_undermethod(string:new('_true'))
else
_error(exception:name_error("_true"))
end
_m__true = nil
if _type(_temp3) == 'number' then
_temp3 = number:new(_temp3)
elseif object._is_callable(_temp3) then
_temp3 = brat_function:new(_temp3)
end
local _m__temp3_set = _temp3.set
if object._is_callable(_m__temp3_set) then
_temp2 = _m__temp3_set(_temp3, _temp4, _temp5)
elseif _m__temp3_set ~= nil then
_error(exception:argument_error('function', 0, 1))
elseif _temp3.no_undermethod then
_temp2 = _temp3:no_undermethod(string:new('set'), _temp4, _temp5)
else
_error(exception:method_error(_temp3, 'set'))
end
_m__temp3_set = nil
end
local _result = coxpcall(_main, exception._handler)
if not _lib then
if not _result then
os.exit(-1)
else
os.exit(0)
end
end