diff --git a/darwintest/newtypes.go b/darwintest/newtypes.go index eface0d..f054a54 100644 --- a/darwintest/newtypes.go +++ b/darwintest/newtypes.go @@ -13,30 +13,34 @@ import ( // extern void windowShouldClose(id, SEL, id); // extern void buttonClicked(id, SEL, id); // extern void gotNotification(id, SEL, id); -// /* cgo doesn't like nil or Nil */ -// extern Class NilClass; /* in runtimetest.go because of cgo limitations */ -// extern id Nilid; +// /* because cgo doesn't like Nil */ +// extern Class NilClass; /* defined in runtimetest.go due to cgo limitations */ import "C" -var NSObject = C.object_getClass(objc_getClass("NSObject")) +var ( + _NSObject_Class = C.object_getClass(_NSObject) +) func newClass(name string) C.Class { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - c := C.objc_allocateClassPair(NSObject, cname, 0) + c := C.objc_allocateClassPair(_NSObject_Class, cname, 0) if c == C.NilClass { panic("unable to create Objective-C class " + name) } return c } +// TODO move these around later +var ( + _stop = sel_getUid("stop:") +) + //export windowShouldClose func windowShouldClose(self C.id, sel C.SEL, sender C.id) { fmt.Println("-[hello windowShouldClose:]") - C.objc_msgSend_id(NSApp, - sel_getUid("stop:"), - sender) + C.objc_msgSend_id(NSApp, _stop, sender) } //export buttonClicked @@ -47,11 +51,7 @@ func buttonClicked(self C.id, sel C.SEL, sender C.id) { //export gotNotification func gotNotification(self C.id, sel C.SEL, object C.id) { - source := (*C.char)(unsafe.Pointer( - C.objc_msgSend_noargs(object, - sel_getUid("UTF8String")))) - fmt.Println("got notification from %s", - C.GoString(source)) + fmt.Printf("got notification from %s\n", fromNSString(object)) } func addOurMethod(class C.Class, sel C.SEL, imp C.IMP) { diff --git a/darwintest/objc_darwin.go b/darwintest/objc_darwin.go index ce6b87b..524d9f7 100644 --- a/darwintest/objc_darwin.go +++ b/darwintest/objc_darwin.go @@ -26,6 +26,7 @@ func sel_getUid(sel string) C.SEL { // Common Objective-C types and selectors. var ( + _NSObject = objc_getClass("NSObject") _NSString = objc_getClass("NSString") _alloc = sel_getUid("alloc") diff --git a/darwintest/runtimetest.go b/darwintest/runtimetest.go index 1b0b825..aafc074 100644 --- a/darwintest/runtimetest.go +++ b/darwintest/runtimetest.go @@ -9,8 +9,7 @@ import ( // #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit // #include // #include "objc_darwin.h" -// Class NilClass = Nil; /* for newtypes.go */ -// id Nilid = nil; +// Class NilClass = Nil; /* for newtypes.go; here due to cgo limitations */ import "C" var (