Access to pointers inside __eq meta method?

时间:2018-02-03 09:18:15

标签: lua meta-method

I have Lua objects which share a metatable which has a __eq metamethod. Inside this meta method, I want to check if the two objects are the same object before even comparing them. Similar to how in java you would do a == b || a.compareTo(b). The issue though is by doing == inside of __eq, it calls __eq and thus Stack Overflow. How can I achieve this?

local t1 = { x = 3 }
local t2 = { x = 3 }
local t3 = t1
print(t1 == t3) -- true, they pointer to same memory
local mt = {
    __eq = function(lhs, rhs)
        if lhs == rhs then return true end -- causes stack overflow
        return lhs.x == rhs.x
    end
}
setmetatable(t1, mt)
setmetatable(t2, mt)

-- stack overflow below
print(t1 == t2) -- this should compare 'x' variables
print(t1 == t3) -- this shouldn't need to do so, same memory location

2 个答案:

答案 0 :(得分:2)

Check rawequal() function. It will compare instances without calling metamethods.

答案 1 :(得分:2)

There is no need to test for equality inside the __eq metamethod because Lua only calls the __eq metamethod if the userdata pointers are different.

Add print(t1 == t3) right after setting the metatables to confirm this.

The manual says (emphasis added):

__eq: the equal (==) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal.