@@ -32,31 +32,32 @@ func getPtr(v reflect.Value) unsafe.Pointer {
3232type PatchGuard struct {
3333 target reflect.Value
3434 replacement reflect.Value
35+ isSymbol bool
3536}
3637
3738func (g * PatchGuard ) Unpatch () {
3839 unpatchValue (g .target )
3940}
4041
4142func (g * PatchGuard ) Restore () {
42- patchValue (g .target , g .replacement )
43+ patchValue (g .target , g .replacement , g . isSymbol )
4344}
4445
4546// Patch replaces a function with another
4647func Patch (target , replacement interface {}) * PatchGuard {
4748 t := reflect .ValueOf (target )
4849 r := reflect .ValueOf (replacement )
49- patchValue (t , r )
50+ patchValue (t , r , false )
5051
51- return & PatchGuard {t , r }
52+ return & PatchGuard {t , r , false }
5253}
5354
5455func PatchSymbol (target , replacement interface {}) * PatchGuard {
5556 t := reflect .ValueOf (target )
5657 r := reflect .ValueOf (replacement )
5758 patchSymbolValue (t , r )
5859
59- return & PatchGuard {t , r }
60+ return & PatchGuard {t , r , true }
6061}
6162
6263// PatchInstanceMethod replaces an instance method methodName for the type target with replacement
@@ -67,24 +68,24 @@ func PatchInstanceMethod(target reflect.Type, methodName string, replacement int
6768 panic (fmt .Sprintf ("unknown method %s" , methodName ))
6869 }
6970 r := reflect .ValueOf (replacement )
70- patchValue (m .Func , r )
71+ patchValue (m .Func , r , false )
7172
72- return & PatchGuard {m .Func , r }
73+ return & PatchGuard {m .Func , r , false }
7374}
7475
75- func patchValue (target , replacement reflect.Value ) {
76+ func patchValue (target , replacement reflect.Value , isSymbol bool ) {
7677 lock .Lock ()
7778 defer lock .Unlock ()
7879
79- if target .Kind () != reflect .Func {
80+ if ! isSymbol && target .Kind () != reflect .Func {
8081 panic ("target has to be a Func" )
8182 }
8283
8384 if replacement .Kind () != reflect .Func {
8485 panic ("replacement has to be a Func" )
8586 }
8687
87- if target .Type () != replacement .Type () {
88+ if ! isSymbol && target .Type () != replacement .Type () {
8889 panic (fmt .Sprintf ("target and replacement have to have the same type %s != %s" , target .Type (), replacement .Type ()))
8990 }
9091
0 commit comments