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

     1  package base
     2  
     3  import (
     4  	"errors"
     5  
     6  	rt "github.com/arnodel/golua/runtime"
     7  )
     8  
     9  func next(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
    10  	if err := c.Check1Arg(); err != nil {
    11  		return nil, err
    12  	}
    13  	var k rt.Value
    14  	if c.NArgs() >= 2 {
    15  		k = c.Arg(1)
    16  	}
    17  	tbl, err := c.TableArg(0)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	next := c.Next()
    22  	nk, nv, ok := tbl.Next(k)
    23  	if !ok {
    24  		return nil, errors.New("invalid key for 'next'")
    25  	}
    26  	t.Push1(next, nk)
    27  	if !nk.IsNil() {
    28  		t.Push1(next, nv)
    29  	}
    30  	return next, nil
    31  }
    32  
    33  var nextGoFunc = rt.NewGoFunction(next, "next", 2, false)