bridge

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package bridge is the Go side of the Nimbus Native C ABI (include/nn.h).

An app registers its handlers in init() — before the host starts the runtime — and then talks to the host with Send and CallHost. Everything that crosses is a byte slice; the wire format gives the bytes meaning.

On a device the bridge is the C ABI (bridge_native.go). In dev mode (`nimbus native dev`, build tag nimbusdev) the same API runs on the laptop over a socket to the device (bridge_dev.go).

Index

Constants

View Source
const ABIVersion = C.NN_ABI_VERSION

ABIVersion is the nn.h version this runtime speaks.

Variables

View Source
var ErrNotStarted = errors.New("bridge: host has not started the runtime")

ErrNotStarted is returned by Send before the host has started the runtime.

Functions

func CallHost

func CallHost(req []byte) ([]byte, error)

CallHost asks the host something only it can answer, synchronously — text measurement during layout, later native modules. Safe from any goroutine.

func Logf

func Logf(format string, args ...any)

Logf writes a line to the platform log: stderr here (the Xcode and simulator consoles show it), logcat on Android.

func Main

func Main()

Main is for the app's main function. On a device the host owns the process and main never runs; in dev mode (bridge_dev.go) Main runs the app.

func OnCall

func OnCall(fn func(req []byte) []byte)

OnCall sets the handler for synchronous calls from the host. It runs on the host's calling thread — usually the UI thread — so keep it fast.

func OnMessage

func OnMessage(fn func(msg []byte))

OnMessage sets the handler for asynchronous messages from the host. Messages arrive in order, one at a time, on a runtime goroutine.

func OnStart

func OnStart(fn func())

OnStart registers fn to run once the host has started the runtime. Register in init().

func Send

func Send(msg []byte) error

Send queues msg for the host. It is safe from any goroutine and never blocks on the host: messages are batched and the host drains them on its UI thread, in the order Send returned. msg is copied.

Types

type Handle

type Handle uint32

Handle is how a host refers to a Go value — a callback, a view's event handler — across the boundary. Hosts never see Go pointers, so the garbage collector never has to reason about native code.

type Handles

type Handles[T any] struct {
	// contains filtered or unexported fields
}

Handles maps handles to Go values. The zero value is ready to use; it is safe from any goroutine. Handle 0 is never issued, so hosts can use it for "none".

func (*Handles[T]) Get

func (h *Handles[T]) Get(id Handle) (T, bool)

Get returns the value for id, or false if it was never issued or has been released — a host acting on a stale handle gets a miss, not a crash.

func (*Handles[T]) Len

func (h *Handles[T]) Len() int

Len is the number of live handles — a leak shows up as a Len that only grows.

func (*Handles[T]) Put

func (h *Handles[T]) Put(v T) Handle

Put stores v and returns its handle.

func (*Handles[T]) Release

func (h *Handles[T]) Release(id Handle)

Release forgets id; releasing twice is harmless.

type HostError

type HostError struct{ Code int }

HostError is a failed CallHost: Code is an NN_ERR_* value from nn.h.

func (*HostError) Error

func (e *HostError) Error() string

type Stats

type Stats struct {
	Sent      int64 // messages queued by Send
	Wakes     int64 // times the host was woken
	Drains    int64 // non-empty batches the host took
	Received  int64 // messages from nn_send
	Calls     int64 // nn_call invocations
	Panics    int64 // panics recovered at the boundary
	HostCalls int64 // CallHost invocations

	Goroutines int
	HeapAlloc  uint64 // bytes of live Go heap
	Sys        uint64 // bytes the Go runtime holds from the OS
}

Stats is a snapshot of bridge traffic and Go memory, for soak tests and (later) the DevTools inspector.

func ReadStats

func ReadStats(gc bool) Stats

ReadStats returns current counters. With gc, it collects first so HeapAlloc reflects live memory only.

Directories

Path Synopsis
internal
abitest/app command
The Go side of the ABI test: host.c drives it through nn.h exactly as an iOS or Android host would.
The Go side of the ABI test: host.c drives it through nn.h exactly as an iOS or Android host would.

Jump to

Keyboard shortcuts

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