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

     1  package base
     2  
     3  import (
     4  	"errors"
     5  
     6  	rt "github.com/arnodel/golua/runtime"
     7  )
     8  
     9  func selectF(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
    10  	if err := c.Check1Arg(); err != nil {
    11  		return nil, err
    12  	}
    13  	n, err := c.IntArg(0)
    14  	if err != nil {
    15  		var s string
    16  		s, err = c.StringArg(0)
    17  		if err != nil || s != "#" {
    18  			return nil, errors.New("#1 must be an integer or '#'")
    19  		}
    20  		return c.PushingNext1(t.Runtime, rt.IntValue(int64(len(c.Etc())))), nil
    21  	}
    22  	etc := c.Etc()
    23  	if n < 0 {
    24  		n += int64(len(etc)) + 1
    25  	}
    26  	if n < 1 {
    27  		return nil, errors.New("#1 out of range")
    28  	}
    29  	next := c.Next()
    30  	if int(n) <= len(etc) {
    31  		t.Push(next, etc[n-1:]...)
    32  	}
    33  	return next, nil
    34  }