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

     1  package base
     2  
     3  import rt "github.com/arnodel/golua/runtime"
     4  
     5  func assert(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
     6  	if err := c.Check1Arg(); err != nil {
     7  		return nil, err
     8  	}
     9  	arg := c.Arg(0)
    10  	etc := c.Etc()
    11  	if !rt.Truth(arg) {
    12  		var msg rt.Value
    13  		if len(etc) == 0 {
    14  			msg = rt.StringValue("assertion failed!")
    15  		} else {
    16  			msg = etc[0]
    17  		}
    18  		err := rt.NewError(msg).AddContext(c.Next(), 1)
    19  		return nil, err
    20  	}
    21  	next := c.Next()
    22  	t.Push1(next, arg)
    23  	t.Push(next, etc...)
    24  	return next, nil
    25  }