diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-18 08:02:09 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-18 08:02:09 +0000 |
| commit | ac4aac0909da26befaaeb6b415f66cf35b6980e0 (patch) | |
| tree | 3d3289e6192508484dcbefa10e2d862c5cc06d64 /src/except.c | |
| parent | 62799a416d2b29d8058331f3d8725fb67c75d261 (diff) | |
| download | luasocket-ac4aac0909da26befaaeb6b415f66cf35b6980e0.tar.gz luasocket-ac4aac0909da26befaaeb6b415f66cf35b6980e0.tar.bz2 luasocket-ac4aac0909da26befaaeb6b415f66cf35b6980e0.zip | |
Implemented safe exceptions. This looks preeety good.
Diffstat (limited to 'src/except.c')
| -rw-r--r-- | src/except.c | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/except.c b/src/except.c index 0482e1c..53a65ac 100644 --- a/src/except.c +++ b/src/except.c | |||
| @@ -14,17 +14,20 @@ | |||
| 14 | \*=========================================================================*/ | 14 | \*=========================================================================*/ |
| 15 | static int global_try(lua_State *L); | 15 | static int global_try(lua_State *L); |
| 16 | static int global_protect(lua_State *L); | 16 | static int global_protect(lua_State *L); |
| 17 | static int global_newtry(lua_State *L); | ||
| 17 | static int protected(lua_State *L); | 18 | static int protected(lua_State *L); |
| 19 | static int finalize(lua_State *L); | ||
| 18 | 20 | ||
| 19 | /* except functions */ | 21 | /* except functions */ |
| 20 | static luaL_reg func[] = { | 22 | static luaL_reg func[] = { |
| 21 | {"try", global_try}, | 23 | {"try", global_try}, |
| 22 | {"protect", global_protect}, | 24 | {"newtry", global_newtry}, |
| 23 | {NULL, NULL} | 25 | {"protect", global_protect}, |
| 26 | {NULL, NULL} | ||
| 24 | }; | 27 | }; |
| 25 | 28 | ||
| 26 | /*-------------------------------------------------------------------------*\ | 29 | /*-------------------------------------------------------------------------*\ |
| 27 | * Exception handling: try method | 30 | * Try method |
| 28 | \*-------------------------------------------------------------------------*/ | 31 | \*-------------------------------------------------------------------------*/ |
| 29 | static int global_try(lua_State *L) { | 32 | static int global_try(lua_State *L) { |
| 30 | if (lua_isnil(L, 1) || (lua_isboolean(L, 1) && !lua_toboolean(L, 1))) { | 33 | if (lua_isnil(L, 1) || (lua_isboolean(L, 1) && !lua_toboolean(L, 1))) { |
| @@ -35,7 +38,25 @@ static int global_try(lua_State *L) { | |||
| 35 | } | 38 | } |
| 36 | 39 | ||
| 37 | /*-------------------------------------------------------------------------*\ | 40 | /*-------------------------------------------------------------------------*\ |
| 38 | * Exception handling: protect factory | 41 | * Finalizer factory |
| 42 | \*-------------------------------------------------------------------------*/ | ||
| 43 | static int finalize(lua_State *L) { | ||
| 44 | if (lua_isnil(L, 1) || (lua_isboolean(L, 1) && !lua_toboolean(L, 1))) { | ||
| 45 | lua_pushvalue(L, lua_upvalueindex(1)); | ||
| 46 | lua_pcall(L, 0, 0, 0); | ||
| 47 | lua_settop(L, 2); | ||
| 48 | lua_error(L); | ||
| 49 | return 0; | ||
| 50 | } else return lua_gettop(L); | ||
| 51 | } | ||
| 52 | |||
| 53 | static int global_newtry(lua_State *L) { | ||
| 54 | lua_pushcclosure(L, finalize, 1); | ||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | /*-------------------------------------------------------------------------*\ | ||
| 59 | * Protect factory | ||
| 39 | \*-------------------------------------------------------------------------*/ | 60 | \*-------------------------------------------------------------------------*/ |
| 40 | static int protected(lua_State *L) { | 61 | static int protected(lua_State *L) { |
| 41 | lua_pushvalue(L, lua_upvalueindex(1)); | 62 | lua_pushvalue(L, lua_upvalueindex(1)); |
| @@ -48,7 +69,6 @@ static int protected(lua_State *L) { | |||
| 48 | } | 69 | } |
| 49 | 70 | ||
| 50 | static int global_protect(lua_State *L) { | 71 | static int global_protect(lua_State *L) { |
| 51 | lua_insert(L, 1); | ||
| 52 | lua_pushcclosure(L, protected, 1); | 72 | lua_pushcclosure(L, protected, 1); |
| 53 | return 1; | 73 | return 1; |
| 54 | } | 74 | } |
