callbacks

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Rendered for darwin/amd64

Overview

Package callbacks manages the CGo trampolines that allow ObjC method calls on generated subclasses and protocol-conformance objects to invoke Go functions.

ObjC method dispatch is class-based. When the code generator creates a subclass or a protocol-impl class at startup (via objc_allocateClassPair), it registers generated IMP trampolines for each overridable method. At runtime, when an instance is created via [New]NSFooSubclass or [New]NSFooProtocolImpl, the caller supplies Go callback functions. Each callback is registered here (assigned a uint64 handle) and the handle is stored as an ObjC associated object on the instance, keyed by the method's SEL.

When ObjC later invokes the method, the IMP trampoline:

  1. Calls orinCallbackLookup(self, _cmd) to retrieve the uint64 handle.
  2. Calls goCallIMP_<sig>(handle, self, args...) — an //export'd Go function.
  3. goCallIMP_<sig> looks up the handle in the registry and type-asserts the stored func value to the concrete func type for that signature.
  4. The Go callback is invoked.

IMP type signatures are collected across all frameworks during generation and deduplicated; the resulting Go func types are emitted into callbacks_generated.go and the C IMP functions into method_trampolines_generated.m by the code generator.

Index

Constants

This section is empty.

Variables

View Source
var ClassRegistrationMu sync.Mutex

ClassRegistrationMu serializes ObjC dynamic class registration across all generated subclass and protocol-impl factory functions. Apple's objc_allocateClassPair and objc_registerClassPair are not safe to call concurrently; Go's scheduler can context-switch between goroutines at any CGo call boundary, so the full alloc→add_method→register_and_init sequence must be held under this lock.

Functions

func BindMethod

func BindMethod(obj unsafe.Pointer, selName string, fn any) uint64

BindMethod registers fn in the callback registry and stores the resulting handle as an associated object on obj for the given ObjC selector name. The IMP trampoline retrieves this handle at call time via goBridge_Callback_Lookup. obj must point to a valid ObjC instance.

func Free

func Free(key uint64)

Free decrements the reference count for key and removes the entry when it reaches zero. Callers should invoke Free after the ObjC object that holds the binding is released.

func Register

func Register(fn any) uint64

Register stores fn in the callback registry and returns a unique handle. The caller must call Free(handle) when the callback is no longer needed (e.g. when the ObjC object that holds it is deallocated).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL