nixio: store stats and other number information as integer, which works better when lua number support is downgraded from double to float

This commit is contained in:
Felix Fietkau 2009-10-26 04:52:07 +00:00
parent 2181825db5
commit 64d9a00a99
12 changed files with 77 additions and 77 deletions

View file

@ -323,7 +323,7 @@ static int nixio_sock_getsockname(lua_State *L) {
} }
lua_pushstring(L, addr.host); lua_pushstring(L, addr.host);
lua_pushnumber(L, addr.port); lua_pushinteger(L, addr.port);
return 2; return 2;
} }
@ -342,7 +342,7 @@ static int nixio_sock_getpeername(lua_State *L) {
} }
lua_pushstring(L, addr.host); lua_pushstring(L, addr.host);
lua_pushnumber(L, addr.port); lua_pushinteger(L, addr.port);
return 2; return 2;
} }
@ -420,34 +420,34 @@ static int nixio_getifaddrs(lua_State *L) {
lua_createtable(L, 0, 10); lua_createtable(L, 0, 10);
struct nixio__nds *stats = c->ifa_data; struct nixio__nds *stats = c->ifa_data;
lua_pushnumber(L, stats->rx_packets); lua_pushinteger(L, stats->rx_packets);
lua_setfield(L, -2, "rx_packets"); lua_setfield(L, -2, "rx_packets");
lua_pushnumber(L, stats->tx_packets); lua_pushinteger(L, stats->tx_packets);
lua_setfield(L, -2, "tx_packets"); lua_setfield(L, -2, "tx_packets");
lua_pushnumber(L, stats->rx_bytes); lua_pushinteger(L, stats->rx_bytes);
lua_setfield(L, -2, "rx_bytes"); lua_setfield(L, -2, "rx_bytes");
lua_pushnumber(L, stats->tx_bytes); lua_pushinteger(L, stats->tx_bytes);
lua_setfield(L, -2, "tx_bytes"); lua_setfield(L, -2, "tx_bytes");
lua_pushnumber(L, stats->rx_errors); lua_pushinteger(L, stats->rx_errors);
lua_setfield(L, -2, "rx_errors"); lua_setfield(L, -2, "rx_errors");
lua_pushnumber(L, stats->tx_errors); lua_pushinteger(L, stats->tx_errors);
lua_setfield(L, -2, "tx_errors"); lua_setfield(L, -2, "tx_errors");
lua_pushnumber(L, stats->rx_dropped); lua_pushinteger(L, stats->rx_dropped);
lua_setfield(L, -2, "rx_dropped"); lua_setfield(L, -2, "rx_dropped");
lua_pushnumber(L, stats->tx_dropped); lua_pushinteger(L, stats->tx_dropped);
lua_setfield(L, -2, "tx_dropped"); lua_setfield(L, -2, "tx_dropped");
lua_pushnumber(L, stats->multicast); lua_pushinteger(L, stats->multicast);
lua_setfield(L, -2, "multicast"); lua_setfield(L, -2, "multicast");
lua_pushnumber(L, stats->collisions); lua_pushinteger(L, stats->collisions);
lua_setfield(L, -2, "collisions"); lua_setfield(L, -2, "collisions");
} else { } else {
lua_newtable(L); lua_newtable(L);

View file

@ -97,7 +97,7 @@ static const uint32_t nixio__crc32_tbl[] = {
static int nixio_bin_crc32(lua_State *L) { static int nixio_bin_crc32(lua_State *L) {
size_t len; size_t len;
const char *buffer = luaL_checklstring(L, 1, &len); const char *buffer = luaL_checklstring(L, 1, &len);
uint32_t value = luaL_optnumber(L, 2, 0); uint32_t value = luaL_optinteger(L, 2, 0);
value = ~value; value = ~value;
for (size_t i=0; i<len; i++) { for (size_t i=0; i<len; i++) {

View file

@ -261,7 +261,7 @@ static int nixio_sock_accept(lua_State *L) {
if (!nixio__addr_parse(&addr, (struct sockaddr *)&saddr)) { if (!nixio__addr_parse(&addr, (struct sockaddr *)&saddr)) {
lua_pushstring(L, addr.host); lua_pushstring(L, addr.host);
lua_pushnumber(L, addr.port); lua_pushinteger(L, addr.port);
return 3; return 3;
} else { } else {
return 1; return 1;

View file

@ -21,19 +21,19 @@
#include <stdlib.h> #include <stdlib.h>
/* 52 bit maximum precision */ /* 52 bit maximum precision */
#define NIXIO_BIT_BMAX 52 #define NIXIO_BIT_BMAX 32
#define NIXIO_BIT_NMAX 0xfffffffffffff #define NIXIO_BIT_NMAX 0xffffffff
#define NIXIO_BIT_XOP(BIT_XOP) \ #define NIXIO_BIT_XOP(BIT_XOP) \
uint64_t oper = luaL_checknumber(L, 1); \ uint64_t oper = luaL_checkinteger(L, 1); \
const int args = lua_gettop(L); \ const int args = lua_gettop(L); \
\ \
for (int i = 2; i <= args; i++) { \ for (int i = 2; i <= args; i++) { \
uint64_t oper2 = luaL_checknumber(L, i); \ uint64_t oper2 = luaL_checkinteger(L, i); \
oper BIT_XOP oper2; \ oper BIT_XOP oper2; \
} \ } \
\ \
lua_pushnumber(L, oper); \ lua_pushinteger(L, oper); \
return 1; \ return 1; \
@ -54,30 +54,30 @@ static int nixio_bit_unset(lua_State *L) {
} }
static int nixio_bit_not(lua_State *L) { static int nixio_bit_not(lua_State *L) {
lua_pushnumber(L, (~((uint64_t)luaL_checknumber(L, 1))) & NIXIO_BIT_NMAX); lua_pushinteger(L, (~((uint64_t)luaL_checkinteger(L, 1))) & NIXIO_BIT_NMAX);
return 1; return 1;
} }
static int nixio_bit_shl(lua_State *L) { static int nixio_bit_shl(lua_State *L) {
uint64_t oper = luaL_checknumber(L, 1); uint64_t oper = luaL_checkinteger(L, 1);
oper <<= luaL_checkinteger(L, 2); oper <<= luaL_checkinteger(L, 2);
if (oper > NIXIO_BIT_NMAX) { if (oper > NIXIO_BIT_NMAX) {
return luaL_error(L, "arithmetic overflow"); return luaL_error(L, "arithmetic overflow");
} else { } else {
lua_pushnumber(L, oper); lua_pushinteger(L, oper);
return 1; return 1;
} }
} }
static int nixio_bit_ashr(lua_State *L) { static int nixio_bit_ashr(lua_State *L) {
int64_t oper = luaL_checknumber(L, 1); int64_t oper = luaL_checkinteger(L, 1);
lua_pushnumber(L, oper >> luaL_checkinteger(L, 2)); lua_pushinteger(L, oper >> luaL_checkinteger(L, 2));
return 1; return 1;
} }
static int nixio_bit_shr(lua_State *L) { static int nixio_bit_shr(lua_State *L) {
uint64_t oper = luaL_checknumber(L, 1); uint64_t oper = luaL_checkinteger(L, 1);
lua_pushnumber(L, oper >> luaL_checkinteger(L, 2)); lua_pushinteger(L, oper >> luaL_checkinteger(L, 2));
return 1; return 1;
} }
@ -86,21 +86,21 @@ static int nixio_bit_div(lua_State *L) {
} }
static int nixio_bit_check(lua_State *L) { static int nixio_bit_check(lua_State *L) {
uint64_t oper = luaL_checknumber(L, 1); uint64_t oper = luaL_checkinteger(L, 1);
uint64_t oper2 = luaL_checknumber(L, 2); uint64_t oper2 = luaL_checkinteger(L, 2);
lua_pushboolean(L, (oper & oper2) == oper2); lua_pushboolean(L, (oper & oper2) == oper2);
return 1; return 1;
} }
static int nixio_bit_cast(lua_State *L) { static int nixio_bit_cast(lua_State *L) {
lua_pushnumber(L, ((uint64_t)luaL_checknumber(L, 1)) & NIXIO_BIT_NMAX); lua_pushinteger(L, ((uint64_t)luaL_checkinteger(L, 1)) & NIXIO_BIT_NMAX);
return 1; return 1;
} }
static int nixio_bit_swap(lua_State *L) { static int nixio_bit_swap(lua_State *L) {
uint64_t op = luaL_checknumber(L, 1); uint64_t op = luaL_checkinteger(L, 1);
op = (op >> 24) | ((op >> 8) & 0xff00) | ((op & 0xff00) << 8) | (op << 24); op = (op >> 24) | ((op >> 8) & 0xff00) | ((op & 0xff00) << 8) | (op << 24);
lua_pushnumber(L, op); lua_pushinteger(L, op);
return 1; return 1;
} }
@ -126,9 +126,9 @@ static const luaL_reg R[] = {
void nixio_open_bit(lua_State *L) { void nixio_open_bit(lua_State *L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, R); luaL_register(L, NULL, R);
lua_pushnumber(L, NIXIO_BIT_BMAX); lua_pushinteger(L, NIXIO_BIT_BMAX);
lua_setfield(L, -2, "bits"); lua_setfield(L, -2, "bits");
lua_pushnumber(L, NIXIO_BIT_NMAX); lua_pushinteger(L, NIXIO_BIT_NMAX);
lua_setfield(L, -2, "max"); lua_setfield(L, -2, "max");
lua_setfield(L, -2, "bit"); lua_setfield(L, -2, "bit");
} }

View file

@ -226,7 +226,7 @@ static int nixio_file_read(lua_State *L) {
static int nixio_file_seek(lua_State *L) { static int nixio_file_seek(lua_State *L) {
int fd = nixio__checkfd(L, 1); int fd = nixio__checkfd(L, 1);
off_t len = (off_t)luaL_checknumber(L, 2); off_t len = (off_t)luaL_checkinteger(L, 2);
int whence; int whence;
const char *whstr = luaL_optlstring(L, 3, "set", NULL); const char *whstr = luaL_optlstring(L, 3, "set", NULL);
if (!strcmp(whstr, "set")) { if (!strcmp(whstr, "set")) {
@ -242,7 +242,7 @@ static int nixio_file_seek(lua_State *L) {
if (len == -1) { if (len == -1) {
return nixio__perror(L); return nixio__perror(L);
} else { } else {
lua_pushnumber(L, len); lua_pushinteger(L, len);
return 1; return 1;
} }
} }
@ -253,7 +253,7 @@ static int nixio_file_tell(lua_State *L) {
if (pos < 0) { if (pos < 0) {
return nixio__perror(L); return nixio__perror(L);
} else { } else {
lua_pushnumber(L, pos); lua_pushinteger(L, pos);
return 1; return 1;
} }
} }
@ -291,7 +291,7 @@ static int nixio_file_sync(lua_State *L) {
static int nixio_file_lock(lua_State *L) { static int nixio_file_lock(lua_State *L) {
int fd = nixio__checkfd(L, 1); int fd = nixio__checkfd(L, 1);
const char *flag = luaL_checkstring(L, 2); const char *flag = luaL_checkstring(L, 2);
off_t len = (off_t)luaL_optnumber(L, 3, 0); off_t len = (off_t)luaL_optinteger(L, 3, 0);
int stat; int stat;
int cmd = 0; int cmd = 0;

View file

@ -83,7 +83,7 @@ int nixio__check_mode(lua_State *L, int idx, int def) {
if (i == 9) { /* successfully parsed */ if (i == 9) { /* successfully parsed */
return mode; return mode;
} }
} else if (lua_isnumber(L, idx)) { } else if (lua_isinteger(L, idx)) {
int decmode = lua_tointeger(L, idx); int decmode = lua_tointeger(L, idx);
int s = (decmode % 10000) / 1000; int s = (decmode % 10000) / 1000;
int u = (decmode % 1000) / 100; int u = (decmode % 1000) / 100;
@ -257,14 +257,14 @@ static int nixio_utimes(lua_State *L) {
if (lua_gettop(L) < 2 || (lua_isnoneornil(L, 2) && lua_isnoneornil(L, 3))) { if (lua_gettop(L) < 2 || (lua_isnoneornil(L, 2) && lua_isnoneornil(L, 3))) {
return nixio__pstatus(L, !utimes(path, NULL)); return nixio__pstatus(L, !utimes(path, NULL));
} else { } else {
double atime = luaL_checknumber(L, 2); double atime = luaL_checkinteger(L, 2);
double mtime = luaL_optnumber(L, 3, atime); double mtime = luaL_optinteger(L, 3, atime);
struct timeval times[2]; struct timeval times[2];
times[0].tv_sec = atime; times[0].tv_sec = atime;
times[0].tv_usec = (long)((atime - (int64_t)atime) * 1000000); times[0].tv_usec = 0;
times[1].tv_sec = mtime; times[1].tv_sec = mtime;
times[1].tv_usec = (long)((mtime - (int64_t)mtime) * 1000000); times[1].tv_usec = 0;
return nixio__pstatus(L, !utimes(path, times)); return nixio__pstatus(L, !utimes(path, times));
} }
@ -317,7 +317,7 @@ int nixio__push_stat(lua_State *L, nixio_stat_t *buf) {
lua_pushinteger(L, buf->st_rdev); lua_pushinteger(L, buf->st_rdev);
lua_setfield(L, -2, "rdev"); lua_setfield(L, -2, "rdev");
lua_pushnumber(L, buf->st_size); lua_pushinteger(L, buf->st_size);
lua_setfield(L, -2, "size"); lua_setfield(L, -2, "size");
lua_pushinteger(L, buf->st_atime); lua_pushinteger(L, buf->st_atime);
@ -469,37 +469,37 @@ static int nixio_glob(lua_State *L) {
static int nixio__push_statvfs(lua_State *L, struct statvfs *buf) { static int nixio__push_statvfs(lua_State *L, struct statvfs *buf) {
lua_createtable(L, 0, 12); lua_createtable(L, 0, 12);
lua_pushnumber(L, buf->f_bavail); lua_pushinteger(L, buf->f_bavail);
lua_setfield(L, -2, "bavail"); lua_setfield(L, -2, "bavail");
lua_pushnumber(L, buf->f_bfree); lua_pushinteger(L, buf->f_bfree);
lua_setfield(L, -2, "bfree"); lua_setfield(L, -2, "bfree");
lua_pushnumber(L, buf->f_blocks); lua_pushinteger(L, buf->f_blocks);
lua_setfield(L, -2, "blocks"); lua_setfield(L, -2, "blocks");
lua_pushnumber(L, buf->f_bsize); lua_pushinteger(L, buf->f_bsize);
lua_setfield(L, -2, "bsize"); lua_setfield(L, -2, "bsize");
lua_pushnumber(L, buf->f_frsize); lua_pushinteger(L, buf->f_frsize);
lua_setfield(L, -2, "frsize"); lua_setfield(L, -2, "frsize");
lua_pushnumber(L, buf->f_favail); lua_pushinteger(L, buf->f_favail);
lua_setfield(L, -2, "favail"); lua_setfield(L, -2, "favail");
lua_pushnumber(L, buf->f_ffree); lua_pushinteger(L, buf->f_ffree);
lua_setfield(L, -2, "ffree"); lua_setfield(L, -2, "ffree");
lua_pushnumber(L, buf->f_files); lua_pushinteger(L, buf->f_files);
lua_setfield(L, -2, "files"); lua_setfield(L, -2, "files");
lua_pushnumber(L, buf->f_flag); lua_pushinteger(L, buf->f_flag);
lua_setfield(L, -2, "flag"); lua_setfield(L, -2, "flag");
lua_pushnumber(L, buf->f_fsid); lua_pushinteger(L, buf->f_fsid);
lua_setfield(L, -2, "fsid"); lua_setfield(L, -2, "fsid");
lua_pushnumber(L, buf->f_namemax); lua_pushinteger(L, buf->f_namemax);
lua_setfield(L, -2, "namemax"); lua_setfield(L, -2, "namemax");
return 1; return 1;

View file

@ -141,7 +141,7 @@ static int nixio_sock__recvfrom(lua_State *L, int from) {
nixio_addr naddr; nixio_addr naddr;
if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addrobj)) { if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addrobj)) {
lua_pushstring(L, naddr.host); lua_pushstring(L, naddr.host);
lua_pushnumber(L, naddr.port); lua_pushinteger(L, naddr.port);
return 3; return 3;
} else { } else {
return 1; return 1;

View file

@ -147,7 +147,7 @@ NIXIO_API int luaopen_nixio(lua_State *L) {
nixio_open_tls_socket(L); nixio_open_tls_socket(L);
/* module version */ /* module version */
lua_pushnumber(L, VERSION); lua_pushinteger(L, VERSION);
lua_setfield(L, -2, "version"); lua_setfield(L, -2, "version");
/* some constants */ /* some constants */

View file

@ -225,16 +225,16 @@ static int nixio_times(lua_State *L) {
return nixio__perror(L); return nixio__perror(L);
} else { } else {
lua_createtable(L, 0, 4); lua_createtable(L, 0, 4);
lua_pushnumber(L, buf.tms_cstime); lua_pushinteger(L, buf.tms_cstime);
lua_setfield(L, -2, "cstime"); lua_setfield(L, -2, "cstime");
lua_pushnumber(L, buf.tms_cutime); lua_pushinteger(L, buf.tms_cutime);
lua_setfield(L, -2, "cutime"); lua_setfield(L, -2, "cutime");
lua_pushnumber(L, buf.tms_stime); lua_pushinteger(L, buf.tms_stime);
lua_setfield(L, -2, "stime"); lua_setfield(L, -2, "stime");
lua_pushnumber(L, buf.tms_utime); lua_pushinteger(L, buf.tms_utime);
lua_setfield(L, -2, "utime"); lua_setfield(L, -2, "utime");
return 1; return 1;
@ -365,44 +365,44 @@ static int nixio_sysinfo(lua_State *L) {
lua_createtable(L, 0, 12); lua_createtable(L, 0, 12);
lua_pushnumber(L, info.bufferram); lua_pushinteger(L, info.bufferram);
lua_setfield(L, -2, "bufferram"); lua_setfield(L, -2, "bufferram");
lua_pushnumber(L, info.freehigh); lua_pushinteger(L, info.freehigh);
lua_setfield(L, -2, "freehigh"); lua_setfield(L, -2, "freehigh");
lua_pushnumber(L, info.freeram); lua_pushinteger(L, info.freeram);
lua_setfield(L, -2, "freeram"); lua_setfield(L, -2, "freeram");
lua_pushnumber(L, info.freeswap); lua_pushinteger(L, info.freeswap);
lua_setfield(L, -2, "freeswap"); lua_setfield(L, -2, "freeswap");
lua_createtable(L, 0, 3); lua_createtable(L, 0, 3);
for (int i=0; i<3; i++) { for (int i=0; i<3; i++) {
lua_pushnumber(L, info.loads[i] / 65536.); lua_pushinteger(L, info.loads[i] / 65536.);
lua_rawseti(L, -2, i+1); lua_rawseti(L, -2, i+1);
} }
lua_setfield(L, -2, "loads"); lua_setfield(L, -2, "loads");
lua_pushnumber(L, info.mem_unit); lua_pushinteger(L, info.mem_unit);
lua_setfield(L, -2, "mem_unit"); lua_setfield(L, -2, "mem_unit");
lua_pushnumber(L, info.procs); lua_pushinteger(L, info.procs);
lua_setfield(L, -2, "procs"); lua_setfield(L, -2, "procs");
lua_pushnumber(L, info.sharedram); lua_pushinteger(L, info.sharedram);
lua_setfield(L, -2, "sharedram"); lua_setfield(L, -2, "sharedram");
lua_pushnumber(L, info.totalhigh); lua_pushinteger(L, info.totalhigh);
lua_setfield(L, -2, "totalhigh"); lua_setfield(L, -2, "totalhigh");
lua_pushnumber(L, info.totalram); lua_pushinteger(L, info.totalram);
lua_setfield(L, -2, "totalram"); lua_setfield(L, -2, "totalram");
lua_pushnumber(L, info.totalswap); lua_pushinteger(L, info.totalswap);
lua_setfield(L, -2, "totalswap"); lua_setfield(L, -2, "totalswap");
lua_pushnumber(L, info.uptime); lua_pushinteger(L, info.uptime);
lua_setfield(L, -2, "uptime"); lua_setfield(L, -2, "uptime");
return 1; return 1;

View file

@ -204,7 +204,7 @@ static int nixio__gso_mreq6(lua_State *L, int fd, int level, int opt, int set) {
return nixio__perror_s(L); return nixio__perror_s(L);
} }
lua_pushstring(L, buf); lua_pushstring(L, buf);
lua_pushnumber(L, val.ipv6mr_interface); lua_pushinteger(L, val.ipv6mr_interface);
return 2; return 2;
} }
} else { } else {

View file

@ -90,7 +90,7 @@ static int nixio_splice(lua_State *L) {
return nixio__perror(L); return nixio__perror(L);
} }
lua_pushnumber(L, spliced); lua_pushinteger(L, spliced);
return 1; return 1;
} }
@ -151,7 +151,7 @@ static int nixio_sendfile(lua_State *L) {
} }
#endif #endif
lua_pushnumber(L, spliced); lua_pushinteger(L, spliced);
return 1; return 1;
} }

View file

@ -78,7 +78,7 @@ static int nixio_getgr(lua_State *L) {
struct group *gr; struct group *gr;
errno = 0; errno = 0;
if (lua_isnumber(L, 1)) { if (lua_isnumber(L, 1)) {
gr = getgrgid(lua_tonumber(L, 1)); gr = getgrgid(lua_tointeger(L, 1));
} else if (lua_isstring(L, 1)) { } else if (lua_isstring(L, 1)) {
gr = getgrnam(lua_tostring(L, 1)); gr = getgrnam(lua_tostring(L, 1));
} else if (lua_isnoneornil(L, 1)) { } else if (lua_isnoneornil(L, 1)) {
@ -131,7 +131,7 @@ static int nixio_getpw(lua_State *L) {
struct passwd *pw; struct passwd *pw;
errno = 0; errno = 0;
if (lua_isnumber(L, 1)) { if (lua_isnumber(L, 1)) {
pw = getpwuid(lua_tonumber(L, 1)); pw = getpwuid(lua_tointeger(L, 1));
} else if (lua_isstring(L, 1)) { } else if (lua_isstring(L, 1)) {
pw = getpwnam(lua_tostring(L, 1)); pw = getpwnam(lua_tostring(L, 1));
} else if (lua_isnoneornil(L, 1)) { } else if (lua_isnoneornil(L, 1)) {