Lua - implement userdata iterator

Lua 5.2 I need to iterate over a variable userdata

. As I understand it, I can do this with getmetatable

and __pairs

. Like this:

for k, v in getmetatable(userdataVariable).__pairs do
  -- someting
end

      

But I am trying to call the nil value when I try to.

I found an implementation __pairs

here: what is the actual implementation of lua __pairs?

function meta.__pairs(t)
  return function(t, k)
    local v
    repeat
      k, v = next(t, k)
    until k == nil or theseok(t, k, v)
    return k, v
  end, t, nil
end

      

But I don’t understand what should I do with theseok

? What function should I define here?

+3


source to share


1 answer


I think you are looking for a meta table __index

.



0


source







All Articles