Changeset

10799:763bb2ce3f60

util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3] Passing around PIDs, UIDs etc as integers makes it more sane in Lua 5.3. Getting 1234.0 as PID is silly. Shouldn't change any behavior as these are all integers on the C side and the integral floats are accepted as integers when passed back from Lua into C.
author Kim Alvefur <zash@zash.se>
date Mon, 04 May 2020 21:51:30 +0200
parents 10798:b81f4fd7f21a
children 10800:62794e065e33
files util-src/pposix.c util-src/signal.c
diffstat 2 files changed, 34 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/util-src/pposix.c	Sat May 02 20:41:35 2020 +0200
+++ b/util-src/pposix.c	Mon May 04 21:51:30 2020 +0200
@@ -61,6 +61,9 @@
 #if (LUA_VERSION_NUM == 501)
 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
 #endif
+#if (LUA_VERSION_NUM < 503)
+#define lua_isinteger(L, n) lua_isnumber(L, n)
+#endif
 
 #include <fcntl.h>
 #if defined(__linux__)
@@ -106,7 +109,7 @@
 	} else if(pid != 0) {
 		/* We are the parent process */
 		lua_pushboolean(L, 1);
-		lua_pushnumber(L, pid);
+		lua_pushinteger(L, pid);
 		return 2;
 	}
 
@@ -295,7 +298,7 @@
 		return 0;
 	}
 
