native

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package native runs native modules: Go interfaces implemented in a platform's own language, Swift on macOS (see docs/native-modules.md).

A module's package declares its interfaces, and `mygo generate` writes the rest: the constructors and the values that call the implementation, which use this package. An app calls a module's constructor, NewT, and never this package directly, except for its types: errors made with NewError, Buffer and View.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("native: object closed")

ErrClosed is returned by the methods of an object that was closed.

Functions

func Export

func Export(m *Module, v any, dispatch func(ctx context.Context, method int, args Args) (any, error)) uint64

Export registers a value of an interface Go implements, which the platform calls through dispatch, and returns its handle. dispatch calls its method number method with args; ctx is that of a method taking one.

func Flush

func Flush()

Flush waits until the notifications of callbacks the platform has made so far have run, for tools such as mygo native call that exit next.

func Forget

func Forget(ids ...uint64)

Forget releases callbacks and values of Go's that a call failed to give the platform.

func Func

func Func(fn func(Args) (any, error), notify bool) uint64

Func registers a callback, which the platform calls with its arguments, and returns its handle. A notification (notify) returns nothing and runs later, in order, on the main thread when the app runs; a question runs at once on the thread that asks and returns its answer.

func HandleOf

func HandleOf(v any) uint64

HandleOf returns the handle of v when a module returned it, else 0: a value Go made cannot stand for the platform's.

func LibraryName

func LibraryName(module string) string

LibraryName is the file of a module's implementation on macOS.

func Register

func Register(m *Module, wraps map[string]Wrapper)

Register records a module and how to wrap the values of its interfaces, for the modules that use them. Generated code calls it as the package initializes.

func Seq

func Seq[T any](s iter.Seq2[T, error]) iter.Seq[T]

Seq turns a stream of values that cannot fail into an iter.Seq.

func Stream

func Stream[T any](ctx context.Context, o *Object, sym string, flags Flags, prepare func() ([]any, []uint64), keep ...any) iter.Seq2[T, error]

Stream calls a method that returns a stream, as an iterator that starts it. prepare makes the call's arguments as the loop starts, and returns the handles of the callbacks and values of Go's among them, which are forgotten if the stream does not start; keep are the arguments that stand for memory or objects of the platform's, kept reachable until the stream ends.

func StreamMap

func StreamMap[D, T any](ctx context.Context, o *Object, sym string, flags Flags, prepare func() ([]any, []uint64), conv func(D) T, keep ...any) iter.Seq2[T, error]

StreamMap is Stream for values that cross as D and are given to Go as T.

func WindowHandle

func WindowHandle(w *mygo.Window) uint64

WindowHandle returns the native window of w, NSWindow* on macOS, or 0.

func Wrap

func Wrap(h uint64, module, iface string) any

Wrap returns the value of the interface iface of the module named module around the handle h, which another module returned, or nil.

Types

type Args

type Args []json.RawMessage

Args are the arguments the platform passes to Go: to a callback, or to a method of an interface Go implements.

func (Args) Decode

func (a Args) Decode(i int, v any) error

Decode decodes the argument i into v.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is memory that Go and the implementation both use, which crosses without a copy: pass one to have the implementation fill it, as io.Reader fills a slice, or range over the buffers a stream yields. In Swift it is a NativeBuffer, whose bytes are the same memory.

func NewBuffer

func NewBuffer(n int) *Buffer

NewBuffer makes a buffer of n bytes, zeroed.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns the buffer's contents. A buffer a stream yields is lent until the loop asks for the next value: copy what you keep, as with bufio.Scanner.Bytes.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the size of the buffer.

func (*Buffer) MarshalJSON

func (b *Buffer) MarshalJSON() ([]byte, error)

MarshalJSON gives the platform the buffer's address and size.

func (*Buffer) UnmarshalJSON

func (b *Buffer) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a buffer the platform lends.

type Error

type Error struct {
	// Code is the code of an error the module declares, else empty.
	Code string
	// Domain is the error's domain or type, as NSCocoaErrorDomain or
	// ParseError, for an error the module does not declare.
	Domain string
	// Number is the error's code in its domain.
	Number int
	// Message describes the error.
	Message string
}

