** Main operation for equality of Lua values; return 't1 == t2'. ** L == nullptr means raw equality (no metamethods) */
| 451 | ** L == nullptr means raw equality (no metamethods) |
| 452 | */ |
| 453 | int luaV_equalobj(lua_State *L, const TValue *t1, const TValue *t2) { |
| 454 | const TValue *tm; |
| 455 | if (ttype(t1) != ttype(t2)) { /* not the same variant? */ |
| 456 | if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) |
| 457 | return 0; /* only numbers can be equal with different variants */ |
| 458 | else { /* two numbers with different variants */ |
| 459 | lua_Integer i1, i2; /* compare them as integers */ |
| 460 | return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); |
| 461 | } |
| 462 | } |
| 463 | /* values have same type and same variant */ |
| 464 | switch (ttype(t1)) { |
| 465 | case LUA_TNIL: return 1; |
| 466 | case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); |
| 467 | case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); |
| 468 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ |
| 469 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
| 470 | case LUA_TLCF: return fvalue(t1) == fvalue(t2); |
| 471 | case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); |
| 472 | case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); |
| 473 | case LUA_TUSERDATA: { |
| 474 | if (uvalue(t1) == uvalue(t2)) return 1; |
| 475 | else if (L == nullptr) return 0; |
| 476 | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); |
| 477 | if (tm == nullptr) |
| 478 | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); |
| 479 | break; /* will try TM */ |
| 480 | } |
| 481 | case LUA_TTABLE: { |
| 482 | if (hvalue(t1) == hvalue(t2)) return 1; |
| 483 | else if (L == nullptr) return 0; |
| 484 | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); |
| 485 | if (tm == nullptr) |
| 486 | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); |
| 487 | break; /* will try TM */ |
| 488 | } |
| 489 | default: |
| 490 | return gcvalue(t1) == gcvalue(t2); |
| 491 | } |
| 492 | if (tm == nullptr) /* no TM? */ |
| 493 | return 0; /* objects are different */ |
| 494 | luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ |
| 495 | return !l_isfalse(L->top); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ |
no test coverage detected