native

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 23 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 Swift, Rust or Zig (see docs/native-modules.md).

Native modules are experimental: this package, the code MyGo generates and the mygo native commands may change in breaking ways in any release.

A module's package declares its interfaces, and MyGo generates the rest (`mygo dev` keeps it up to date): 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 BoolArg added in v0.9.0

func BoolArg(b bool) uintptr

BoolArg is a bool as Direct passes it.

func Export

func Export(m *Module, v any, dispatch func(ctx context.Context, method int, d *Decoder, e *Encoder) 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 the arguments in d, and writes its result to e; ctx is that of a method taking one.

func FloatArg added in v0.9.0

func FloatArg(f float64) uintptr

FloatArg is a float as Direct passes it.

func FloatResult added in v0.9.0

func FloatResult(r uintptr) float64

FloatResult is the float a Direct call returned.

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(d *Decoder, e *Encoder) error, notify bool) uint64

Func registers a callback, which the platform calls with its arguments, and returns its handle: fn reads them from d, and writes its result to e. A notification (notify) returns nothing and runs later, in order, on the main thread when the app runs, with e nil; 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 this system.

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() (*Encoder, []uint64), dec func(*Decoder) T, 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; dec reads a value; keep are the arguments that stand for memory or objects of the platform's, kept reachable until the stream ends.

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 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.

type Decoder added in v0.9.0

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

Decoder reads the values the implementation returned, for generated code. A value that does not decode reads as its zero, and Close and Err report it.

func (*Decoder) Bool added in v0.9.0

func (d *Decoder) Bool() bool

func (*Decoder) Buffer added in v0.9.0

func (d *Decoder) Buffer() *Buffer

Buffer reads a buffer the implementation lends.

func (*Decoder) Bytes added in v0.9.0

func (d *Decoder) Bytes() []byte

Bytes reads a []byte, nil when empty.

func (*Decoder) Close added in v0.9.0

func (d *Decoder) Close() error

Close lets go of what the decoder read from, and reports whether every value decoded.

func (*Decoder) Duration added in v0.9.0

func (d *Decoder) Duration() time.Duration

func (*Decoder) Err added in v0.9.0

func (d *Decoder) Err() error

Err reports whether a value failed to decode.

func (*Decoder) Float added in v0.9.0

func (d *Decoder) Float() float64

func (*Decoder) Int added in v0.9.0

func (d *Decoder) Int() int64

func (*Decoder) Len added in v0.9.0

func (d *Decoder) Len() int

Len reads the length of a slice or a map. It is no more than the bytes left, as every element takes one at least (a struct without fields crosses as a zero byte), so that a value that does not decode makes nothing large.

func (*Decoder) String added in v0.9.0

func (d *Decoder) String() string

func (*Decoder) Time added in v0.9.0

func (d *Decoder) Time() time.Time

Time reads a time, in UTC.

func (*Decoder) Uint added in v0.9.0

func (d *Decoder) Uint() uint64

type Encoder added in v0.9.0

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

Encoder writes the values of a call, for generated code.

func NewEncoder added in v0.9.0

func NewEncoder() *Encoder

NewEncoder returns an empty encoder, which the call it is given to releases.

func (*Encoder) Bool added in v0.9.0

func (e *Encoder) Bool(v bool)

func (*Encoder) Buffer added in v0.9.0

func (e *Encoder) Buffer(b *Buffer)

Buffer writes a buffer's address and size, which the implementation uses without a copy.

func (*Encoder) Bytes added in v0.9.0

func (e *Encoder) Bytes(b []byte)

func (*Encoder) Duration added in v0.9.0

func (e *Encoder) Duration(d time.Duration)

func (*Encoder) Float added in v0.9.0

func (e *Encoder) Float(v float64)

func (*Encoder) Int added in v0.9.0

func (e *Encoder) Int(v int64)

func (*Encoder) Len added in v0.9.0

func (e *Encoder) Len(n int)

Len writes the length of a slice or a map, whose elements follow.

func (*Encoder) Rect added in v0.9.0

func (e *Encoder) Rect(x, y, w, h float32)

Rect writes a ui.Rect.

func (*Encoder) String added in v0.9.0

func (e *Encoder) String(s string)

func (*Encoder) Texture added in v0.9.0

func (e *Encoder) Texture(t *Texture)

func (*Encoder) Time added in v0.9.0

func (e *Encoder) Time(t time.Time)

func (*Encoder) Uint added in v0.9.0

func (e *Encoder) Uint(v uint64)

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 dev builds the
	// implementation for go test and go run.
	Dir string
	// Langs are the languages the module is implemented in: swift,
	// rust, zig.
	Langs []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 *Encoder) (*Decoder, error)

Call calls a method of the object that returns at once, with the arguments args, which it releases. Its result is read from the decoder, which the caller closes.

func (*Object) CallContext

func (o *Object) CallContext(ctx context.Context, sym string, flags Flags, args *Encoder) (*Decoder, 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) Direct added in v0.9.0

func (o *Object) Direct(sym string, args ...uintptr) uintptr

Direct calls a method whose arguments and result are numbers, as the registers of a C function, without encoding them: integers as their bits, floats as those of a float64 (FloatArg), bools as 0 or 1 (BoolArg). It returns 0 if the object is closed or the implementation does not load, as such methods return no error.

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 *Encoder, 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.

Directories

Path Synopsis
internal
roundtrip
Package roundtrip is a native module for the tests of the runtime and the generator: every kind of value that crosses, sent to the platform and back, with calls of each shape.
Package roundtrip is a native module for the tests of the runtime and the generator: every kind of value that crosses, sent to the platform and back, with calls of each shape.
roundtrip/uses
Package uses is a native module that uses another, roundtrip: its structs, enums and objects cross from one module to the other.
Package uses is a native module that uses another, roundtrip: its structs, enums and objects cross from one module to the other.

Jump to

Keyboard shortcuts

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