diff options
| author | Pietro Gagliardi <[email protected]> | 2014-02-27 20:48:32 -0500 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2014-02-27 20:48:32 -0500 |
| commit | f949b7f8d2c53036d55d5e101c86ad35fbc706a5 (patch) | |
| tree | 43ffbfbf943e4e07c223863711dafbb148fa96af | |
| parent | fa7ef40c42c30807395ab3fe5019e04928102a20 (diff) | |
Ported the runtime test to Go. It works in both 32-bit and 64-bit!
| -rw-r--r-- | darwintest/runtimetest.c | 22 | ||||
| -rw-r--r-- | darwintest/runtimetest.go | 41 |
2 files changed, 41 insertions, 22 deletions
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 <stdio.h> -#include <objc/message.h> -#include <objc/objc.h> -#include <objc/runtime.h> - -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 <stdlib.h> +// #include <objc/message.h> +// #include <objc/objc.h> +// #include <objc/runtime.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); } +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)) +} |
