Boxed integers cannot be compared to numbers
Repro code:
print(ffi.new('uintptr_t', 20) == 20)
It will print true with LuaJIT's ffi, and print false with this ffi.
Actually I'm not entirely sure if LuaJIT's behavior is strictly better since this returns true in LuaJIT:
print(0xfffffffffffffffffffffffffffffffffffffffff == 0xffffffffffffffffULL)
Checked the docs and yea there's a discrepancy here:
https://github.com/q66/cffi-lua/blob/master/docs/semantics.md?plain=1#L396-L398
- 64-bit integer comparison: two
cdatanumber or acdatanumber and a Lua number can be compared. The same conversions as in arithmetic are performed first, same withenums.
So unless I am horribly mistaken, either the docs need to be changed or the behavior needs to be changed
Checked the docs and yea there's a discrepancy here:
https://github.com/q66/cffi-lua/blob/master/docs/semantics.md?plain=1#L396-L398
- 64-bit integer comparison: two
cdatanumber or acdatanumber and a Lua number can be compared. The same conversions as in arithmetic are performed first, same withenums.So unless I am horribly mistaken, either the docs need to be changed or the behavior needs to be changed
readme.md
Equality comparisons between cdata and Lua values are always false
the lua metamethod semantics don't allow this to work, quoting reference manual:
"eq": the == operation. The function getcomphandler defines how Lua chooses a metamethod for comparison operators. A metamethod only is selected when both objects being compared have the same type and the same metamethod for the selected operation.
the cffi doc states that immediately after:
- Equality comparisons never raise an error, but a notable difference from LuaJIT is that metamethods are only ever triggered on compatible Lua types, which means comparisons of
cdataandnil(or other Lua values) will always befalseno matter what. Incompatiblecdatapointers can always be tested for address equality.
"comparison" in the above doc refers to operators like </>, which do work
Thanks. That is unfortunate. I guess in order to get boxedInt == luaNumber to behave like one would expect, you would have to do something like boxedInt >= luaNumber and boxedInt <= luaNumber. Very strange.
Edit: Maybe change the docs to say something like "64-bit integer >, >=, <, <= comparisons" instead of "64-bit integer comparison"?
you can convert the lua number to a boxed number and then do equality comparison
I suppose that works too. I forgot why I didn't go with that approach.... it's been a while.