2012-09-24 18:06:32 -06:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// 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.
|
|
|
|
type makeFuncImpl struct {
|
2013-02-22 13:23:57 -07:00
|
|
|
code uintptr
|
|
|
|
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:
|
|
|
|
//
|
|
|
|
// - converts its arguments to a list of Values args.
|
|
|
|
// - 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-02-22 13:23:57 -07:00
|
|
|
// indirect Go func value (dummy) to obtain
|
|
|
|
// actual code address. (A Go func is a pointer
|
|
|
|
// to a C function pointer. http://golang.org/s/go11func.)
|
|
|
|
dummy := makeFuncStub
|
|
|
|
code := **(**uintptr)(unsafe.Pointer(&dummy))
|
2012-09-24 18:06:32 -06:00
|
|
|
|
2013-02-22 13:23:57 -07:00
|
|
|
impl := &makeFuncImpl{code: code, typ: ftyp, fn: fn}
|
2012-09-24 18:06:32 -06:00
|
|
|
|
2013-02-21 15:01:13 -07:00
|
|
|
return Value{t, unsafe.Pointer(impl), flag(Func) << flagKindShift}
|
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()
|