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

     1  package base
     2  
     3  import (
     4  	rt "github.com/arnodel/golua/runtime"
     5  )
     6  
     7  func loadfile(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
     8  	chunk, chunkName, err := loadChunk(t, c.Args())
     9  	defer t.ReleaseBytes(len(chunk))
    10  	if err != nil {
    11  		return t.ProcessIoError(c.Next(), err)
    12  	}
    13  	var (
    14  		next      = c.Next()
    15  		chunkMode = "bt"
    16  		chunkEnv  = rt.TableValue(t.GlobalEnv())
    17  	)
    18  	switch nargs := c.NArgs(); {
    19  	case nargs >= 3:
    20  		chunkEnv = c.Arg(2)
    21  		fallthrough
    22  	case nargs >= 2:
    23  		mode, err := c.StringArg(1)
    24  		if err != nil {
    25  			return nil, err
    26  		}
    27  		chunkMode = string(mode)
    28  	}
    29  	clos, err := t.LoadFromSourceOrCode(chunkName, chunk, chunkMode, chunkEnv, true)
    30  	if err != nil {
    31  		t.Push(next, rt.NilValue, rt.StringValue(err.Error()))
    32  	} else {
    33  		t.Push1(next, rt.FunctionValue(clos))
    34  	}
    35  	return next, nil
    36  }