From 56a436bc804539db271471b357c5b3e5d6465238 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 28 Feb 2014 17:20:22 -0500 Subject: [PATCH] Changed the main thread signaling to use NSObject's performSelectorOnMainThread: functionality, which settles that. NSString requires an autorelease pool; in testing, so does NSValue, which we are eventually going to use. NSAutoreleasePool's docs suggest we should create a temporary pool for things running in other threads, but then we have to release it... but I guess we're waiting for the function to complete on other platforms already, so no big deal here. --- darwintest/newtypes.go | 9 ++------- darwintest/runtimetest.go | 33 ++++++++------------------------- 2 files changed, 10 insertions(+), 32 deletions(-) diff --git a/darwintest/newtypes.go b/darwintest/newtypes.go index 7bcb898..4bb04f7 100644 --- a/darwintest/newtypes.go +++ b/darwintest/newtypes.go @@ -51,14 +51,9 @@ func buttonClicked(self C.id, sel C.SEL, sender C.id) { } //export gotNotification -func gotNotification(self C.id, sel C.SEL, note C.id) { - data := C.objc_msgSend_noargs(note, - sel_getUid("userInfo")) - val := C.objc_msgSend_id(data, - sel_getUid("objectForKey:"), - notekey) +func gotNotification(self C.id, sel C.SEL, object C.id) { source := (*C.char)(unsafe.Pointer( - C.objc_msgSend_noargs(val, + C.objc_msgSend_noargs(object, sel_getUid("UTF8String")))) fmt.Println("got notification from %s", C.GoString(source)) diff --git a/darwintest/runtimetest.go b/darwintest/runtimetest.go index c19d326..e84dc89 100644 --- a/darwintest/runtimetest.go +++ b/darwintest/runtimetest.go @@ -28,6 +28,7 @@ import ( // id objc_msgSend_id_sel_id_id(id obj, SEL sel, id a, SEL b, id c, id d) { return objc_msgSend(obj, sel, a, b, c, d); } // id objc_msgSend_id_id_id(id obj, SEL sel, id a, id b, id c) { return objc_msgSend(obj, sel, a, b, c); } // id objc_msgSend_id_id(id obj, SEL sel, id a, id b) { return objc_msgSend(obj, sel, a, b); } +// id objc_msgSend_sel_id_bool(id obj, SEL sel, SEL a, id b, BOOL c) { return objc_msgSend(obj, sel, a, b, c); } // Class NilClass = Nil; /* for newtypes.go */ // id Nilid = nil; import "C" @@ -49,8 +50,7 @@ func sel_getUid(sel string) C.SEL { var NSApp C.id var defNC C.id var delegate C.id -var note C.id -var notekey C.id +var notesel C.SEL func init() { // need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21 @@ -70,19 +70,7 @@ func init() { objc_getClass("hello"), alloc) - noteStr := []C.char{'g', 'o', 'n', 'o', 't', 'e', 0} - note = C.objc_msgSend_strarg( - objc_getClass("NSString"), - sel_getUid("stringWithUTF8String:"), - ¬eStr[0]) - notekey = note - C.objc_msgSend_id_sel_id_id( - defNC, - sel_getUid("addObserver:selector:name:object:"), - delegate, - selN, - note, - C.Nilid) + notesel = selN } const ( @@ -114,17 +102,12 @@ func notify(source string) { objc_getClass("NSString"), sel_getUid("stringWithUTF8String:"), csource) - dict := C.objc_msgSend_id_id( - objc_getClass("NSDictionary"), - sel_getUid("dictionaryWithObject:forKey:"), + C.objc_msgSend_sel_id_bool( + delegate, + sel_getUid("performSelectorOnMainThread:withObject:waitUntilDone:"), + notesel, src, - notekey) - C.objc_msgSend_id_id_id( - defNC, - sel_getUid("postNotificationName:object:userInfo:"), - note, - C.Nilid, - dict) + C.BOOL(C.FALSE)) // don't wait; we're using a channel for this (in the final ui code) } func main() {