Skip to content

Commit

Permalink
objc: remove deprecated APIs (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi authored Apr 6, 2024
1 parent d0aedd0 commit f88cc4c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 66 deletions.
29 changes: 0 additions & 29 deletions objc/objc_runtime_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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

Expand Down
129 changes: 92 additions & 37 deletions objc/objc_runtime_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}

Expand Down Expand Up @@ -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!
}
Expand Down Expand Up @@ -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
}

0 comments on commit f88cc4c

Please sign in to comment.