2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
2012-09-24 18:06:32 -06: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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// makeFuncImpl is the closure value implementing the function
|
|
|
|
// returned by MakeFunc.
|
2016-12-16 09:57:25 -07:00
|
|
|
// The first two words of this type must be kept in sync with
|
|
|
|
// methodValue and runtime.reflectMethodValue.
|
|
|
|
// Any changes should be reflected in all three.
|
2012-09-24 18:06:32 -06:00
|
|
|
type makeFuncImpl struct {
|
2014-09-12 05:29:19 -06:00
|
|
|
code uintptr
|
2016-12-16 09:57:25 -07:00
|
|
|
stack *bitVector
|
2014-09-12 05:29:19 -06:00
|
|
|
typ *funcType
|
|
|
|
fn func([]Value) []Value
|
2012-09-24 18:06:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeFunc returns a new function of the given Type
|
|
|
|
// that wraps the function fn. When called, that new function
|
|
|
|
// does the following:
|
|
|
|
//
|
2013-10-04 14:12:50 -06:00
|
|
|
// - converts its arguments to a slice of Values.
|
2012-09-24 18:06:32 -06: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")
|
|
|
|
}
|
|
|
|
|
|
|
|
t := typ.common()
|
2013-02-22 13:23:57 -07:00
|
|
|
ftyp := (*funcType)(unsafe.Pointer(t))
|
2012-09-24 18:06:32 -06:00
|
|
|
|
2013-03-21 14:59:16 -06:00
|
|
|
// Indirect Go func value (dummy) to obtain
|
|
|
|
// actual code address. (A Go func value is a pointer
|
2015-07-10 17:17:11 -06:00
|
|
|
// to a C function pointer. https://golang.org/s/go11func.)
|
2013-02-22 13:23:57 -07:00
|
|
|
dummy := makeFuncStub
|
|
|
|
code := **(**uintptr)(unsafe.Pointer(&dummy))
|
2012-09-24 18:06:32 -06:00
|
|
|
|
2014-09-12 05:29:19 -06:00
|
|
|
// makeFuncImpl contains a stack map for use by the runtime
|
2014-12-22 12:31:55 -07:00
|
|
|
_, _, _, stack, _ := funcLayout(t, nil)
|
2014-09-12 05:29:19 -06:00
|
|
|
|
|
|
|
impl := &makeFuncImpl{code: code, stack: stack, typ: ftyp, fn: fn}
|
2012-09-24 18:06:32 -06:00
|
|
|
|
2014-10-17 10:54:31 -06:00
|
|
|
return Value{t, unsafe.Pointer(impl), flag(Func)}
|
2012-09-24 18:06:32 -06:00
|
|
|
}
|
|
|
|
|
2013-02-22 13:23:57 -07:00
|
|
|
// makeFuncStub is an assembly function that is the code half of
|
|
|
|
// the function returned from MakeFunc. It expects a *callReflectFunc
|
|
|
|
// as its context register, and its job is to invoke callReflect(ctxt, frame)
|
|
|
|
// where ctxt is the context register and frame is a pointer to the first
|
|
|
|
// word in the passed-in argument frame.
|
2012-09-24 18:06:32 -06:00
|
|
|
func makeFuncStub()
|
2013-03-21 14:59:16 -06:00
|
|
|
|
2016-12-16 09:57:25 -07:00
|
|
|
// The first two words of this type must be kept in sync with
|
|
|
|
// makeFuncImpl and runtime.reflectMethodValue.
|
|
|
|
// Any changes should be reflected in all three.
|
2013-03-21 14:59:16 -06:00
|
|
|
type methodValue struct {
|
|
|
|
fn uintptr
|
2016-12-16 09:57:25 -07:00
|
|
|
stack *bitVector
|
2013-03-21 14:59:16 -06:00
|
|
|
method int
|
|
|
|
rcvr Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2013-12-12 19:54:48 -07:00
|
|
|
panic("reflect: internal error: invalid use of makeMethodValue")
|
2013-03-21 14:59:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignoring the flagMethod bit, v describes the receiver, not the method type.
|
|
|
|
fl := v.flag & (flagRO | flagAddr | flagIndir)
|
2014-10-17 10:54:31 -06:00
|
|
|
fl |= flag(v.typ.Kind())
|
2014-10-15 12:24:18 -06:00
|
|
|
rcvr := Value{v.typ, v.ptr, fl}
|
2013-03-21 14:59:16 -06:00
|
|
|
|
|
|
|
// v.Type returns the actual type of the method value.
|
|
|
|
funcType := v.Type().(*rtype)
|
|
|
|
|
|
|
|
// Indirect Go func value (dummy) to obtain
|
|
|
|
// actual code address. (A Go func value is a pointer
|
2015-07-10 17:17:11 -06:00
|
|
|
// to a C function pointer. https://golang.org/s/go11func.)
|
2013-03-21 14:59:16 -06:00
|
|
|
dummy := methodValueCall
|
|
|
|
code := **(**uintptr)(unsafe.Pointer(&dummy))
|
|
|
|
|
2014-09-12 05:29:19 -06:00
|
|
|
// methodValue contains a stack map for use by the runtime
|
2014-12-22 12:31:55 -07:00
|
|
|
_, _, _, stack, _ := funcLayout(funcType, nil)
|
2014-09-12 05:29:19 -06:00
|
|
|
|
2013-03-21 14:59:16 -06:00
|
|
|
fv := &methodValue{
|
|
|
|
fn: code,
|
2014-09-12 05:29:19 -06:00
|
|
|
stack: stack,
|
2013-03-21 14:59:16 -06:00
|
|
|
method: int(v.flag) >> flagMethodShift,
|
|
|
|
rcvr: rcvr,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
methodReceiver(op, fv.rcvr, fv.method)
|
|
|
|
|
2014-10-17 10:54:31 -06:00
|
|
|
return Value{funcType, unsafe.Pointer(fv), v.flag&flagRO | flag(Func)}
|
2013-03-21 14:59:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// methodValueCall is an assembly function that is the code half of
|
|
|
|
// the function returned from makeMethodValue. It expects a *methodValue
|
|
|
|
// as its context register, and its job is to invoke callMethod(ctxt, frame)
|
|
|
|
// where ctxt is the context register and frame is a pointer to the first
|
|
|
|
// word in the passed-in argument frame.
|
|
|
|
func methodValueCall()
|