github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/base/lua/pairs.lua (about)

     1  function _pairs(t)
     2      return next, t, nil
     3  end
     4  
     5  local t = {x=1, y=2, z="hello"}
     6  local u = {}
     7  for k, v in _pairs(t) do
     8      u[k] = v
     9  end
    10  print(u.x, u.y, u.z)
    11  --> =1	2	hello
    12  
    13  u = {}
    14  for k, v in pairs(t) do
    15      u[k] = v .. "!"
    16  end
    17  print(u.x, u.y, u.z)
    18  --> =1!	2!	hello!
    19  
    20  local tt = {}
    21  setmetatable(tt, {__pairs=function(x) return "what" end})
    22  print(pairs(tt))
    23  --> =what
    24  
    25  print(pcall(pairs))
    26  --> ~false\t.*value needed
    27  
    28  setmetatable(tt, {__pairs=function(x) error("nono") end})
    29  print(pcall(pairs, tt))
    30  --> ~false\t.* nono