Comparison

util/array.lua @ 10592:9918b4b0cd58

util.array: Fix equality metamethod in Lua 5.3 Lua 5.2 only used the __eq metamethod if both operands have the same __eq, but Lua 5.3 will pick one from either operands that has one as long as both are tables. This results in array() == {} and all sorts of odd behavior, including array() == util.json.null. <MattJ> I think [array() == {}] should have the same semantics as {} == {}
author Kim Alvefur <zash@zash.se>
date Wed, 15 Jan 2020 21:08:01 +0100
parent 9529:6a1e7723208b
child 10895:5777968301e8
comparison
equal deleted inserted replaced
10591:d78c5c9b0cf6 10592:9918b4b0cd58
8 8
9 local t_insert, t_sort, t_remove, t_concat 9 local t_insert, t_sort, t_remove, t_concat
10 = table.insert, table.sort, table.remove, table.concat; 10 = table.insert, table.sort, table.remove, table.concat;
11 11
12 local setmetatable = setmetatable; 12 local setmetatable = setmetatable;
13 local getmetatable = getmetatable;
13 local math_random = math.random; 14 local math_random = math.random;
14 local math_floor = math.floor; 15 local math_floor = math.floor;
15 local pairs, ipairs = pairs, ipairs; 16 local pairs, ipairs = pairs, ipairs;
16 local tostring = tostring; 17 local tostring = tostring;
17 local type = type; 18 local type = type;
38 local res = new_array(); 39 local res = new_array();
39 return res:append(a1):append(a2); 40 return res:append(a1):append(a2);
40 end 41 end
41 42
42 function array_mt.__eq(a, b) 43 function array_mt.__eq(a, b)
44 if getmetatable(a) ~= array_mt or getmetatable(b) ~= array_mt then
45 -- Lua 5.3+ calls this if both operands are tables, even if metatables differ
46 return false;
47 end
43 if #a == #b then 48 if #a == #b then
44 for i = 1, #a do 49 for i = 1, #a do
45 if a[i] ~= b[i] then 50 if a[i] ~= b[i] then
46 return false; 51 return false;
47 end 52 end