From Lua 5.2: Allow mixed metamethods for ordered comparisons.

Needs -DLUAJIT_ENABLE_LUA52COMPAT.
This commit is contained in:
Mike Pall
2012-09-28 19:30:08 +02:00
parent 550ac44e41
commit d08e7bd518
2 changed files with 22 additions and 3 deletions

View File

@@ -386,7 +386,8 @@ TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op)
cTValue *mo = lj_meta_lookup(L, tviscdata(o1) ? o1 : o2, mm);
if (LJ_UNLIKELY(tvisnil(mo))) goto err;
return mmcall(L, cont, mo, o1, o2);
} else if (itype(o1) == itype(o2)) { /* Never called with two numbers. */
} else if (LJ_52 || itype(o1) == itype(o2)) {
/* Never called with two numbers. */
if (tvisstr(o1) && tvisstr(o2)) {
int32_t res = lj_str_cmp(strV(o1), strV(o2));
return (TValue *)(intptr_t)(((op&2) ? res <= 0 : res < 0) ^ (op&1));
@@ -396,8 +397,13 @@ TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op)
ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt;
MMS mm = (op & 2) ? MM_le : MM_lt;
cTValue *mo = lj_meta_lookup(L, o1, mm);
#if LJ_52
if (tvisnil(mo) && tvisnil((mo = lj_meta_lookup(L, o2, mm))))
#else
cTValue *mo2 = lj_meta_lookup(L, o2, mm);
if (tvisnil(mo) || !lj_obj_equal(mo, mo2)) {
if (tvisnil(mo) || !lj_obj_equal(mo, mo2))
#endif
{
if (op & 2) { /* MM_le not found: retry with MM_lt. */
cTValue *ot = o1; o1 = o2; o2 = ot; /* Swap operands. */
op ^= 3; /* Use LT and flip condition. */