diff --git a/darwintest/runtimetest.c b/darwintest/runtimetest.c deleted file mode 100644 index c0109d3..0000000 --- a/darwintest/runtimetest.c +++ /dev/null @@ -1,22 +0,0 @@ -// 27 february 2014 -#include -#include -#include -#include - -int main(void) -{ - id NSString = objc_getClass("NSString"); - SEL stringFromUTF8String = - sel_getUid("stringWithUTF8String:"); - id str = objc_msgSend(NSString, - stringFromUTF8String, - "hello, world\n"); - SEL UTF8String = - sel_getUid("UTF8String"); - - printf("%s", - (char *) objc_msgSend(str, - UTF8String)); - return 0; -} diff --git a/darwintest/runtimetest.go b/darwintest/runtimetest.go new file mode 100644 index 0000000..7a36fb7 --- /dev/null +++ b/darwintest/runtimetest.go @@ -0,0 +1,41 @@ +// 27 february 2014 +package main + +import ( + "fmt" + "unsafe" +) + +// #cgo LDFLAGS: -lobjc -framework Foundation +// #include +// #include +// #include +// #include +// /* 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); } +import "C" + +func main() { + _NSString := C.CString("NSString") + defer C.free(unsafe.Pointer(_NSString)) + _stringWithUTF8String := C.CString("stringWithUTF8String:") + defer C.free(unsafe.Pointer(_stringWithUTF8String)) + _UTF8String := C.CString("UTF8String") + defer C.free(unsafe.Pointer(_UTF8String)) + _hello := C.CString("hello, world\n") + defer C.free(unsafe.Pointer(_hello)) + + NSString := C.objc_getClass(_NSString) + stringWithUTF8String := + C.sel_getUid(_stringWithUTF8String) + str := C.objc_msgSend_strarg(NSString, + stringWithUTF8String, + _hello) + UTF8String := + C.sel_getUid(_UTF8String) + res := C.objc_msgSend_noargs(str, + UTF8String) + cres := (*C.char)(unsafe.Pointer(res)) + fmt.Printf("%s", C.GoString(cres)) +}