diff options
author | Steven Barth <steven@midlink.org> | 2009-11-28 10:24:19 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2009-11-28 10:24:19 +0000 |
commit | 87491bf031881a4f5b828aaf6826cf98bda1252e (patch) | |
tree | a67086f36d95dbac5ea02f3c06ecf827fa171fbf | |
parent | 7f9c9d208e2465e80cba92dcc8d450022a8cfd1e (diff) |
nixio: Improve number handling with non-double Lua setups
-rw-r--r-- | libs/nixio/src/bit.c | 51 | ||||
-rw-r--r-- | libs/nixio/src/file.c | 8 | ||||
-rw-r--r-- | libs/nixio/src/fs.c | 28 | ||||
-rw-r--r-- | libs/nixio/src/nixio.h | 13 | ||||
-rw-r--r-- | libs/nixio/src/process.c | 24 |
5 files changed, 76 insertions, 48 deletions
diff --git a/libs/nixio/src/bit.c b/libs/nixio/src/bit.c index 5b41e91aa..1b352c51c 100644 --- a/libs/nixio/src/bit.c +++ b/libs/nixio/src/bit.c @@ -21,19 +21,24 @@ #include <stdlib.h> /* 52 bit maximum precision */ +#ifdef NIXIO_DOUBLE +#define NIXIO_BIT_BMAX 52 +#define NIXIO_BIT_NMAX 0xfffffffffffff +#else #define NIXIO_BIT_BMAX 32 #define NIXIO_BIT_NMAX 0xffffffff +#endif #define NIXIO_BIT_XOP(BIT_XOP) \ - uint64_t oper = luaL_checkinteger(L, 1); \ + uint64_t oper = nixio__checknumber(L, 1); \ const int args = lua_gettop(L); \ \ for (int i = 2; i <= args; i++) { \ - uint64_t oper2 = luaL_checkinteger(L, i); \ + uint64_t oper2 = nixio__checknumber(L, i); \ oper BIT_XOP oper2; \ } \ \ - lua_pushinteger(L, oper); \ + nixio__pushnumber(L, oper); \ return 1; \ @@ -54,53 +59,63 @@ static int nixio_bit_unset(lua_State *L) { } static int nixio_bit_not(lua_State *L) { - lua_pushinteger(L, (~((uint64_t)luaL_checkinteger(L, 1))) & NIXIO_BIT_NMAX); + nixio__pushnumber(L, + (~((uint64_t)nixio__checknumber(L, 1))) & NIXIO_BIT_NMAX); return 1; } static int nixio_bit_shl(lua_State *L) { - uint64_t oper = luaL_checkinteger(L, 1); + uint64_t oper = nixio__checknumber(L, 1); oper <<= luaL_checkinteger(L, 2); if (oper > NIXIO_BIT_NMAX) { return luaL_error(L, "arithmetic overflow"); } else { - lua_pushinteger(L, oper); + nixio__pushnumber(L, oper); return 1; } } static int nixio_bit_ashr(lua_State *L) { - int64_t oper = luaL_checkinteger(L, 1); - lua_pushinteger(L, oper >> luaL_checkinteger(L, 2)); + int64_t oper = nixio__checknumber(L, 1); + nixio__pushnumber(L, oper >> luaL_checkinteger(L, 2)); return 1; } static int nixio_bit_shr(lua_State *L) { - uint64_t oper = luaL_checkinteger(L, 1); - lua_pushinteger(L, oper >> luaL_checkinteger(L, 2)); + uint64_t oper = nixio__checknumber(L, 1); + nixio__pushnumber(L, oper >> luaL_checkinteger(L, 2)); return 1; } static int nixio_bit_div(lua_State *L) { - NIXIO_BIT_XOP(/=); + uint64_t oper = luaL_checknumber(L, 1); + const int args = lua_gettop(L); + + for (int i = 2; i <= args; i++) { + uint64_t oper2 = nixio__checknumber(L, i); + oper /= oper2; + } + + nixio__pushnumber(L, oper); + return 1; } static int nixio_bit_check(lua_State *L) { - uint64_t oper = luaL_checkinteger(L, 1); - uint64_t oper2 = luaL_checkinteger(L, 2); + uint64_t oper = nixio__checknumber(L, 1); + uint64_t oper2 = nixio__checknumber(L, 2); lua_pushboolean(L, (oper & oper2) == oper2); return 1; } static int nixio_bit_cast(lua_State *L) { - lua_pushinteger(L, ((uint64_t)luaL_checkinteger(L, 1)) & NIXIO_BIT_NMAX); + nixio__pushnumber(L, ((uint64_t)nixio__checknumber(L, 1)) & NIXIO_BIT_NMAX); return 1; } static int nixio_bit_swap(lua_State *L) { - uint64_t op = luaL_checkinteger(L, 1); + uint64_t op = nixio__checknumber(L, 1); op = (op >> 24) | ((op >> 8) & 0xff00) | ((op & 0xff00) << 8) | (op << 24); - lua_pushinteger(L, op); + nixio__pushnumber(L, op); return 1; } @@ -126,9 +141,9 @@ static const luaL_reg R[] = { void nixio_open_bit(lua_State *L) { lua_newtable(L); luaL_register(L, NULL, R); - lua_pushinteger(L, NIXIO_BIT_BMAX); + nixio__pushnumber(L, NIXIO_BIT_BMAX); lua_setfield(L, -2, "bits"); - lua_pushinteger(L, NIXIO_BIT_NMAX); + nixio__pushnumber(L, NIXIO_BIT_NMAX); lua_setfield(L, -2, "max"); lua_setfield(L, -2, "bit"); } diff --git a/libs/nixio/src/file.c b/libs/nixio/src/file.c index bb9e0844b..b86e040e1 100644 --- a/libs/nixio/src/file.c +++ b/libs/nixio/src/file.c @@ -226,7 +226,7 @@ static int nixio_file_read(lua_State *L) { static int nixio_file_seek(lua_State *L) { int fd = nixio__checkfd(L, 1); - off_t len = (off_t)luaL_checkinteger(L, 2); + off_t len = (off_t)nixio__checknumber(L, 2); int whence; const char *whstr = luaL_optlstring(L, 3, "set", NULL); if (!strcmp(whstr, "set")) { @@ -242,7 +242,7 @@ static int nixio_file_seek(lua_State *L) { if (len == -1) { return nixio__perror(L); } else { - lua_pushinteger(L, len); + nixio__pushnumber(L, len); return 1; } } @@ -253,7 +253,7 @@ static int nixio_file_tell(lua_State *L) { if (pos < 0) { return nixio__perror(L); } else { - lua_pushinteger(L, pos); + nixio__pushnumber(L, pos); return 1; } } @@ -291,7 +291,7 @@ static int nixio_file_sync(lua_State *L) { static int nixio_file_lock(lua_State *L) { int fd = nixio__checkfd(L, 1); const char *flag = luaL_checkstring(L, 2); - off_t len = (off_t)luaL_optinteger(L, 3, 0); + off_t len = (off_t)nixio__optnumber(L, 3, 0); int stat; int cmd = 0; diff --git a/libs/nixio/src/fs.c b/libs/nixio/src/fs.c index 53a406081..12ca111ac 100644 --- a/libs/nixio/src/fs.c +++ b/libs/nixio/src/fs.c @@ -257,8 +257,8 @@ static int nixio_utimes(lua_State *L) { if (lua_gettop(L) < 2 || (lua_isnoneornil(L, 2) && lua_isnoneornil(L, 3))) { return nixio__pstatus(L, !utimes(path, NULL)); } else { - double atime = luaL_checkinteger(L, 2); - double mtime = luaL_optinteger(L, 3, atime); + double atime = nixio__checknumber(L, 2); + double mtime = nixio__optnumber(L, 3, atime); struct timeval times[2]; times[0].tv_sec = atime; @@ -317,7 +317,7 @@ int nixio__push_stat(lua_State *L, nixio_stat_t *buf) { lua_pushinteger(L, buf->st_rdev); lua_setfield(L, -2, "rdev"); - lua_pushinteger(L, buf->st_size); + nixio__pushnumber(L, buf->st_size); lua_setfield(L, -2, "size"); 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) { lua_createtable(L, 0, 12); - lua_pushinteger(L, buf->f_bavail); + nixio__pushnumber(L, buf->f_bavail); lua_setfield(L, -2, "bavail"); - lua_pushinteger(L, buf->f_bfree); + nixio__pushnumber(L, buf->f_bfree); lua_setfield(L, -2, "bfree"); - lua_pushinteger(L, buf->f_blocks); + nixio__pushnumber(L, buf->f_blocks); lua_setfield(L, -2, "blocks"); - lua_pushinteger(L, buf->f_bsize); + nixio__pushnumber(L, buf->f_bsize); lua_setfield(L, -2, "bsize"); - lua_pushinteger(L, buf->f_frsize); + nixio__pushnumber(L, buf->f_frsize); lua_setfield(L, -2, "frsize"); - lua_pushinteger(L, buf->f_favail); + nixio__pushnumber(L, buf->f_favail); lua_setfield(L, -2, "favail"); - lua_pushinteger(L, buf->f_ffree); + nixio__pushnumber(L, buf->f_ffree); lua_setfield(L, -2, "ffree"); - lua_pushinteger(L, buf->f_files); + nixio__pushnumber(L, buf->f_files); lua_setfield(L, -2, "files"); - lua_pushinteger(L, buf->f_flag); + nixio__pushnumber(L, buf->f_flag); lua_setfield(L, -2, "flag"); - lua_pushinteger(L, buf->f_fsid); + nixio__pushnumber(L, buf->f_fsid); lua_setfield(L, -2, "fsid"); - lua_pushinteger(L, buf->f_namemax); + nixio__pushnumber(L, buf->f_namemax); lua_setfield(L, -2, "namemax"); return 1; diff --git a/libs/nixio/src/nixio.h b/libs/nixio/src/nixio.h index 0c43cfe8a..ff75cfd98 100644 --- a/libs/nixio/src/nixio.h +++ b/libs/nixio/src/nixio.h @@ -19,6 +19,7 @@ #include <lua.h> #include <lualib.h> #include <lauxlib.h> +#include <luaconf.h> #define NIXIO_BUFFERSIZE 8192 @@ -39,6 +40,18 @@ typedef struct nixio_address { int nixio__perror(lua_State *L); int nixio__pstatus(lua_State *L, int condition); +#if defined(LUA_NUMBER_DOUBLE) || defined(LNUM_DOUBLE) || defined(LNUM_LDOUBLE) +#define NIXIO_DOUBLE 1 +#define nixio__checknumber luaL_checknumber +#define nixio__pushnumber lua_pushnumber +#define nixio__optnumber luaL_optnumber +#else +#define nixio__checknumber luaL_checkinteger +#define nixio__pushnumber lua_pushinteger +#define nixio__optnumber luaL_optinteger +#endif + + #ifndef __WINNT__ #define NIXIO_API extern diff --git a/libs/nixio/src/process.c b/libs/nixio/src/process.c index f3bb2f400..45c06a644 100644 --- a/libs/nixio/src/process.c +++ b/libs/nixio/src/process.c @@ -225,16 +225,16 @@ static int nixio_times(lua_State *L) { return nixio__perror(L); } else { lua_createtable(L, 0, 4); - lua_pushinteger(L, buf.tms_cstime); + nixio__pushnumber(L, buf.tms_cstime); lua_setfield(L, -2, "cstime"); - lua_pushinteger(L, buf.tms_cutime); + nixio__pushnumber(L, buf.tms_cutime); lua_setfield(L, -2, "cutime"); - lua_pushinteger(L, buf.tms_stime); + nixio__pushnumber(L, buf.tms_stime); lua_setfield(L, -2, "stime"); - lua_pushinteger(L, buf.tms_utime); + nixio__pushnumber(L, buf.tms_utime); lua_setfield(L, -2, "utime"); return 1; @@ -365,16 +365,16 @@ static int nixio_sysinfo(lua_State *L) { lua_createtable(L, 0, 12); - lua_pushinteger(L, info.bufferram); + nixio__pushnumber(L, info.bufferram); lua_setfield(L, -2, "bufferram"); - lua_pushinteger(L, info.freehigh); + nixio__pushnumber(L, info.freehigh); lua_setfield(L, -2, "freehigh"); - lua_pushinteger(L, info.freeram); + nixio__pushnumber(L, info.freeram); lua_setfield(L, -2, "freeram"); - lua_pushinteger(L, info.freeswap); + nixio__pushnumber(L, info.freeswap); lua_setfield(L, -2, "freeswap"); lua_createtable(L, 0, 3); @@ -390,16 +390,16 @@ static int nixio_sysinfo(lua_State *L) { lua_pushinteger(L, info.procs); lua_setfield(L, -2, "procs"); - lua_pushinteger(L, info.sharedram); + nixio__pushnumber(L, info.sharedram); lua_setfield(L, -2, "sharedram"); - lua_pushinteger(L, info.totalhigh); + nixio__pushnumber(L, info.totalhigh); lua_setfield(L, -2, "totalhigh"); - lua_pushinteger(L, info.totalram); + nixio__pushnumber(L, info.totalram); lua_setfield(L, -2, "totalram"); - lua_pushinteger(L, info.totalswap); + nixio__pushnumber(L, info.totalswap); lua_setfield(L, -2, "totalswap"); lua_pushinteger(L, info.uptime); |