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