emit

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package emit contains the per-construct emitters for the purego code generator. All emitted files are pure Go — no CGo, no C headers, no .m files.

Index

Constants

View Source
const (
	PureobjcImport = "github.com/deploymenttheory/go-bindings-macosplatform/bindings/runtime/purego"
	PureobjcPkg    = "purego"
	ObjcImport     = "github.com/ebitengine/purego/objc"
	ObjcPkg        = "objc"
)

Variables

This section is empty.

Functions

func BlockGoFuncType

func BlockGoFuncType(
	objcType string,
	ctx typemap.Context,
	mapper *typemap.Mapper,
	imports typemap.ImportSet,
	ownerIndex map[string]string,
) string

BlockGoFuncType returns the public Go type the raw emitter gives a block-typed parameter: the adapter's closure type, or objc.Block when the block cannot be bridged. Downstream emitters (idiomatic layer) must use this instead of Mapper.GoType so their signatures match the raw bindings.

func ClassMethodGoName

func ClassMethodGoName(className, selector string) string

ClassMethodGoName returns the expected exported Go function name for an ObjC class method. The result is className + MethodName(selector), which matches the emitter's output for the common case. Name-collision avoidance may produce a different name in rare cases; tests generated from this name should be treated as best-effort.

("NSBundle", "mainBundle")        → "NSBundleMainBundle"
("NSProcessInfo", "processInfo")  → "NSProcessInfoProcessInfo"

func ClassMethodGoNameFromMeta

func ClassMethodGoNameFromMeta(className, selector string, m *meta.FrameworkMeta) string

ClassMethodGoNameFromMeta is like ClassMethodGoName but also applies the same name-collision avoidance the emitter uses: if the derived candidate collides with an enum, struct, or class name in the same framework, a "Class" suffix is appended (matching classMethodName in classes.go).

("SFSpeechRecognizer", "authorizationStatus", m) → "SFSpeechRecognizerAuthorizationStatusClass"
("CBManager",          "authorization",        m) → "CBManagerAuthorizationClass"

func EmitClasses

func EmitClasses(
	outDir, packageName, framework string,
	m *meta.FrameworkMeta,
	mapper *typemap.Mapper,
	reg *RegistrySnapshot,
) error

EmitClasses writes one .go file per ObjC class in the framework to outDir.

func EmitEnums

func EmitEnums(w io.Writer, framework *meta.FrameworkMeta) error

EmitEnums writes all enum declarations for the framework to w.

func EmitExterns

func EmitExterns(
	w io.Writer,
	framework *meta.FrameworkMeta,
	mapper *typemap.Mapper,
	dylibVarName string,
) (typemap.ImportSet, error)

EmitExterns writes accessor functions for extern global symbols. Each extern is accessed via purego.Dlsym at runtime.

func EmitProtocols

func EmitProtocols(
	w io.Writer,
	framework *meta.FrameworkMeta,
	mapper *typemap.Mapper,
	classNameOwner map[string]string,
) (typemap.ImportSet, error)

EmitProtocols writes Go interface types for all ObjC protocols in the framework.

func EmitStructs

func EmitStructs(
	w io.Writer,
	framework *meta.FrameworkMeta,
	mapper *typemap.Mapper,
) (typemap.ImportSet, error)

EmitStructs writes all struct type declarations for the framework to w. Returns cross-framework imports discovered during field type resolution.

func EmittableFunctions

func EmittableFunctions(
	framework *meta.FrameworkMeta,
	diag func(format string, args ...any),
) []meta.Function

EmittableFunctions returns the free C functions the raw emitter emits for framework, sorted by name. It applies the skip rules (unavailable, inline, non-format variadic, duplicates) and the exported-name collision rules (naming.ExportedFunctionName vs other package-level identifiers). The optional diag callback receives a message for each collision skip; pass nil to filter silently. Downstream emitters (idiomatic layer, genacceptance) must use this so they cannot drift from the raw emission set.

func FormatGoSource added in v0.5.0

func FormatGoSource(src []byte) ([]byte, error)

FormatGoSource runs src through go/format (canonical gofmt). It returns an error — failing generation loudly — when src is not valid Go, so an emitter bug surfaces at generation time rather than as broken output on disk.

func FunctionGoName

