瀏覽代碼

more relaxed rules for __eq metamethod (more similar to other
operators)

Roberto Ierusalimschy 11 年之前
父節點
當前提交
1a3656e56e
共有 3 個文件被更改,包括 11 次插入20 次删除
  1. 1 14
      ltm.c
  2. 1 2
      ltm.h
  3. 9 4
      lvm.c

+ 1 - 14
ltm.c

@@ -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
 ** Tag methods
 ** See Copyright Notice in lua.h
 ** 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,
 int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
                       TMS event) {
                       TMS event) {
   if (!luaT_callbinTM(L, p1, p2, L->top, event))
   if (!luaT_callbinTM(L, p1, p2, L->top, event))

+ 1 - 2
ltm.h

@@ -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
 ** Tag methods
 ** See Copyright Notice in lua.h
 ** 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);
                               StkId res, TMS event);
 LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
 LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
                               StkId res, TMS event);
                               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,
 LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
                                 const TValue *p2, TMS event);
                                 const TValue *p2, TMS event);
 
 

+ 9 - 4
lvm.c

@@ -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
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** 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: {
     case LUA_TUSERDATA: {
       if (uvalue(t1) == uvalue(t2)) return 1;
       if (uvalue(t1) == uvalue(t2)) return 1;
       else if (L == NULL) return 0;
       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 */
       break;  /* will try TM */
     }
     }
     case LUA_TTABLE: {
     case LUA_TTABLE: {
       if (hvalue(t1) == hvalue(t2)) return 1;
       if (hvalue(t1) == hvalue(t2)) return 1;
       else if (L == NULL) return 0;
       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 */
       break;  /* will try TM */
     }
     }
     default:
     default:
       return gcvalue(t1) == gcvalue(t2);
       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 */
   luaT_callTM(L, tm, t1, t2, L->top, 1);  /* call TM */
   return !l_isfalse(L->top);
   return !l_isfalse(L->top);
 }
 }