lua-users home
lua-l archive

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


The following function, when invoked, causes a C stack overflow
in Lua 5.3.

local subsystem_mt = {__index =
   function(spec,auction)
      for key,bid in ipairs(spec) do
         print("Checking '"..auction.."' against '"..key.."'")
      end
   end}

It works under 5.1 and 5.2. It also works if "ipairs" is replaced
by "pairs", which is what the application code does now.

Having scrutinized the C source of "ipairs", I know why this
happens: ipairs just goes on until an item is not found, and
that triggers a call to the metamethod, bang.

After that exercise, I can now do what I could not do at first
reading: deduce from the deceptively simple specification
of 'ipairs' that a metamethod for __index should not invoke
'ipairs' on the same table.

Should the manual contain an explicit warning for this gotcha?
I usually say "no" to such suggestions, but having been caught
out this time ...