2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
2012-10-23 06:31:11 +02:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// MakeFunc implementation.
|
|
|
|
|
|
|
|
package reflect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// makeFuncImpl is the closure value implementing the function
|
|
|
|
// returned by MakeFunc.
|
2017-01-14 01:05:42 +01:00
|
|
|
// The first three words are layed out like ffi_go_closure.
|
2013-07-16 08:54:42 +02:00
|
|
|
type makeFuncImpl struct {
|
2015-01-16 23:58:53 +01:00
|
|
|
code uintptr
|
|
|
|
ffi_cif unsafe.Pointer
|
|
|
|
ffi_fun func(unsafe.Pointer, unsafe.Pointer)
|
|
|
|
|
|
|
|
typ *funcType
|
|
|
|
fn func([]Value) []Value
|
2013-12-12 02:08:52 +01:00
|
|
|
|
|
|
|
// For gccgo we use the same entry point for functions and for
|
|
|
|
// method values.
|
|
|
|
method int
|
|
|
|
rcvr Value
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// MakeFunc returns a new function of the given Type
|
|
|
|
// that wraps the function fn. When called, that new function
|
|
|
|
// does the following:
|
|
|
|
//
|
2013-11-06 20:49:01 +01:00
|
|
|
// - converts its arguments to a slice of Values.
|
2012-10-23 06:31:11 +02:00
|
|
|
// - runs results := fn(args).
|
|
|
|
// - returns the results as a slice of Values, one per formal result.
|
|
|
|
//
|
|
|
|
// The implementation fn can assume that the argument Value slice
|
|
|
|
// has the number and type of arguments given by typ.
|
|
|
|
// If typ describes a variadic function, the final Value is itself
|
|
|
|
// a slice representing the variadic arguments, as in the
|
|
|
|
// body of a variadic function. The result Value slice returned by fn
|
|
|
|
// must have the number and type of results given by typ.
|
|
|
|
//
|
|
|
|
// The Value.Call method allows the caller to invoke a typed function
|
|
|
|
// in terms of Values; in contrast, MakeFunc allows the caller to implement
|
|
|
|
// a typed function in terms of Values.
|
|
|
|
//
|
|
|
|
// The Examples section of the documentation includes an illustration
|
|
|
|
// of how to use MakeFunc to build a swap function for different types.
|
|
|
|
//
|
|
|
|
func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
|
|
|
|
if typ.Kind() != Func {
|
|
|
|
panic("reflect: call of MakeFunc with non-Func type")
|
|
|
|
}
|
|
|
|
|
2014-07-19 23:36:26 +02:00
|
|
|
t := typ.common()
|
|
|
|
ftyp := (*funcType)(unsafe.Pointer(t))
|
|
|
|
|
|
|
|
impl := &makeFuncImpl{
|
|
|
|
typ: ftyp,
|
|
|
|
fn: fn,
|
|
|
|
method: -1,
|
|
|
|
}
|
2013-09-27 19:53:46 +02:00
|
|
|
|
2016-11-18 01:15:38 +01:00
|
|
|
makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(impl))
|
2015-01-16 23:58:53 +01:00
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
return Value{t, unsafe.Pointer(&impl), flag(Func) | flagIndir}
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// makeMethodValue converts v from the rcvr+method index representation
|
|
|
|
// of a method value to an actual method func value, which is
|
|
|
|
// basically the receiver value with a special bit set, into a true
|
|
|
|
// func value - a value holding an actual func. The output is
|
|
|
|
// semantically equivalent to the input as far as the user of package
|
|
|
|
// reflect can tell, but the true func representation can be handled
|
|
|
|
// by code like Convert and Interface and Assign.
|
|
|
|
func makeMethodValue(op string, v Value) Value {
|
|
|
|
if v.flag&flagMethod == 0 {
|
2014-06-05 01:15:33 +02:00
|
|
|
panic("reflect: internal error: invalid use of makeMethodValue")
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Ignoring the flagMethod bit, v describes the receiver, not the method type.
|
|
|
|
fl := v.flag & (flagRO | flagAddr | flagIndir)
|
2015-01-15 01:27:56 +01:00
|
|
|
fl |= flag(v.typ.Kind())
|
|
|
|
rcvr := Value{v.typ, v.ptr, fl}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2013-12-12 02:08:52 +01:00
|
|
|
// v.Type returns the actual type of the method value.
|
|
|
|
ft := v.Type().(*rtype)
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// Cause panic if method is not appropriate.
|
|
|
|
// The panic would still happen during the call if we omit this,
|
|
|
|
// but we want Interface() and other operations to fail early.
|
2014-07-19 10:53:52 +02:00
|
|
|
_, t, _ := methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift)
|
2013-12-12 02:08:52 +01:00
|
|
|
|
2014-07-19 23:36:26 +02:00
|
|
|
ftyp := (*funcType)(unsafe.Pointer(t))
|
|
|
|
method := int(v.flag) >> flagMethodShift
|
|
|
|
|
2014-10-03 17:51:38 +02:00
|
|
|
fv := &makeFuncImpl{
|
|
|
|
typ: ftyp,
|
|
|
|
method: method,
|
|
|
|
rcvr: rcvr,
|
|
|
|
}
|
|
|
|
|
2016-11-18 01:15:38 +01:00
|
|
|
makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(fv))
|
2013-12-12 02:08:52 +01:00
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
return Value{ft, unsafe.Pointer(&fv), v.flag&flagRO | flag(Func) | flagIndir}
|
2013-12-12 02:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// makeValueMethod takes a method function and returns a function that
|
|
|
|
// takes a value receiver and calls the real method with a pointer to
|
|
|
|
// it.
|
|
|
|
func makeValueMethod(v Value) Value {
|
|
|
|
typ := v.typ
|
|
|
|
if typ.Kind() != Func {
|
|
|
|
panic("reflect: call of makeValueMethod with non-Func type")
|
|
|
|
}
|
|
|
|
if v.flag&flagMethodFn == 0 {
|
|
|
|
panic("reflect: call of makeValueMethod with non-MethodFn")
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2014-07-19 23:36:26 +02:00
|
|
|
t := typ.common()
|
|
|
|
ftyp := (*funcType)(unsafe.Pointer(t))
|
|
|
|
|
2014-10-03 17:51:38 +02:00
|
|
|
impl := &makeFuncImpl{
|
|
|
|
typ: ftyp,
|
|
|
|
method: -2,
|
|
|
|
rcvr: v,
|
|
|
|
}
|
|
|
|
|
2016-11-18 01:15:38 +01:00
|
|
|
makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(impl))
|
2013-12-12 02:08:52 +01:00
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
return Value{t, unsafe.Pointer(&impl), v.flag&flagRO | flag(Func) | flagIndir}
|
2013-12-12 02:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the function represented by a makeFuncImpl.
|
|
|
|
func (c *makeFuncImpl) call(in []Value) []Value {
|
|
|
|
if c.method == -1 {
|
|
|
|
return c.fn(in)
|
|
|
|
} else if c.method == -2 {
|
2014-10-03 17:51:38 +02:00
|
|
|
if c.typ.IsVariadic() {
|
|
|
|
return c.rcvr.CallSlice(in)
|
|
|
|
} else {
|
|
|
|
return c.rcvr.Call(in)
|
|
|
|
}
|
2013-12-12 02:08:52 +01:00
|
|
|
} else {
|
|
|
|
m := c.rcvr.Method(c.method)
|
2014-10-03 17:51:38 +02:00
|
|
|
if c.typ.IsVariadic() {
|
|
|
|
return m.CallSlice(in)
|
|
|
|
} else {
|
|
|
|
return m.Call(in)
|
|
|
|
}
|
2013-12-12 02:08:52 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|