blob: 032057d2a15413b501d5afbacf2c8a0817550c8b [file] [log] [blame]
Russ Coxba4625c2012-09-24 20:06:32 -04001// 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
7package reflect
8
9import (
Russ Coxba4625c2012-09-24 20:06:32 -040010 "unsafe"
11)
12
13// makeFuncImpl is the closure value implementing the function
14// returned by MakeFunc.
15type makeFuncImpl struct {
Russ Coxf0d44db2014-09-12 07:29:19 -040016 code uintptr
17 stack *bitVector // stack bitmap for args - offset known to runtime
18 typ *funcType
19 fn func([]Value) []Value
Russ Coxba4625c2012-09-24 20:06:32 -040020}
21
22// MakeFunc returns a new function of the given Type
23// that wraps the function fn. When called, that new function
24// does the following:
25//
Ian Lance Tayloref4e12a2013-10-04 13:12:50 -070026// - converts its arguments to a slice of Values.
Russ Coxba4625c2012-09-24 20:06:32 -040027// - runs results := fn(args).
28// - returns the results as a slice of Values, one per formal result.
29//
30// The implementation fn can assume that the argument Value slice
31// has the number and type of arguments given by typ.
32// If typ describes a variadic function, the final Value is itself
33// a slice representing the variadic arguments, as in the
34// body of a variadic function. The result Value slice returned by fn
35// must have the number and type of results given by typ.
36//
37// The Value.Call method allows the caller to invoke a typed function
38// in terms of Values; in contrast, MakeFunc allows the caller to implement
39// a typed function in terms of Values.
40//
41// The Examples section of the documentation includes an illustration
42// of how to use MakeFunc to build a swap function for different types.
43//
44func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
45 if typ.Kind() != Func {
46 panic("reflect: call of MakeFunc with non-Func type")
47 }
48
Russ Coxba4625c2012-09-24 20:06:32 -040049 t := typ.common()
Russ Coxb1b67a32013-02-22 15:23:57 -050050 ftyp := (*funcType)(unsafe.Pointer(t))
Russ Coxba4625c2012-09-24 20:06:32 -040051
Russ Cox3be70362013-03-21 16:59:16 -040052 // Indirect Go func value (dummy) to obtain
53 // actual code address. (A Go func value is a pointer
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -060054 // to a C function pointer. https://golang.org/s/go11func.)
Russ Coxb1b67a32013-02-22 15:23:57 -050055 dummy := makeFuncStub
56 code := **(**uintptr)(unsafe.Pointer(&dummy))
Russ Coxba4625c2012-09-24 20:06:32 -040057
Russ Coxf0d44db2014-09-12 07:29:19 -040058 // makeFuncImpl contains a stack map for use by the runtime
Dmitry Vyukov67f8a812014-12-22 22:31:55 +030059 _, _, _, stack, _ := funcLayout(t, nil)
Russ Coxf0d44db2014-09-12 07:29:19 -040060
61 impl := &makeFuncImpl{code: code, stack: stack, typ: ftyp, fn: fn}
Russ Coxba4625c2012-09-24 20:06:32 -040062
Russ Cox0d81b722014-10-17 12:54:31 -040063 return Value{t, unsafe.Pointer(impl), flag(Func)}
Russ Coxba4625c2012-09-24 20:06:32 -040064}
65
Russ Coxb1b67a32013-02-22 15:23:57 -050066// makeFuncStub is an assembly function that is the code half of
67// the function returned from MakeFunc. It expects a *callReflectFunc
68// as its context register, and its job is to invoke callReflect(ctxt, frame)
69// where ctxt is the context register and frame is a pointer to the first
70// word in the passed-in argument frame.
Russ Coxba4625c2012-09-24 20:06:32 -040071func makeFuncStub()
Russ Cox3be70362013-03-21 16:59:16 -040072
73type methodValue struct {
74 fn uintptr
Russ Coxf0d44db2014-09-12 07:29:19 -040075 stack *bitVector // stack bitmap for args - offset known to runtime
Russ Cox3be70362013-03-21 16:59:16 -040076 method int
77 rcvr Value
78}
79
80// makeMethodValue converts v from the rcvr+method index representation
81// of a method value to an actual method func value, which is
82// basically the receiver value with a special bit set, into a true
83// func value - a value holding an actual func. The output is
84// semantically equivalent to the input as far as the user of package
85// reflect can tell, but the true func representation can be handled
86// by code like Convert and Interface and Assign.
87func makeMethodValue(op string, v Value) Value {
88 if v.flag&flagMethod == 0 {
Ian Lance Taylorc0946af2013-12-12 18:54:48 -080089 panic("reflect: internal error: invalid use of makeMethodValue")
Russ Cox3be70362013-03-21 16:59:16 -040090 }
91
92 // Ignoring the flagMethod bit, v describes the receiver, not the method type.
93 fl := v.flag & (flagRO | flagAddr | flagIndir)
Russ Cox0d81b722014-10-17 12:54:31 -040094 fl |= flag(v.typ.Kind())
Russ Coxa1616d42014-10-15 14:24:18 -040095 rcvr := Value{v.typ, v.ptr, fl}
Russ Cox3be70362013-03-21 16:59:16 -040096
97 // v.Type returns the actual type of the method value.
98 funcType := v.Type().(*rtype)
99
100 // Indirect Go func value (dummy) to obtain
101 // actual code address. (A Go func value is a pointer
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -0600102 // to a C function pointer. https://golang.org/s/go11func.)
Russ Cox3be70362013-03-21 16:59:16 -0400103 dummy := methodValueCall
104 code := **(**uintptr)(unsafe.Pointer(&dummy))
105
Russ Coxf0d44db2014-09-12 07:29:19 -0400106 // methodValue contains a stack map for use by the runtime
Dmitry Vyukov67f8a812014-12-22 22:31:55 +0300107 _, _, _, stack, _ := funcLayout(funcType, nil)
Russ Coxf0d44db2014-09-12 07:29:19 -0400108
Russ Cox3be70362013-03-21 16:59:16 -0400109 fv := &methodValue{
110 fn: code,
Russ Coxf0d44db2014-09-12 07:29:19 -0400111 stack: stack,
Russ Cox3be70362013-03-21 16:59:16 -0400112 method: int(v.flag) >> flagMethodShift,
113 rcvr: rcvr,
114 }
115
116 // Cause panic if method is not appropriate.
117 // The panic would still happen during the call if we omit this,
118 // but we want Interface() and other operations to fail early.
119 methodReceiver(op, fv.rcvr, fv.method)
120
Russ Cox0d81b722014-10-17 12:54:31 -0400121 return Value{funcType, unsafe.Pointer(fv), v.flag&flagRO | flag(Func)}
Russ Cox3be70362013-03-21 16:59:16 -0400122}
123
124// methodValueCall is an assembly function that is the code half of
125// the function returned from makeMethodValue. It expects a *methodValue
126// as its context register, and its job is to invoke callMethod(ctxt, frame)
127// where ctxt is the context register and frame is a pointer to the first
128// word in the passed-in argument frame.
129func methodValueCall()