aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-06-20 14:37:31 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-06-20 14:37:31 -0300
commitf45177f2d325d76e0622109ddf4f534a05e721e8 (patch)
tree3adad0c9c836262a620b1814173282b2db06aca1
parentd6f5fb2d2c5cfeab1e0aaddac7f121a909eb9408 (diff)
downloadlua-f45177f2d325d76e0622109ddf4f534a05e721e8.tar.gz
lua-f45177f2d325d76e0622109ddf4f534a05e721e8.tar.bz2
lua-f45177f2d325d76e0622109ddf4f534a05e721e8.zip
In the table that hashes constants, use a light userdata as keys
to integer values to avoid collisions with floats with the same numerical value
-rw-r--r--lcode.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/lcode.c b/lcode.c
index ffb263f7..208b2f9a 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.68 2013/05/02 12:37:24 roberto Exp roberto $ 2** $Id: lcode.c,v 2.69 2013/05/06 17:22:16 roberto Exp roberto $
3** Code generator for Lua 3** Code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -333,9 +333,12 @@ int luaK_stringK (FuncState *fs, TString *s) {
333 333
334 334
335int luaK_intK (FuncState *fs, lua_Integer n) { 335int luaK_intK (FuncState *fs, lua_Integer n) {
336 TValue o; 336 TValue k, o;
337 /* use userdata as key to avoid collision with float with same value;
338 conversion to 'void*' used only for hash, no "precision" problems */
339 setpvalue(&k, cast(void*, cast(size_t, n)));
337 setivalue(&o, n); 340 setivalue(&o, n);
338 return addk(fs, &o, &o); 341 return addk(fs, &k, &o);
339} 342}
340 343
341 344
@@ -344,17 +347,14 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
344 lua_State *L = fs->ls->L; 347 lua_State *L = fs->ls->L;
345 TValue o; 348 TValue o;
346 setnvalue(&o, r); 349 setnvalue(&o, r);
347 if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ 350 if (r != 0 && !luai_numisnan(NULL, r)) /* avoid -0 and NaN */
351 n = addk(fs, &o, &o); /* regular case */
352 else { /* handle -0 and NaN */
348 /* use raw representation as key to avoid numeric problems */ 353 /* use raw representation as key to avoid numeric problems */
349 setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r))); 354 setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r)));
350 n = addk(fs, L->top - 1, &o); 355 n = addk(fs, L->top - 1, &o);
351 L->top--; 356 L->top--;
352 } 357 }
353 else {
354 TValue k;
355 setnvalue(&k, r + 0.5); /* ???? (avoid some collisions with ints) */
356 n = addk(fs, &k, &o); /* regular case */
357 }
358 return n; 358 return n;
359} 359}
360 360