summaryrefslogtreecommitdiff
path: root/darwintest/runtimetest.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-02-27 20:48:32 -0500
committerPietro Gagliardi <[email protected]>2014-02-27 20:48:32 -0500
commitf949b7f8d2c53036d55d5e101c86ad35fbc706a5 (patch)
tree43ffbfbf943e4e07c223863711dafbb148fa96af /darwintest/runtimetest.go
parentfa7ef40c42c30807395ab3fe5019e04928102a20 (diff)
Ported the runtime test to Go. It works in both 32-bit and 64-bit!
Diffstat (limited to 'darwintest/runtimetest.go')
-rw-r--r--darwintest/runtimetest.go41
1 files changed, 41 insertions, 0 deletions
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))
+}