diff --git a/examples/app/main.go b/examples/app/main.go index d21e5a8..d74c56c 100644 --- a/examples/app/main.go +++ b/examples/app/main.go @@ -6,7 +6,7 @@ import ( "gitlab.wow.st/gmp/nswrap/examples/app/ns" ) -func nsmgr() { +func app() { //Lock OS thread because Cocoa uses thread-local storage runtime.LockOSThread() a := ns.NSApplicationSharedApplication() @@ -50,7 +50,7 @@ func nsmgr() { } func main() { - go nsmgr() + go ns.Autorelease(app) select { } } diff --git a/examples/app/nswrap.yaml b/examples/app/nswrap.yaml index 68504b9..f05d0aa 100644 --- a/examples/app/nswrap.yaml +++ b/examples/app/nswrap.yaml @@ -3,7 +3,8 @@ inputfiles: - /System/Library/Frameworks/AppKit.framework/Headers/AppKit.h classes: - - NSArray" + - NSAutoreleasePool + - NSArray - NSMutableArray - NSDictionary - NSEnumerator diff --git a/examples/foundation/nswrap.yaml b/examples/foundation/nswrap.yaml index 7f4b802..b1f2a67 100644 --- a/examples/foundation/nswrap.yaml +++ b/examples/foundation/nswrap.yaml @@ -1,6 +1,7 @@ inputfiles: - /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h classes: + - NSAutoreleasePool - NSArray - NSMutableArray - NSDictionary diff --git a/wrap/main.go b/wrap/main.go index c33ed22..66fad60 100644 --- a/wrap/main.go +++ b/wrap/main.go @@ -562,6 +562,9 @@ func (w *Wrapper) processType(tp *types.Type) { if gt == "SEL" { w.SelectorHelpers() } + if gt == "NSAutoreleasePool" { + w.AutoreleaseHelpers() + } if bt.IsFunction() || bt.IsFunctionPtr() { return } @@ -576,6 +579,9 @@ func (w *Wrapper) processType(tp *types.Type) { func (w *Wrapper) ObjectHelpers() { w.goHelpers.WriteString(` +func (o *Id) Retain() { + C.retain(unsafe.Pointer(o)) +} func (o *Id) Release() { C.release(unsafe.Pointer(o)) } @@ -585,6 +591,10 @@ func (o *Id) Autorelease() { `) w.cCode.WriteString(` void +retain(void* obj) { + [(NSObject*)obj retain]; +} +void release(void* obj) { [(NSObject*)obj release]; } @@ -621,6 +631,27 @@ func (e *NSEnumerator) ForIn(f func(*Id) bool) { `) } +func (w *Wrapper) AutoreleaseHelpers() { + //not sure why this is not coming up automatically... + w.cCode.WriteString(` +void* _Nonnull +NSAutoreleasePool_init(void* o) { + return [(NSAutoreleasePool*)o init]; +} +`) + w.goHelpers.WriteString(` +func (o *NSAutoreleasePool) Init() *NSAutoreleasePool { + return (*NSAutoreleasePool)(unsafe.Pointer(C.NSAutoreleasePool_init(o.Ptr()))) +} + +func Autorelease(f func()) { + pool := NSAutoreleasePoolAlloc().Init() + f() + pool.Drain() +} +`) +} + func (w *Wrapper) SelectorHelpers() { w.cCode.WriteString(` void*