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.
13 // makeFuncImpl is the closure value implementing the function
14 // returned by MakeFunc.
15 // The first three words are layed out like ffi_go_closure.
16 type makeFuncImpl
struct {
18 ffi_cif unsafe
.Pointer
19 ffi_fun
func(unsafe
.Pointer
, unsafe
.Pointer
)
22 fn
func([]Value
) []Value
24 // For gccgo we use the same entry point for functions and for
30 // MakeFunc returns a new function of the given Type
31 // that wraps the function fn. When called, that new function
32 // does the following:
34 // - converts its arguments to a slice of Values.
35 // - runs results := fn(args).
36 // - returns the results as a slice of Values, one per formal result.
38 // The implementation fn can assume that the argument Value slice
39 // has the number and type of arguments given by typ.
40 // If typ describes a variadic function, the final Value is itself
41 // a slice representing the variadic arguments, as in the
42 // body of a variadic function. The result Value slice returned by fn
43 // must have the number and type of results given by typ.
45 // The Value.Call method allows the caller to invoke a typed function
46 // in terms of Values; in contrast, MakeFunc allows the caller to implement
47 // a typed function in terms of Values.
49 // The Examples section of the documentation includes an illustration
50 // of how to use MakeFunc to build a swap function for different types.
52 func MakeFunc(typ Type
, fn
func(args
[]Value
) (results
[]Value
)) Value
{
53 if typ
.Kind() != Func
{
54 panic("reflect: call of MakeFunc with non-Func type")
58 ftyp
:= (*funcType
)(unsafe
.Pointer(t
))
60 impl
:= &makeFuncImpl
{
66 makeFuncFFI(makeCIF(ftyp
), unsafe
.Pointer(impl
))
68 return Value
{t
, unsafe
.Pointer(&impl
), flag(Func
) | flagIndir
}
71 // makeMethodValue converts v from the rcvr+method index representation
72 // of a method value to an actual method func value, which is
73 // basically the receiver value with a special bit set, into a true
74 // func value - a value holding an actual func. The output is
75 // semantically equivalent to the input as far as the user of package
76 // reflect can tell, but the true func representation can be handled
77 // by code like Convert and Interface and Assign.
78 func makeMethodValue(op
string, v Value
) Value
{
79 if v
.flag
&flagMethod
== 0 {
80 panic("reflect: internal error: invalid use of makeMethodValue")
83 // Ignoring the flagMethod bit, v describes the receiver, not the method type.
84 fl
:= v
.flag
& (flagRO | flagAddr | flagIndir
)
85 fl |
= flag(v
.typ
.Kind())
86 rcvr
:= Value
{v
.typ
, v
.ptr
, fl
}
88 // v.Type returns the actual type of the method value.
89 ft
:= v
.Type().(*rtype
)
91 // Cause panic if method is not appropriate.
92 // The panic would still happen during the call if we omit this,
93 // but we want Interface() and other operations to fail early.
94 _
, t
, _
:= methodReceiver(op
, rcvr
, int(v
.flag
)>>flagMethodShift
)
96 ftyp
:= (*funcType
)(unsafe
.Pointer(t
))
97 method
:= int(v
.flag
) >> flagMethodShift
105 makeFuncFFI(makeCIF(ftyp
), unsafe
.Pointer(fv
))
107 return Value
{ft
, unsafe
.Pointer(&fv
), v
.flag
&flagRO |
flag(Func
) | flagIndir
}
110 // makeValueMethod takes a method function and returns a function that
111 // takes a value receiver and calls the real method with a pointer to
113 func makeValueMethod(v Value
) Value
{
115 if typ
.Kind() != Func
{
116 panic("reflect: call of makeValueMethod with non-Func type")
118 if v
.flag
&flagMethodFn
== 0 {
119 panic("reflect: call of makeValueMethod with non-MethodFn")
123 ftyp
:= (*funcType
)(unsafe
.Pointer(t
))
125 impl
:= &makeFuncImpl
{
131 makeFuncFFI(makeCIF(ftyp
), unsafe
.Pointer(impl
))
133 return Value
{t
, unsafe
.Pointer(&impl
), v
.flag
&flagRO |
flag(Func
) | flagIndir
}
136 // Call the function represented by a makeFuncImpl.
137 func (c
*makeFuncImpl
) call(in
[]Value
) []Value
{
140 } else if c
.method
== -2 {
141 if c
.typ
.IsVariadic() {
142 return c
.rcvr
.CallSlice(in
)
144 return c
.rcvr
.Call(in
)
147 m
:= c
.rcvr
.Method(c
.method
)
148 if c
.typ
.IsVariadic() {
149 return m
.CallSlice(in
)