diff options
Diffstat (limited to 'src/luasocket.c')
| -rw-r--r-- | src/luasocket.c | 71 |
1 files changed, 68 insertions, 3 deletions
diff --git a/src/luasocket.c b/src/luasocket.c index ca3a52c..2b0a1fa 100644 --- a/src/luasocket.c +++ b/src/luasocket.c | |||
| @@ -26,7 +26,7 @@ | |||
| 26 | #include "luasocket.h" | 26 | #include "luasocket.h" |
| 27 | 27 | ||
| 28 | #include "auxiliar.h" | 28 | #include "auxiliar.h" |
| 29 | #include "base.h" | 29 | #include "except.h" |
| 30 | #include "timeout.h" | 30 | #include "timeout.h" |
| 31 | #include "buffer.h" | 31 | #include "buffer.h" |
| 32 | #include "inet.h" | 32 | #include "inet.h" |
| @@ -35,11 +35,18 @@ | |||
| 35 | #include "select.h" | 35 | #include "select.h" |
| 36 | 36 | ||
| 37 | /*-------------------------------------------------------------------------*\ | 37 | /*-------------------------------------------------------------------------*\ |
| 38 | * Modules | 38 | * Internal function prototypes |
| 39 | \*-------------------------------------------------------------------------*/ | ||
| 40 | static int global_skip(lua_State *L); | ||
| 41 | static int global_unload(lua_State *L); | ||
| 42 | static int base_open(lua_State *L); | ||
| 43 | |||
| 44 | /*-------------------------------------------------------------------------*\ | ||
| 45 | * Modules and functions | ||
| 39 | \*-------------------------------------------------------------------------*/ | 46 | \*-------------------------------------------------------------------------*/ |
| 40 | static const luaL_reg mod[] = { | 47 | static const luaL_reg mod[] = { |
| 41 | {"auxiliar", aux_open}, | 48 | {"auxiliar", aux_open}, |
| 42 | {"base", base_open}, | 49 | {"except", except_open}, |
| 43 | {"timeout", tm_open}, | 50 | {"timeout", tm_open}, |
| 44 | {"buffer", buf_open}, | 51 | {"buffer", buf_open}, |
| 45 | {"inet", inet_open}, | 52 | {"inet", inet_open}, |
| @@ -49,11 +56,69 @@ static const luaL_reg mod[] = { | |||
| 49 | {NULL, NULL} | 56 | {NULL, NULL} |
| 50 | }; | 57 | }; |
| 51 | 58 | ||
| 59 | static luaL_reg func[] = { | ||
| 60 | {"skip", global_skip}, | ||
| 61 | {"__unload", global_unload}, | ||
| 62 | {NULL, NULL} | ||
| 63 | }; | ||
| 64 | |||
| 65 | /*-------------------------------------------------------------------------*\ | ||
| 66 | * Skip a few arguments | ||
| 67 | \*-------------------------------------------------------------------------*/ | ||
| 68 | static int global_skip(lua_State *L) { | ||
| 69 | int amount = luaL_checkint(L, 1); | ||
| 70 | int ret = lua_gettop(L) - amount - 1; | ||
| 71 | return ret >= 0 ? ret : 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | /*-------------------------------------------------------------------------*\ | ||
| 75 | * Unloads the library | ||
| 76 | \*-------------------------------------------------------------------------*/ | ||
| 77 | static int global_unload(lua_State *L) { | ||
| 78 | sock_close(); | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | /*-------------------------------------------------------------------------*\ | ||
| 83 | * Setup basic stuff. | ||
| 84 | \*-------------------------------------------------------------------------*/ | ||
| 85 | static int base_open(lua_State *L) { | ||
| 86 | if (sock_open()) { | ||
| 87 | /* whoever is loading the library replaced the global environment | ||
| 88 | * with the namespace table */ | ||
| 89 | lua_pushvalue(L, LUA_GLOBALSINDEX); | ||
| 90 | /* make sure library is still "requirable" if initialized staticaly */ | ||
| 91 | lua_pushstring(L, "_LOADEDLIB"); | ||
| 92 | lua_gettable(L, -2); | ||
| 93 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
| 94 | lua_pushcfunction(L, (lua_CFunction) luaopen_socket); | ||
| 95 | lua_settable(L, -3); | ||
| 96 | lua_pop(L, 1); | ||
| 97 | #ifdef LUASOCKET_DEBUG | ||
| 98 | lua_pushstring(L, "DEBUG"); | ||
| 99 | lua_pushboolean(L, 1); | ||
| 100 | lua_rawset(L, -3); | ||
| 101 | #endif | ||
| 102 | /* make version string available to scripts */ | ||
| 103 | lua_pushstring(L, "VERSION"); | ||
| 104 | lua_pushstring(L, LUASOCKET_VERSION); | ||
| 105 | lua_rawset(L, -3); | ||
| 106 | /* export other functions */ | ||
| 107 | luaL_openlib(L, NULL, func, 0); | ||
| 108 | return 1; | ||
| 109 | } else { | ||
| 110 | lua_pushstring(L, "unable to initialize library"); | ||
| 111 | lua_error(L); | ||
| 112 | return 0; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 52 | /*-------------------------------------------------------------------------*\ | 116 | /*-------------------------------------------------------------------------*\ |
| 53 | * Initializes all library modules. | 117 | * Initializes all library modules. |
| 54 | \*-------------------------------------------------------------------------*/ | 118 | \*-------------------------------------------------------------------------*/ |
| 55 | LUASOCKET_API int luaopen_socket(lua_State *L) { | 119 | LUASOCKET_API int luaopen_socket(lua_State *L) { |
| 56 | int i; | 120 | int i; |
| 121 | base_open(L); | ||
| 57 | for (i = 0; mod[i].name; i++) mod[i].func(L); | 122 | for (i = 0; mod[i].name; i++) mod[i].func(L); |
| 58 | return 1; | 123 | return 1; |
| 59 | } | 124 | } |
