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

     1  package base
     2  
     3  import (
     4  	"errors"
     5  
     6  	rt "github.com/arnodel/golua/runtime"
     7  )
     8  
     9  func print(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
    10  	err := Print(t, c.Etc())
    11  	if err != nil {
    12  		return nil, err
    13  	}
    14  	return c.Next(), nil
    15  }
    16  
    17  func Print(t *rt.Thread, args []rt.Value) error {
    18  	tostring := t.GlobalEnv().Get(rt.StringValue("tostring"))
    19  	for i, v := range args {
    20  		if i > 0 {
    21  			t.Stdout.Write([]byte{'\t'})
    22  		}
    23  		vs, err := rt.Call1(t, tostring, v)
    24  		if err != nil {
    25  			return err
    26  		}
    27  		if s, ok := vs.TryString(); ok {
    28  			t.Stdout.Write([]byte(s))
    29  		} else {
    30  			return errors.New("tostring must return a string")
    31  		}
    32  	}
    33  	t.Stdout.Write([]byte{'\n'})
    34  	return nil
    35  }