lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I found the answer in the source code for lua:
function int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) in lvm.c
only tries to find the metatable __eq function if the data is FULL userdata. If it is lightuserdata, only the pvalues are compared.

I felt the manual was rather unclear on this.

2011/3/10 Oliver <ogtifs@gmail.com>
Hi all,

I am trying to make Lua a == b call a C function. I have provided a metatable from C following the example described here:
http://lua-users.org/wiki/SimplerCppBinding

To debug I have tried to follow the logic steps described in the manual
http://www.lua.org/manual/5.1/manual.html#2.8

Yet the following script (invoked from lua_pcall after pushing instances onto the stack and setting their metatables):

function testfunc(a, b)
print("Type:")
print(type(a))
print("Metatables equal?")
print(type(a) == type(b))
print(getmetatable(a) == getmetatable(b))
print(getmetatable(a)["__eq"] == getmetatable(b)["__eq"])
eqfunc = getmetatable(a)["__eq"]
print("Calling __eq directly")
print(eqfunc(a, b))
print("Calling via ==")
print(a == b)
end

produces

Type:
userdata
Metatables equal?
true
true
true
Calling __eq directly
*** INSIDE C FUNCTION ***
false
Calling via ==
false

i.e. the == operator does not call the C function

Could anyone please shed some light on this issue?

Thanks
Oliver