comparison src/except.c @ 0:4b915342e2a8

LuaSocket 2.0.2 + CMake build description.
author Eric Wing <ewing . public |-at-| gmail . com>
date Tue, 26 Aug 2008 18:40:01 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4b915342e2a8
1 /*=========================================================================*\
2 * Simple exception support
3 * LuaSocket toolkit
4 *
5 * RCS ID: $Id: except.c,v 1.8 2005/09/29 06:11:41 diego Exp $
6 \*=========================================================================*/
7 #include <stdio.h>
8
9 #include "lua.h"
10 #include "lauxlib.h"
11
12 #include "except.h"
13
14 /*=========================================================================*\
15 * Internal function prototypes.
16 \*=========================================================================*/
17 static int global_protect(lua_State *L);
18 static int global_newtry(lua_State *L);
19 static int protected_(lua_State *L);
20 static int finalize(lua_State *L);
21 static int do_nothing(lua_State *L);
22
23 /* except functions */
24 static luaL_reg func[] = {
25 {"newtry", global_newtry},
26 {"protect", global_protect},
27 {NULL, NULL}
28 };
29
30 /*-------------------------------------------------------------------------*\
31 * Try factory
32 \*-------------------------------------------------------------------------*/
33 static void wrap(lua_State *L) {
34 lua_newtable(L);
35 lua_pushnumber(L, 1);
36 lua_pushvalue(L, -3);
37 lua_settable(L, -3);
38 lua_insert(L, -2);
39 lua_pop(L, 1);
40 }
41
42 static int finalize(lua_State *L) {
43 if (!lua_toboolean(L, 1)) {
44 lua_pushvalue(L, lua_upvalueindex(1));
45 lua_pcall(L, 0, 0, 0);
46 lua_settop(L, 2);
47 wrap(L);
48 lua_error(L);
49 return 0;
50 } else return lua_gettop(L);
51 }
52
53 static int do_nothing(lua_State *L) {
54 (void) L;
55 return 0;
56 }
57
58 static int global_newtry(lua_State *L) {
59 lua_settop(L, 1);
60 if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing);
61 lua_pushcclosure(L, finalize, 1);
62 return 1;
63 }
64
65 /*-------------------------------------------------------------------------*\
66 * Protect factory
67 \*-------------------------------------------------------------------------*/
68 static int unwrap(lua_State *L) {
69 if (lua_istable(L, -1)) {
70 lua_pushnumber(L, 1);
71 lua_gettable(L, -2);
72 lua_pushnil(L);
73 lua_insert(L, -2);
74 return 1;
75 } else return 0;
76 }
77
78 static int protected_(lua_State *L) {
79 lua_pushvalue(L, lua_upvalueindex(1));
80 lua_insert(L, 1);
81 if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) {
82 if (unwrap(L)) return 2;
83 else lua_error(L);
84 return 0;
85 } else return lua_gettop(L);
86 }
87
88 static int global_protect(lua_State *L) {
89 lua_pushcclosure(L, protected_, 1);
90 return 1;
91 }
92
93 /*-------------------------------------------------------------------------*\
94 * Init module
95 \*-------------------------------------------------------------------------*/
96 int except_open(lua_State *L) {
97 luaL_openlib(L, NULL, func, 0);
98 return 0;
99 }