From 340f2f4777e8ad427a364c9c1ef562d40c60cb1d Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Sat, 13 Jan 2018 19:57:19 +0100 Subject: lua_tointeger(x) rejects non-ints for Lua 5.1. --- c-api/compat-5.3.c | 15 +++++++++++---- c-api/compat-5.3.h | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'c-api') diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index b82c654..ebaf03f 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c @@ -205,11 +205,18 @@ COMPAT53_API void lua_rawsetp (lua_State *L, int i, const void *p) { COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) { - lua_Integer n = lua_tointeger(L, i); - if (isnum != NULL) { - *isnum = (n != 0 || lua_isnumber(L, i)); + int ok = 0; + lua_Number n = lua_tonumberx(L, i, &ok); + lua_Integer j = (lua_tointeger)(L, i); /* native lua_tointeger */ + if (isnum == NULL) + isnum = &ok; + if (ok && n == j) { + *isnum = 1; + return j; + } else { + *isnum = 0; + return 0; } - return n; } diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h index 755c23e..6b6e35c 100644 --- a/c-api/compat-5.3.h +++ b/c-api/compat-5.3.h @@ -165,6 +165,8 @@ COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p); #define lua_rawlen(L, i) lua_objlen((L), (i)) +#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL) + #define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx) COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum); -- cgit v1.2.3-55-g6feb