more relaxed rules for __eq metamethod (more similar to other

operators)
This commit is contained in:
Roberto Ierusalimschy 2014-06-10 15:53:18 -03:00
parent 542b6cfc02
commit 1a3656e56e
3 changed files with 11 additions and 20 deletions

15
ltm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ltm.c,v 2.25 2013/12/30 20:47:58 roberto Exp $
** $Id: ltm.c,v 2.26 2014/04/11 20:17:39 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@ -129,19 +129,6 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
}
const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2) {
const TValue *tm1 = fasttm(L, mt1, TM_EQ);
const TValue *tm2;
if (tm1 == NULL) return NULL; /* no metamethod */
if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
tm2 = fasttm(L, mt2, TM_EQ);
if (tm2 == NULL) return NULL; /* no metamethod */
if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
return tm1;
return NULL;
}
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
TMS event) {
if (!luaT_callbinTM(L, p1, p2, L->top, event))

3
ltm.h
View File

@ -1,5 +1,5 @@
/*
** $Id: ltm.h,v 2.18 2013/12/18 14:12:03 roberto Exp roberto $
** $Id: ltm.h,v 2.19 2013/12/30 20:47:58 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@ -67,7 +67,6 @@ LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event);
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event);
LUAI_FUNC const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2);
LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
const TValue *p2, TMS event);

13
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.213 2014/05/23 18:32:21 roberto Exp roberto $
** $Id: lvm.c,v 2.214 2014/05/26 17:10:22 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -342,19 +342,24 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
case LUA_TUSERDATA: {
if (uvalue(t1) == uvalue(t2)) return 1;
else if (L == NULL) return 0;
tm = luaT_getequalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable);
tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
if (tm == NULL)
tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
break; /* will try TM */
}
case LUA_TTABLE: {
if (hvalue(t1) == hvalue(t2)) return 1;
else if (L == NULL) return 0;
tm = luaT_getequalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable);
tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
if (tm == NULL)
tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
break; /* will try TM */
}
default:
return gcvalue(t1) == gcvalue(t2);
}
if (tm == NULL) return 0; /* no TM? */
if (tm == NULL) /* no TM? */
return 0; /* objects are different */
luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */
return !l_isfalse(L->top);
}