** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) */
| 405 | ** L == NULL means raw equality (no metamethods) |
| 406 | */ |
| 407 | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { |
| 408 | const TValue *tm; |
| 409 | if (ttype(t1) != ttype(t2)) { /* not the same variant? */ |
| 410 | if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) |
| 411 | return 0; /* only numbers can be equal with different variants */ |
| 412 | else { /* two numbers with different variants */ |
| 413 | lua_Integer i1, i2; /* compare them as integers */ |
| 414 | return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); |
| 415 | } |
| 416 | } |
| 417 | /* values have same type and same variant */ |
| 418 | switch (ttype(t1)) { |
| 419 | case LUA_TNIL: return 1; |
| 420 | case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); |
| 421 | case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); |
| 422 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ |
| 423 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
| 424 | case LUA_TLCF: return fvalue(t1) == fvalue(t2); |
| 425 | case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); |
| 426 | case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); |
| 427 | case LUA_TUSERDATA: { |
| 428 | if (uvalue(t1) == uvalue(t2)) return 1; |
| 429 | else if (L == NULL) return 0; |
| 430 | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); |
| 431 | if (tm == NULL) |
| 432 | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); |
| 433 | break; /* will try TM */ |
| 434 | } |
| 435 | case LUA_TTABLE: { |
| 436 | if (hvalue(t1) == hvalue(t2)) return 1; |
| 437 | else if (L == NULL) return 0; |
| 438 | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); |
| 439 | if (tm == NULL) |
| 440 | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); |
| 441 | break; /* will try TM */ |
| 442 | } |
| 443 | default: |
| 444 | return gcvalue(t1) == gcvalue(t2); |
| 445 | } |
| 446 | if (tm == NULL) /* no TM? */ |
| 447 | return 0; /* objects are different */ |
| 448 | luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ |
| 449 | return !l_isfalse(L->top); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ |
no test coverage detected