func FunctionGoName(fn meta.Function) string

FunctionGoName returns the exported Go name the emitter uses for a free C function (snake_case names become PascalCase; already-exported C names are unchanged).

func FunctionWillBeEmitted

func FunctionWillBeEmitted(fn meta.Function) bool

FunctionWillBeEmitted reports whether a free C function will appear in the generated purego bindings as an exported Go symbol.

It does not account for exported-name collision skips (seedReservedGoNames in EmitFunctions); callers must tolerate rare false positives.

func MapEnumGoType added in v0.5.0

func MapEnumGoType(t string) string

MapEnumGoType maps an ObjC/C integer type string to the Go integer type used as an enum's underlying type. Exported so the idiomatic emitter can derive the exact same underlying type for its re-emitted concrete enums.

func MethodWillBeEmitted

func MethodWillBeEmitted(method meta.Method) bool

MethodWillBeEmitted reports whether an ObjC method will appear in the generated purego bindings. It mirrors isMethodBridgeable in classes.go — any changes to the emitter's skip rules must be reflected here.

It does not account for name-collision avoidance (classMethodName), which silently drops class methods whose derived Go name would collide with a package-level type. Those cases are rare and impossible to detect without a full registry load; callers of this function must tolerate rare false positives (a function name that was ultimately not emitted).

func ReturnIsVoid

func ReturnIsVoid(retType meta.ReturnType) bool

ReturnIsVoid reports whether a method's return type is void.

func UpgradeEnumTypeIfOverflow added in v0.5.0

func UpgradeEnumTypeIfOverflow(goType string, members []meta.EnumMember) string

UpgradeEnumTypeIfOverflow upgrades a signed Go integer type to its unsigned variant when a member value would overflow it. Exported for reuse alongside MapEnumGoType.

func WriteGoFile added in v0.5.0

func WriteGoFile(path string, src []byte) error

WriteGoFile formats src with go/format and writes the canonical result to path. It is the single choke point for every generated .go file so output is always gofmt-canonical and emitters/templates need not hand-manage whitespace, alignment, or import grouping.

Types

type FunctionRegistration

type FunctionRegistration struct {
	Symbol string
	Line   string
}

FunctionRegistration pairs a C symbol with its purego.RegisterLibFunc call so the runtime emitter can record per-symbol registration failures.

func EmitFunctions

func EmitFunctions(
	w io.Writer,
	framework *meta.FrameworkMeta,
	mapper *typemap.Mapper,
	dylibVarName string,
	ownerIndex map[string]string,
) (imports typemap.ImportSet, regLines []FunctionRegistration, err error)

EmitFunctions writes Go var declarations and wrapper functions for free C functions in the framework. Registration lines (purego.RegisterLibFunc calls) are returned as regLines so they can be embedded in the runtime.go init() AFTER a successful Dlopen — preventing the init-order bug where a separate _functions.go init() would run before _runtime.go's init() and see a zero library handle. ownerIndex maps ObjC class names to their owning framework — used to distinguish ObjC object pointers (need .Ptr()) from C struct pointers. Returns the cross-framework imports discovered and the registration lines.

type RegistrySnapshot

type RegistrySnapshot struct {
	OwnerIndex         map[string]string
	GenericClasses     map[string]bool
	GenericParamIndex  map[string][]string
	ClassIndex         map[string]meta.Class
	BlockedImports     map[string]map[string]bool
	EnumGoTypeIndex    map[string]string // enum name → underlying Go type (e.g. "int64")
	UnavailableClasses map[string]bool   // classes marked IsUnavailable in metadata
	ModulePrefix       string            // Go module path prefix for framework packages
}

RegistrySnapshot carries the cross-framework lookup tables the class emitter needs.

Directories

Path Synopsis
Package idiomatic emits fluent Go wrapper types for ObjC frameworks.
Package idiomatic emits fluent Go wrapper types for ObjC frameworks.
render
Package render turns a fully-resolved view (the IR built by the gather phase) into Go source.
Package render turns a fully-resolved view (the IR built by the gather phase) into Go source.
view
Package view is the intermediate representation (IR) of a fully-resolved idiomatic framework package.
Package view is the intermediate representation (IR) of a fully-resolved idiomatic framework package.

Jump to

Keyboard shortcuts

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