github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/object/reflect/bool.go (about)

     1  package reflect
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/hirochachacha/plua/internal/tables"
     7  	"github.com/hirochachacha/plua/object"
     8  	"github.com/hirochachacha/plua/object/fnutil"
     9  )
    10  
    11  func buildBoolMT() {
    12  	mt := tables.NewTableSize(0, 4)
    13  
    14  	mt.Set(object.TM_METATABLE, object.True)
    15  	mt.Set(object.TM_NAME, object.String("BOOL*"))
    16  	mt.Set(object.TM_TOSTRING, tostring(toBool))
    17  	mt.Set(object.TM_INDEX, index(toBool))
    18  
    19  	mt.Set(object.TM_EQ, cmp(func(x, y reflect.Value) bool { return x.Bool() == y.Bool() }, toBool))
    20  
    21  	boolMT = mt
    22  }
    23  
    24  func toBool(ap *fnutil.ArgParser, n int) (reflect.Value, *object.RuntimeError) {
    25  	if b, err := ap.ToGoBool(n); err == nil {
    26  		return reflect.ValueOf(b), nil
    27  	}
    28  	val, err := toValue(ap, n, "BOOL*")
    29  	if err != nil {
    30  		return reflect.Value{}, err
    31  	}
    32  	if val.Kind() != reflect.Bool {
    33  		return reflect.Value{}, ap.TypeError(n, "BOOL*")
    34  	}
    35  	return val, nil
    36  }