Error is an error an implementation returned. The errors a module declares are made with NewError and compare by their code, so errors.Is(err, language.ErrUndetermined) holds for the error the platform threw as LanguageError.undetermined. Any other error the platform throws has no code, and says what the platform said of it.

func NewError

func NewError(code string) *Error

NewError makes an error a module declares, which its implementations throw by code: in Swift, a case of the enum <Module>Error.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is reports whether target is an error the module declares with the same code.

type Flags

type Flags int

Flags say how a method is called.

const (
	// MainThread runs the method on the main thread.
	MainThread Flags = 1 << iota
)

type Module

type Module struct {
	// Name is the Swift module's: Language.
	Name string
	// Symbol prefixes the functions the implementation exports: language.
	Symbol string
	// Dir is the directory of the package, when the binary knows it
	// (built without -trimpath), where `mygo generate` builds the
	// implementation for go test and go run.
	Dir string
	// MinMacOS is the oldest macOS the implementation runs on, from the
	// platforms of its Package.swift.
	MinMacOS string
	// Errors are the errors the module declares.
	Errors []*Error
	// Deps are the Swift modules of the native modules whose types it
	// uses, which load before it.
	Deps []string
	// contains filtered or unexported fields
}

Module describes a module to the runtime. Generated code declares one per package; its fields are not for apps.

func (*Module) New

func (m *Module) New(entry string, flags Flags) (*Object, error)

New makes the implementation of an entry point.

func (*Module) Object

func (m *Module) Object(h uint64, flags Flags) *Object

Object wraps the handle of an object the platform returned, whose methods flags describe.

type Object

type Object struct {
	// contains filtered or unexported fields
}

Object is a value of the platform: what a constructor made, or a method returned. Its methods are for generated code.

func (*Object) Call

func (o *Object) Call(sym string, flags Flags, args []any, result any) error

Call calls a method of the object that returns at once, decoding what it returns into result unless nil.

func (*Object) CallContext

func (o *Object) CallContext(ctx context.Context, sym string, flags Flags, args []any, result any) error

CallContext calls an asynchronous method of the object, which ctx cancels.

func (*Object) Close

func (o *Object) Close() error

Close closes the object: the platform's close(), then its release. Later calls return ErrClosed.

func (*Object) Handle

func (o *Object) Handle() uint64

Handle returns the object's handle, to pass it back to the platform.

func (*Object) Module

func (o *Object) Module() *Module

Module returns the module the object belongs to.

func (*Object) Subscribe

func (o *Object) Subscribe(sym string, flags Flags, args []any, funcs ...uint64) io.Closer

Subscribe calls a method that returns a Closer, which stops what it started and releases the callbacks funcs, those the call passed.

func (*Object) View

func (o *Object) View() uintptr

View returns the object's view, for the NativeView of the proxies of views: the object itself, or 0 once closed.

type Texture

type Texture = texture.Texture

Texture is an image on the GPU that a module's implementation draws into, and that native UI shows with no copy through the CPU: it is a ui.ImageSource, as a *ui.Bitmap is, so ui.Image shows it, and it can be covered, clipped, scaled, rotated and faded like any image. Pass it to a method of the module; in Swift it is a NativeTexture, whose present shows a frame (an IOSurface, or a CVPixelBuffer backed by one) in the next frame of the windows showing the texture, without the app calling Invalidate.

func NewTexture

func NewTexture() *Texture

NewTexture makes a texture, which shows nothing until the implementation presents a frame.

type View

type View interface {
	// NativeView returns the platform's view: an NSView* on macOS. It is
	// 0 once the view is closed, which takes it out of the windows
	// showing it.
	NativeView() uintptr
}

View is embedded by the interfaces of a module that a view of the platform implements, which ui.NativeView shows in native UI.

type Wrapper

type Wrapper struct {
	Flags Flags
	Wrap  func(*Object) any
}

Wrapper makes the value of an interface of the platform's around an object, for other modules returning it.

Jump to

Keyboard shortcuts

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