Documentation
¶
Overview ¶
Package cocoa is the small part of the Objective-C runtime that package keychain needs: building NSString, NSData and NSDictionary values, reading bytes back out, and running work inside an autorelease pool.
Nothing here uses cgo. purego resolves symbols with dlopen and dlsym and implements the C calling convention in assembly, so a program importing package keychain still builds with CGO_ENABLED=0 and still cross-compiles.
Together with package keychain this is the only place in the module that imports purego.
Index ¶
- func Array(items []objc.ID) objc.ID
- func Autorelease(id objc.ID) objc.ID
- func Bool(v bool) objc.ID
- func Count(array objc.ID) uint64
- func Data(b []byte) objc.ID
- func Dict(pairs ...objc.ID) objc.ID
- func GoBytes(id objc.ID) []byte
- func Load() error
- func ObjectAt(array objc.ID, i uint64) objc.ID
- func Release(id objc.ID)
- func String(s string) objc.ID
- func WithPool(fn func())
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Autorelease ¶
Autorelease adds an object to the current autorelease pool.
func Data ¶
Data wraps a Go byte slice in an autoreleased NSData. The bytes are copied, so the caller's slice may be reused immediately.
func Dict ¶
Dict builds an autoreleased NSMutableDictionary from alternating keys and values. It is toll-free bridged to the CFDictionaryRef the Security functions expect, so it is passed to them as is.
func Load ¶
func Load() error
Load maps the Foundation and Security frameworks. It is safe to call from many goroutines and does its work once.
func String ¶
String converts a Go string to an autoreleased NSString, or 0 if it cannot be carried: invalid UTF-8, which +stringWithUTF8String: rejects by returning nil, or an interior NUL, which the C string API would silently treat as the end.
func WithPool ¶
func WithPool(fn func())
WithPool runs fn inside an autorelease pool. Every goroutine that touches the Objective-C runtime must go through this, or the objects it creates leak for the life of the process.
The goroutine is locked to its OS thread for the duration, and that is not a precaution — it is required for correctness. Autorelease pools are thread-local: pushing returns a token that belongs to the calling thread's pool stack, and popping must hand it back to that same thread. A goroutine has no such affinity; the scheduler may move it to another thread at any preemption point, a function call being enough. Without the lock a pool can be pushed on one thread and popped on another, which corrupts the other thread's pool stack and surfaces later, somewhere unrelated, as an over-released object or an "Attempt to use unknown class" from the runtime.
LockOSThread nests, so nested calls behave correctly.
Types ¶
This section is empty.