| 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. |
| 4 |
|
| 5 |
// MakeFunc implementation. |
| 6 |
|
| 7 |
package reflect |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/abi" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
// makeFuncImpl is the closure value implementing the function |
| 15 |
// returned by MakeFunc. |
| 16 |
// The first three words are layed out like ffi_go_closure. |
| 17 |
type makeFuncImpl struct { |
| 18 |
code uintptr |
| 19 |
ffi_cif unsafe.Pointer |
| 20 |
ffi_fun func(unsafe.Pointer, unsafe.Pointer) |
| 21 |
|
| 22 |
typ *funcType |
| 23 |
fn func([]Value) []Value |
| 24 |
|
| 25 |
// For gccgo we use the same entry point for functions and for |
| 26 |
// method values. |
| 27 |
method int |
| 28 |
rcvr Value |
| 29 |
} |
| 30 |
|
| 31 |
// MakeFunc returns a new function of the given Type |
| 32 |
// that wraps the function fn. When called, that new function |
| 33 |
// does the following: |
| 34 |
// |
| 35 |
// - converts its arguments to a slice of Values. |
| 36 |
// - runs results := fn(args). |
| 37 |
// - returns the results as a slice of Values, one per formal result. |
| 38 |
// |
| 39 |
// The implementation fn can assume that the argument Value slice |
| 40 |
// has the number and type of arguments given by typ. |
| 41 |
// If typ describes a variadic function, the final Value is itself |
| 42 |
// a slice representing the variadic arguments, as in the |
| 43 |
// body of a variadic function. The result Value slice returned by fn |
| 44 |
// must have the number and type of results given by typ. |
| 45 |
// |
| 46 |
// The Value.Call method allows the caller to invoke a typed function |
| 47 |
// in terms of Values; in contrast, MakeFunc allows the caller to implement |
| 48 |
// a typed function in terms of Values. |
| 49 |
// |
| 50 |
// The Examples section of the documentation includes an illustration |
| 51 |
// of how to use MakeFunc to build a swap function for different types. |
| 52 |
// |
| 53 |
func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value { |
| 54 |
if typ.Kind() != Func { |
| 55 |
panic("reflect: call of MakeFunc with non-Func type") |
| 56 |
} |
| 57 |
|
| 58 |
t := typ.common() |
| 59 |
ftyp := (*funcType)(unsafe.Pointer(t)) |
| 60 |
|
| 61 |
impl := &makeFuncImpl{ |
| 62 |
typ: ftyp, |
| 63 |
fn: fn, |
| 64 |
method: -1, |
| 65 |
} |
| 66 |
|
| 67 |
makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(impl)) |
| 68 |
|
| 69 |
return Value{t, unsafe.Pointer(&impl), flag(Func) | flagIndir} |
| 70 |
} |
| 71 |
|
| 72 |
// makeMethodValue converts v from the rcvr+method index representation |
| 73 |
// of a method value to an actual method func value, which is |
| 74 |
// basically the receiver value with a special bit set, into a true |
| 75 |
// func value - a value holding an actual func. The output is |
| 76 |
// semantically equivalent to the input as far as the user of package |
| 77 |
// reflect can tell, but the true func representation can be handled |
| 78 |
// by code like Convert and Interface and Assign. |
| 79 |
func makeMethodValue(op string, v Value) Value { |
| 80 |
if v.flag&flagMethod == 0 { |
| 81 |
panic("reflect: internal error: invalid use of makeMethodValue") |
| 82 |
} |
| 83 |
|
| 84 |
// Ignoring the flagMethod bit, v describes the receiver, not the method type. |
| 85 |
fl := v.flag & (flagRO | flagAddr | flagIndir) |
| 86 |
fl |= flag(v.typ.Kind()) |
| 87 |
rcvr := Value{v.typ, v.ptr, fl} |
| 88 |
|
| 89 |
// v.Type returns the actual type of the method value. |
| 90 |
ft := v.Type().(*rtype) |
| 91 |
|
| 92 |
// Cause panic if method is not appropriate. |
| 93 |
// The panic would still happen during the call if we omit this, |
| 94 |
// but we want Interface() and other operations to fail early. |
| 95 |
_, t, _ := methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift) |
| 96 |
|
| 97 |
ftyp := (*funcType)(unsafe.Pointer(t)) |
| 98 |
method := int(v.flag) >> flagMethodShift |
| 99 |
|
| 100 |
fv := &makeFuncImpl{ |
| 101 |
typ: ftyp, |
| 102 |
method: method, |
| 103 |
rcvr: rcvr, |
| 104 |
} |
| 105 |
|
| 106 |
makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(fv)) |
| 107 |
|
| 108 |
return Value{ft, unsafe.Pointer(&fv), v.flag&flagRO | flag(Func) | flagIndir} |
| 109 |
} |
| 110 |
|
| 111 |
// makeValueMethod takes a method function and returns a function that |
| 112 |
// takes a value receiver and calls the real method with a pointer to |
| 113 |
// it. |
| 114 |
func makeValueMethod(v Value) Value { |
| 115 |
typ := v.typ |
| 116 |
if typ.Kind() != Func { |
| 117 |
panic("reflect: call of makeValueMethod with non-Func type") |
| 118 |
} |
| 119 |
if v.flag&flagMethodFn == 0 { |
| 120 |
panic("reflect: call of makeValueMethod with non-MethodFn") |
| 121 |
} |
| 122 |
|
| 123 |
t := typ.common() |
| 124 |
ftyp := (*funcType)(unsafe.Pointer(t)) |
| 125 |
|
| 126 |
impl := &makeFuncImpl{ |
| 127 |
typ: ftyp, |
| 128 |
method: -2, |
| 129 |
rcvr: v, |
| 130 |
} |
| 131 |
|
| 132 |
makeFuncFFI(makeCIF(ftyp), unsafe.Pointer(impl)) |
| 133 |
|
| 134 |
return Value{t, unsafe.Pointer(&impl), v.flag&flagRO | flag(Func) | flagIndir} |
| 135 |
} |
| 136 |
|
| 137 |
// methodValueCallCodePtr and methodValueCall are only here to provide |
| 138 |
// something to return for the UnsafePointer method of a method value. |
| 139 |
func methodValueCallCodePtr() uintptr { |
| 140 |
return abi.FuncPCABI0(methodValueCall) |
| 141 |
} |
| 142 |
|
| 143 |
func methodValueCall() { |
| 144 |
panic("methodValueCall") |
| 145 |
} |
| 146 |
|
| 147 |
// Call the function represented by a makeFuncImpl. |
| 148 |
func (c *makeFuncImpl) call(in []Value) []Value { |
| 149 |
if c.method == -1 { |
| 150 |
return c.fn(in) |
| 151 |
} else if c.method == -2 { |
| 152 |
if c.typ.IsVariadic() { |
| 153 |
return c.rcvr.CallSlice(in) |
| 154 |
} else { |
| 155 |
return c.rcvr.Call(in) |
| 156 |
} |
| 157 |
} else { |
| 158 |
m := c.rcvr.Method(c.method) |
| 159 |
if c.typ.IsVariadic() { |
| 160 |
return m.CallSlice(in) |
| 161 |
} else { |
| 162 |
return m.Call(in) |
| 163 |
} |
| 164 |
} |
| 165 |
} |