aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2016-02-24 13:23:20 -0300
committerDiego Nehab <diego.nehab@gmail.com>2016-02-24 13:23:20 -0300
commitfe7b37acedd61f425b8d0b0531e78cc45554b332 (patch)
tree00f58b0d7dcb593fafaa69a4c45b9d76fbd24e2e
parent9ffd96724da09352abe99c8525bbccb6bc34cb48 (diff)
parent0341516a2932e077b547f8105e0ea13f3f56f868 (diff)
downloadluasocket-fe7b37acedd61f425b8d0b0531e78cc45554b332.tar.gz
luasocket-fe7b37acedd61f425b8d0b0531e78cc45554b332.tar.bz2
luasocket-fe7b37acedd61f425b8d0b0531e78cc45554b332.zip
Merge pull request #166 from siffiejoe/exception-tweaks
Exception tweaks
-rw-r--r--doc/socket.html13
-rw-r--r--src/except.c12
-rw-r--r--test/excepttest.lua1
3 files changed, 14 insertions, 12 deletions
diff --git a/doc/socket.html b/doc/socket.html
index a43a208..fec09c4 100644
--- a/doc/socket.html
+++ b/doc/socket.html
@@ -167,8 +167,7 @@ is raised.
167 167
168<p class=parameters> 168<p class=parameters>
169<tt>Finalizer</tt> is a function that will be called before 169<tt>Finalizer</tt> is a function that will be called before
170<tt>try</tt> throws the exception. It will be called 170<tt>try</tt> throws the exception.
171in <em>protected</em> mode.
172</p> 171</p>
173 172
174<p class=return> 173<p class=return>
@@ -216,8 +215,9 @@ to throw exceptions.
216</p> 215</p>
217 216
218<p class=return> 217<p class=return>
219Returns an equivalent function that instead of throwing exceptions, 218Returns an equivalent function that instead of throwing exceptions in case of
220returns <tt><b>nil</b></tt> followed by an error message. 219a failed <a href=#try><tt>try</tt></a> call, returns <tt><b>nil</b></tt>
220followed by an error message.
221</p> 221</p>
222 222
223<!-- select +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 223<!-- select +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -416,8 +416,9 @@ socket.<b>try(</b>ret<sub>1</sub> [, ret<sub>2</sub> ... ret<sub>N</sub>]<b>)</b
416</p> 416</p>
417 417
418<p class=description> 418<p class=description>
419Throws an exception in case of error. The exception can only be caught 419Throws an exception in case <tt>ret<sub>1</sub></tt> is falsy, using
420by the <a href=#protect><tt>protect</tt></a> function. 420<tt>ret<sub>2</sub></tt> as the error message. The exception is supposed to be caught
421by a <a href=#protect><tt>protect</tt></a>ed function only.
421</p> 422</p>
422 423
423<p class=parameters> 424<p class=parameters>
diff --git a/src/except.c b/src/except.c
index def35a0..60b5005 100644
--- a/src/except.c
+++ b/src/except.c
@@ -42,14 +42,14 @@ static void wrap(lua_State *L) {
42 lua_createtable(L, 1, 0); 42 lua_createtable(L, 1, 0);
43 lua_pushvalue(L, -2); 43 lua_pushvalue(L, -2);
44 lua_rawseti(L, -2, 1); 44 lua_rawseti(L, -2, 1);
45 lua_pushvalue(L, lua_upvalueindex(2)); 45 lua_pushvalue(L, lua_upvalueindex(1));
46 lua_setmetatable(L, -2); 46 lua_setmetatable(L, -2);
47} 47}
48 48
49static int finalize(lua_State *L) { 49static int finalize(lua_State *L) {
50 if (!lua_toboolean(L, 1)) { 50 if (!lua_toboolean(L, 1)) {
51 lua_pushvalue(L, lua_upvalueindex(1)); 51 lua_pushvalue(L, lua_upvalueindex(2));
52 lua_pcall(L, 0, 0, 0); 52 lua_call(L, 0, 0);
53 lua_settop(L, 2); 53 lua_settop(L, 2);
54 wrap(L); 54 wrap(L);
55 lua_error(L); 55 lua_error(L);
@@ -66,6 +66,7 @@ static int global_newtry(lua_State *L) {
66 lua_settop(L, 1); 66 lua_settop(L, 1);
67 if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing); 67 if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing);
68 lua_pushvalue(L, lua_upvalueindex(1)); 68 lua_pushvalue(L, lua_upvalueindex(1));
69 lua_insert(L, -2);
69 lua_pushcclosure(L, finalize, 2); 70 lua_pushcclosure(L, finalize, 2);
70 return 1; 71 return 1;
71} 72}
@@ -75,7 +76,7 @@ static int global_newtry(lua_State *L) {
75\*-------------------------------------------------------------------------*/ 76\*-------------------------------------------------------------------------*/
76static int unwrap(lua_State *L) { 77static int unwrap(lua_State *L) {
77 if (lua_istable(L, -1) && lua_getmetatable(L, -1)) { 78 if (lua_istable(L, -1) && lua_getmetatable(L, -1)) {
78 int r = lua_rawequal(L, -1, lua_upvalueindex(2)); 79 int r = lua_rawequal(L, -1, lua_upvalueindex(1));
79 lua_pop(L, 1); 80 lua_pop(L, 1);
80 if (r) { 81 if (r) {
81 lua_pushnil(L); 82 lua_pushnil(L);
@@ -106,7 +107,7 @@ static int protected_cont(lua_State *L) {
106 107
107static int protected_(lua_State *L) { 108static int protected_(lua_State *L) {
108 int status; 109 int status;
109 lua_pushvalue(L, lua_upvalueindex(1)); 110 lua_pushvalue(L, lua_upvalueindex(2));
110 lua_insert(L, 1); 111 lua_insert(L, 1);
111 status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont); 112 status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont);
112 return protected_finish(L, status, 0); 113 return protected_finish(L, status, 0);
@@ -115,6 +116,7 @@ static int protected_(lua_State *L) {
115static int global_protect(lua_State *L) { 116static int global_protect(lua_State *L) {
116 lua_settop(L, 1); 117 lua_settop(L, 1);
117 lua_pushvalue(L, lua_upvalueindex(1)); 118 lua_pushvalue(L, lua_upvalueindex(1));
119 lua_insert(L, 1);
118 lua_pushcclosure(L, protected_, 2); 120 lua_pushcclosure(L, protected_, 2);
119 return 1; 121 return 1;
120} 122}
diff --git a/test/excepttest.lua b/test/excepttest.lua
index 6904545..80c9cb8 100644
--- a/test/excepttest.lua
+++ b/test/excepttest.lua
@@ -5,7 +5,6 @@ local finalizer_called
5local func = socket.protect(function(err, ...) 5local func = socket.protect(function(err, ...)
6 local try = socket.newtry(function() 6 local try = socket.newtry(function()
7 finalizer_called = true 7 finalizer_called = true
8 error("ignored")
9 end) 8 end)
10 9
11 if err then 10 if err then