github.com/goplus/xtypes@v0.2.1/internal/reflect/xcall.go (about)

     1  package reflect
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  func fromValue(v reflect.Value) Value {
     9  	return *(*Value)(unsafe.Pointer(&v))
    10  }
    11  
    12  func toValue(v Value) reflect.Value {
    13  	return *(*reflect.Value)(unsafe.Pointer(&v))
    14  }
    15  
    16  func MethodByName(v reflect.Value, name string) Value {
    17  	return fromValue(v).MethodByName(name)
    18  }
    19  
    20  func Call(v Value, args []reflect.Value) []reflect.Value {
    21  	var res []Value
    22  	n := len(args)
    23  	if n == 0 {
    24  		res = v.Call(nil)
    25  	} else {
    26  		a := make([]Value, n, n)
    27  		for i := 0; i < n; i++ {
    28  			a[i] = fromValue(args[i])
    29  		}
    30  		res = v.Call(a)
    31  	}
    32  	nout := len(res)
    33  	if nout == 0 {
    34  		return nil
    35  	}
    36  	r := make([]reflect.Value, nout, nout)
    37  	for i := 0; i < nout; i++ {
    38  		r[i] = toValue(res[i])
    39  	}
    40  	return r
    41  }