diff --git a/darwintest/newtypes.go b/darwintest/newtypes.go index 4bb04f7..eface0d 100644 --- a/darwintest/newtypes.go +++ b/darwintest/newtypes.go @@ -9,16 +9,11 @@ import ( // #cgo CFLAGS: -Dqqq // #cgo LDFLAGS: -lobjc -framework Foundation // #include -// #include -// #include -// #include +// #include "objc_darwin.h" // extern void windowShouldClose(id, SEL, id); -// extern id objc_msgSend_id(id, SEL, id); // extern void buttonClicked(id, SEL, id); // extern void gotNotification(id, SEL, id); -// extern id objc_msgSend_id_id_id(id, SEL, id, id, id); // /* cgo doesn't like nil or Nil */ -// extern id objc_msgSend_noargs(id, SEL); // extern Class NilClass; /* in runtimetest.go because of cgo limitations */ // extern id Nilid; import "C" diff --git a/darwintest/objc_darwin.h b/darwintest/objc_darwin.h new file mode 100644 index 0000000..85bf0b4 --- /dev/null +++ b/darwintest/objc_darwin.h @@ -0,0 +1,61 @@ +/* 28 february 2014 */ + +/* +This includes all Objective-C runtime headers for convenience. It also creates wrappers around objc_msgSend() out of necessity. + +cgo doesn't support calling variable argument list C functions, so objc_msgSend() cannot be called directly. + +Furthermore, Objective-C selectors work by basically sending the arguments to objc_msgSend() verbatim across the wire. This basically means we're stuck making wrapper functions for every possible argument list. What fun! + +The format should be self-explanatory. +*/ + +#include +#include +#include + +/* TODO this HAS to be unsafe, but not found?! */ +typedef unsigned long NSUInteger; + +inline id objc_msgSend_noargs(id obj, SEL sel) +{ + return objc_msgSend(obj, sel); +} + +#define m1(name, type1) \ + inline id objc_msgSend_ ## name (id obj, SEL sel, type1 a) \ + { \ + return objc_msgSend(obj, sel, a); \ + } + +#define m2(name, type1, type2) \ + inline id objc_msgSend_ ## name (id obj, SEL sel, type1 a, type2 b) \ + { \ + return objc_msgSend(obj, sel, a, b); \ + } + +#define m3(name, type1, type2, type3) \ + inline id objc_msgSend_ ## name (id obj, SEL sel, type1 a, type2 b, type3 c) \ + { \ + return objc_msgSend(obj, sel, a, b, c); \ + } + +#define m4(name, type1, type2, type3, type4) \ + inline id objc_msgSend_ ## name (id obj, SEL sel, type1 a, type2 b, type3 c, type4 d) \ + { \ + return objc_msgSend(obj, sel, a, b, c, d); \ + } + +m1(str, char *) /* TODO Go string? */ +m1(id, id) +/* TODO NSRect */ +m1(sel, SEL) +m1(uint, NSUInteger) + +m2(id_id, id, id) + +m3(id_id_id, id, id, id) +m3(sel_id_bool, SEL, id, BOOL) + +/* TODO NSRect */ +m4(id_sel_id_id, id, SEL, id, id) diff --git a/darwintest/runtimetest.go b/darwintest/runtimetest.go index d3cf3c1..f92e0f8 100644 --- a/darwintest/runtimetest.go +++ b/darwintest/runtimetest.go @@ -9,26 +9,12 @@ import ( // #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit // #include -// #include -// #include -// #include -// /* TODO // /* cgo doesn't handle ... */ -// id objc_msgSend_noargs(id obj, SEL sel) { return objc_msgSend(obj, sel); } -// id objc_msgSend_strarg(id obj, SEL sel, char *a) { return objc_msgSend(obj, sel, a); } // id objc_msgSend_NSRect_uint_uint_bool(id obj, SEL sel, CGRect a, NSUInteger b, NSUInteger c, BOOL d) { return objc_msgSend(obj, sel, a, b, c, d); } -// id objc_msgSend_id(id obj, SEL sel, id a) { return objc_msgSend(obj, sel, a); } // id objc_msgSend_NSRect(id obj, SEL sel, CGRect a) { return objc_msgSend(obj, sel, a); } -// id objc_msgSend_sel(id obj, SEL sel, SEL a) { return objc_msgSend(obj, sel, a); } -// id objc_msgSend_uint(id obj, SEL sel, NSUInteger a) { return objc_msgSend(obj, sel, a); } -// 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" @@ -102,7 +88,7 @@ func notify(source string) { pool := C.objc_msgSend_noargs( objc_getClass("NSAutoreleasePool"), sel_getUid("new")) - src := C.objc_msgSend_strarg( + src := C.objc_msgSend_str( objc_getClass("NSString"), sel_getUid("stringWithUTF8String:"), csource) @@ -178,7 +164,7 @@ func helloworld() { NSString := objc_getClass("NSString") stringWithUTF8String := sel_getUid("stringWithUTF8String:") - str := C.objc_msgSend_strarg(NSString, + str := C.objc_msgSend_str(NSString, stringWithUTF8String, _hello) UTF8String := sel_getUid("UTF8String")