aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2018-02-11 12:44:09 +0100
committerPhilipp Janda <siffiejoe@gmx.net>2018-02-11 12:44:09 +0100
commitd48f7f5e0932f573d18952642cfbd6da017f431c (patch)
treeb343f3dc4787990bc4d669e9e4e465e14c639111
parent340f2f4777e8ad427a364c9c1ef562d40c60cb1d (diff)
downloadlua-compat-5.3-d48f7f5e0932f573d18952642cfbd6da017f431c.tar.gz
lua-compat-5.3-d48f7f5e0932f573d18952642cfbd6da017f431c.tar.bz2
lua-compat-5.3-d48f7f5e0932f573d18952642cfbd6da017f431c.zip
Provide strict lua_tointegerx for Lua 5.2 as well.
Closes #40.
-rw-r--r--c-api/compat-5.3.c32
-rw-r--r--c-api/compat-5.3.h6
2 files changed, 19 insertions, 19 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c
index ebaf03f..370027a 100644
--- a/c-api/compat-5.3.c
+++ b/c-api/compat-5.3.c
@@ -204,22 +204,6 @@ COMPAT53_API void lua_rawsetp (lua_State *L, int i, const void *p) {
204} 204}
205 205
206 206
207COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) {
208 int ok = 0;
209 lua_Number n = lua_tonumberx(L, i, &ok);
210 lua_Integer j = (lua_tointeger)(L, i); /* native lua_tointeger */
211 if (isnum == NULL)
212 isnum = &ok;
213 if (ok && n == j) {
214 *isnum = 1;
215 return j;
216 } else {
217 *isnum = 0;
218 return 0;
219 }
220}
221
222
223COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) { 207COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) {
224 lua_Number n = lua_tonumber(L, i); 208 lua_Number n = lua_tonumber(L, i);
225 if (isnum != NULL) { 209 if (isnum != NULL) {
@@ -747,6 +731,22 @@ COMPAT53_API int lua_isinteger (lua_State *L, int index) {
747} 731}
748 732
749 733
734COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) {
735 int ok = 0;
736 lua_Number n = lua_tonumberx(L, i, &ok);
737 if (ok) {
738 if (n == (lua_Integer)n) {
739 if (isnum)
740 *isnum = 1;
741 return (lua_Integer)n;
742 }
743 }
744 if (isnum)
745 *isnum = 0;
746 return 0;
747}
748
749
750static void compat53_reverse (lua_State *L, int a, int b) { 750static void compat53_reverse (lua_State *L, int a, int b) {
751 for (; a < b; ++a, --b) { 751 for (; a < b; ++a, --b) {
752 lua_pushvalue(L, a); 752 lua_pushvalue(L, a);
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h
index 6b6e35c..8e10893 100644
--- a/c-api/compat-5.3.h
+++ b/c-api/compat-5.3.h
@@ -167,9 +167,6 @@ COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
167 167
168#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL) 168#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
169 169
170#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx)
171COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum);
172
173#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx) 170#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
174COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum); 171COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum);
175 172
@@ -299,6 +296,9 @@ COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i);
299#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger) 296#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
300COMPAT53_API int lua_isinteger (lua_State *L, int index); 297COMPAT53_API int lua_isinteger (lua_State *L, int index);
301 298
299#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
300COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum);
301
302#define lua_numbertointeger(n, p) \ 302#define lua_numbertointeger(n, p) \
303 ((*(p) = (lua_Integer)(n)), 1) 303 ((*(p) = (lua_Integer)(n)), 1)
304 304