objc: remove deprecated APIs (#226)
- commit
- f88cc4c6b4837eae57b41a0354377246e0920a6b
- author
- Hajime Hoshi · · 2024-04-06 11:50 UTC
- committer
- GitHub · · 2024-04-06 11:50 UTC
- parent
- d0aedd0
- signature
- GPG Unverified
B5690EEEBB952194The key is unknown
2 files · +92 -66
M objc/objc_runtime_darwin.go +0 -29
| @@ -193,13 +193,6 @@ func GetClass(name string) Class { | |||
| 193 | 193 | return objc_getClass(name) | |
| 194 | 194 | } | |
| 195 | 195 | ||
| 196 | - | // AllocateClassPair creates a new class and metaclass. Then returns the new class, or Nil if the class could not be created | |
| 197 | - | // | |
| 198 | - | // Deprecated: use RegisterClass instead | |
| 199 | - | func AllocateClassPair(super Class, name string, extraBytes uintptr) Class { | |
| 200 | - | return objc_allocateClassPair(super, name, extraBytes) | |
| 201 | - | } | |
| 202 | - | ||
| 203 | 196 | // MethodDef represents the Go function and the selector that ObjC uses to access that function. | |
| 204 | 197 | type MethodDef struct { | |
| 205 | 198 | Cmd SEL | |
| @@ -506,20 +499,6 @@ func (c Class) AddMethod(name SEL, imp IMP, types string) bool { | |||
| 506 | 499 | return class_addMethod(c, name, imp, types) | |
| 507 | 500 | } | |
| 508 | 501 | ||
| 509 | - | // AddIvar adds a new instance variable to a class. | |
| 510 | - | // It may only be called after AllocateClassPair and before Register. | |
| 511 | - | // Adding an instance variable to an existing class is not supported. | |
| 512 | - | // The class must not be a metaclass. Adding an instance variable to a metaclass is not supported. | |
| 513 | - | // It takes the instance of the type of the Ivar and a string representing the type. | |
| 514 | - | // | |
| 515 | - | // Deprecated: use RegisterClass instead | |
| 516 | - | func (c Class) AddIvar(name string, ty interface{}, types string) bool { | |
| 517 | - | typeOf := reflect.TypeOf(ty) | |
| 518 | - | size := typeOf.Size() | |
| 519 | - | alignment := uint8(math.Log2(float64(typeOf.Align()))) | |
| 520 | - | return class_addIvar(c, name, size, alignment, types) | |
| 521 | - | } | |
| 522 | - | ||
| 523 | 502 | // AddProtocol adds a protocol to a class. | |
| 524 | 503 | // Returns true if the protocol was added successfully, otherwise false (for example, | |
| 525 | 504 | // the class already conforms to that protocol). | |
| @@ -537,14 +516,6 @@ func (c Class) InstanceVariable(name string) Ivar { | |||
| 537 | 516 | return class_getInstanceVariable(c, name) | |
| 538 | 517 | } | |
| 539 | 518 | ||
| 540 | - | // Register registers a class that was allocated using AllocateClassPair. | |
| 541 | - | // It can now be used to make objects by sending it either alloc and init or new. | |
| 542 | - | // | |
| 543 | - | // Deprecated: use RegisterClass instead | |
| 544 | - | func (c Class) Register() { | |
| 545 | - | objc_registerClassPair(c) | |
| 546 | - | } | |
| 547 | - | ||
| 548 | 519 | // Ivar an opaque type that represents an instance variable. | |
| 549 | 520 | type Ivar uintptr | |
| 550 | 521 | ||
| @@ -193,13 +193,6 @@ func GetClass(name string) Class { | |||||
| 193 | return objc_getClass(name) | 193 | return objc_getClass(name) | ||
| 194 | } | 194 | } | ||
| 195 | 195 | ||||
| 196 | - | // AllocateClassPair creates a new class and metaclass. Then returns the new class, or Nil if the class could not be created | |||
| 197 | - | // | |||
| 198 | - | // Deprecated: use RegisterClass instead | |||
| 199 | - | func AllocateClassPair(super Class, name string, extraBytes uintptr) Class { | |||
| 200 | - | return objc_allocateClassPair(super, name, extraBytes) | |||
| 201 | - | } | |||
| 202 | - | ||||
| 203 | // MethodDef represents the Go function and the selector that ObjC uses to access that function. | 196 | // MethodDef represents the Go function and the selector that ObjC uses to access that function. | ||
| 204 | type MethodDef struct { | 197 | type MethodDef struct { | ||
| 205 | Cmd SEL | 198 | Cmd SEL | ||
| @@ -506,20 +499,6 @@ func (c Class) AddMethod(name SEL, imp IMP, types string) bool { | |||||
| 506 | return class_addMethod(c, name, imp, types) | 499 | return class_addMethod(c, name, imp, types) | ||
| 507 | } | 500 | } | ||
| 508 | 501 | ||||
| 509 | - | // AddIvar adds a new instance variable to a class. | |||
| 510 | - | // It may only be called after AllocateClassPair and before Register. | |||
| 511 | - | // Adding an instance variable to an existing class is not supported. | |||
| 512 | - | // The class must not be a metaclass. Adding an instance variable to a metaclass is not supported. | |||
| 513 | - | // It takes the instance of the type of the Ivar and a string representing the type. | |||
| 514 | - | // | |||
| 515 | - | // Deprecated: use RegisterClass instead | |||
| 516 | - | func (c Class) AddIvar(name string, ty interface{}, types string) bool { | |||
| 517 | - | typeOf := reflect.TypeOf(ty) | |||
| 518 | - | size := typeOf.Size() | |||
| 519 | - | alignment := uint8(math.Log2(float64(typeOf.Align()))) | |||
| 520 | - | return class_addIvar(c, name, size, alignment, types) | |||
| 521 | - | } | |||
| 522 | - | ||||
| 523 | // AddProtocol adds a protocol to a class. | 502 | // AddProtocol adds a protocol to a class. | ||
| 524 | // Returns true if the protocol was added successfully, otherwise false (for example, | 503 | // Returns true if the protocol was added successfully, otherwise false (for example, | ||
| 525 | // the class already conforms to that protocol). | 504 | // the class already conforms to that protocol). | ||
| @@ -537,14 +516,6 @@ func (c Class) InstanceVariable(name string) Ivar { | |||||
| 537 | return class_getInstanceVariable(c, name) | 516 | return class_getInstanceVariable(c, name) | ||
| 538 | } | 517 | } | ||
| 539 | 518 | ||||
| 540 | - | // Register registers a class that was allocated using AllocateClassPair. | |||
| 541 | - | // It can now be used to make objects by sending it either alloc and init or new. | |||
| 542 | - | // | |||
| 543 | - | // Deprecated: use RegisterClass instead | |||
| 544 | - | func (c Class) Register() { | |||
| 545 | - | objc_registerClassPair(c) | |||
| 546 | - | } | |||
| 547 | - | ||||
| 548 | // Ivar an opaque type that represents an instance variable. | 519 | // Ivar an opaque type that represents an instance variable. | ||
| 549 | type Ivar uintptr | 520 | type Ivar uintptr | ||
| 550 | 521 | ||||
M objc/objc_runtime_darwin_test.go +92 -37
| @@ -12,15 +12,27 @@ import ( | |||
| 12 | 12 | "github.com/ebitengine/purego/objc" | |
| 13 | 13 | ) | |
| 14 | 14 | ||
| 15 | - | func ExampleAllocateClassPair() { | |
| 16 | - | class := objc.AllocateClassPair(objc.GetClass("NSObject"), "FooObject", 0) | |
| 17 | - | class.AddMethod(objc.RegisterName("run"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | |
| 18 | - | fmt.Println("Hello World!") | |
| 19 | - | }), "v@:") | |
| 20 | - | class.Register() | |
| 21 | - | ||
| 22 | - | fooObject := objc.ID(class).Send(objc.RegisterName("new")) | |
| 23 | - | fooObject.Send(objc.RegisterName("run")) | |
| 15 | + | func ExampleRegisterClass_helloworld() { | |
| 16 | + | class, err := objc.RegisterClass( | |
| 17 | + | "FooObject", | |
| 18 | + | objc.GetClass("NSObject"), | |
| 19 | + | nil, | |
| 20 | + | nil, | |
| 21 | + | []objc.MethodDef{ | |
| 22 | + | { | |
| 23 | + | Cmd: objc.RegisterName("run"), | |
| 24 | + | Fn: func(self objc.ID, _cmd objc.SEL) { | |
| 25 | + | fmt.Println("Hello World!") | |
| 26 | + | }, | |
| 27 | + | }, | |
| 28 | + | }, | |
| 29 | + | ) | |
| 30 | + | if err != nil { | |
| 31 | + | panic(err) | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | object := objc.ID(class).Send(objc.RegisterName("new")) | |
| 35 | + | object.Send(objc.RegisterName("run")) | |
| 24 | 36 | // Output: Hello World! | |
| 25 | 37 | } | |
| 26 | 38 | ||
| @@ -78,26 +90,48 @@ func ExampleIMP() { | |||
| 78 | 90 | }) | |
| 79 | 91 | ||
| 80 | 92 | purego.SyscallN(uintptr(imp), 105, 567, 9, 2, 3, ^uintptr(4), 4, 8, 9) | |
| 81 | - | ||
| 82 | 93 | // Output: IMP: 105 567 9 2 3 -5 4 8 9 | |
| 83 | 94 | } | |
| 84 | 95 | ||
| 85 | 96 | func ExampleID_SendSuper() { | |
| 86 | - | super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject", 0) | |
| 87 | - | super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | |
| 88 | - | fmt.Println("In Super!") | |
| 89 | - | }), "v@:") | |
| 90 | - | super.Register() | |
| 91 | - | ||
| 92 | - | child := objc.AllocateClassPair(super, "ChildObject", 0) | |
| 93 | - | child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | |
| 94 | - | fmt.Println("In Child") | |
| 95 | - | self.SendSuper(_cmd) | |
| 96 | - | }), "v@:") | |
| 97 | - | child.Register() | |
| 97 | + | super, err := objc.RegisterClass( | |
| 98 | + | "SuperObject", | |
| 99 | + | objc.GetClass("NSObject"), | |
| 100 | + | nil, | |
| 101 | + | nil, | |
| 102 | + | []objc.MethodDef{ | |
| 103 | + | { | |
| 104 | + | Cmd: objc.RegisterName("doSomething"), | |
| 105 | + | Fn: func(self objc.ID, _cmd objc.SEL) { | |
| 106 | + | fmt.Println("In Super!") | |
| 107 | + | }, | |
| 108 | + | }, | |
| 109 | + | }, | |
| 110 | + | ) | |
| 111 | + | if err != nil { | |
| 112 | + | panic(err) | |
| 113 | + | } | |
| 98 | 114 | ||
| 99 | - | objc.ID(child).Send(objc.RegisterName("new")).Send(objc.RegisterName("doSomething")) | |
| 115 | + | child, err := objc.RegisterClass( | |
| 116 | + | "ChildObject", | |
| 117 | + | super, | |
| 118 | + | nil, | |
| 119 | + | nil, | |
| 120 | + | []objc.MethodDef{ | |
| 121 | + | { | |
| 122 | + | Cmd: objc.RegisterName("doSomething"), | |
| 123 | + | Fn: func(self objc.ID, _cmd objc.SEL) { | |
| 124 | + | fmt.Println("In Child") | |
| 125 | + | self.SendSuper(_cmd) | |
| 126 | + | }, | |
| 127 | + | }, | |
| 128 | + | }, | |
| 129 | + | ) | |
| 130 | + | if err != nil { | |
| 131 | + | panic(err) | |
| 132 | + | } | |
| 100 | 133 | ||
| 134 | + | objc.ID(child).Send(objc.RegisterName("new")).Send(objc.RegisterName("doSomething")) | |
| 101 | 135 | // Output: In Child | |
| 102 | 136 | // In Super! | |
| 103 | 137 | } | |
| @@ -129,27 +163,48 @@ func ExampleSend() { | |||
| 129 | 163 | subString := objc.ID(class_NSString).Send(sel_stringWithUTF8String, "lo, Wor\x00") | |
| 130 | 164 | ||
| 131 | 165 | r := objc.Send[NSRange](fullString, objc.RegisterName("rangeOfString:"), subString) | |
| 132 | - | ||
| 133 | 166 | fmt.Println(r) | |
| 134 | - | ||
| 135 | 167 | // Output: {3 7} | |
| 136 | 168 | } | |
| 137 | 169 | ||
| 138 | 170 | func ExampleSendSuper() { | |
| 139 | - | super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject2", 0) | |
| 140 | - | super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { | |
| 141 | - | return 16 | |
| 142 | - | }), "i@:") | |
| 143 | - | super.Register() | |
| 144 | - | ||
| 145 | - | child := objc.AllocateClassPair(super, "ChildObject2", 0) | |
| 146 | - | child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { | |
| 147 | - | return 24 | |
| 148 | - | }), "i@:") | |
| 149 | - | child.Register() | |
| 171 | + | super, err := objc.RegisterClass( | |
| 172 | + | "SuperObject2", | |
| 173 | + | objc.GetClass("NSObject"), | |
| 174 | + | nil, | |
| 175 | + | nil, | |
| 176 | + | []objc.MethodDef{ | |
| 177 | + | { | |
| 178 | + | Cmd: objc.RegisterName("doSomething"), | |
| 179 | + | Fn: func(self objc.ID, _cmd objc.SEL) int { | |
| 180 | + | return 16 | |
| 181 | + | }, | |
| 182 | + | }, | |
| 183 | + | }, | |
| 184 | + | ) | |
| 185 | + | if err != nil { | |
| 186 | + | panic(err) | |
| 187 | + | } | |
| 150 | 188 | ||
| 151 | - | res := objc.SendSuper[int](objc.ID(child).Send(objc.RegisterName("new")), objc.RegisterName("doSomething")) | |
| 189 | + | child, err := objc.RegisterClass( | |
| 190 | + | "ChildObject2", | |
| 191 | + | super, | |
| 192 | + | nil, | |
| 193 | + | nil, | |
| 194 | + | []objc.MethodDef{ | |
| 195 | + | { | |
| 196 | + | Cmd: objc.RegisterName("doSomething"), | |
| 197 | + | Fn: func(self objc.ID, _cmd objc.SEL) int { | |
| 198 | + | return 24 | |
| 199 | + | }, | |
| 200 | + | }, | |
| 201 | + | }, | |
| 202 | + | ) | |
| 203 | + | if err != nil { | |
| 204 | + | panic(err) | |
| 205 | + | } | |
| 152 | 206 | ||
| 207 | + | res := objc.SendSuper[int](objc.ID(child).Send(objc.RegisterName("new")), objc.RegisterName("doSomething")) | |
| 153 | 208 | fmt.Println(res) | |
| 154 | 209 | // Output: 16 | |
| 155 | 210 | } | |
| @@ -12,15 +12,27 @@ import ( | |||||
| 12 | "github.com/ebitengine/purego/objc" | 12 | "github.com/ebitengine/purego/objc" | ||
| 13 | ) | 13 | ) | ||
| 14 | 14 | ||||
| 15 | - | func ExampleAllocateClassPair() { | 15 | + | func ExampleRegisterClass_helloworld() { |
| 16 | - | class := objc.AllocateClassPair(objc.GetClass("NSObject"), "FooObject", 0) | 16 | + | class, err := objc.RegisterClass( |
| 17 | - | class.AddMethod(objc.RegisterName("run"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | 17 | + | "FooObject", |
| 18 | - | fmt.Println("Hello World!") | 18 | + | objc.GetClass("NSObject"), |
| 19 | - | }), "v@:") | 19 | + | nil, |
| 20 | - | class.Register() | 20 | + | nil, |
| 21 | - | 21 | + | []objc.MethodDef{ | |
| 22 | - | fooObject := objc.ID(class).Send(objc.RegisterName("new")) | 22 | + | { |
| 23 | - | fooObject.Send(objc.RegisterName("run")) | 23 | + | Cmd: objc.RegisterName("run"), |
| 24 | + | Fn: func(self objc.ID, _cmd objc.SEL) { | |||
| 25 | + | fmt.Println("Hello World!") | |||
| 26 | + | }, | |||
| 27 | + | }, | |||
| 28 | + | }, | |||
| 29 | + | ) | |||
| 30 | + | if err != nil { | |||
| 31 | + | panic(err) | |||
| 32 | + | } | |||
| 33 | + | ||||
| 34 | + | object := objc.ID(class).Send(objc.RegisterName("new")) | |||
| 35 | + | object.Send(objc.RegisterName("run")) | |||
| 24 | // Output: Hello World! | 36 | // Output: Hello World! | ||
| 25 | } | 37 | } | ||
| 26 | 38 | ||||
| @@ -78,26 +90,48 @@ func ExampleIMP() { | |||||
| 78 | }) | 90 | }) | ||
| 79 | 91 | ||||
| 80 | purego.SyscallN(uintptr(imp), 105, 567, 9, 2, 3, ^uintptr(4), 4, 8, 9) | 92 | purego.SyscallN(uintptr(imp), 105, 567, 9, 2, 3, ^uintptr(4), 4, 8, 9) | ||
| 81 | - | ||||
| 82 | // Output: IMP: 105 567 9 2 3 -5 4 8 9 | 93 | // Output: IMP: 105 567 9 2 3 -5 4 8 9 | ||
| 83 | } | 94 | } | ||
| 84 | 95 | ||||
| 85 | func ExampleID_SendSuper() { | 96 | func ExampleID_SendSuper() { | ||
| 86 | - | super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject", 0) | 97 | + | super, err := objc.RegisterClass( |
| 87 | - | super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | 98 | + | "SuperObject", |
| 88 | - | fmt.Println("In Super!") | 99 | + | objc.GetClass("NSObject"), |
| 89 | - | }), "v@:") | 100 | + | nil, |
| 90 | - | super.Register() | 101 | + | nil, |
| 91 | - | 102 | + | []objc.MethodDef{ | |
| 92 | - | child := objc.AllocateClassPair(super, "ChildObject", 0) | 103 | + | { |
| 93 | - | child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { | 104 | + | Cmd: objc.RegisterName("doSomething"), |
| 94 | - | fmt.Println("In Child") | 105 | + | Fn: func(self objc.ID, _cmd objc.SEL) { |
| 95 | - | self.SendSuper(_cmd) | 106 | + | fmt.Println("In Super!") |
| 96 | - | }), "v@:") | 107 | + | }, |
| 97 | - | child.Register() | 108 | + | }, |
| 109 | + | }, | |||
| 110 | + | ) | |||
| 111 | + | if err != nil { | |||
| 112 | + | panic(err) | |||
| 113 | + | } | |||
| 98 | 114 | ||||
| 99 | - | objc.ID(child).Send(objc.RegisterName("new")).Send(objc.RegisterName("doSomething")) | 115 | + | child, err := objc.RegisterClass( |
| 116 | + | "ChildObject", | |||
| 117 | + | super, | |||
| 118 | + | nil, | |||
| 119 | + | nil, | |||
| 120 | + | []objc.MethodDef{ | |||
| 121 | + | { | |||
| 122 | + | Cmd: objc.RegisterName("doSomething"), | |||
| 123 | + | Fn: func(self objc.ID, _cmd objc.SEL) { | |||
| 124 | + | fmt.Println("In Child") | |||
| 125 | + | self.SendSuper(_cmd) | |||
| 126 | + | }, | |||
| 127 | + | }, | |||
| 128 | + | }, | |||
| 129 | + | ) | |||
| 130 | + | if err != nil { | |||
| 131 | + | panic(err) | |||
| 132 | + | } | |||
| 100 | 133 | ||||
| 134 | + | objc.ID(child).Send(objc.RegisterName("new")).Send(objc.RegisterName("doSomething")) | |||
| 101 | // Output: In Child | 135 | // Output: In Child | ||
| 102 | // In Super! | 136 | // In Super! | ||
| 103 | } | 137 | } | ||
| @@ -129,27 +163,48 @@ func ExampleSend() { | |||||
| 129 | subString := objc.ID(class_NSString).Send(sel_stringWithUTF8String, "lo, Wor\x00") | 163 | subString := objc.ID(class_NSString).Send(sel_stringWithUTF8String, "lo, Wor\x00") | ||
| 130 | 164 | ||||
| 131 | r := objc.Send[NSRange](fullString, objc.RegisterName("rangeOfString:"), subString) | 165 | r := objc.Send[NSRange](fullString, objc.RegisterName("rangeOfString:"), subString) | ||
| 132 | - | ||||
| 133 | fmt.Println(r) | 166 | fmt.Println(r) | ||
| 134 | - | ||||
| 135 | // Output: {3 7} | 167 | // Output: {3 7} | ||
| 136 | } | 168 | } | ||
| 137 | 169 | ||||
| 138 | func ExampleSendSuper() { | 170 | func ExampleSendSuper() { | ||
| 139 | - | super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject2", 0) | 171 | + | super, err := objc.RegisterClass( |
| 140 | - | super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { | 172 | + | "SuperObject2", |
| 141 | - | return 16 | 173 | + | objc.GetClass("NSObject"), |
| 142 | - | }), "i@:") | 174 | + | nil, |
| 143 | - | super.Register() | 175 | + | nil, |
| 144 | - | 176 | + | []objc.MethodDef{ | |
| 145 | - | child := objc.AllocateClassPair(super, "ChildObject2", 0) | 177 | + | { |
| 146 | - | child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { | 178 | + | Cmd: objc.RegisterName("doSomething"), |
| 147 | - | return 24 | 179 | + | Fn: func(self objc.ID, _cmd objc.SEL) int { |
| 148 | - | }), "i@:") | 180 | + | return 16 |
| 149 | - | child.Register() | 181 | + | }, |
| 182 | + | }, | |||
| 183 | + | }, | |||
| 184 | + | ) | |||
| 185 | + | if err != nil { | |||
| 186 | + | panic(err) | |||
| 187 | + | } | |||
| 150 | 188 | ||||
| 151 | - | res := objc.SendSuper[int](objc.ID(child).Send(objc.RegisterName("new")), objc.RegisterName("doSomething")) | 189 | + | child, err := objc.RegisterClass( |
| 190 | + | "ChildObject2", | |||
| 191 | + | super, | |||
| 192 | + | nil, | |||
| 193 | + | nil, | |||
| 194 | + | []objc.MethodDef{ | |||
| 195 | + | { | |||
| 196 | + | Cmd: objc.RegisterName("doSomething"), | |||
| 197 | + | Fn: func(self objc.ID, _cmd objc.SEL) int { | |||
| 198 | + | return 24 | |||
| 199 | + | }, | |||
| 200 | + | }, | |||
| 201 | + | }, | |||
| 202 | + | ) | |||
| 203 | + | if err != nil { | |||
| 204 | + | panic(err) | |||
| 205 | + | } | |||
| 152 | 206 | ||||
| 207 | + | res := objc.SendSuper[int](objc.ID(child).Send(objc.RegisterName("new")), objc.RegisterName("doSomething")) | |||
| 153 | fmt.Println(res) | 208 | fmt.Println(res) | ||
| 154 | // Output: 16 | 209 | // Output: 16 | ||
| 155 | } | 210 | } | ||