BUG: problems with negative indexes

This commit is contained in:
Roberto Ierusalimschy 1996-11-18 11:48:44 -02:00
parent 81411e8913
commit 62e1a4c84d
1 changed files with 21 additions and 19 deletions

40
hash.c
View File

@ -3,7 +3,7 @@
** hash manager for lua ** hash manager for lua
*/ */
char *rcs_hash="$Id: hash.c,v 2.30 1996/05/06 14:30:27 roberto Exp roberto $"; char *rcs_hash="$Id: hash.c,v 2.31 1996/07/12 20:00:26 roberto Exp roberto $";
#include "mem.h" #include "mem.h"
@ -48,24 +48,26 @@ int luaI_redimension (int nhash)
static int hashindex (Hash *t, Object *ref) /* hash function */ static int hashindex (Hash *t, Object *ref) /* hash function */
{ {
switch (tag(ref)) long int h;
{ switch (tag(ref)) {
case LUA_T_NIL: case LUA_T_NIL:
lua_error ("unexpected type to index table"); lua_error ("unexpected type to index table");
return -1; /* UNREACHEABLE */ h = 0; /* UNREACHEABLE */
case LUA_T_NUMBER: case LUA_T_NUMBER:
return (((int)nvalue(ref))%nhash(t)); h = (long int)nvalue(ref); break;
case LUA_T_STRING: case LUA_T_STRING:
return (int)((tsvalue(ref)->hash)%nhash(t)); /* make it a valid index */ h = tsvalue(ref)->hash; break;
case LUA_T_FUNCTION: case LUA_T_FUNCTION:
return (((IntPoint)ref->value.tf)%nhash(t)); h = (IntPoint)ref->value.tf; break;
case LUA_T_CFUNCTION: case LUA_T_CFUNCTION:
return (((IntPoint)fvalue(ref))%nhash(t)); h = (IntPoint)fvalue(ref); break;
case LUA_T_ARRAY: case LUA_T_ARRAY:
return (((IntPoint)avalue(ref))%nhash(t)); h = (IntPoint)avalue(ref); break;
default: /* user data */ default: /* user data */
return (((IntPoint)uvalue(ref))%nhash(t)); h = (IntPoint)uvalue(ref); break;
} }
if (h < 0) h = -h;
return h%nhash(t); /* make it a valid index */
} }
int lua_equalObj (Object *t1, Object *t2) int lua_equalObj (Object *t1, Object *t2)