-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtroop_call.go
180 lines (149 loc) · 4.14 KB
/
troop_call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package goof
import (
"debug/dwarf"
"reflect"
"sort"
"unsafe"
"github.com/zeebo/errs"
)
func (t *Troop) addFunctions() error {
reader := t.data.Reader()
for {
entry, err := reader.Next()
if err != nil {
return errs.Wrap(err)
}
if entry == nil {
break
}
if entry.Tag != dwarf.TagSubprogram {
continue
}
name, ok := entry.Val(dwarf.AttrName).(string)
if !ok {
continue
}
pc, ok := entry.Val(dwarf.AttrLowpc).(uint64)
if !ok {
continue
}
dtypes, err := getFunctionArgTypes(t.data, entry)
if err != nil {
continue
}
_, err = t.findDwarfTypes(dtypes)
if err != nil {
continue
}
t.functions[name] = functionCacheEntry{
pc: pc,
dtypes: dtypes,
}
}
return nil
}
func (t *Troop) Functions() ([]string, error) {
if err := t.check(); err != nil {
return nil, err
}
out := make([]string, 0, len(t.functions))
for name := range t.functions {
out = append(out, name)
}
sort.Strings(out)
return out, nil
}
func (t *Troop) Call(name string, args ...interface{}) ([]interface{}, error) {
if err := t.check(); err != nil {
return nil, err
}
// and so it begins. find the function, the pc, and the types of the args
// and return values. we don't know which is which, but we assume the
// caller passed the appropriate things.
entry, ok := t.functions[name]
if !ok {
return nil, errs.New("call %s: unknown or uncallable function", name)
}
pc, dtypes := entry.pc, entry.dtypes
// make sure they didn't pass more arguments than total types
num_results := len(dtypes) - len(args)
if num_results < 0 {
return nil, errs.New("call %s: too many args", name)
}
// build the arguments, checking consistency and doing hacks to make it
// nice to use.
in, in_types, err := t.buildArguments(args, dtypes[:len(args)])
if err != nil {
return nil, errs.New("call %s: %v", name, err)
}
// the rest must be the return values, RIGHT? LOL
out_types, err := t.findDwarfTypes(dtypes[len(args):])
if err != nil {
return nil, errs.New("call %s: %v", name, err)
}
// make it happen
fn_typ := reflect.FuncOf(in_types, out_types, false)
fn := reflect.ValueOf(makeInterface(dataPtr(fn_typ), unsafe.Pointer(&pc)))
return ifaces(fn.Call(in)), nil
}
func (t *Troop) buildArguments(args []interface{}, dtypes []dwarf.Type) (
[]reflect.Value, []reflect.Type, error) {
if len(args) != len(dtypes) {
return nil, nil, errs.New("number of arguments does not match")
}
// so I want the Call signatrue to have args passed as ...interface{}
// because that makes the api nice: taking a reflect.Value is hard for the
// consumer.
//
// Unfortunately, that means that if you pass an interface type in, they
// get down-casted to just interface{}. Doubly unfortunately, the itab
// pointer was lost when the value was converted to an interface{} instead
// of whatever interface it was in the first place.
//
// So let's just look and see if we can find the interface type in the
// types map based on the dwarf type. If not, shoot. Hopefully that's
// rare! Maybe we can expose a CallReflect api that can get around this.
//
// Heh.
in_types := make([]reflect.Type, 0, len(args))
in := make([]reflect.Value, 0, len(args))
for i, arg := range args {
dtyp := dtypes[i]
val, err := t.constructValue(arg, dtyp)
if err != nil {
return nil, nil, errs.New("arg %d: %v", i, err)
}
in_types = append(in_types, val.Type())
in = append(in, val)
}
return in, in_types, nil
}
func (t *Troop) constructValue(arg interface{}, dtyp dwarf.Type) (
val reflect.Value, err error) {
rtyp, err := t.consistentValue(arg, dtyp)
if err != nil {
return val, err
}
if arg == nil {
return reflect.New(rtyp).Elem(), nil
}
return reflect.ValueOf(arg).Convert(rtyp), nil
}
func (t *Troop) consistentValue(arg interface{}, dtyp dwarf.Type) (
reflect.Type, error) {
rtyp, err := t.findDwarfType(dtyp)
if err != nil {
return nil, err
}
if arg == nil {
if !reflectCanBeNil(rtyp) {
return nil, errs.New("passing nil to non-nillable type: %v",
rtyp)
}
return rtyp, nil
}
if !reflect.TypeOf(arg).ConvertibleTo(rtyp) {
return nil, errs.New("cannot convert %v to %T", rtyp, arg)
}
return rtyp, nil
}