aboutsummaryrefslogtreecommitdiff
path: root/src/except.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/except.c')
-rw-r--r--src/except.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/except.c b/src/except.c
index 80d7e5d..dabaf19 100644
--- a/src/except.c
+++ b/src/except.c
@@ -29,11 +29,21 @@ static luaL_reg func[] = {
29/*-------------------------------------------------------------------------*\ 29/*-------------------------------------------------------------------------*\
30* Try factory 30* Try factory
31\*-------------------------------------------------------------------------*/ 31\*-------------------------------------------------------------------------*/
32static void wrap(lua_State *L) {
33 lua_newtable(L);
34 lua_pushnumber(L, 1);
35 lua_pushvalue(L, -3);
36 lua_settable(L, -3);
37 lua_insert(L, -2);
38 lua_pop(L, 1);
39}
40
32static int finalize(lua_State *L) { 41static int finalize(lua_State *L) {
33 if (!lua_toboolean(L, 1)) { 42 if (!lua_toboolean(L, 1)) {
34 lua_pushvalue(L, lua_upvalueindex(1)); 43 lua_pushvalue(L, lua_upvalueindex(1));
35 lua_pcall(L, 0, 0, 0); 44 lua_pcall(L, 0, 0, 0);
36 lua_settop(L, 2); 45 lua_settop(L, 2);
46 wrap(L);
37 lua_error(L); 47 lua_error(L);
38 return 0; 48 return 0;
39 } else return lua_gettop(L); 49 } else return lua_gettop(L);
@@ -54,13 +64,23 @@ static int global_newtry(lua_State *L) {
54/*-------------------------------------------------------------------------*\ 64/*-------------------------------------------------------------------------*\
55* Protect factory 65* Protect factory
56\*-------------------------------------------------------------------------*/ 66\*-------------------------------------------------------------------------*/
67static int unwrap(lua_State *L) {
68 if (lua_istable(L, -1)) {
69 lua_pushnumber(L, 1);
70 lua_gettable(L, -2);
71 lua_pushnil(L);
72 lua_insert(L, -2);
73 return 1;
74 } else return 0;
75}
76
57static int protected_(lua_State *L) { 77static int protected_(lua_State *L) {
58 lua_pushvalue(L, lua_upvalueindex(1)); 78 lua_pushvalue(L, lua_upvalueindex(1));
59 lua_insert(L, 1); 79 lua_insert(L, 1);
60 if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) { 80 if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) {
61 lua_pushnil(L); 81 if (unwrap(L)) return 2;
62 lua_insert(L, 1); 82 else lua_error(L);
63 return 2; 83 return 0;
64 } else return lua_gettop(L); 84 } else return lua_gettop(L);
65} 85}
66 86