1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // MakeFunc implementation.
14 // makeFuncImpl is the closure value implementing the function
15 // returned by MakeFunc.
16 type makeFuncImpl
struct {
19 fn
func([]Value
) []Value
21 // For gccgo we use the same entry point for functions and for
27 // MakeFunc returns a new function of the given Type
28 // that wraps the function fn. When called, that new function
29 // does the following:
31 // - converts its arguments to a slice of Values.
32 // - runs results := fn(args).
33 // - returns the results as a slice of Values, one per formal result.
35 // The implementation fn can assume that the argument Value slice
36 // has the number and type of arguments given by typ.
37 // If typ describes a variadic function, the final Value is itself
38 // a slice representing the variadic arguments, as in the
39 // body of a variadic function. The result Value slice returned by fn
40 // must have the number and type of results given by typ.
42 // The Value.Call method allows the caller to invoke a typed function
43 // in terms of Values; in contrast, MakeFunc allows the caller to implement
44 // a typed function in terms of Values.
46 // The Examples section of the documentation includes an illustration
47 // of how to use MakeFunc to build a swap function for different types.
49 func MakeFunc(typ Type
, fn
func(args
[]Value
) (results
[]Value
)) Value
{
50 if typ
.Kind() != Func
{
51 panic("reflect: call of MakeFunc with non-Func type")
54 switch runtime
.GOARCH
{
57 panic("reflect.MakeFunc not implemented for " + runtime
.GOARCH
)
61 ftyp
:= (*funcType
)(unsafe
.Pointer(t
))
63 // Indirect Go func value (dummy) to obtain
64 // actual code address. (A Go func value is a pointer
65 // to a C function pointer. http://golang.org/s/go11func.)
67 code
:= **(**uintptr)(unsafe
.Pointer(&dummy
))
69 impl
:= &makeFuncImpl
{code
: code
, typ
: ftyp
, fn
: fn
, method
: -1}
71 return Value
{t
, unsafe
.Pointer(&impl
), flag(Func
<<flagKindShift
) | flagIndir
}
74 // makeFuncStub is an assembly function that is the code half of
75 // the function returned from MakeFunc. It expects a *callReflectFunc
76 // as its context register, and its job is to invoke callReflect(ctxt, frame)
77 // where ctxt is the context register and frame is a pointer to the first
78 // word in the passed-in argument frame.
81 // makeMethodValue converts v from the rcvr+method index representation
82 // of a method value to an actual method func value, which is
83 // basically the receiver value with a special bit set, into a true
84 // func value - a value holding an actual func. The output is
85 // semantically equivalent to the input as far as the user of package
86 // reflect can tell, but the true func representation can be handled
87 // by code like Convert and Interface and Assign.
88 func makeMethodValue(op
string, v Value
) Value
{
89 if v
.flag
&flagMethod
== 0 {
90 panic("reflect: internal error: invalid use of makeMethodValue")
93 switch runtime
.GOARCH
{
96 panic("reflect.makeMethodValue not implemented for " + runtime
.GOARCH
)
99 // Ignoring the flagMethod bit, v describes the receiver, not the method type.
100 fl
:= v
.flag
& (flagRO | flagAddr | flagIndir
)
101 fl |
= flag(v
.typ
.Kind()) << flagKindShift
102 rcvr
:= Value
{v
.typ
, v
.ptr
/* v.scalar, */, fl
}
104 // v.Type returns the actual type of the method value.
105 ft
:= v
.Type().(*rtype
)
107 // Indirect Go func value (dummy) to obtain
108 // actual code address. (A Go func value is a pointer
109 // to a C function pointer. http://golang.org/s/go11func.)
110 dummy
:= makeFuncStub
111 code
:= **(**uintptr)(unsafe
.Pointer(&dummy
))
113 // Cause panic if method is not appropriate.
114 // The panic would still happen during the call if we omit this,
115 // but we want Interface() and other operations to fail early.
116 t
, _
:= methodReceiver(op
, rcvr
, int(v
.flag
)>>flagMethodShift
)
120 typ
: (*funcType
)(unsafe
.Pointer(t
)),
121 method
: int(v
.flag
) >> flagMethodShift
,
125 return Value
{ft
, unsafe
.Pointer(&fv
), v
.flag
&flagRO |
flag(Func
)<<flagKindShift | flagIndir
}
128 // makeValueMethod takes a method function and returns a function that
129 // takes a value receiver and calls the real method with a pointer to
131 func makeValueMethod(v Value
) Value
{
133 if typ
.Kind() != Func
{
134 panic("reflect: call of makeValueMethod with non-Func type")
136 if v
.flag
&flagMethodFn
== 0 {
137 panic("reflect: call of makeValueMethod with non-MethodFn")
140 switch runtime
.GOARCH
{
143 panic("reflect.makeValueMethod not implemented for " + runtime
.GOARCH
)
147 ftyp
:= (*funcType
)(unsafe
.Pointer(t
))
149 // Indirect Go func value (dummy) to obtain
150 // actual code address. (A Go func value is a pointer
151 // to a C function pointer. http://golang.org/s/go11func.)
152 dummy
:= makeFuncStub
153 code
:= **(**uintptr)(unsafe
.Pointer(&dummy
))
155 impl
:= &makeFuncImpl
{
162 return Value
{t
, unsafe
.Pointer(&impl
), flag(Func
<<flagKindShift
) | flagIndir
}
165 // Call the function represented by a makeFuncImpl.
166 func (c
*makeFuncImpl
) call(in
[]Value
) []Value
{
169 } else if c
.method
== -2 {
170 if c
.typ
.IsVariadic() {
171 return c
.rcvr
.CallSlice(in
)
173 return c
.rcvr
.Call(in
)
176 m
:= c
.rcvr
.Method(c
.method
)
177 if c
.typ
.IsVariadic() {
178 return m
.CallSlice(in
)