diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-06-10 15:53:18 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-06-10 15:53:18 -0300 |
| commit | 1a3656e56eb1c873a4d74661eb44c96c07d662c1 (patch) | |
| tree | b5622ff8a45cc79a982b06540ea0df8427decca0 | |
| parent | 542b6cfc02d57e66db7afc23a1d8350ae189e3c6 (diff) | |
| download | lua-1a3656e56eb1c873a4d74661eb44c96c07d662c1.tar.gz lua-1a3656e56eb1c873a4d74661eb44c96c07d662c1.tar.bz2 lua-1a3656e56eb1c873a4d74661eb44c96c07d662c1.zip | |
more relaxed rules for __eq metamethod (more similar to other
operators)
| -rw-r--r-- | ltm.c | 15 | ||||
| -rw-r--r-- | ltm.h | 3 | ||||
| -rw-r--r-- | lvm.c | 13 |
3 files changed, 11 insertions, 20 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltm.c,v 2.25 2013/12/30 20:47:58 roberto Exp $ | 2 | ** $Id: ltm.c,v 2.26 2014/04/11 20:17:39 roberto Exp roberto $ |
| 3 | ** Tag methods | 3 | ** Tag methods |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -129,19 +129,6 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, | |||
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | 131 | ||
| 132 | const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2) { | ||
| 133 | const TValue *tm1 = fasttm(L, mt1, TM_EQ); | ||
| 134 | const TValue *tm2; | ||
| 135 | if (tm1 == NULL) return NULL; /* no metamethod */ | ||
| 136 | if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ | ||
| 137 | tm2 = fasttm(L, mt2, TM_EQ); | ||
| 138 | if (tm2 == NULL) return NULL; /* no metamethod */ | ||
| 139 | if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */ | ||
| 140 | return tm1; | ||
| 141 | return NULL; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, | 132 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, |
| 146 | TMS event) { | 133 | TMS event) { |
| 147 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) | 134 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltm.h,v 2.18 2013/12/18 14:12:03 roberto Exp roberto $ | 2 | ** $Id: ltm.h,v 2.19 2013/12/30 20:47:58 roberto Exp roberto $ |
| 3 | ** Tag methods | 3 | ** Tag methods |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -67,7 +67,6 @@ LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, | |||
| 67 | StkId res, TMS event); | 67 | StkId res, TMS event); |
| 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, | 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, |
| 69 | StkId res, TMS event); | 69 | StkId res, TMS event); |
| 70 | LUAI_FUNC const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2); | ||
| 71 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, | 70 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, |
| 72 | const TValue *p2, TMS event); | 71 | const TValue *p2, TMS event); |
| 73 | 72 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.213 2014/05/23 18:32:21 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.214 2014/05/26 17:10:22 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -342,19 +342,24 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { | |||
| 342 | case LUA_TUSERDATA: { | 342 | case LUA_TUSERDATA: { |
| 343 | if (uvalue(t1) == uvalue(t2)) return 1; | 343 | if (uvalue(t1) == uvalue(t2)) return 1; |
| 344 | else if (L == NULL) return 0; | 344 | else if (L == NULL) return 0; |
| 345 | tm = luaT_getequalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable); | 345 | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); |
| 346 | if (tm == NULL) | ||
| 347 | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); | ||
| 346 | break; /* will try TM */ | 348 | break; /* will try TM */ |
| 347 | } | 349 | } |
| 348 | case LUA_TTABLE: { | 350 | case LUA_TTABLE: { |
| 349 | if (hvalue(t1) == hvalue(t2)) return 1; | 351 | if (hvalue(t1) == hvalue(t2)) return 1; |
| 350 | else if (L == NULL) return 0; | 352 | else if (L == NULL) return 0; |
| 351 | tm = luaT_getequalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable); | 353 | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); |
| 354 | if (tm == NULL) | ||
| 355 | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); | ||
| 352 | break; /* will try TM */ | 356 | break; /* will try TM */ |
| 353 | } | 357 | } |
| 354 | default: | 358 | default: |
| 355 | return gcvalue(t1) == gcvalue(t2); | 359 | return gcvalue(t1) == gcvalue(t2); |
| 356 | } | 360 | } |
| 357 | if (tm == NULL) return 0; /* no TM? */ | 361 | if (tm == NULL) /* no TM? */ |
| 362 | return 0; /* objects are different */ | ||
| 358 | luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ | 363 | luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ |
| 359 | return !l_isfalse(L->top); | 364 | return !l_isfalse(L->top); |
| 360 | } | 365 | } |
