aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--llimits.h8
-rw-r--r--ltable.c12
-rw-r--r--lvm.c20
3 files changed, 30 insertions, 10 deletions
diff --git a/llimits.h b/llimits.h
index e15c1fd5..04405019 100644
--- a/llimits.h
+++ b/llimits.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llimits.h,v 1.104 2013/04/25 21:15:37 roberto Exp roberto $ 2** $Id: llimits.h,v 1.105 2013/05/23 21:27:06 roberto Exp roberto $
3** Limits, basic types, and some other `installation-dependent' definitions 3** Limits, basic types, and some other `installation-dependent' definitions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -36,6 +36,12 @@ typedef unsigned char lu_byte;
36 36
37#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 37#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
38 38
39
40/* minimum and maximum values for lua_Integer */
41#define MAX_INTEGER ((lua_Integer)(~(lua_Unsigned)0 >> 1))
42#define MIN_INTEGER (~MAX_INTEGER)
43
44
39/* 45/*
40** conversion of pointer to integer 46** conversion of pointer to integer
41** this is for hashing only; there is no problem if the integer 47** this is for hashing only; there is no problem if the integer
diff --git a/ltable.c b/ltable.c
index 2e95ce26..ae92cbd9 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 2.74 2013/04/26 15:39:25 roberto Exp roberto $ 2** $Id: ltable.c,v 2.75 2013/04/29 17:12:50 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -65,6 +65,12 @@
65#define hashpointer(t,p) hashmod(t, IntPoint(p)) 65#define hashpointer(t,p) hashmod(t, IntPoint(p))
66 66
67 67
68/* checks whether a float has a value representable as a lua_Integer
69 (and does the conversion if so) */
70#define numisinteger(x,i) \
71 (((x) == l_mathop(floor)(x)) && luaV_numtointeger(x, i))
72
73
68#define dummynode (&dummynode_) 74#define dummynode (&dummynode_)
69 75
70#define isdummy(n) ((n) == dummynode) 76#define isdummy(n) ((n) == dummynode)
@@ -412,7 +418,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
412 lua_Integer k; 418 lua_Integer k;
413 if (luai_numisnan(L, n)) 419 if (luai_numisnan(L, n))
414 luaG_runerror(L, "table index is NaN"); 420 luaG_runerror(L, "table index is NaN");
415 if (luaV_numtointeger(n, &k)) { /* index is int? */ 421 if (numisinteger(n, &k)) { /* index is int? */
416 setivalue(&aux, k); 422 setivalue(&aux, k);
417 key = &aux; /* insert it as an integer */ 423 key = &aux; /* insert it as an integer */
418 } 424 }
@@ -494,7 +500,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
494 case LUA_TNIL: return luaO_nilobject; 500 case LUA_TNIL: return luaO_nilobject;
495 case LUA_TNUMFLT: { 501 case LUA_TNUMFLT: {
496 lua_Integer k; 502 lua_Integer k;
497 if (luaV_numtointeger(fltvalue(key), &k)) /* index is int? */ 503 if (numisinteger(fltvalue(key), &k)) /* index is int? */
498 return luaH_getint(t, k); /* use specialized version */ 504 return luaH_getint(t, k); /* use specialized version */
499 /* else go through */ 505 /* else go through */
500 } 506 }
diff --git a/lvm.c b/lvm.c
index 6265a603..c857238d 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.169 2013/05/06 17:17:09 roberto Exp roberto $ 2** $Id: lvm.c,v 2.170 2013/05/26 14:47:51 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -58,17 +58,25 @@ int luaV_tostring (lua_State *L, StkId obj) {
58} 58}
59 59
60 60
61/*
62** Check whether a float number is within the range of a lua_Integer.
63** (The comparisons are tricky because of rounding, which can or
64** not occur depending on the relative sizes of floats and integers.)
65** This function is called only when 'n' has an integer value.
66*/
61int luaV_numtointeger (lua_Number n, lua_Integer *p) { 67int luaV_numtointeger (lua_Number n, lua_Integer *p) {
62 lua_Integer k; 68 if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) {
63 lua_number2integer(k, n); 69 *p = cast_integer(n);
64 if (luai_numeq(cast_num(k), n)) { /* 'k' is int? */ 70 lua_assert(cast_num(*p) == n);
65 *p = k;
66 return 1; 71 return 1;
67 } 72 }
68 return 0; 73 return 0; /* number is outside integer limits */
69} 74}
70 75
71 76
77/*
78** try to convert a non-integer value to an integer
79*/
72int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { 80int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
73 lua_Number n; 81 lua_Number n;
74 lua_assert(!ttisinteger(obj)); 82 lua_assert(!ttisinteger(obj));