| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package event |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"runtime" |
| 12 |
"sort" |
| 13 |
"strings" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
|
| 18 |
helperDepthLimit = 5 |
| 19 |
) |
| 20 |
|
| 21 |
type sources struct { |
| 22 |
entries []caller |
| 23 |
} |
| 24 |
|
| 25 |
type Source struct { |
| 26 |
Space string |
| 27 |
Owner string |
| 28 |
Name string |
| 29 |
} |
| 30 |
|
| 31 |
type caller struct { |
| 32 |
helper bool |
| 33 |
pc uintptr |
| 34 |
source Source |
| 35 |
} |
| 36 |
|
| 37 |
var globalCallers chan *sources |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
func RegisterHelper(v interface{}) { |
| 48 |
g := <-globalCallers |
| 49 |
defer func() { globalCallers <- g }() |
| 50 |
switch v := v.(type) { |
| 51 |
case string: |
| 52 |
g.entries = append(g.entries, caller{source: splitName(v), helper: true}) |
| 53 |
default: |
| 54 |
g.helperFunction(v) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func init() { |
| 59 |
g := &sources{} |
| 60 |
|
| 61 |
globalCallers = make(chan *sources, 1) |
| 62 |
globalCallers <- g |
| 63 |
RegisterHelper("golang.org/x/exp/event") |
| 64 |
} |
| 65 |
|
| 66 |
func newCallers() sources { |
| 67 |
g := <-globalCallers |
| 68 |
defer func() { globalCallers <- g }() |
| 69 |
c := sources{} |
| 70 |
c.entries = make([]caller, len(g.entries)) |
| 71 |
copy(c.entries, g.entries) |
| 72 |
return c |
| 73 |
} |
| 74 |
|
| 75 |
func (c *sources) addCaller(entry caller) { |
| 76 |
i := sort.Search(len(c.entries), func(i int) bool { |
| 77 |
return c.entries[i].pc >= entry.pc |
| 78 |
}) |
| 79 |
if i >= len(c.entries) { |
| 80 |
|
| 81 |
c.entries = append(c.entries, entry) |
| 82 |
return |
| 83 |
} |
| 84 |
if c.entries[i].pc == entry.pc { |
| 85 |
|
| 86 |
return |
| 87 |
} |
| 88 |
|
| 89 |
c.entries = append(c.entries, caller{}) |
| 90 |
|
| 91 |
copy(c.entries[i+1:], c.entries[i:]) |
| 92 |
|
| 93 |
c.entries[i] = entry |
| 94 |
} |
| 95 |
|
| 96 |
func (c *sources) getCaller(pc uintptr) (caller, bool) { |
| 97 |
i := sort.Search(len(c.entries), func(i int) bool { |
| 98 |
return c.entries[i].pc >= pc |
| 99 |
}) |
| 100 |
if i == len(c.entries) || c.entries[i].pc != pc { |
| 101 |
return caller{}, false |
| 102 |
} |
| 103 |
return c.entries[i], true |
| 104 |
} |
| 105 |
|
| 106 |
func scanStack() Source { |
| 107 |
g := <-globalCallers |
| 108 |
defer func() { globalCallers <- g }() |
| 109 |
return g.scanStack() |
| 110 |
} |
| 111 |
|
| 112 |
func (c *sources) scanStack() Source { |
| 113 |
|
| 114 |
var stack [helperDepthLimit]uintptr |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
depth := runtime.Callers(3, stack[:]) |
| 120 |
|
| 121 |
for i := 0; i < depth; i++ { |
| 122 |
pc := stack[i] |
| 123 |
e, found := c.getCaller(pc) |
| 124 |
if found { |
| 125 |
if !e.helper { |
| 126 |
|
| 127 |
return e.source |
| 128 |
} |
| 129 |
|
| 130 |
continue |
| 131 |
} |
| 132 |
|
| 133 |
f := runtime.FuncForPC(stack[i]) |
| 134 |
if f == nil { |
| 135 |
|
| 136 |
continue |
| 137 |
} |
| 138 |
e = caller{ |
| 139 |
source: splitName(f.Name()), |
| 140 |
pc: pc, |
| 141 |
} |
| 142 |
e.helper = c.isHelper(e) |
| 143 |
c.addCaller(e) |
| 144 |
if !e.helper { |
| 145 |
|
| 146 |
return e.source |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return Source{} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
func (c *sources) isHelper(entry caller) bool { |
| 156 |
|
| 157 |
|
| 158 |
for _, e := range c.entries { |
| 159 |
if !e.helper { |
| 160 |
|
| 161 |
continue |
| 162 |
} |
| 163 |
if isMatch(entry.source.Space, e.source.Space) && |
| 164 |
isMatch(entry.source.Owner, e.source.Owner) && |
| 165 |
isMatch(entry.source.Name, e.source.Name) { |
| 166 |
return true |
| 167 |
} |
| 168 |
} |
| 169 |
return false |
| 170 |
} |
| 171 |
|
| 172 |
func isMatch(value, against string) bool { |
| 173 |
return len(against) == 0 || value == against |
| 174 |
} |
| 175 |
|
| 176 |
func (c *sources) helperFunction(v interface{}) { |
| 177 |
r := reflect.ValueOf(v) |
| 178 |
pc := r.Pointer() |
| 179 |
f := runtime.FuncForPC(pc) |
| 180 |
entry := caller{ |
| 181 |
source: splitName(f.Name()), |
| 182 |
pc: f.Entry(), |
| 183 |
helper: true, |
| 184 |
} |
| 185 |
c.addCaller(entry) |
| 186 |
if entry.pc != pc { |
| 187 |
entry.pc = pc |
| 188 |
c.addCaller(entry) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func splitName(full string) Source { |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
entry := Source{Space: full} |
| 197 |
slash := strings.LastIndexByte(full, '/') |
| 198 |
if slash < 0 { |
| 199 |
slash = 0 |
| 200 |
} |
| 201 |
if dot := strings.IndexByte(full[slash:], '.'); dot >= 0 { |
| 202 |
entry.Space = full[:slash+dot] |
| 203 |
entry.Name = full[slash+dot+1:] |
| 204 |
if dot = strings.LastIndexByte(entry.Name, '.'); dot >= 0 { |
| 205 |
entry.Owner = entry.Name[:dot] |
| 206 |
entry.Name = entry.Name[dot+1:] |
| 207 |
} |
| 208 |
} |
| 209 |
return entry |
| 210 |
} |