123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package vfunc
- import (
- "reflect"
- "unsafe"
- )
- // UCastFunc create function caller by unsafe pointer
- func UCastFunc(fn interface{}) func(...interface{}) interface{} {
- f := fn
- fRefl := reflect.ValueOf(f)
- return *(*func(...interface{}) interface{})(unsafe.Pointer(fRefl.Pointer()))
- }
- // NCastFunc creates a function caller by Reflection
- func NCastFunc(fn interface{}) func(...interface{}) interface{} {
- fnRefl := reflect.ValueOf(fn)
- ret := func(args ...interface{}) interface{} {
- nargs := []reflect.Value{}
- for _, a := range args {
- nargs = append(nargs, reflect.ValueOf(a))
- }
- return fnRefl.Call(nargs)[0].Interface()
- }
- return ret
- }
- //CastFunc creates a function caller by MakeFunc
- func RCastFunc(fn interface{}) func(...interface{}) interface{} {
- var dummy func(...interface{}) interface{}
- fnRefl := reflect.ValueOf(fn)
- myFunci := reflect.MakeFunc(reflect.TypeOf(dummy), func(args []reflect.Value) (results []reflect.Value) {
- // Expect variadic so
- var nargs []reflect.Value
- for _, v := range args[0].Interface().([]interface{}) {
- nargs = append(nargs, reflect.ValueOf(v))
- }
- ret := fnRefl.Call(nargs)[0].Interface()
- return []reflect.Value{reflect.ValueOf(&ret).Elem()}
- // Convert the fucker"
- }).Interface().(func(...interface{}) interface{})
- return myFunci
- }
|