Comparison

util-src/poll.c @ 10921:6eb5d2bb11af

util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions Actually just an alias of pushnil, but it does make it more obvious where the failure conditions are, which is good for readability.
author Kim Alvefur <zash@zash.se>
date Sun, 07 Jun 2020 02:25:56 +0200
parent 10096:46a7792fdac5
child 12314:898554323338
comparison
equal deleted inserted replaced
10920:c171b4c59bd1 10921:6eb5d2bb11af
35 #endif 35 #endif
36 36
37 #if (LUA_VERSION_NUM == 501) 37 #if (LUA_VERSION_NUM == 501)
38 #define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2) 38 #define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2)
39 #endif 39 #endif
40 #if (LUA_VERSION_NUM < 504)
41 #define luaL_pushfail lua_pushnil
42 #endif
40 43
41 /* 44 /*
42 * Structure to keep state for each type of API 45 * Structure to keep state for each type of API
43 */ 46 */
44 typedef struct Lpoll_state { 47 typedef struct Lpoll_state {
65 68
66 int wantread = lua_toboolean(L, 3); 69 int wantread = lua_toboolean(L, 3);
67 int wantwrite = lua_toboolean(L, 4); 70 int wantwrite = lua_toboolean(L, 4);
68 71
69 if(fd < 0) { 72 if(fd < 0) {
70 lua_pushnil(L); 73 luaL_pushfail(L);
71 lua_pushstring(L, strerror(EBADF)); 74 lua_pushstring(L, strerror(EBADF));
72 lua_pushinteger(L, EBADF); 75 lua_pushinteger(L, EBADF);
73 return 3; 76 return 3;
74 } 77 }
75 78
82 85
83 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event); 86 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event);
84 87
85 if(ret < 0) { 88 if(ret < 0) {
86 ret = errno; 89 ret = errno;
87 lua_pushnil(L); 90 luaL_pushfail(L);
88 lua_pushstring(L, strerror(ret)); 91 lua_pushstring(L, strerror(ret));
89 lua_pushinteger(L, ret); 92 lua_pushinteger(L, ret);
90 return 3; 93 return 3;
91 } 94 }
92 95
94 return 1; 97 return 1;
95 98
96 #else 99 #else
97 100
98 if(fd > FD_SETSIZE) { 101 if(fd > FD_SETSIZE) {
99 lua_pushnil(L); 102 luaL_pushfail(L);
100 lua_pushstring(L, strerror(EBADF)); 103 lua_pushstring(L, strerror(EBADF));
101 lua_pushinteger(L, EBADF); 104 lua_pushinteger(L, EBADF);
102 return 3; 105 return 3;
103 } 106 }
104 107
105 if(FD_ISSET(fd, &state->all)) { 108 if(FD_ISSET(fd, &state->all)) {
106 lua_pushnil(L); 109 luaL_pushfail(L);
107 lua_pushstring(L, strerror(EEXIST)); 110 lua_pushstring(L, strerror(EEXIST));
108 lua_pushinteger(L, EEXIST); 111 lua_pushinteger(L, EEXIST);
109 return 3; 112 return 3;
110 } 113 }
111 114
158 lua_pushboolean(L, 1); 161 lua_pushboolean(L, 1);
159 return 1; 162 return 1;
160 } 163 }
161 else { 164 else {
162 ret = errno; 165 ret = errno;
163 lua_pushnil(L); 166 luaL_pushfail(L);
164 lua_pushstring(L, strerror(ret)); 167 lua_pushstring(L, strerror(ret));
165 lua_pushinteger(L, ret); 168 lua_pushinteger(L, ret);
166 return 3; 169 return 3;
167 } 170 }
168 171
169 #else 172 #else
170 173
171 if(!FD_ISSET(fd, &state->all)) { 174 if(!FD_ISSET(fd, &state->all)) {
172 lua_pushnil(L); 175 luaL_pushfail(L);
173 lua_pushstring(L, strerror(ENOENT)); 176 lua_pushstring(L, strerror(ENOENT));
174 lua_pushinteger(L, ENOENT); 177 lua_pushinteger(L, ENOENT);
175 return 3; 178 return 3;
176 } 179 }
177 180
216 lua_pushboolean(L, 1); 219 lua_pushboolean(L, 1);
217 return 1; 220 return 1;
218 } 221 }
219 else { 222 else {
220 ret = errno; 223 ret = errno;
221 lua_pushnil(L); 224 luaL_pushfail(L);
222 lua_pushstring(L, strerror(ret)); 225 lua_pushstring(L, strerror(ret));
223 lua_pushinteger(L, ret); 226 lua_pushinteger(L, ret);
224 return 3; 227 return 3;
225 } 228 }
226 229
227 #else 230 #else
228 231
229 if(!FD_ISSET(fd, &state->all)) { 232 if(!FD_ISSET(fd, &state->all)) {
230 lua_pushnil(L); 233 luaL_pushfail(L);
231 lua_pushstring(L, strerror(ENOENT)); 234 lua_pushstring(L, strerror(ENOENT));
232 lua_pushinteger(L, ENOENT); 235 lua_pushinteger(L, ENOENT);
233 return 3; 236 return 3;
234 } 237 }
235 238
312 315
313 ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv); 316 ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv);
314 #endif 317 #endif
315 318
316 if(ret == 0) { 319 if(ret == 0) {
320 /* Is this an error? */
317 lua_pushnil(L); 321 lua_pushnil(L);
318 lua_pushstring(L, "timeout"); 322 lua_pushstring(L, "timeout");
319 return 2; 323 return 2;
320 } 324 }
321 else if(ret < 0 && errno == EINTR) { 325 else if(ret < 0 && errno == EINTR) {
326 /* Is this an error? */
322 lua_pushnil(L); 327 lua_pushnil(L);
323 lua_pushstring(L, "signal"); 328 lua_pushstring(L, "signal");
324 return 2; 329 return 2;
325 } 330 }
326 else if(ret < 0) { 331 else if(ret < 0) {
327 ret = errno; 332 ret = errno;
328 lua_pushnil(L); 333 luaL_pushfail(L);
329 lua_pushstring(L, strerror(ret)); 334 lua_pushstring(L, strerror(ret));
330 lua_pushinteger(L, ret); 335 lua_pushinteger(L, ret);
331 return 3; 336 return 3;
332 } 337 }
333 338
397 state->processed = 0; 402 state->processed = 0;
398 403
399 int epoll_fd = epoll_create1(EPOLL_CLOEXEC); 404 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
400 405
401 if(epoll_fd <= 0) { 406 if(epoll_fd <= 0) {
402 lua_pushnil(L); 407 luaL_pushfail(L);
403 lua_pushstring(L, strerror(errno)); 408 lua_pushstring(L, strerror(errno));
404 lua_pushinteger(L, errno); 409 lua_pushinteger(L, errno);
405 return 3; 410 return 3;
406 } 411 }
407 412