-
Notifications
You must be signed in to change notification settings - Fork 5
/
component.go
211 lines (176 loc) · 4.25 KB
/
component.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//go:generate go run github.com/cocotyty/dpig/generator
package dpig
import (
"fmt"
"reflect"
"unsafe"
)
type MethodSelector struct {
Object string
Method string
}
var initState = map[MethodSelector]*Extend{}
func InitEnv(enhances map[MethodSelector]*Extend) {
initState = enhances
}
func Change(s MethodSelector, e Extend) {
appContext[s.Object].table[s.Method].extend = &e
}
type PostCall func(in, out []reflect.Value)
type PreCall func(in []reflect.Value)
type AroundCall func(in []reflect.Value, next func([]reflect.Value) []reflect.Value) (out []reflect.Value)
type fnContext struct {
Name string
Origin reflect.Value
Methods []*wrapper
}
type object struct {
table map[string]*wrapper
fc *fnContext // avoid gc
ref []int64 // avoid gc
}
var appContext = map[string]*object{}
type wrapper struct {
method reflect.Value
impl makeFuncImpl
extend *Extend
}
type Extend struct {
Pre []PreCall
Around []AroundCall
Post []PostCall
}
type aroundState struct {
current int
calls []AroundCall
final func(in []reflect.Value) (out []reflect.Value)
}
func (a *aroundState) Call(in []reflect.Value) (out []reflect.Value) {
curr := a.current
if len(a.calls) == curr {
return a.final(in)
}
a.current++
return a.calls[curr](in, a.Call)
}
func (w *wrapper) Call(in []reflect.Value) (out []reflect.Value) {
for _, itt := range w.extend.Pre {
itt(in)
}
as := &aroundState{
current: 0,
calls: w.extend.Around,
final: w.method.Call,
}
out = as.Call(in)
for _, itt := range w.extend.Post {
itt(in, out)
}
return out
}
func Component(i interface{}) {
iv := reflect.ValueOf(i).Elem()
name := iv.Type().Name()
obj := &object{}
appContext[name] = obj
n := iv.NumMethod()
ivv := (*iFaceValue)(unsafe.Pointer(&iv))
// old value
ovv := *ivv
ovv.ptr = &nonEmptyInterface{
itab: ivv.ptr.itab,
word: ivv.ptr.word,
}
ovp := (*reflect.Value)(unsafe.Pointer(&ovv))
fc := &fnContext{Origin: *ovp}
ivv.ptr.word = unsafe.Pointer(fc)
length := n + 3
arr := make([]int64, length)
sl := (*[]int64)(unsafe.Pointer(&slice{Data: unsafe.Pointer(ivv.ptr.itab), Len: length, Cap: length}))
copy(arr, *sl)
nItab := (*itab)(unsafe.Pointer(&arr[0]))
fc.Methods = make([]*wrapper, n)
obj.table = make(map[string]*wrapper, n)
obj.fc = fc
obj.ref = arr
for i := 0; i < n; i++ {
wp := buildWrapper(name, ovp.Type().Method(i).Name, ovp.Method(i), ovp.Type().Method(i).Type)
obj.table[ovp.Type().Method(i).Name] = wp
fc.Methods[i] = wp
var err error
nItab.fun[i], err = getMethods(i)
if err != nil {
panic(err)
}
}
ivv.ptr.itab = nItab
}
func buildWrapper(o, m string, method reflect.Value, mType reflect.Type) *wrapper {
w := &wrapper{
extend: &Extend{},
method: method,
impl: makeFuncImpl{
ftyp: reflect.ValueOf(mType).Pointer(),
},
}
if exd, ok := initState[MethodSelector{
Object: o,
Method: m,
}]; ok {
w.extend = exd
}
w.impl.fn = w.Call
return w
}
type funcValue struct {
typ uintptr
ptr unsafe.Pointer
flag uintptr
}
type iFaceValue struct {
typ uintptr
ptr *nonEmptyInterface
flag uintptr
}
// nonEmptyInterface is the header for an interface value with methods.
type nonEmptyInterface struct {
// see ../runtime/iface.go:/Itab
itab *itab
word unsafe.Pointer
}
type itab struct {
ityp uintptr // static interface type
typ uintptr // dynamic concrete type
hash uint32 // copy of typ.hash
_ [4]byte
fun [100000]unsafe.Pointer // method table
}
type slice struct {
Data unsafe.Pointer
Len int
Cap int
}
var ErrLargeSizeMethods = fmt.Errorf("methods size more than %d", len(methods))
func getMethods(i int) (fn unsafe.Pointer, err error) {
if i >= len(methods) {
return nil, ErrLargeSizeMethods
}
return methods[i], nil
}
type makeFuncImpl struct {
code uintptr
stack uintptr // ptrmap for both args and results
argLen uintptr // just args
ftyp uintptr
fn func([]reflect.Value) []reflect.Value
}
//go:linkname callReflect reflect.callReflect
func callReflect(impl *makeFuncImpl, frame unsafe.Pointer, retValid *bool)
func myCallReflect(n int, frame unsafe.Pointer, retValid *bool) {
// Copy argument frame into Values.
ptr := frame
off := uintptr(0)
var fc = *(**fnContext)(ptr)
off += 8
callReflect(&fc.Methods[n].impl, unsafe.Pointer(uintptr(ptr)+off), retValid)
}