mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 04:28:20 +01:00
Merge pull request #166 from siffiejoe/exception-tweaks
Exception tweaks
This commit is contained in:
commit
fe7b37aced
@ -167,8 +167,7 @@ is raised.
|
|||||||
|
|
||||||
<p class=parameters>
|
<p class=parameters>
|
||||||
<tt>Finalizer</tt> is a function that will be called before
|
<tt>Finalizer</tt> is a function that will be called before
|
||||||
<tt>try</tt> throws the exception. It will be called
|
<tt>try</tt> throws the exception.
|
||||||
in <em>protected</em> mode.
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class=return>
|
<p class=return>
|
||||||
@ -216,8 +215,9 @@ to throw exceptions.
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class=return>
|
<p class=return>
|
||||||
Returns an equivalent function that instead of throwing exceptions,
|
Returns an equivalent function that instead of throwing exceptions in case of
|
||||||
returns <tt><b>nil</b></tt> followed by an error message.
|
a failed <a href=#try><tt>try</tt></a> call, returns <tt><b>nil</b></tt>
|
||||||
|
followed by an error message.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<!-- select +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
<!-- select +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||||
@ -416,8 +416,9 @@ socket.<b>try(</b>ret<sub>1</sub> [, ret<sub>2</sub> ... ret<sub>N</sub>]<b>)</b
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class=description>
|
<p class=description>
|
||||||
Throws an exception in case of error. The exception can only be caught
|
Throws an exception in case <tt>ret<sub>1</sub></tt> is falsy, using
|
||||||
by the <a href=#protect><tt>protect</tt></a> function.
|
<tt>ret<sub>2</sub></tt> as the error message. The exception is supposed to be caught
|
||||||
|
by a <a href=#protect><tt>protect</tt></a>ed function only.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class=parameters>
|
<p class=parameters>
|
||||||
|
12
src/except.c
12
src/except.c
@ -42,14 +42,14 @@ static void wrap(lua_State *L) {
|
|||||||
lua_createtable(L, 1, 0);
|
lua_createtable(L, 1, 0);
|
||||||
lua_pushvalue(L, -2);
|
lua_pushvalue(L, -2);
|
||||||
lua_rawseti(L, -2, 1);
|
lua_rawseti(L, -2, 1);
|
||||||
lua_pushvalue(L, lua_upvalueindex(2));
|
lua_pushvalue(L, lua_upvalueindex(1));
|
||||||
lua_setmetatable(L, -2);
|
lua_setmetatable(L, -2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int finalize(lua_State *L) {
|
static int finalize(lua_State *L) {
|
||||||
if (!lua_toboolean(L, 1)) {
|
if (!lua_toboolean(L, 1)) {
|
||||||
lua_pushvalue(L, lua_upvalueindex(1));
|
lua_pushvalue(L, lua_upvalueindex(2));
|
||||||
lua_pcall(L, 0, 0, 0);
|
lua_call(L, 0, 0);
|
||||||
lua_settop(L, 2);
|
lua_settop(L, 2);
|
||||||
wrap(L);
|
wrap(L);
|
||||||
lua_error(L);
|
lua_error(L);
|
||||||
@ -66,6 +66,7 @@ static int global_newtry(lua_State *L) {
|
|||||||
lua_settop(L, 1);
|
lua_settop(L, 1);
|
||||||
if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing);
|
if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing);
|
||||||
lua_pushvalue(L, lua_upvalueindex(1));
|
lua_pushvalue(L, lua_upvalueindex(1));
|
||||||
|
lua_insert(L, -2);
|
||||||
lua_pushcclosure(L, finalize, 2);
|
lua_pushcclosure(L, finalize, 2);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -75,7 +76,7 @@ static int global_newtry(lua_State *L) {
|
|||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
static int unwrap(lua_State *L) {
|
static int unwrap(lua_State *L) {
|
||||||
if (lua_istable(L, -1) && lua_getmetatable(L, -1)) {
|
if (lua_istable(L, -1) && lua_getmetatable(L, -1)) {
|
||||||
int r = lua_rawequal(L, -1, lua_upvalueindex(2));
|
int r = lua_rawequal(L, -1, lua_upvalueindex(1));
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
if (r) {
|
if (r) {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
@ -106,7 +107,7 @@ static int protected_cont(lua_State *L) {
|
|||||||
|
|
||||||
static int protected_(lua_State *L) {
|
static int protected_(lua_State *L) {
|
||||||
int status;
|
int status;
|
||||||
lua_pushvalue(L, lua_upvalueindex(1));
|
lua_pushvalue(L, lua_upvalueindex(2));
|
||||||
lua_insert(L, 1);
|
lua_insert(L, 1);
|
||||||
status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont);
|
status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont);
|
||||||
return protected_finish(L, status, 0);
|
return protected_finish(L, status, 0);
|
||||||
@ -115,6 +116,7 @@ static int protected_(lua_State *L) {
|
|||||||
static int global_protect(lua_State *L) {
|
static int global_protect(lua_State *L) {
|
||||||
lua_settop(L, 1);
|
lua_settop(L, 1);
|
||||||
lua_pushvalue(L, lua_upvalueindex(1));
|
lua_pushvalue(L, lua_upvalueindex(1));
|
||||||
|
lua_insert(L, 1);
|
||||||
lua_pushcclosure(L, protected_, 2);
|
lua_pushcclosure(L, protected_, 2);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ local finalizer_called
|
|||||||
local func = socket.protect(function(err, ...)
|
local func = socket.protect(function(err, ...)
|
||||||
local try = socket.newtry(function()
|
local try = socket.newtry(function()
|
||||||
finalizer_called = true
|
finalizer_called = true
|
||||||
error("ignored")
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if err then
|
if err then
|
||||||
|
Loading…
Reference in New Issue
Block a user