@@ -34,13 +34,11 @@ var (
34
34
func PatchMethod (target , redirection interface {}) (* Patch , error ) {
35
35
tValue := getValueFrom (target )
36
36
rValue := getValueFrom (redirection )
37
- err := isPatchable (& tValue , & rValue )
38
- if err != nil {
37
+ if err := isPatchable (& tValue , & rValue ); err != nil {
39
38
return nil , err
40
39
}
41
40
patch := & Patch {target : & tValue , redirection : & rValue }
42
- err = applyPatch (patch )
43
- if err != nil {
41
+ if err := applyPatch (patch ); err != nil {
44
42
return nil , err
45
43
}
46
44
return patch , nil
@@ -54,37 +52,32 @@ func PatchInstanceMethodByName(target reflect.Type, methodName string, redirecti
54
52
if ! ok {
55
53
return nil , errors .New (fmt .Sprintf ("Method '%v' not found" , methodName ))
56
54
}
57
- return PatchMethodByReflect (method , redirection )
55
+ return PatchMethodByReflect (method . Func , redirection )
58
56
}
59
- func PatchMethodByReflect (target reflect.Method , redirection interface {}) (* Patch , error ) {
60
- tValue := & target . Func
57
+ func PatchMethodByReflect (target reflect.Value , redirection interface {}) (* Patch , error ) {
58
+ tValue := & target
61
59
rValue := getValueFrom (redirection )
62
- err := isPatchable (tValue , & rValue )
63
- if err != nil {
60
+ if err := isPatchable (tValue , & rValue ); err != nil {
64
61
return nil , err
65
62
}
66
63
patch := & Patch {target : tValue , redirection : & rValue }
67
- err = applyPatch (patch )
68
- if err != nil {
64
+ if err := applyPatch (patch ); err != nil {
69
65
return nil , err
70
66
}
71
67
return patch , nil
72
68
}
73
- func PatchMethodWithMakeFunc (target reflect.Method , fn func (args []reflect.Value ) (results []reflect.Value )) (* Patch , error ) {
74
- rValue := reflect .MakeFunc (target .Type , fn )
75
- return PatchMethodByReflect (target , rValue )
69
+ func PatchMethodWithMakeFunc (target reflect.Value , fn func (args []reflect.Value ) (results []reflect.Value )) (* Patch , error ) {
70
+ return PatchMethodByReflect (target , reflect .MakeFunc (target .Type (), fn ))
76
71
}
77
72
78
73
func (p * Patch ) Patch () error {
79
74
if p == nil {
80
75
return errors .New ("patch is nil" )
81
76
}
82
- err := isPatchable (p .target , p .redirection )
83
- if err != nil {
77
+ if err := isPatchable (p .target , p .redirection ); err != nil {
84
78
return err
85
79
}
86
- err = applyPatch (p )
87
- if err != nil {
80
+ if err := applyPatch (p ); err != nil {
88
81
return err
89
82
}
90
83
return nil
@@ -103,7 +96,7 @@ func isPatchable(target, redirection *reflect.Value) error {
103
96
if target .Type () != redirection .Type () {
104
97
return errors .New (fmt .Sprintf ("the target and/or redirection doesn't have the same type: %s != %s" , target .Type (), redirection .Type ()))
105
98
}
106
- if _ , ok := patches [getSafePointer (target )]; ok {
99
+ if _ , ok := patches [getSafeCodePointer (target )]; ok {
107
100
return errors .New ("the target is already patched" )
108
101
}
109
102
return nil
@@ -112,7 +105,7 @@ func isPatchable(target, redirection *reflect.Value) error {
112
105
func applyPatch (patch * Patch ) error {
113
106
patchLock .Lock ()
114
107
defer patchLock .Unlock ()
115
- tPointer := getSafePointer (patch .target )
108
+ tPointer := getSafeCodePointer (patch .target )
116
109
rPointer := getInternalPtrFromValue (patch .redirection )
117
110
rPointerJumpBytes , err := getJumpFuncBytes (rPointer )
118
111
if err != nil {
@@ -121,8 +114,7 @@ func applyPatch(patch *Patch) error {
121
114
tPointerBytes := getMemorySliceFromPointer (tPointer , len (rPointerJumpBytes ))
122
115
targetBytes := make ([]byte , len (tPointerBytes ))
123
116
copy (targetBytes , tPointerBytes )
124
- err = copyDataToPtr (tPointer , rPointerJumpBytes )
125
- if err != nil {
117
+ if err := copyDataToPtr (tPointer , rPointerJumpBytes ); err != nil {
126
118
return err
127
119
}
128
120
patch .targetBytes = targetBytes
@@ -136,7 +128,7 @@ func applyUnpatch(patch *Patch) error {
136
128
if patch .targetBytes == nil || len (patch .targetBytes ) == 0 {
137
129
return errors .New ("the target is not patched" )
138
130
}
139
- tPointer := getSafePointer (patch .target )
131
+ tPointer := getSafeCodePointer (patch .target )
140
132
if _ , ok := patches [tPointer ]; ! ok {
141
133
return errors .New ("the target is not patched" )
142
134
}
@@ -164,7 +156,7 @@ func getMemorySliceFromPointer(p unsafe.Pointer, length int) []byte {
164
156
}))
165
157
}
166
158
167
- func getSafePointer (value * reflect.Value ) unsafe.Pointer {
159
+ func getSafeCodePointer (value * reflect.Value ) unsafe.Pointer {
168
160
p := getInternalPtrFromValue (value )
169
161
if p != nil {
170
162
p = * (* unsafe .Pointer )(p )
0 commit comments