Split most of the objc_msgSend() wrappers into a C header file for convenience. The NSRect functions require special handling because of Apple stupidities that will be discussed when I get to that.

This commit is contained in:
Pietro Gagliardi 2014-02-28 18:30:06 -05:00
parent e4339d3c49
commit 8af591388f
3 changed files with 65 additions and 23 deletions

View File

@ -9,16 +9,11 @@ import (
// #cgo CFLAGS: -Dqqq
// #cgo LDFLAGS: -lobjc -framework Foundation
// #include <stdlib.h>
// #include <objc/message.h>
// #include <objc/objc.h>
// #include <objc/runtime.h>
// #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"

61
darwintest/objc_darwin.h Normal file
View File

@ -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 <objc/message.h>
#include <objc/objc.h>
#include <objc/runtime.h>
/* TODO this HAS to be unsafe, but <objc/NSObjCRuntime.h> 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)

View File

@ -9,26 +9,12 @@ import (
// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
// #include <stdlib.h>
// #include <objc/message.h>
// #include <objc/objc.h>
// #include <objc/runtime.h>
// /* TODO <objc/NSObjCRuntime.h not found?!?! */
// /* TODO this HAS to be unsafe */
// typedef unsigned long NSUInteger;
// #include "objc_darwin.h"
// /* avoid depending on Objective-C */
// #include <CoreGraphics/CGGeometry.h>
// /* 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")