1 /*=========================================================================*\
2 * Simple exception support
4 \*=========================================================================*/
12 /*=========================================================================*\
13 * Internal function prototypes.
14 \*=========================================================================*/
15 static int global_protect(lua_State
*L
);
16 static int global_newtry(lua_State
*L
);
17 static int protected_(lua_State
*L
);
18 static int finalize(lua_State
*L
);
19 static int do_nothing(lua_State
*L
);
21 /* except functions */
22 static luaL_Reg func
[] = {
23 {"newtry", global_newtry
},
24 {"protect", global_protect
},
28 /*-------------------------------------------------------------------------*\
30 \*-------------------------------------------------------------------------*/
31 static void wrap(lua_State
*L
) {
40 static int finalize(lua_State
*L
) {
41 if (!lua_toboolean(L
, 1)) {
42 lua_pushvalue(L
, lua_upvalueindex(1));
43 lua_pcall(L
, 0, 0, 0);
48 } else return lua_gettop(L
);
51 static int do_nothing(lua_State
*L
) {
56 static int global_newtry(lua_State
*L
) {
58 if (lua_isnil(L
, 1)) lua_pushcfunction(L
, do_nothing
);
59 lua_pushcclosure(L
, finalize
, 1);
63 /*-------------------------------------------------------------------------*\
65 \*-------------------------------------------------------------------------*/
66 static int unwrap(lua_State
*L
) {
67 if (lua_istable(L
, -1)) {
76 static int protected_(lua_State
*L
) {
77 lua_pushvalue(L
, lua_upvalueindex(1));
79 if (lua_pcall(L
, lua_gettop(L
) - 1, LUA_MULTRET
, 0) != 0) {
80 if (unwrap(L
)) return 2;
83 } else return lua_gettop(L
);
86 static int global_protect(lua_State
*L
) {
87 lua_pushcclosure(L
, protected_
, 1);
91 /*-------------------------------------------------------------------------*\
93 \*-------------------------------------------------------------------------*/
94 int except_open(lua_State
*L
) {
95 luaL_openlib(L
, NULL
, func
, 0);