Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go言語からCocoaを使ってみた

safx
August 09, 2014

 Go言語からCocoaを使ってみた

safx

August 09, 2014
Tweet

Other Decks in Programming

Transcript

  1. ! func main() { app := cocoa.NSApplication_sharedApplication() win := cocoa.NSWindow_initWithContentRectStyleMaskBackingDefer(

    cocoa.NSRect{0,0, 200,200}, NSTitledWindowMask, NSBackingStoreBuffered, false) win.CascadeTopLeftFromPoint(cocoa.NSPoint{20,20}) win.SetTitle(cocoa.NSString_initWithUTF8String("SomeApp")) win.MakeKeyAndOrderFront(nil) app.ActivateIgnoringOtherApps(true) ! fmt.Println(app, win) fmt.Println(app.GetClassName(), win.GetClassName()) ! app.Run(); } ͜ͷίʔυ͕ಈ͘Α͏ʹ͍ͨ͠ʂ
  2. $HP

  3. 1BDLBHFz$z ! package main ! // #include <stdio.h> import "C"

    ! func main() { C.puts(C.CString("hello, world!")) } ௚લͷίϝϯτ͕ ύοέʔδCͷͨΊͷ ίʔυʹͳΔ (Pจࣈྻˠ$จࣈྻ $ݴޠͷTUEJPI಺ͷؔ਺
  4. 1BDLBHF$ )FMQFSGVODUJPO • func C.CString(string) *C.char (Pจࣈྻˠ$จࣈྻ • func C.GoString(*C.char)

    string $จࣈྻˠ(Pจࣈྻ • func C.GoBytes(unsafe.Pointer, C.int) []byte $όΠτྻˠ(PόΠτྻ
  5. ! package main ! /* #include <stdio.h> ! char* hello()

    { return "hello, world!"; } */ import "C" ! func main() { C.puts(C.hello()) }
  6. ! package main ! /* int add(int x, int y)

    { return x + y; } */ import "C" import "fmt" ! func main() { fmt.Println(C.add(2, 3)) }
  7. HPCVJMEYGPPHP foo.go foo.cgo1.go foo.cgo2.c Object Object Object cgo Binary 6l

    6g clang pack _cgo_defun.c Object 6c ࣮ࡍ͸΋ͬͱෳࡶ‥
  8. (PDPNNBOET 6a 8a 5a 6c 8c 5c 6g 8g 5g

    6l 8l 5l assembler c compiler go compiler linker 6 (x86-64) 8 (x86-32) 5 (ARM) Plan 9 ༝དྷ ΞʔΩςΫνϟ πʔϧ
  9. ! void _cgo_2434c0b5a67e_Cfunc_add(void *v) { struct { int p0; int

    p1; int r; char __pad12[4]; } __attribute__((__packed__)) *a = v; a->r = add(a->p0, a->p1); } ! int add(int x, int y) { return x + y; } ! void ɾ_Cfunc_add(struct{void *y[2];}p) { runtimeɾcgocall(_cgo_2434c0b5a67e_Cfunc_add, &p); } foo.cgo2.c _cgo_defun.c
  10. ! package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:

    -framework Foundation #import <Cocoa/Cocoa.h> ! NSString* NSString_initWithUTF8String(char* text) { return [[NSString alloc] initWithUTF8String:text]; } ! const char* NSString_descripton(NSString* obj) { return [[obj description] UTF8String]; } */ import "C" import "fmt" ! func main() { str := C.NSString_initWithUTF8String(C.CString("foobar")) fmt.Println(C.GoString(C.NSString_descripton(str))) }
  11. ! package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:

    -framework Cocoa #import <Cocoa/Cocoa.h> int StartApp(void) { [NSAutoreleasePool new]; [NSApplication sharedApplication]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; id menubar = [[NSMenu new] autorelease]; id appMenuItem = [[NSMenuItem new] autorelease]; [menubar addItem:appMenuItem]; [NSApp setMainMenu:menubar]; id appMenu = [[NSMenu new] autorelease]; id appName = [[NSProcessInfo processInfo] processName]; id quitTitle = [@"Quit " stringByAppendingString:appName]; id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle action:@selector(terminate:) keyEquivalent:@“q"] autorelease]; [appMenu addItem:quitMenuItem]; [appMenuItem setSubmenu:appMenu]; id window = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO] autorelease]; [window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; [window setTitle:appName]; [window makeKeyAndOrderFront:nil]; [NSApp activateIgnoringOtherApps:YES]; [NSApp run]; return 0; } */ import "C" ! func main() { C.StartApp() } LBWVFYBNQMFHP ˒
  12. ! func main() { app := cocoa.NSApplication_sharedApplication() win := cocoa.NSWindow_initWithContentRectStyleMaskBackingDefer(

    cocoa.NSRect{0,0, 200,200}, NSTitledWindowMask, NSBackingStoreBuffered, false) win.CascadeTopLeftFromPoint(cocoa.NSPoint{20,20}) win.SetTitle(cocoa.NSString_initWithUTF8String("SomeApp")) win.MakeKeyAndOrderFront(nil) app.ActivateIgnoringOtherApps(true) ! fmt.Println(app, win) fmt.Println(app.GetClassName(), win.GetClassName()) ! app.Run(); }
  13. $MBOH1ZUIPO#JOEJOHT w ιʔείʔυ͔Βந৅ߏจ໦ "45 Λੜ੒ ! w 1ZUIPO͔Β؆୯ʹ͍͡ΕΔ w 4BGY$MBOHͷ1ZUIPOόΠϯσΟϯάͰ0CKFDUJWF$ͷ

    ιʔείʔυΛύʔεͯ͠ΈΔ TBGYEFWCMPHTQPUKQ DMBOHQZUIPOPCKFDUJWFDIUNM python cindex-dump.py sample.h -c -m64 -ObjC DOMπϦʔΈ͍ͨͳ΋ͷ
  14. translation_unit objc_interface_decl objc_property_decl objc_class_ref objc_instance_method_decl objc_instance_method_decl parm_decl objc_instance_method_decl parm_decl objc_class_ref

    parm_decl sample.h foo hoge NSArray hoge setHoge: hoge barWithArray:limit: array NSArray limit @class NSArray; ! @interface Foo @property NSArray* hoge; - (int)barWithArray:(NSArray*)array limit:(int)limit; @end
  15. ؔ࿈ϓϩδΣΫτ NLSBVU[PCKD ˒   PCKD@NTH4FOEͰ0CKFDUJWF$Λ࢖͑ΔΑ͏ʹ͢Δ(PϥΠϒϥϦ  NLSBVU[WBSJBEJD ˒ 

     $ͷՄม௕Ҿ਺ͷؔ਺Λݺ΂ΔΑ͏ʹͳΔ(PϥΠϒϥϦ   3JQ3JQDMBOH@DPNQMFUF ˒   WJNͷิ׬ϓϥάΠϯ DMBOHQZUIPOόΠϯσΟϯάར༻    TIJMHBQJSB0CK$SVTU ˒   3VTUͰJ04ͷϥΠϒϥϦΛ࡞Δαϯϓϧ