diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-02-20 12:27:53 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-02-20 12:27:53 -0200 |
commit | 81245b1ad51c5f4a4dd71da272b65b2450929b80 (patch) | |
tree | 25b69ceb42d06e1c3a32aabda7212b805f605349 | |
parent | 397ce11996bb1b5a6ef81fdf44252cf58b230937 (diff) | |
download | lua-81245b1ad51c5f4a4dd71da272b65b2450929b80.tar.gz lua-81245b1ad51c5f4a4dd71da272b65b2450929b80.tar.bz2 lua-81245b1ad51c5f4a4dd71da272b65b2450929b80.zip |
'numisinteger' (for table keys) replaced by 'luaV_tointeger' (old
'tointeger_aux'), which can do the same job.
-rw-r--r-- | ltable.c | 22 | ||||
-rw-r--r-- | lvm.c | 24 | ||||
-rw-r--r-- | lvm.h | 16 |
3 files changed, 21 insertions, 41 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.103 2015/02/16 13:15:00 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.104 2015/02/20 14:05:01 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 | */ |
@@ -84,17 +84,6 @@ static const Node dummynode_ = { | |||
84 | 84 | ||
85 | 85 | ||
86 | /* | 86 | /* |
87 | ** Checks whether a float has a value representable as a lua_Integer | ||
88 | ** (and does the conversion if so) | ||
89 | */ | ||
90 | static int numisinteger (lua_Number x, lua_Integer *p) { | ||
91 | if ((x) == l_floor(x)) /* integral value? */ | ||
92 | return lua_numbertointeger(x, p); /* try as an integer */ | ||
93 | else return 0; | ||
94 | } | ||
95 | |||
96 | |||
97 | /* | ||
98 | ** Hash for floating-point numbers. | 87 | ** Hash for floating-point numbers. |
99 | ** The main computation should be just | 88 | ** The main computation should be just |
100 | ** n = frepx(n, &i); return (n * INT_MAX) + i | 89 | ** n = frepx(n, &i); return (n * INT_MAX) + i |
@@ -455,14 +444,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
455 | TValue aux; | 444 | TValue aux; |
456 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | 445 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
457 | else if (ttisfloat(key)) { | 446 | else if (ttisfloat(key)) { |
458 | lua_Number n = fltvalue(key); | ||
459 | lua_Integer k; | 447 | lua_Integer k; |
460 | if (luai_numisnan(n)) | 448 | if (luaV_tointeger(key, &k, 0)) { /* index is int? */ |
461 | luaG_runerror(L, "table index is NaN"); | ||
462 | if (numisinteger(n, &k)) { /* index is int? */ | ||
463 | setivalue(&aux, k); | 449 | setivalue(&aux, k); |
464 | key = &aux; /* insert it as an integer */ | 450 | key = &aux; /* insert it as an integer */ |
465 | } | 451 | } |
452 | else if (luai_numisnan(fltvalue(key))) | ||
453 | luaG_runerror(L, "table index is NaN"); | ||
466 | } | 454 | } |
467 | mp = mainposition(t, key); | 455 | mp = mainposition(t, key); |
468 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ | 456 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ |
@@ -556,7 +544,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
556 | case LUA_TNIL: return luaO_nilobject; | 544 | case LUA_TNIL: return luaO_nilobject; |
557 | case LUA_TNUMFLT: { | 545 | case LUA_TNUMFLT: { |
558 | lua_Integer k; | 546 | lua_Integer k; |
559 | if (numisinteger(fltvalue(key), &k)) /* index is int? */ | 547 | if (luaV_tointeger(key, &k, 0)) /* index is int? */ |
560 | return luaH_getint(t, k); /* use specialized version */ | 548 | return luaH_getint(t, k); /* use specialized version */ |
561 | /* else go through */ | 549 | /* else go through */ |
562 | } | 550 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.233 2015/01/16 16:54:37 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.234 2015/02/05 17:15:33 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 | */ |
@@ -31,16 +31,6 @@ | |||
31 | #include "lvm.h" | 31 | #include "lvm.h" |
32 | 32 | ||
33 | 33 | ||
34 | /* | ||
35 | ** You can define LUA_FLOORN2I if you want to convert floats to integers | ||
36 | ** by flooring them (instead of raising an error if they are not | ||
37 | ** integral values) | ||
38 | */ | ||
39 | #if !defined(LUA_FLOORN2I) | ||
40 | #define LUA_FLOORN2I 0 | ||
41 | #endif | ||
42 | |||
43 | |||
44 | /* limit for table tag-method chains (to avoid loops) */ | 34 | /* limit for table tag-method chains (to avoid loops) */ |
45 | #define MAXTAGLOOP 2000 | 35 | #define MAXTAGLOOP 2000 |
46 | 36 | ||
@@ -89,7 +79,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | |||
89 | ** mode == 1: takes the floor of the number | 79 | ** mode == 1: takes the floor of the number |
90 | ** mode == 2: takes the ceil of the number | 80 | ** mode == 2: takes the ceil of the number |
91 | */ | 81 | */ |
92 | static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { | 82 | int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { |
93 | TValue v; | 83 | TValue v; |
94 | again: | 84 | again: |
95 | if (ttisfloat(obj)) { | 85 | if (ttisfloat(obj)) { |
@@ -116,14 +106,6 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { | |||
116 | 106 | ||
117 | 107 | ||
118 | /* | 108 | /* |
119 | ** try to convert a value to an integer | ||
120 | */ | ||
121 | int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { | ||
122 | return tointeger_aux(obj, p, LUA_FLOORN2I); | ||
123 | } | ||
124 | |||
125 | |||
126 | /* | ||
127 | ** Try to convert a 'for' limit to an integer, preserving the | 109 | ** Try to convert a 'for' limit to an integer, preserving the |
128 | ** semantics of the loop. | 110 | ** semantics of the loop. |
129 | ** (The following explanation assumes a non-negative step; it is valid | 111 | ** (The following explanation assumes a non-negative step; it is valid |
@@ -141,7 +123,7 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { | |||
141 | static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, | 123 | static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, |
142 | int *stopnow) { | 124 | int *stopnow) { |
143 | *stopnow = 0; /* usually, let loops run */ | 125 | *stopnow = 0; /* usually, let loops run */ |
144 | if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ | 126 | if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ |
145 | lua_Number n; /* try to convert to float */ | 127 | lua_Number n; /* try to convert to float */ |
146 | if (!tonumber(obj, &n)) /* cannot convert to float? */ | 128 | if (!tonumber(obj, &n)) /* cannot convert to float? */ |
147 | return 0; /* not a number */ | 129 | return 0; /* not a number */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 2.33 2014/07/30 14:42:44 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 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 | */ |
@@ -27,11 +27,21 @@ | |||
27 | #endif | 27 | #endif |
28 | 28 | ||
29 | 29 | ||
30 | /* | ||
31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers | ||
32 | ** by flooring them (instead of raising an error if they are not | ||
33 | ** integral values) | ||
34 | */ | ||
35 | #if !defined(LUA_FLOORN2I) | ||
36 | #define LUA_FLOORN2I 0 | ||
37 | #endif | ||
38 | |||
39 | |||
30 | #define tonumber(o,n) \ | 40 | #define tonumber(o,n) \ |
31 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) | 41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) |
32 | 42 | ||
33 | #define tointeger(o,i) \ | 43 | #define tointeger(o,i) \ |
34 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) | 44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) |
35 | 45 | ||
36 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) | 46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) |
37 | 47 | ||
@@ -42,7 +52,7 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); | |||
42 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); | 52 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); |
43 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); | 53 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); |
44 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); | 54 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); |
45 | LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); | 55 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); |
46 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, | 56 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, |
47 | StkId val); | 57 | StkId val); |
48 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, | 58 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, |