From 9fe38c654f6c62a4f11d993bd79c710af9313fbd Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Wed, 24 Feb 2016 00:48:43 +0100 Subject: [PATCH 1/3] Don't swallow errors in finalizers. --- doc/socket.html | 3 +-- src/except.c | 2 +- test/excepttest.lua | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/doc/socket.html b/doc/socket.html index a43a208..08ef784 100644 --- a/doc/socket.html +++ b/doc/socket.html @@ -167,8 +167,7 @@ is raised.

Finalizer is a function that will be called before -try throws the exception. It will be called -in protected mode. +try throws the exception.

diff --git a/src/except.c b/src/except.c index def35a0..309f8f0 100644 --- a/src/except.c +++ b/src/except.c @@ -49,7 +49,7 @@ static void wrap(lua_State *L) { static int finalize(lua_State *L) { if (!lua_toboolean(L, 1)) { lua_pushvalue(L, lua_upvalueindex(1)); - lua_pcall(L, 0, 0, 0); + lua_call(L, 0, 0); lua_settop(L, 2); wrap(L); lua_error(L); 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 local func = socket.protect(function(err, ...) local try = socket.newtry(function() finalizer_called = true - error("ignored") end) if err then From 4392bdcdd40dd433fc8bf4347653ce1a796cb572 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Wed, 24 Feb 2016 00:57:42 +0100 Subject: [PATCH 2/3] Always put metatable in first upvalue. --- src/except.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/except.c b/src/except.c index 309f8f0..60b5005 100644 --- a/src/except.c +++ b/src/except.c @@ -42,13 +42,13 @@ static void wrap(lua_State *L) { lua_createtable(L, 1, 0); lua_pushvalue(L, -2); lua_rawseti(L, -2, 1); - lua_pushvalue(L, lua_upvalueindex(2)); + lua_pushvalue(L, lua_upvalueindex(1)); lua_setmetatable(L, -2); } static int finalize(lua_State *L) { if (!lua_toboolean(L, 1)) { - lua_pushvalue(L, lua_upvalueindex(1)); + lua_pushvalue(L, lua_upvalueindex(2)); lua_call(L, 0, 0); lua_settop(L, 2); wrap(L); @@ -66,6 +66,7 @@ static int global_newtry(lua_State *L) { lua_settop(L, 1); if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing); lua_pushvalue(L, lua_upvalueindex(1)); + lua_insert(L, -2); lua_pushcclosure(L, finalize, 2); return 1; } @@ -75,7 +76,7 @@ static int global_newtry(lua_State *L) { \*-------------------------------------------------------------------------*/ static int unwrap(lua_State *L) { 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); if (r) { lua_pushnil(L); @@ -106,7 +107,7 @@ static int protected_cont(lua_State *L) { static int protected_(lua_State *L) { int status; - lua_pushvalue(L, lua_upvalueindex(1)); + lua_pushvalue(L, lua_upvalueindex(2)); lua_insert(L, 1); status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont); return protected_finish(L, status, 0); @@ -115,6 +116,7 @@ static int protected_(lua_State *L) { static int global_protect(lua_State *L) { lua_settop(L, 1); lua_pushvalue(L, lua_upvalueindex(1)); + lua_insert(L, 1); lua_pushcclosure(L, protected_, 2); return 1; } From 0341516a2932e077b547f8105e0ea13f3f56f868 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Wed, 24 Feb 2016 06:59:37 +0100 Subject: [PATCH 3/3] Clarify documentation for try/protect. --- doc/socket.html | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/socket.html b/doc/socket.html index 08ef784..fec09c4 100644 --- a/doc/socket.html +++ b/doc/socket.html @@ -215,8 +215,9 @@ to throw exceptions.

-Returns an equivalent function that instead of throwing exceptions, -returns nil followed by an error message. +Returns an equivalent function that instead of throwing exceptions in case of +a failed try call, returns nil +followed by an error message.

@@ -415,8 +416,9 @@ socket.try(ret1 [, ret2 ... retN])

-Throws an exception in case of error. The exception can only be caught -by the protect function. +Throws an exception in case ret1 is falsy, using +ret2 as the error message. The exception is supposed to be caught +by a protected function only.