cc-tweaked / Cobalt

A re-entrant fork of LuaJ
Other
72 stars 13 forks source link

`table.sort` does not inspect the hash part #58

Closed Wojbie closed 1 year ago

Wojbie commented 3 years ago

Found this issue when i was messing with table.sort.

print("first")
local test = {"d","a","b","c"}
table.sort(test, function(a, b) return a < b end)
for k, v in ipairs(test) do
  print(k, v)
end
print("second")
local test = {[1]="d",[2]="a",[3]="b",[4]="c"}
table.sort(test, function(a, b) return a < b end)
for k, v in ipairs(test) do
  print(k, v)
end

This code generates this output in Cobalt. As one can see second table is not getting sorted... obraz While in other lua instances it creates:

first
1    a
2    b
3    c
4    d
second
1    a
2    b
3    c
4    d

This seems to imply there is something wrong with table related logic in Cobalt.

SquidDev commented 3 years ago

Well, this is embarrassing. It only uses the array part. I suspect the fix here is to shift everything to use rawget/rawset instead of indexing array directly.

https://github.com/SquidDev/Cobalt/blob/859205c40f1366dcf1bfff71ef7cd38038bcb221/src/main/java/org/squiddev/cobalt/LuaTable.java#L645-L675

SquidDev commented 3 years ago

Technically we should actually be supporting __index/__newindex here. Ughrhrhrrh.