lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I think there is either an error in the manual or a bug in the implementation regarding the next function.
The manual state for the lua function next:
"The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may set existing fields to nil."

However, in the following example during the traversal all elements are set to nil. This is allowed by the manual but it throws an error if the collectgarbage call isn't commented out.

function LoopAndEmpty(t)
    for k, v in pairs(t) do
        t[k] = nil
        coroutine.yield(k, v)

local c = coroutine.create(LoopAndEmpty)
local t = {}
t["no" .. "ref1"] = 1
t["no" .. "ref2"] = 2
print(coroutine.resume(c, t))

The problem seems to be that the key "noref1" or "noref2" is white when the table t is travered and then in traversestrongtable in the key is cleared. This should not be the case since it could be marked later.