diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-27 09:43:37 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-27 09:43:37 -0300 |
| commit | d630daca1a3dad6357226fdc6472692fa7e4b5c0 (patch) | |
| tree | c4fa77406b680091b4325e46736fe85ac012279c /ltable.c | |
| parent | 8c883cb4e86a6c176465c9347dfff5e0044f1c93 (diff) | |
| download | lua-d630daca1a3dad6357226fdc6472692fa7e4b5c0.tar.gz lua-d630daca1a3dad6357226fdc6472692fa7e4b5c0.tar.bz2 lua-d630daca1a3dad6357226fdc6472692fa7e4b5c0.zip | |
"legal" way to convert a float to an integer in C
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 12 |
1 files changed, 9 insertions, 3 deletions
| @@ -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 | } |
