** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) */
| 303 | ** L == NULL means raw equality (no metamethods) |
| 304 | */ |
| 305 | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { |
| 306 | const TValue *tm; |
| 307 | if (ttype(t1) != ttype(t2)) { /* not the same variant? */ |
| 308 | if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) |
| 309 | return 0; /* only numbers can be equal with different variants */ |
| 310 | else { /* two numbers with different variants */ |
| 311 | lua_Number n1, n2; /* compare them as floats */ |
| 312 | lua_assert(ttisnumber(t1) && ttisnumber(t2)); |
| 313 | cast_void(tofloat(t1, &n1)); cast_void(tofloat(t2, &n2)); |
| 314 | return luai_numeq(n1, n2); |
| 315 | } |
| 316 | } |
| 317 | /* values have same type and same variant */ |
| 318 | switch (ttype(t1)) { |
| 319 | case LUA_TNIL: return 1; |
| 320 | case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); |
| 321 | case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); |
| 322 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ |
| 323 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
| 324 | case LUA_TLCF: return fvalue(t1) == fvalue(t2); |
| 325 | case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); |
| 326 | case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); |
| 327 | case LUA_TUSERDATA: { |
| 328 | if (uvalue(t1) == uvalue(t2)) return 1; |
| 329 | else if (L == NULL) return 0; |
| 330 | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); |
| 331 | if (tm == NULL) |
| 332 | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); |
| 333 | break; /* will try TM */ |
| 334 | } |
| 335 | case LUA_TTABLE: { |
| 336 | if (hvalue(t1) == hvalue(t2)) return 1; |
| 337 | else if (L == NULL) return 0; |
| 338 | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); |
| 339 | if (tm == NULL) |
| 340 | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); |
| 341 | break; /* will try TM */ |
| 342 | } |
| 343 | default: |
| 344 | return gcvalue(t1) == gcvalue(t2); |
| 345 | } |
| 346 | if (tm == NULL) /* no TM? */ |
| 347 | return 0; /* objects are different */ |
| 348 | luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ |
| 349 | return !l_isfalse(L->top); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ |
no test coverage detected