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

     1  package base
     2  
     3  import (
     4  	"errors"
     5  
     6  	rt "github.com/arnodel/golua/runtime"
     7  )
     8  
     9  func setmetatable(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
    10  	if err := c.CheckNArgs(2); err != nil {
    11  		return nil, err
    12  	}
    13  	tbl, err := c.TableArg(0)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	if !rt.RawGet(tbl.Metatable(), rt.StringValue("__metatable")).IsNil() {
    18  		return nil, errors.New("cannot set metatable")
    19  	}
    20  	if c.Arg(1).IsNil() {
    21  		tbl.SetMetatable(nil)
    22  	} else if meta, err := c.TableArg(1); err == nil {
    23  		t.SetRawMetatable(c.Arg(0), meta)
    24  	} else {
    25  		return nil, err
    26  	}
    27  	return c.PushingNext1(t.Runtime, c.Arg(0)), nil
    28  }