Diff

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
line wrap: on
line diff
--- a/util-src/poll.c	Sun Jun 07 02:14:55 2020 +0200
+++ b/util-src/poll.c	Sun Jun 07 02:25:56 2020 +0200
@@ -37,6 +37,9 @@
 #if (LUA_VERSION_NUM == 501)
 #define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname); lua_setmetatable(L, -2)
 #endif
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
 
 /*
  * Structure to keep state for each type of API
@@ -67,7 +70,7 @@
 	int wantwrite = lua_toboolean(L, 4);
 
 	if(fd < 0) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(EBADF));
 		lua_pushinteger(L, EBADF);
 		return 3;
@@ -84,7 +87,7 @@
 
 	if(ret < 0) {
 		ret = errno;
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(ret));
 		lua_pushinteger(L, ret);
 		return 3;
@@ -96,14 +99,14 @@
 #else
 
 	if(fd > FD_SETSIZE) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(EBADF));
 		lua_pushinteger(L, EBADF);
 		return 3;
 	}
 
 	if(FD_ISSET(fd, &state->all)) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(EEXIST));
 		lua_pushinteger(L, EEXIST);
 		return 3;
@@ -160,7 +163,7 @@
 	}
 	else {
 		ret = errno;
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(ret));
 		lua_pushinteger(L, ret);
 		return 3;
@@ -169,7 +172,7 @@
 #else
 
 	if(!FD_ISSET(fd, &state->all)) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(ENOENT));
 		lua_pushinteger(L, ENOENT);
 		return 3;
@@ -218,7 +221,7 @@
 	}
 	else {
 		ret = errno;
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(ret));
 		lua_pushinteger(L, ret);
 		return 3;
@@ -227,7 +230,7 @@
 #else
 
 	if(!FD_ISSET(fd, &state->all)) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(ENOENT));
 		lua_pushinteger(L, ENOENT);
 		return 3;
@@ -314,18 +317,20 @@
 #endif
 
 	if(ret == 0) {
+		/* Is this an error? */
 		lua_pushnil(L);
 		lua_pushstring(L, "timeout");
 		return 2;
 	}
 	else if(ret < 0 && errno == EINTR) {
+		/* Is this an error? */
 		lua_pushnil(L);
 		lua_pushstring(L, "signal");
 		return 2;
 	}
 	else if(ret < 0) {
 		ret = errno;
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(ret));
 		lua_pushinteger(L, ret);
 		return 3;
@@ -399,7 +404,7 @@
 	int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
 
 	if(epoll_fd <= 0) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(errno));
 		lua_pushinteger(L, errno);
 		return 3;