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

     1  package base
     2  
     3  import (
     4  	"errors"
     5  
     6  	rt "github.com/arnodel/golua/runtime"
     7  )
     8  
     9  func rawlen(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
    10  	if err := c.Check1Arg(); err != nil {
    11  		return nil, err
    12  	}
    13  	next := c.Next()
    14  	switch x := c.Arg(0); x.Type() {
    15  	case rt.StringType:
    16  		t.Push1(next, rt.IntValue(int64(len(x.AsString()))))
    17  		return next, nil
    18  	case rt.TableType:
    19  		t.Push1(next, rt.IntValue(x.AsTable().Len()))
    20  		return next, nil
    21  	}
    22  	return nil, errors.New("#1 must be a string or table")
    23  }