compat: __iter walks 1..#t skipping holes (sparse integer maps / arrays with holes visited fully on LuaJIT, as in Luau)
This commit is contained in:
1 parent
7c59c12348
commit
87811057f6
2 files changed
+37
-1
No files matched your search
@@ -299,4 +299,25 @@ do -- float reads: NaNs with a payload come back as the canonical NaN (LuaJIT: n
|
||||
t:eq(buffer.readf64(b, 8), -2.25, "f64 normal")
|
||||
end
|
||||
|
||||
do -- generalized iteration visits every key of sparse integer maps and arrays with holes (LuaJIT array-part borders)
|
||||
local function collect(t)
|
||||
local ks = {}
|
||||
for k, v in t do
|
||||
table.insert(ks, tostring(k) .. "=" .. tostring(v))
|
||||
end
|
||||
table.sort(ks)
|
||||
return table.concat(ks, ",")
|
||||
end
|
||||
local sparse = {}
|
||||
sparse[6] = "a"
|
||||
sparse[7] = "b"
|
||||
t:eq(collect(sparse), "6=a,7=b", "sparse integer map")
|
||||
local holes = { 1, 2, 3, 4 }
|
||||
holes[2] = nil
|
||||
t:eq(collect(holes), "1=1,3=3,4=4", "array with a hole")
|
||||
t:eq(collect({ 10, 20, 30 }), "1=10,2=20,3=30", "dense array")
|
||||
t:eq(collect({ [0] = "z", "a" }), "0=z,1=a", "index 0")
|
||||
t:eq(collect({ "a", x = 1 }), "1=a,x=1", "mixed")
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user