github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/base/ipairs.go (about) 1 package base 2 3 import rt "github.com/arnodel/golua/runtime" 4 5 func ipairsIteratorF(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { 6 if err := c.CheckNArgs(2); err != nil { 7 return nil, err 8 } 9 coll := c.Arg(0) 10 n, err := c.IntArg(1) 11 if err != nil { 12 return nil, err 13 } 14 next := c.Next() 15 n++ 16 nv := rt.IntValue(n) 17 v, err := rt.Index(t, coll, nv) 18 if err != nil { 19 return nil, err 20 } 21 if !v.IsNil() { 22 t.Push1(next, nv) 23 t.Push1(next, v) 24 } 25 return next, nil 26 } 27 28 var ipairsIterator = rt.NewGoFunction(ipairsIteratorF, "ipairsiterator", 2, false) 29 30 func ipairs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { 31 if err := c.Check1Arg(); err != nil { 32 return nil, err 33 } 34 next := c.Next() 35 t.Push1(next, rt.FunctionValue(ipairsIterator)) 36 t.Push1(next, c.Arg(0)) 37 t.Push1(next, rt.IntValue(0)) 38 return next, nil 39 }