objc: rename IMP to NewIMP and _IMP to IMP (#23)
- commit
- ffec45151ddbd20ddf02f5c04f6fa83b5e07910b
- author
- Hajime Hoshi · · 2022-08-20 19:33 UTC
- committer
- GitHub · · 2022-08-20 19:33 UTC
- parent
- 8dbe340
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown
2 files · +14 -22
M objc/objc_runtime_darwin.go +8 -16
| @@ -87,7 +87,7 @@ func createArgs(cls ID, sel SEL, args ...interface{}) (out []uintptr, keepAlive | |||
| 87 | 87 | out = append(out, uintptr(v)) | |
| 88 | 88 | case SEL: | |
| 89 | 89 | out = append(out, uintptr(v)) | |
| 90 | - | case _IMP: | |
| 90 | + | case IMP: | |
| 91 | 91 | out = append(out, uintptr(v)) | |
| 92 | 92 | case bool: | |
| 93 | 93 | if v { | |
| @@ -165,7 +165,7 @@ func (c Class) SuperClass() Class { | |||
| 165 | 165 | // The types argument is a string containing the mapping of parameters and return type. | |
| 166 | 166 | // Since the function must take at least two arguments—self and _cmd, the second and third | |
| 167 | 167 | // characters must be “@:” (the first character is the return type). | |
| 168 | - | func (c Class) AddMethod(name SEL, imp _IMP, types string) bool { | |
| 168 | + | func (c Class) AddMethod(name SEL, imp IMP, types string) bool { | |
| 169 | 169 | t := strings.CString(types) | |
| 170 | 170 | ret, _, _ := purego.SyscallN(class_addMethod, uintptr(c), uintptr(name), uintptr(imp), uintptr(unsafe.Pointer(t))) | |
| 171 | 171 | runtime.KeepAlive(t) | |
| @@ -215,20 +215,12 @@ func (i Ivar) Offset() uintptr { | |||
| 215 | 215 | return ret | |
| 216 | 216 | } | |
| 217 | 217 | ||
| 218 | - | // _IMP is unexported so that the only way to make this type is by providing a Go function and casting | |
| 219 | - | // it with the IMP function | |
| 220 | - | type _IMP uintptr | |
| 218 | + | // IMP is a function pointer that can be called by Objective-C code. | |
| 219 | + | type IMP uintptr | |
| 221 | 220 | ||
| 222 | - | // IMP takes a Go function that takes (ID, SEL) as its first two arguments. It returns an _IMP function | |
| 221 | + | // NewIMP takes a Go function that takes (ID, SEL) as its first two arguments. It returns an IMP function | |
| 223 | 222 | // pointer that can be called by Objective-C code. The function pointer is never deallocated. | |
| 224 | - | func IMP(fn interface{}) _IMP { | |
| 225 | - | // this is only here so that it is easier to port C code to Go. | |
| 226 | - | // this is not guaranteed to be here forever so make sure to port your callbacks to Go | |
| 227 | - | // If you have a C function pointer cast it to a uintptr before passing it | |
| 228 | - | // to this function. | |
| 229 | - | if x, ok := fn.(uintptr); ok { | |
| 230 | - | return _IMP(x) | |
| 231 | - | } | |
| 223 | + | func NewIMP(fn interface{}) IMP { | |
| 232 | 224 | ty := reflect.TypeOf(fn) | |
| 233 | 225 | if ty.Kind() != reflect.Func { | |
| 234 | 226 | panic("objc: not a function") | |
| @@ -241,7 +233,7 @@ func IMP(fn interface{}) _IMP { | |||
| 241 | 233 | case ty.In(0).Kind() != reflect.Uintptr: | |
| 242 | 234 | fallthrough | |
| 243 | 235 | case ty.In(1).Kind() != reflect.Uintptr: | |
| 244 | - | panic("objc: IMP must take a (id, SEL) as its first two arguments") | |
| 236 | + | panic("objc: NewIMP must take a (id, SEL) as its first two arguments") | |
| 245 | 237 | } | |
| 246 | - | return _IMP(purego.NewCallback(fn)) | |
| 238 | + | return IMP(purego.NewCallback(fn)) | |
| 247 | 239 | } | |
| @@ -87,7 +87,7 @@ func createArgs(cls ID, sel SEL, args ...interface{}) (out []uintptr, keepAlive | |||||
| 87 | out = append(out, uintptr(v)) | 87 | out = append(out, uintptr(v)) | ||
| 88 | case SEL: | 88 | case SEL: | ||
| 89 | out = append(out, uintptr(v)) | 89 | out = append(out, uintptr(v)) | ||
| 90 | - | case _IMP: | 90 | + | case IMP: |
| 91 | out = append(out, uintptr(v)) | 91 | out = append(out, uintptr(v)) | ||
| 92 | case bool: | 92 | case bool: | ||
| 93 | if v { | 93 | if v { | ||
| @@ -165,7 +165,7 @@ func (c Class) SuperClass() Class { | |||||
| 165 | // The types argument is a string containing the mapping of parameters and return type. | 165 | // The types argument is a string containing the mapping of parameters and return type. | ||
| 166 | // Since the function must take at least two arguments—self and _cmd, the second and third | 166 | // Since the function must take at least two arguments—self and _cmd, the second and third | ||
| 167 | // characters must be “@:” (the first character is the return type). | 167 | // characters must be “@:” (the first character is the return type). | ||
| 168 | - | func (c Class) AddMethod(name SEL, imp _IMP, types string) bool { | 168 | + | func (c Class) AddMethod(name SEL, imp IMP, types string) bool { |
| 169 | t := strings.CString(types) | 169 | t := strings.CString(types) | ||
| 170 | ret, _, _ := purego.SyscallN(class_addMethod, uintptr(c), uintptr(name), uintptr(imp), uintptr(unsafe.Pointer(t))) | 170 | ret, _, _ := purego.SyscallN(class_addMethod, uintptr(c), uintptr(name), uintptr(imp), uintptr(unsafe.Pointer(t))) | ||
| 171 | runtime.KeepAlive(t) | 171 | runtime.KeepAlive(t) | ||
| @@ -215,20 +215,12 @@ func (i Ivar) Offset() uintptr { | |||||
| 215 | return ret | 215 | return ret | ||
| 216 | } | 216 | } | ||
| 217 | 217 | ||||
| 218 | - | // _IMP is unexported so that the only way to make this type is by providing a Go function and casting | 218 | + | // IMP is a function pointer that can be called by Objective-C code. |
| 219 | - | // it with the IMP function | 219 | + | type IMP uintptr |
| 220 | - | type _IMP uintptr | |||
| 221 | 220 | ||||
| 222 | - | // IMP takes a Go function that takes (ID, SEL) as its first two arguments. It returns an _IMP function | 221 | + | // NewIMP takes a Go function that takes (ID, SEL) as its first two arguments. It returns an IMP function |
| 223 | // pointer that can be called by Objective-C code. The function pointer is never deallocated. | 222 | // pointer that can be called by Objective-C code. The function pointer is never deallocated. | ||
| 224 | - | func IMP(fn interface{}) _IMP { | 223 | + | func NewIMP(fn interface{}) IMP { |
| 225 | - | // this is only here so that it is easier to port C code to Go. | |||
| 226 | - | // this is not guaranteed to be here forever so make sure to port your callbacks to Go | |||
| 227 | - | // If you have a C function pointer cast it to a uintptr before passing it | |||
| 228 | - | // to this function. | |||
| 229 | - | if x, ok := fn.(uintptr); ok { | |||
| 230 | - | return _IMP(x) | |||
| 231 | - | } | |||
| 232 | ty := reflect.TypeOf(fn) | 224 | ty := reflect.TypeOf(fn) | ||
| 233 | if ty.Kind() != reflect.Func { | 225 | if ty.Kind() != reflect.Func { | ||
| 234 | panic("objc: not a function") | 226 | panic("objc: not a function") | ||
| @@ -241,7 +233,7 @@ func IMP(fn interface{}) _IMP { | |||||
| 241 | case ty.In(0).Kind() != reflect.Uintptr: | 233 | case ty.In(0).Kind() != reflect.Uintptr: | ||
| 242 | fallthrough | 234 | fallthrough | ||
| 243 | case ty.In(1).Kind() != reflect.Uintptr: | 235 | case ty.In(1).Kind() != reflect.Uintptr: | ||
| 244 | - | panic("objc: IMP must take a (id, SEL) as its first two arguments") | 236 | + | panic("objc: NewIMP must take a (id, SEL) as its first two arguments") |
| 245 | } | 237 | } | ||
| 246 | - | return _IMP(purego.NewCallback(fn)) | 238 | + | return IMP(purego.NewCallback(fn)) |
| 247 | } | 239 | } | ||
M objc/objc_runtime_darwin_test.go +6 -6
| @@ -22,7 +22,7 @@ func init() { | |||
| 22 | 22 | ||
| 23 | 23 | func ExampleAllocateClassPair() { | |
| 24 | 24 | var class = objc.AllocateClassPair(objc.GetClass("NSObject"), "FooObject", 0) | |
| 25 | - | class.AddMethod(objc.RegisterName("run"), objc.IMP(func(self objc.ID, _cmd objc.SEL) { | |
| 25 | + | class.AddMethod(objc.RegisterName("run"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | |
| 26 | 26 | fmt.Println("Hello World!") | |
| 27 | 27 | }), "v@:") | |
| 28 | 28 | class.Register() | |
| @@ -36,10 +36,10 @@ func ExampleClass_AddIvar() { | |||
| 36 | 36 | var class = objc.AllocateClassPair(objc.GetClass("NSObject"), "BarObject", 0) | |
| 37 | 37 | class.AddIvar("bar", int(0), "q") | |
| 38 | 38 | var barOffset = class.InstanceVariable("bar").Offset() | |
| 39 | - | class.AddMethod(objc.RegisterName("bar"), objc.IMP(func(self objc.ID, _cmd objc.SEL) int { | |
| 39 | + | class.AddMethod(objc.RegisterName("bar"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { | |
| 40 | 40 | return *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset)) | |
| 41 | 41 | }), "q@:") | |
| 42 | - | class.AddMethod(objc.RegisterName("setBar:"), objc.IMP(func(self objc.ID, _cmd objc.SEL, bar int) { | |
| 42 | + | class.AddMethod(objc.RegisterName("setBar:"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL, bar int) { | |
| 43 | 43 | *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset)) = bar | |
| 44 | 44 | }), "v@:q") | |
| 45 | 45 | class.Register() | |
| @@ -52,7 +52,7 @@ func ExampleClass_AddIvar() { | |||
| 52 | 52 | } | |
| 53 | 53 | ||
| 54 | 54 | func ExampleIMP() { | |
| 55 | - | imp := objc.IMP(func(self objc.ID, _cmd objc.SEL) { | |
| 55 | + | imp := objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | |
| 56 | 56 | fmt.Println("IMP:", self, _cmd) | |
| 57 | 57 | }) | |
| 58 | 58 | ||
| @@ -63,13 +63,13 @@ func ExampleIMP() { | |||
| 63 | 63 | ||
| 64 | 64 | func ExampleID_SendSuper() { | |
| 65 | 65 | super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject", 0) | |
| 66 | - | super.AddMethod(objc.RegisterName("doSomething"), objc.IMP(func(self objc.ID, _cmd objc.SEL) { | |
| 66 | + | super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | |
| 67 | 67 | fmt.Println("In Super!") | |
| 68 | 68 | }), "v@:") | |
| 69 | 69 | super.Register() | |
| 70 | 70 | ||
| 71 | 71 | child := objc.AllocateClassPair(super, "ChildObject", 0) | |
| 72 | - | child.AddMethod(objc.RegisterName("doSomething"), objc.IMP(func(self objc.ID, _cmd objc.SEL) { | |
| 72 | + | child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | |
| 73 | 73 | fmt.Println("In Child") | |
| 74 | 74 | self.SendSuper(_cmd) | |
| 75 | 75 | }), "v@:") | |
| @@ -22,7 +22,7 @@ func init() { | |||||
| 22 | 22 | ||||
| 23 | func ExampleAllocateClassPair() { | 23 | func ExampleAllocateClassPair() { | ||
| 24 | var class = objc.AllocateClassPair(objc.GetClass("NSObject"), "FooObject", 0) | 24 | var class = objc.AllocateClassPair(objc.GetClass("NSObject"), "FooObject", 0) | ||
| 25 | - | class.AddMethod(objc.RegisterName("run"), objc.IMP(func(self objc.ID, _cmd objc.SEL) { | 25 | + | class.AddMethod(objc.RegisterName("run"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { |
| 26 | fmt.Println("Hello World!") | 26 | fmt.Println("Hello World!") | ||
| 27 | }), "v@:") | 27 | }), "v@:") | ||
| 28 | class.Register() | 28 | class.Register() | ||
| @@ -36,10 +36,10 @@ func ExampleClass_AddIvar() { | |||||
| 36 | var class = objc.AllocateClassPair(objc.GetClass("NSObject"), "BarObject", 0) | 36 | var class = objc.AllocateClassPair(objc.GetClass("NSObject"), "BarObject", 0) | ||
| 37 | class.AddIvar("bar", int(0), "q") | 37 | class.AddIvar("bar", int(0), "q") | ||
| 38 | var barOffset = class.InstanceVariable("bar").Offset() | 38 | var barOffset = class.InstanceVariable("bar").Offset() | ||
| 39 | - | class.AddMethod(objc.RegisterName("bar"), objc.IMP(func(self objc.ID, _cmd objc.SEL) int { | 39 | + | class.AddMethod(objc.RegisterName("bar"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { |
| 40 | return *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset)) | 40 | return *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset)) | ||
| 41 | }), "q@:") | 41 | }), "q@:") | ||
| 42 | - | class.AddMethod(objc.RegisterName("setBar:"), objc.IMP(func(self objc.ID, _cmd objc.SEL, bar int) { | 42 | + | class.AddMethod(objc.RegisterName("setBar:"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL, bar int) { |
| 43 | *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset)) = bar | 43 | *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset)) = bar | ||
| 44 | }), "v@:q") | 44 | }), "v@:q") | ||
| 45 | class.Register() | 45 | class.Register() | ||
| @@ -52,7 +52,7 @@ func ExampleClass_AddIvar() { | |||||
| 52 | } | 52 | } | ||
| 53 | 53 | ||||
| 54 | func ExampleIMP() { | 54 | func ExampleIMP() { | ||
| 55 | - | imp := objc.IMP(func(self objc.ID, _cmd objc.SEL) { | 55 | + | imp := objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { |
| 56 | fmt.Println("IMP:", self, _cmd) | 56 | fmt.Println("IMP:", self, _cmd) | ||
| 57 | }) | 57 | }) | ||
| 58 | 58 | ||||
| @@ -63,13 +63,13 @@ func ExampleIMP() { | |||||
| 63 | 63 | ||||
| 64 | func ExampleID_SendSuper() { | 64 | func ExampleID_SendSuper() { | ||
| 65 | super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject", 0) | 65 | super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject", 0) | ||
| 66 | - | super.AddMethod(objc.RegisterName("doSomething"), objc.IMP(func(self objc.ID, _cmd objc.SEL) { | 66 | + | super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { |
| 67 | fmt.Println("In Super!") | 67 | fmt.Println("In Super!") | ||
| 68 | }), "v@:") | 68 | }), "v@:") | ||
| 69 | super.Register() | 69 | super.Register() | ||
| 70 | 70 | ||||
| 71 | child := objc.AllocateClassPair(super, "ChildObject", 0) | 71 | child := objc.AllocateClassPair(super, "ChildObject", 0) | ||
| 72 | - | child.AddMethod(objc.RegisterName("doSomething"), objc.IMP(func(self objc.ID, _cmd objc.SEL) { | 72 | + | child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { |
| 73 | fmt.Println("In Child") | 73 | fmt.Println("In Child") | ||
| 74 | self.SendSuper(_cmd) | 74 | self.SendSuper(_cmd) | ||
| 75 | }), "v@:") | 75 | }), "v@:") | ||