moonbridge
diff moonbridge_io.c @ 152:2a5bd37034c6
Removed moonbridge_io.run(...); yielding read and write methods do not yield any values
author | jbe |
---|---|
date | Fri May 15 16:42:50 2015 +0200 (2015-05-15) |
parents | c820130f55d7 |
children | 08cf8e1865e9 |
line diff
1.1 --- a/moonbridge_io.c Fri May 08 03:35:32 2015 +0200 1.2 +++ b/moonbridge_io.c Fri May 15 16:42:50 2015 +0200 1.3 @@ -39,9 +39,6 @@ 1.4 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public" 1.5 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener" 1.6 1.7 -char moonbr_io_block_udata = 0; 1.8 -char moonbr_io_multiblock_udata = 0; 1.9 - 1.10 typedef struct { 1.11 int fd; 1.12 int issock; 1.13 @@ -337,10 +334,7 @@ 1.14 lua_replace(L, 3); 1.15 } 1.16 lua_pushvalue(L, 2); 1.17 - lua_pushlightuserdata(L, &moonbr_io_block_udata); 1.18 - lua_pushvalue(L, 1); 1.19 - lua_pushliteral(L, "r"); 1.20 - lua_callk(L, 3, 0, ctx, moonbr_io_read_cont); 1.21 + lua_callk(L, 0, 0, ctx, moonbr_io_read_cont); 1.22 } 1.23 if (ctx == 1) { 1.24 lua_pushvalue(L, 5); 1.25 @@ -402,10 +396,7 @@ 1.26 lua_replace(L, 3); 1.27 } 1.28 lua_pushvalue(L, 2); 1.29 - lua_pushlightuserdata(L, &moonbr_io_block_udata); 1.30 - lua_pushvalue(L, 1); 1.31 - lua_pushliteral(L, "r"); 1.32 - lua_callk(L, 3, 0, ctx, moonbr_io_drain_cont); 1.33 + lua_callk(L, 0, 0, ctx, moonbr_io_drain_cont); 1.34 } 1.35 lua_pushinteger(L, totallen); 1.36 lua_pushvalue(L, -2); 1.37 @@ -583,10 +574,7 @@ 1.38 } 1.39 lua_pop(L, 2); 1.40 lua_pushvalue(L, 2); 1.41 - lua_pushlightuserdata(L, &moonbr_io_block_udata); 1.42 - lua_pushvalue(L, 1); 1.43 - lua_pushliteral(L, "w"); 1.44 - lua_callk(L, 3, 0, 0, moonbr_io_write_cont); 1.45 + lua_callk(L, 0, 0, 0, moonbr_io_write_cont); 1.46 } 1.47 } 1.48 1.49 @@ -1337,154 +1325,6 @@ 1.50 return 1; 1.51 } 1.52 1.53 -#define MOONBR_IO_RUN_STACKBASE 7 1.54 - 1.55 -#if LUA_VERSION_NUM >= 503 1.56 -static int moonbr_io_run_cont(lua_State *L, int status, lua_KContext ctx) { 1.57 -#else 1.58 -static int moonbr_io_run_cont(lua_State *L) { 1.59 -#endif 1.60 -#if !(LUA_VERSION_NUM >= 503) 1.61 - int ctx = 0; 1.62 - lua_getctx(L, &ctx); 1.63 -#endif 1.64 - while (1) { 1.65 - int work_to_do = 0; 1.66 - assert(lua_gettop(L) == 7); 1.67 - if (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) return 1; 1.68 - lua_pop(L, 1); 1.69 - if (ctx) { 1.70 - for (lua_pushnil(L); lua_next(L, 4); lua_pop(L, 1)) { 1.71 - lua_pushvalue(L, -2); 1.72 - lua_pushnil(L); 1.73 - lua_rawset(L, 4); 1.74 - } 1.75 - for (lua_pushnil(L); lua_next(L, 5); lua_pop(L, 1)) { 1.76 - lua_pushvalue(L, -2); 1.77 - lua_pushnil(L); 1.78 - lua_rawset(L, 5); 1.79 - } 1.80 - } 1.81 - assert(lua_gettop(L) == 6); 1.82 - while (lua_next(L, 1)) { 1.83 - void *marker; 1.84 - assert(lua_gettop(L) == MOONBR_IO_RUN_STACKBASE); 1.85 - while (1) { 1.86 - lua_pushvalue(L, -2); 1.87 - lua_call(L, 0, LUA_MULTRET); 1.88 - if (!lua_checkstack(L, LUA_MINSTACK)) luaL_error(L, "Lua stack exhausted"); 1.89 - marker = lua_touserdata(L, MOONBR_IO_RUN_STACKBASE+1); 1.90 - if (marker == &moonbr_io_block_udata) { 1.91 - const char *mode = lua_tostring(L, MOONBR_IO_RUN_STACKBASE+3); 1.92 - if (mode && !lua_isnoneornil(L, MOONBR_IO_RUN_STACKBASE+2)) { 1.93 - if (strchr(mode, 'r')) { 1.94 - lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE+2); 1.95 - lua_pushboolean(L, 1); 1.96 - lua_rawset(L, 4); 1.97 - } 1.98 - if (strchr(mode, 'w')) { 1.99 - lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE+2); 1.100 - lua_pushboolean(L, 1); 1.101 - lua_rawset(L, 5); 1.102 - } 1.103 - } 1.104 - work_to_do = 1; 1.105 - break; 1.106 - } else if (marker == &moonbr_io_multiblock_udata) { 1.107 - if (lua_type(L, MOONBR_IO_RUN_STACKBASE+2) == LUA_TTABLE) { 1.108 - for (lua_pushnil(L); lua_next(L, MOONBR_IO_RUN_STACKBASE+2); lua_pop(L, 1)) { 1.109 - if (lua_toboolean(L, -1)) { 1.110 - lua_pushvalue(L, -2); 1.111 - lua_pushboolean(L, 1); 1.112 - lua_rawset(L, 4); 1.113 - } 1.114 - } 1.115 - } 1.116 - if (lua_type(L, MOONBR_IO_RUN_STACKBASE+3) == LUA_TTABLE) { 1.117 - for (lua_pushnil(L); lua_next(L, MOONBR_IO_RUN_STACKBASE+3); lua_pop(L, 1)) { 1.118 - if (lua_toboolean(L, -1)) { 1.119 - lua_pushvalue(L, -2); 1.120 - lua_pushboolean(L, 1); 1.121 - lua_rawset(L, 5); 1.122 - } 1.123 - } 1.124 - } 1.125 - work_to_do = 1; 1.126 - break; 1.127 - } else if (lua_isboolean(L, MOONBR_IO_RUN_STACKBASE)) { 1.128 - lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE-1); 1.129 - lua_pushnil(L); 1.130 - lua_rawset(L, 1); 1.131 - break; 1.132 - } else { 1.133 - lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE); 1.134 - lua_insert(L, MOONBR_IO_RUN_STACKBASE+1); 1.135 - lua_call(L, lua_gettop(L)-1-MOONBR_IO_RUN_STACKBASE, 1); 1.136 - if (lua_toboolean(L, -1)) { 1.137 - lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE-1); 1.138 - lua_pushnil(L); 1.139 - lua_rawset(L, 1); 1.140 - break; 1.141 - } 1.142 - } 1.143 - lua_settop(L, MOONBR_IO_RUN_STACKBASE); 1.144 - } 1.145 - lua_settop(L, MOONBR_IO_RUN_STACKBASE-1); 1.146 - } 1.147 - if (!work_to_do) { 1.148 - lua_pushboolean(L, 1); 1.149 - return 1; 1.150 - } 1.151 - lua_pushnil(L); 1.152 - assert(lua_gettop(L) == 6); 1.153 - ctx = 1; 1.154 - if (lua_isfunction(L, 2)) { 1.155 - lua_pushvalue(L, 2); 1.156 - lua_pushlightuserdata(L, &moonbr_io_multiblock_udata); 1.157 - lua_pushvalue(L, 4); 1.158 - lua_pushvalue(L, 5); 1.159 - lua_callk(L, 3, 1, ctx, moonbr_io_run_cont); 1.160 - } else { 1.161 - lua_pushcfunction(L, moonbr_io_poll); 1.162 - lua_pushvalue(L, 4); 1.163 - lua_pushvalue(L, 5); 1.164 - if (lua_isnil(L, 2)) { 1.165 - lua_call(L, 2, 1); 1.166 - } else { 1.167 - lua_pushvalue(L, 2); 1.168 - lua_pushcfunction(L, moonbr_io_timeref); 1.169 - lua_pushvalue(L, 3); 1.170 - lua_call(L, 1, 1); 1.171 - lua_arith(L, LUA_OPSUB); 1.172 - lua_call(L, 3, 1); 1.173 - } 1.174 - } 1.175 - assert(lua_gettop(L) == 7); 1.176 - } 1.177 -} 1.178 - 1.179 -static int moonbr_io_run(lua_State *L) { 1.180 - lua_settop(L, 2); 1.181 - luaL_checktype(L, 1, LUA_TTABLE); 1.182 - if (lua_isnil(L, 2) || lua_isfunction(L, 2)) { 1.183 - lua_pushnil(L); 1.184 - } else if (!lua_isnil(L, 2)) { 1.185 - luaL_checknumber(L, 2); 1.186 - lua_pushcfunction(L, moonbr_io_timeref); 1.187 - lua_call(L, 0, 1); 1.188 - } 1.189 - assert(lua_gettop(L) == 3); 1.190 - lua_newtable(L); /* read_fds at stack position 4 */ 1.191 - lua_newtable(L); /* write_fds at stack position 5 */ 1.192 - lua_pushnil(L); /* current thread */ 1.193 - lua_pushnil(L); 1.194 -#if LUA_VERSION_NUM >= 503 1.195 - return moonbr_io_run_cont(L, 0, 0); 1.196 -#else 1.197 - return moonbr_io_run_cont(L); 1.198 -#endif 1.199 -} 1.200 - 1.201 static const struct luaL_Reg moonbr_io_handle_methods[] = { 1.202 {"read", moonbr_io_read}, 1.203 {"read_nb", moonbr_io_read_nb}, 1.204 @@ -1536,7 +1376,6 @@ 1.205 {"tcplisten", moonbr_io_tcplisten}, 1.206 {"poll", moonbr_io_poll}, 1.207 {"timeref", moonbr_io_timeref}, 1.208 - {"run", moonbr_io_run}, 1.209 {NULL, NULL} 1.210 }; 1.211 1.212 @@ -1546,11 +1385,6 @@ 1.213 1.214 lua_newtable(L); // module 1.215 1.216 - lua_pushlightuserdata(L, &moonbr_io_block_udata); 1.217 - lua_setfield(L, -2, "block"); 1.218 - lua_pushlightuserdata(L, &moonbr_io_multiblock_udata); 1.219 - lua_setfield(L, -2, "multiblock"); 1.220 - 1.221 lua_newtable(L); // public metatable 1.222 lua_newtable(L); // handle methods 1.223 luaL_setfuncs(L, moonbr_io_handle_methods, 0);