Cobalt icon indicating copy to clipboard operation
Cobalt copied to clipboard

Small issue with tables created in Cobalt.

Open Wojbie opened this issue 2 years ago • 2 comments

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.

Wojbie avatar Sep 01 '21 00:09 Wojbie

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 avatar Sep 01 '21 08:09 SquidDev

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

SquidDev avatar Sep 01 '21 08:09 SquidDev