Skip to content

Commit

Permalink
objc: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Apr 6, 2024
1 parent 470e3bd commit c047aff
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
2 changes: 1 addition & 1 deletion objc/objc_runtime_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ type IMP uintptr
func NewIMP(fn interface{}) IMP {
ty := reflect.TypeOf(fn)
if ty.Kind() != reflect.Func {
panic("objc: not a function")
panic(fmt.Sprintf("objc: not a function"))
}
// IMP is stricter than a normal callback
// id (*IMP)(id, SEL, ...)
Expand Down
112 changes: 109 additions & 3 deletions objc/objc_runtime_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ import (
"github.com/ebitengine/purego/objc"
)

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!
}

func ExampleRegisterClass() {
var (
sel_new = objc.RegisterName("new")
Expand Down Expand Up @@ -66,10 +90,52 @@ 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, 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)
}

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!
}

func TestSend(t *testing.T) {
// NSNumber comes from Foundation so make sure we have linked to that framework.
_, err := purego.Dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", purego.RTLD_GLOBAL)
Expand Down Expand Up @@ -97,8 +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, 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)
}

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 c047aff

Please sign in to comment.