detached ffec451

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 Unverified4AEE18F83AFDEB23 The 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
8787 out = append(out, uintptr(v))
8888 case SEL:
8989 out = append(out, uintptr(v))
90- case _IMP:
90+ case IMP:
9191 out = append(out, uintptr(v))
9292 case bool:
9393 if v {
@@ -165,7 +165,7 @@ func (c Class) SuperClass() Class {
165165 // The types argument is a string containing the mapping of parameters and return type.
166166 // Since the function must take at least two arguments—self and _cmd, the second and third
167167 // 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 {
169169 t := strings.CString(types)
170170 ret, _, _ := purego.SyscallN(class_addMethod, uintptr(c), uintptr(name), uintptr(imp), uintptr(unsafe.Pointer(t)))
171171 runtime.KeepAlive(t)
@@ -215,20 +215,12 @@ func (i Ivar) Offset() uintptr {
215215 return ret
216216 }
217217
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
221220
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
223222 // 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 {
232224 ty := reflect.TypeOf(fn)
233225 if ty.Kind() != reflect.Func {
234226 panic("objc: not a function")
@@ -241,7 +233,7 @@ func IMP(fn interface{}) _IMP {
241233 case ty.In(0).Kind() != reflect.Uintptr:
242234 fallthrough
243235 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")
245237 }
246- return _IMP(purego.NewCallback(fn))
238+ return IMP(purego.NewCallback(fn))
247239 }
@@ -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 third166 // 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 ret215 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 casting218+// IMP is a function pointer that can be called by Objective-C code.
219-// it with the IMP function219+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 function221+// 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 fallthrough234 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() {
2222
2323 func ExampleAllocateClassPair() {
2424 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) {
2626 fmt.Println("Hello World!")
2727 }), "v@:")
2828 class.Register()
@@ -36,10 +36,10 @@ func ExampleClass_AddIvar() {
3636 var class = objc.AllocateClassPair(objc.GetClass("NSObject"), "BarObject", 0)
3737 class.AddIvar("bar", int(0), "q")
3838 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 {
4040 return *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset))
4141 }), "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) {
4343 *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(self)) + barOffset)) = bar
4444 }), "v@:q")
4545 class.Register()
@@ -52,7 +52,7 @@ func ExampleClass_AddIvar() {
5252 }
5353
5454 func ExampleIMP() {
55- imp := objc.IMP(func(self objc.ID, _cmd objc.SEL) {
55+ imp := objc.NewIMP(func(self objc.ID, _cmd objc.SEL) {
5656 fmt.Println("IMP:", self, _cmd)
5757 })
5858
@@ -63,13 +63,13 @@ func ExampleIMP() {
6363
6464 func ExampleID_SendSuper() {
6565 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) {
6767 fmt.Println("In Super!")
6868 }), "v@:")
6969 super.Register()
7070
7171 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) {
7373 fmt.Println("In Child")
7474 self.SendSuper(_cmd)
7575 }), "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)) = bar43 *(*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@:")

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close