Overview
50 | 51 |Coxpcall encapsulates the protected calls with a coroutine based loop,
52 | so errors can be dealed without the usual pcall
/xpcall
53 | issues with coroutines.
54 |
Using Coxpcall usually consists in simply loading the module and then
57 | replacing Lua pcall
, xpcall
and coroutine.running
58 | by copcall
, coxpcall
, and running
.
Coxpcall is free software and uses the same 61 | license as Lua 5.1 and 5.2. 62 |
63 | 64 |Status
65 | 66 |It supports Lua 5.1, 5.2 and 5.3. Lua 5.2 was extended with the Coxpcall 67 | functionality and hence coxpcall is no longer required. The 68 | 5.2+ compatibility by coxpcall means that it maintains backward compatibility 69 | while using the built-in implementation.
70 | 71 |Download
72 | 73 |74 | You can get Coxpcall using LuaRocks: 75 |
76 | 77 |78 | luarocks install coxpcall 79 |80 | 81 |
82 | See also its 83 | GitHub page. 84 |
85 | 86 |Reference
87 | 88 |Coxpcall module offers three functions that reproduce the behaviour of
89 | pcall
, xpcall
, and coroutine.running
:
92 | local coxpcall = require "coxpcall" 93 |94 | 95 |
-
96 |
coxpcall.xpcall(f, err)
97 | - Offers the same functionality as Lua
xpcall(f, err)
, 98 | but calls the error handler after the error unwinds the stack, 99 | so that it's impossible to gather more information about the error. 100 | To compensate, if the error is a string, stack trace is appended to it. 101 | Also set as globalcoxpcall
.
102 |
103 | coxpcall.pcall(f, ...)
104 | - Offers the same functionality as Lua
pcall(f, ...)
. 105 | Also set as globalcopcall
.
106 |
107 | coxpcall.running([coro])
108 | - Because
coxpcall
andcopcall
run the function to protect 109 | inside a new coroutine,coroutine.running()
will return an unexpected 110 | coroutine when used inside the protected function. If the coroutinecoro
was 111 | created by the coxpcall module, thenrunning(coro)
will return the original 112 | coroutine that created it. Ifcoro
is not provided, it will default to the 113 | currently running coroutine.
114 |
Credits
117 | 118 |119 | Coxpcall was designed and implemented by Roberto Ierusalimschy and 120 | André Carregal with the colaboration of Thomas Harning Jr., Ignacio Burgueño, 121 | Gary NG and Fábio Mascarenhas as part of the 122 | Kepler Project which holds its copyright. 123 |
124 | 125 |