From 3e06a2c1bda16fdee1ed0beba9e0f5163a1ec44b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 6 Apr 2024 02:57:27 +0900 Subject: [PATCH] objc: remove deprecated APIs --- objc/objc_runtime_darwin.go | 29 ------- objc/objc_runtime_darwin_test.go | 129 ++++++++++++++++++++++--------- 2 files changed, 92 insertions(+), 66 deletions(-) diff --git a/objc/objc_runtime_darwin.go b/objc/objc_runtime_darwin.go index b6855fd7..73e1e881 100644 --- a/objc/objc_runtime_darwin.go +++ b/objc/objc_runtime_darwin.go @@ -193,13 +193,6 @@ func GetClass(name string) Class { return objc_getClass(name) } -// AllocateClassPair creates a new class and metaclass. Then returns the new class, or Nil if the class could not be created -// -// Deprecated: use RegisterClass instead -func AllocateClassPair(super Class, name string, extraBytes uintptr) Class { - return objc_allocateClassPair(super, name, extraBytes) -} - // MethodDef represents the Go function and the selector that ObjC uses to access that function. type MethodDef struct { Cmd SEL @@ -506,20 +499,6 @@ func (c Class) AddMethod(name SEL, imp IMP, types string) bool { return class_addMethod(c, name, imp, types) } -// AddIvar adds a new instance variable to a class. -// It may only be called after AllocateClassPair and before Register. -// Adding an instance variable to an existing class is not supported. -// The class must not be a metaclass. Adding an instance variable to a metaclass is not supported. -// It takes the instance of the type of the Ivar and a string representing the type. -// -// Deprecated: use RegisterClass instead -func (c Class) AddIvar(name string, ty interface{}, types string) bool { - typeOf := reflect.TypeOf(ty) - size := typeOf.Size() - alignment := uint8(math.Log2(float64(typeOf.Align()))) - return class_addIvar(c, name, size, alignment, types) -} - // AddProtocol adds a protocol to a class. // Returns true if the protocol was added successfully, otherwise false (for example, // the class already conforms to that protocol). @@ -537,14 +516,6 @@ func (c Class) InstanceVariable(name string) Ivar { return class_getInstanceVariable(c, name) } -// Register registers a class that was allocated using AllocateClassPair. -// It can now be used to make objects by sending it either alloc and init or new. -// -// Deprecated: use RegisterClass instead -func (c Class) Register() { - objc_registerClassPair(c) -} - // Ivar an opaque type that represents an instance variable. type Ivar uintptr diff --git a/objc/objc_runtime_darwin_test.go b/objc/objc_runtime_darwin_test.go index ba2e7b70..9aa35d2d 100644 --- a/objc/objc_runtime_darwin_test.go +++ b/objc/objc_runtime_darwin_test.go @@ -12,15 +12,27 @@ import ( "github.com/ebitengine/purego/objc" ) -func ExampleAllocateClassPair() { - class := objc.AllocateClassPair(objc.GetClass("NSObject"), "FooObject", 0) - class.AddMethod(objc.RegisterName("run"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { - fmt.Println("Hello World!") - }), "v@:") - class.Register() - - fooObject := objc.ID(class).Send(objc.RegisterName("new")) - fooObject.Send(objc.RegisterName("run")) +func ExampleRegisterClass_helloworld() { + class, err := objc.RegisterClass( + "FooObject", + objc.GetClass("NSObject"), + nil, + nil, + []objc.MethodDef{ + { + Cmd: objc.RegisterName("run"), + Fn: func(self objc.ID, _cmd objc.SEL) { + fmt.Println("Hello World!") + }, + }, + }, + ) + if err != nil { + panic(err) + } + + object := objc.ID(class).Send(objc.RegisterName("new")) + object.Send(objc.RegisterName("run")) // Output: Hello World! } @@ -78,26 +90,48 @@ func ExampleIMP() { }) purego.SyscallN(uintptr(imp), 105, 567, 9, 2, 3, ^uintptr(4), 4, 8, 9) - // Output: IMP: 105 567 9 2 3 -5 4 8 9 } func ExampleID_SendSuper() { - super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject", 0) - super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { - fmt.Println("In Super!") - }), "v@:") - super.Register() - - child := objc.AllocateClassPair(super, "ChildObject", 0) - child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) { - fmt.Println("In Child") - self.SendSuper(_cmd) - }), "v@:") - child.Register() + super, err := objc.RegisterClass( + "SuperObject", + objc.GetClass("NSObject"), + nil, + nil, + []objc.MethodDef{ + { + Cmd: objc.RegisterName("doSomething"), + Fn: func(self objc.ID, _cmd objc.SEL) { + fmt.Println("In Super!") + }, + }, + }, + ) + if err != nil { + panic(err) + } - objc.ID(child).Send(objc.RegisterName("new")).Send(objc.RegisterName("doSomething")) + child, err := objc.RegisterClass( + "ChildObject", + super, + nil, + nil, + []objc.MethodDef{ + { + Cmd: objc.RegisterName("doSomething"), + Fn: func(self objc.ID, _cmd objc.SEL) { + fmt.Println("In Child") + self.SendSuper(_cmd) + }, + }, + }, + ) + if err != nil { + panic(err) + } + objc.ID(child).Send(objc.RegisterName("new")).Send(objc.RegisterName("doSomething")) // Output: In Child // In Super! } @@ -129,27 +163,48 @@ func ExampleSend() { subString := objc.ID(class_NSString).Send(sel_stringWithUTF8String, "lo, Wor\x00") r := objc.Send[NSRange](fullString, objc.RegisterName("rangeOfString:"), subString) - fmt.Println(r) - // Output: {3 7} } func ExampleSendSuper() { - super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject2", 0) - super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { - return 16 - }), "i@:") - super.Register() - - child := objc.AllocateClassPair(super, "ChildObject2", 0) - child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) int { - return 24 - }), "i@:") - child.Register() + super, err := objc.RegisterClass( + "SuperObject2", + objc.GetClass("NSObject"), + nil, + nil, + []objc.MethodDef{ + { + Cmd: objc.RegisterName("doSomething"), + Fn: func(self objc.ID, _cmd objc.SEL) int { + return 16 + }, + }, + }, + ) + if err != nil { + panic(err) + } - res := objc.SendSuper[int](objc.ID(child).Send(objc.RegisterName("new")), objc.RegisterName("doSomething")) + child, err := objc.RegisterClass( + "ChildObject2", + super, + nil, + nil, + []objc.MethodDef{ + { + Cmd: objc.RegisterName("doSomething"), + Fn: func(self objc.ID, _cmd objc.SEL) int { + return 24 + }, + }, + }, + ) + if err != nil { + panic(err) + } + res := objc.SendSuper[int](objc.ID(child).Send(objc.RegisterName("new")), objc.RegisterName("doSomething")) fmt.Println(res) // Output: 16 }