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

     1  package stringlib
     2  
     3  import (
     4  	"bytes"
     5  
     6  	rt "github.com/arnodel/golua/runtime"
     7  )
     8  
     9  func dump(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
    10  	if err := c.Check1Arg(); err != nil {
    11  		return nil, err
    12  	}
    13  	cl, err := c.ClosureArg(0)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	strip := false
    18  	if c.NArgs() >= 2 {
    19  		strip = rt.Truth(c.Arg(1))
    20  	}
    21  	// TODO: support strip
    22  	_ = strip
    23  	var w bytes.Buffer
    24  	code := t.RefactorCodeConsts(cl.Code)
    25  	used, mErr := rt.MarshalConst(&w, rt.CodeValue(code), t.LinearUnused(10))
    26  	// This will cause a panic if MarshalConst was interupted, so no need to
    27  	// worry about the rest of this codepath in this case.
    28  	t.LinearRequire(10, used)
    29  	if err != nil {
    30  		return nil, mErr
    31  	}
    32  	return c.PushingNext1(t.Runtime, rt.StringValue(w.String())), nil
    33  }