test/fuzz: fix errors "attempt to compare"
Running of automatically generated Lua programs sometimes failed with an error "attempt to compare" (5%). The error is caused by comparison of non-comparable Lua values: string and boolean, number and function, boolean and number etc. In Lua 5.1 and LuaJIT 2.1 (which follow the semantics and behaviour of Lua 5.1), it is not possible to overload Lua metamethods __le and __lt for these base types. Therefore, an approach with metamethods doesn't help there. See an LuaJIT implementation in `src/lj_meta.c:lj_meta_comp()` and result of comparison values with different types and metamethods `__le` and `__lt`: ``` $ cat compare.lua local mt_table = {} mt_table.__le = function() assert() end mt_table.__lt = function() assert() end local res local str = 'str1' debug.setmetatable(str, mt_table) -- __le res = str <= 10 -- __lt res = str < 10 $ luajit compare.lua luajit: compare.lua:10: attempt to compare string with number stack traceback: compare.lua:10: in main chunk [C]: at 0x55908425f2e0 $ lua5.1 compare.lua lua5.1: compare.lua:10: attempt to compare string with number stack traceback: compare.lua:10: in main chunk [C]: ? $ lua5.2 compare.lua lua5.2: compare.lua:2: assertion failed! stack traceback: [C]: in function 'assert' compare.lua:2: in function '__le' compare.lua:10: in main chunk [C]: in ? $ ``` To fix errors triggered by comparison of non-comparable values introduced a metamethods `__le` and `__lt` and comparison operators are wrapped by a Lua function `only_numbers_cmp` that is implemented in `preamble.lua`. The function perform comparison only when both values have type 'number', otherwise it returns `false`. NO_CHANGELOG=testing NO_DOC=testing NO_TEST=testing
Loading
Please register or sign in to comment