-	if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) {
+	if(!lua_isinteger(L, 1) && lua_tostring(L, 1)) {
 		/* Passed UID is actually a string, so look up the UID */
 		struct passwd *p;
 		p = getpwnam(lua_tostring(L, 1));
@@ -308,7 +311,7 @@
 
 		uid = p->pw_uid;
 	} else {
-		uid = lua_tonumber(L, 1);
+		uid = lua_tointeger(L, 1);
 	}
 
 	if(uid > -1) {
@@ -353,7 +356,7 @@
 		return 0;
 	}
 
-	if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) {
+	if(!lua_isinteger(L, 1) && lua_tostring(L, 1)) {
 		/* Passed GID is actually a string, so look up the GID */
 		struct group *g;
 		g = getgrnam(lua_tostring(L, 1));
@@ -366,7 +369,7 @@
 
 		gid = g->gr_gid;
 	} else {
-		gid = lua_tonumber(L, 1);
+		gid = lua_tointeger(L, 1);
 	}
 
 	if(gid > -1) {
@@ -647,13 +650,13 @@
 	if(lim.rlim_cur == RLIM_INFINITY) {
 		lua_pushstring(L, "unlimited");
 	} else {
-		lua_pushnumber(L, lim.rlim_cur);
+		lua_pushinteger(L, lim.rlim_cur);
 	}
 
 	if(lim.rlim_max == RLIM_INFINITY) {
 		lua_pushstring(L, "unlimited");
 	} else {
-		lua_pushnumber(L, lim.rlim_max);
+		lua_pushinteger(L, lim.rlim_max);
 	}
 
 	return 3;
--- a/util-src/signal.c	Sat May 02 20:41:35 2020 +0200
+++ b/util-src/signal.c	Mon May 04 21:51:30 2020 +0200
@@ -39,6 +39,9 @@
 #if (LUA_VERSION_NUM == 501)
 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
 #endif
+#if (LUA_VERSION_NUM < 503)
+#define lua_isinteger(L, n) lua_isnumber(L, n)
+#endif
 
 #ifndef lsig
 
@@ -176,7 +179,7 @@
 	lua_gettable(L, LUA_REGISTRYINDEX);
 
 	for(int i = 0; i < nsig; i++) {
-		lua_pushnumber(L, signals[i]);
+		lua_pushinteger(L, signals[i]);
 		lua_gettable(L, -2);
 		lua_call(L, 0, 0);
 	};
@@ -223,18 +226,18 @@
 	t = lua_type(L, 1);
 
 	if(t == LUA_TNUMBER) {
-		sig = (int) lua_tonumber(L, 1);
+		sig = (int) lua_tointeger(L, 1);
 	} else if(t == LUA_TSTRING) {
 		lua_pushstring(L, LUA_SIGNAL);
 		lua_gettable(L, LUA_REGISTRYINDEX);
 		lua_pushvalue(L, 1);
 		lua_gettable(L, -2);
 
-		if(!lua_isnumber(L, -1)) {
+		if(!lua_isinteger(L, -1)) {
 			return luaL_error(L, "invalid signal string");
 		}
 
-		sig = (int) lua_tonumber(L, -1);
+		sig = (int) lua_tointeger(L, -1);
 		lua_pop(L, 1); /* get rid of number we pushed */
 	} else {
 		luaL_checknumber(L, 1);    /* will always error, with good error msg */
@@ -245,9 +248,9 @@
 	if(args == 1 || lua_isnil(L, 2)) { /* clear handler */
 		lua_pushstring(L, LUA_SIGNAL);
 		lua_gettable(L, LUA_REGISTRYINDEX);
-		lua_pushnumber(L, sig);
+		lua_pushinteger(L, sig);
 		lua_gettable(L, -2); /* return old handler */
-		lua_pushnumber(L, sig);
+		lua_pushinteger(L, sig);
 		lua_pushnil(L);
 		lua_settable(L, -4);
 		lua_remove(L, -2); /* remove LUA_SIGNAL table */
@@ -258,7 +261,7 @@
 		lua_pushstring(L, LUA_SIGNAL);
 		lua_gettable(L, LUA_REGISTRYINDEX);
 
-		lua_pushnumber(L, sig);
+		lua_pushinteger(L, sig);
 		lua_pushvalue(L, 2);
 		lua_settable(L, -3);
 
@@ -292,15 +295,15 @@
 static int l_raise(lua_State *L) {
 	/* int args = lua_gettop(L); */
 	int t = 0; /* type */
-	lua_Number ret;
+	lua_Integer ret;
 
 	luaL_checkany(L, 1);
 
 	t = lua_type(L, 1);
 
 	if(t == LUA_TNUMBER) {
-		ret = (lua_Number) raise((int) lua_tonumber(L, 1));
-		lua_pushnumber(L, ret);
+		ret = (lua_Integer) raise((int) lua_tointeger(L, 1));
+		lua_pushinteger(L, ret);
 	} else if(t == LUA_TSTRING) {
 		lua_pushstring(L, LUA_SIGNAL);
 		lua_gettable(L, LUA_REGISTRYINDEX);
@@ -311,9 +314,9 @@
 			return luaL_error(L, "invalid signal string");
 		}
 
-		ret = (lua_Number) raise((int) lua_tonumber(L, -1));
+		ret = (lua_Integer) raise((int) lua_tointeger(L, -1));
 		lua_pop(L, 1); /* get rid of number we pushed */
-		lua_pushnumber(L, ret);
+		lua_pushinteger(L, ret);
 	} else {
 		luaL_checknumber(L, 1);    /* will always error, with good error msg */
 	}
@@ -334,7 +337,7 @@
 
 static int l_kill(lua_State *L) {
 	int t; /* type */
-	lua_Number ret; /* return value */
+	lua_Integer ret; /* return value */
 
 	luaL_checknumber(L, 1); /* must be int for pid */
 	luaL_checkany(L, 2); /* check for a second arg */
@@ -342,9 +345,9 @@
 	t = lua_type(L, 2);
 
 	if(t == LUA_TNUMBER) {
-		ret = (lua_Number) kill((int) lua_tonumber(L, 1),
-		                        (int) lua_tonumber(L, 2));
-		lua_pushnumber(L, ret);
+		ret = (lua_Integer) kill((int) lua_tointeger(L, 1),
+		                         (int) lua_tointeger(L, 2));
+		lua_pushinteger(L, ret);
 	} else if(t == LUA_TSTRING) {
 		lua_pushstring(L, LUA_SIGNAL);
 		lua_gettable(L, LUA_REGISTRYINDEX);
@@ -355,10 +358,10 @@
 			return luaL_error(L, "invalid signal string");
 		}
 
-		ret = (lua_Number) kill((int) lua_tonumber(L, 1),
-		                        (int) lua_tonumber(L, -1));
+		ret = (lua_Integer) kill((int) lua_tointeger(L, 1),
+		                         (int) lua_tointeger(L, -1));
 		lua_pop(L, 1); /* get rid of number we pushed */
-		lua_pushnumber(L, ret);
+		lua_pushinteger(L, ret);
 	} else {
 		luaL_checknumber(L, 2);    /* will always error, with good error msg */
 	}
@@ -396,11 +399,11 @@
 	while(lua_signals[i].name != NULL) {
 		/* registry table */
 		lua_pushstring(L, lua_signals[i].name);
-		lua_pushnumber(L, lua_signals[i].sig);
+		lua_pushinteger(L, lua_signals[i].sig);
 		lua_settable(L, -3);
 		/* signal table */
 		lua_pushstring(L, lua_signals[i].name);
-		lua_pushnumber(L, lua_signals[i].sig);
+		lua_pushinteger(L, lua_signals[i].sig);
 		lua_settable(L, -5);
 		i++;
 	}