value equality check does not work as described

Issue #39 resolved
mezzodrinker created an issue

In the MobTalkerScript source code (namely the documentation of net.mobtalker.mobtalkerscript.v3.value.MtsValue.isEqual(MtsValue)), the description of the == (equals) operator states that two values are only equal if...

  • ...their equals(Object) method returns true or...
  • ...both objects' __eq meta methods are the same AND return true.

Currently, the check if the __eq meta methods are the same is redundant because it always yields false (as in, they always equal):

MtsValue tagA = getMetaTag( __eq );
if ( tagA.isNil() || !tagA.equals( getMetaTag( __eq ) ) )
    return false;

The error is located in line 402. It should be !tagA.equals( b.getMetaTag( __eq ) ) instead of !tagA.equals( getMetaTag( __eq ) ).

Comments (8)

  1. mezzodrinker reporter

    Tested with the following script:

    MyClass = {}
    function MyClass:new()
      local newInstance = {}
      setmetatable(newInstance, {
        __index = self,
        __eq = function(a, b)
          return true
        end
      })
      return newInstance
    end
    
    MyOtherClass = {}
    function MyOtherClass:new()
      local newInstance = {}
      setmetatable(newInstance, {
        __index = self,
        __eq = function(a, b)
          return true
        end
      })
      return newInstance
    end
    
    instA = MyClass.new()
    instB = MyOtherClass.new()
    
    print(instA == instB)
    

    This script will output true with the current implementation, but output false with line 402 changed.

  2. mezzodrinker reporter
    • changed status to open

    Reopening because the behaviour of the value equality check is not equal to how Lua handles it.

    Code

    C1 = {}
    function C1:new()
      local inst = {}
      setmetatable(inst, {
        class = 'C1',
        __index = self,
        __eq = function (a,b)
          print('Hello from C1 __eq function')
          return true
        end
      })
      return inst
    end
    
    C2 = {}
    function C2:new()
      local inst = {}
      setmetatable(inst, {
        class = 'C2',
        __index = self,
        __eq = function (a,b)
          print('Hello from C2 __eq function')
          return false
        end
      })
      return inst
    end
    
    equals = function (a,b)
      print('Hello from equals function')
      return true
    end
    
    C3 = {}
    function C3:new()
      local inst = {}
      setmetatable(inst, {
        class = 'C3',
        __index = self,
        __eq = equals
      })
      return inst
    end
    
    C4 = {}
    function C4:new()
      local inst = {}
      setmetatable(inst, {
        class = 'C4',
        __index = self,
        __eq = equals
      })
      return inst
    end
    
    -- Tests
    inst1 = C1.new()
    inst2 = C2.new()
    inst3 = C3.new()
    inst4 = C4.new()
    
    print('inst1 == inst1? (expected true) ', inst1 == inst1)
    print('inst2 == inst2? (expected true) ', inst2 == inst2)
    print('inst3 == inst3? (expected true) ', inst3 == inst3)
    print('inst4 == inst4? (expected true) ', inst4 == inst4)
    print('inst1 == inst2? (expected true) ', inst1 == inst2)
    print('inst2 == inst1? (expected false)', inst2 == inst1)
    print('inst1 == inst3? (expected true) ', inst1 == inst3)
    print('inst3 == inst1? (expected true) ', inst3 == inst1)
    print('inst2 == inst3? (expected false)', inst2 == inst3)
    print('inst3 == inst2? (expected true) ', inst3 == inst2)
    print('inst3 == inst4? (expected true) ', inst3 == inst4)
    print('inst4 == inst3? (expected true) ', inst4 == inst3)
    

    Lua output

    inst1 == inst1? (expected true)     true
    inst2 == inst2? (expected true)     true
    inst3 == inst3? (expected true)     true
    inst4 == inst4? (expected true)     true
    Hello from C1 __eq function
    inst1 == inst2? (expected true)     true
    Hello from C2 __eq function
    inst2 == inst1? (expected false)    false
    Hello from C1 __eq function
    inst1 == inst3? (expected true)     true
    Hello from equals function
    inst3 == inst1? (expected true)     true
    Hello from C2 __eq function
    inst2 == inst3? (expected false)    false
    Hello from equals function
    inst3 == inst2? (expected true)     true
    Hello from equals function
    inst3 == inst4? (expected true)     true
    Hello from equals function
    inst4 == inst3? (expected true)     true
    

    MTS 3.0.4 output

    inst1 == inst1? (expected true) true
    inst2 == inst2? (expected true) true
    inst3 == inst3? (expected true) true
    inst4 == inst4? (expected true) true
    inst1 == inst2? (expected true) false
    inst2 == inst1? (expected false)false
    inst1 == inst3? (expected true) false
    inst3 == inst1? (expected true) false
    inst2 == inst3? (expected false)false
    inst3 == inst2? (expected true) false
    Hello from equals function
    inst3 == inst4? (expected true) true
    Hello from equals function
    inst4 == inst3? (expected true) true
    

    MTS 3.0.6 output

    inst1 == inst1? (expected true) true
    inst2 == inst2? (expected true) true
    inst3 == inst3? (expected true) true
    inst4 == inst4? (expected true) true
    inst1 == inst2? (expected true) false
    inst2 == inst1? (expected false)false
    inst1 == inst3? (expected true) false
    inst3 == inst1? (expected true) false
    inst2 == inst3? (expected false)false
    inst3 == inst2? (expected true) false
    Hello from equals function
    inst3 == inst4? (expected true) true
    Hello from equals function
    inst4 == inst3? (expected true) true
    
  3. Log in to comment