robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1------------------------------------------------------------------------------- 2-- (Not needed for LuaJIT or Lua 5.2+) 3-- 4-- Coroutine safe xpcall and pcall versions 5-- 6-- https://keplerproject.github.io/coxpcall/ 7-- 8-- Encapsulates the protected calls with a coroutine based loop, so errors can 9-- be dealed without the usual Lua 5.x pcall/xpcall issues with coroutines 10-- yielding inside the call to pcall or xpcall. 11-- 12-- Authors: Roberto Ierusalimschy and Andre Carregal 13-- Contributors: Thomas Harning Jr., Ignacio Burgueño, Fabio Mascarenhas 14-- 15-- Copyright 2005 - Kepler Project 16-- 17-- $Id: coxpcall.lua,v 1.13 2008/05/19 19:20:02 mascarenhas Exp $ 18------------------------------------------------------------------------------- 19 20------------------------------------------------------------------------------- 21-- Checks if (x)pcall function is coroutine safe 22------------------------------------------------------------------------------- 23--- @param func function 24local function isCoroutineSafe ( func ) 25local co =coroutine . create ( function () 26return func ( coroutine . yield , function () end ) 27end ) 28 29coroutine . resume ( co ) 30return coroutine . resume ( co ) 31end 32 33-- No need to do anything if pcall and xpcall are already safe. 34if isCoroutineSafe ( pcall ) and isCoroutineSafe ( xpcall ) then 35_G . copcall =pcall 36_G . coxpcall =xpcall 37return { pcall =pcall , xpcall =xpcall , running =coroutine . running } 38end 39 40------------------------------------------------------------------------------- 41-- Implements xpcall with coroutines 42------------------------------------------------------------------------------- 43---@type fun(err: function, co: thread, ...: any): boolean, any... 44local performResume 45local oldpcall , oldxpcall =pcall , xpcall 46local pack =table . pack or function ( ... ) return { n =select ( "#" , ... ), ... } end 47local unpack =table . unpack or unpack 48local running =coroutine . running 49--- @type table<thread,thread> 50local coromap =setmetatable ( {} , { __mode ="k" } ) 51 52--- @async 53--- @param err function 54--- @param co thread 55--- @param status boolean 56--- @param ... any 57--- @return boolean, any... 58local function handleReturnValue ( err , co , status , ... ) 59if not status then 60return false , err ( debug . traceback ( co , ( ... )), ... ) 61end 62if coroutine . status ( co ) == 'suspended' then 63return performResume ( err , co , coroutine . yield ( ... )) 64else 65return true , ... 66end 67end 68 69--- @async 70--- @param err function 71--- @param co thread 72--- @param ... any 73function performResume ( err , co , ... ) 74return handleReturnValue ( err , co , coroutine . resume ( co , ... )) 75end 76 77--- @diagnostic disable-next-line: unused-vararg 78--- @generic T 79--- @param trace T 80--- @param ... any 81local function id ( trace , ... ) 82return trace 83end 84 85--- @param f function 86--- @param err function 87--- @param ... any 88--- @return boolean, any... 89function _G . coxpcall ( f , err , ... ) 90local current =running () 91if not current then 92if err == id then 93return oldpcall ( f , ... ) 94else 95if select ( "#" , ... ) > 0 then 96local oldf , params =f , pack ( ... ) 97f =function () return oldf ( unpack ( params , 1 , params . n )) end 98end 99return oldxpcall ( f , err ) 100end 101else 102local res , co =oldpcall ( coroutine . create , f ) 103if not res then 104--- @param ... any 105local newf =function ( ... ) return f ( ... ) end 106co =coroutine . create ( newf ) 107end 108coromap [ co ] =current 109-- This branch only runs inside a coroutine. 110--- @diagnostic disable-next-line: await-in-sync 111return performResume ( err , co , ... ) 112end 113end 114 115--- @param coro? thread 116local function corunning ( coro ) 117if coro ~= nil then 118assert ( type ( coro ) == "thread" , "Bad argument; expected thread, got: " .. type ( coro )) 119else 120coro =running () 121end 122while coromap [ coro ] do 123coro =coromap [ coro ] 124end 125if coro == "mainthread" then return nil end 126return coro 127end 128 129------------------------------------------------------------------------------- 130-- Implements pcall with coroutines 131------------------------------------------------------------------------------- 132 133--- @param f function 134--- @param ... any 135--- @return boolean, any... 136function _G . copcall ( f , ... ) 137-- EmmyLua does not distribute the union of xpcall return tuples. 138--- @diagnostic disable-next-line: return-type-mismatch 139return coxpcall ( f , id , ... ) 140end 141 142return { pcall =copcall , xpcall =coxpcall , running =corunning }