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 ¶
- Variables
- func BoolArg(b bool) uintptr
- func Export(m *Module, v any, ...) uint64
- func FloatArg(f float64) uintptr
- func FloatResult(r uintptr) float64
- func Flush()
- func Forget(ids ...uint64)
- func Func(fn func(d *Decoder, e *Encoder) error, notify bool) uint64
- func HandleOf(v any) uint64
- func LibraryName(module string) string
- func Register(m *Module, wraps map[string]Wrapper)
- func Seq[T any](s iter.Seq2[T, error]) iter.Seq[T]
- func Stream[T any](ctx context.Context, o *Object, sym string, flags Flags, ...) iter.Seq2[T, error]
- func WindowHandle(w *mygo.Window) uint64
- func Wrap(h uint64, module, iface string) any
- type Buffer
- type Decoder
- func (d *Decoder) Bool() bool
- func (d *Decoder) Buffer() *Buffer
- func (d *Decoder) Bytes() []byte
- func (d *Decoder) Close() error
- func (d *Decoder) Duration() time.Duration
- func (d *Decoder) Err() error
- func (d *Decoder) Float() float64
- func (d *Decoder) Int() int64
- func (d *Decoder) Len() int
- func (d *Decoder) String() string
- func (d *Decoder) Time() time.Time
- func (d *Decoder) Uint() uint64
- type Encoder
- func (e *Encoder) Bool(v bool)
- func (e *Encoder) Buffer(b *Buffer)
- func (e *Encoder) Bytes(b []byte)
- func (e *Encoder) Duration(d time.Duration)
- func (e *Encoder) Float(v float64)
- func (e *Encoder) Int(v int64)
- func (e *Encoder) Len(n int)
- func (e *Encoder) Rect(x, y, w, h float32)
- func (e *Encoder) String(s string)
- func (e *Encoder) Texture(t *Texture)
- func (e *Encoder) Time(t time.Time)
- func (e *Encoder) Uint(v uint64)
- type Error
- type Flags
- type Module
- type Object
- func (o *Object) Call(sym string, flags Flags, args *Encoder) (*Decoder, error)
- func (o *Object) CallContext(ctx context.Context, sym string, flags Flags, args *Encoder) (*Decoder, error)
- func (o *Object) Close() error
- func (o *Object) Direct(sym string, args ...uintptr) uintptr
- func (o *Object) Handle() uint64
- func (o *Object) Module() *Module
- func (o *Object) Subscribe(sym string, flags Flags, args *Encoder, funcs ...uint64) io.Closer
- func (o *Object) View() uintptr
- type Texture
- type View
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
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, 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 FloatResult ¶ added in v0.9.0
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 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 ¶
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 ¶
LibraryName is the file of a module's implementation on this system.
func Register ¶
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 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 ¶
WindowHandle returns the native window of w, NSWindow* on macOS, or 0.
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.
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) Close ¶ added in v0.9.0
Close lets go of what the decoder read from, and reports whether every value decoded.
func (*Decoder) Len ¶ added in v0.9.0
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.
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) Buffer ¶ added in v0.9.0
Buffer writes a buffer's address and size, which the implementation uses without a copy.
func (*Encoder) Len ¶ added in v0.9.0
Len writes the length of a slice or a map, whose elements follow.
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.
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.
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 ¶
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 ¶
Close closes the object: the platform's close(), then its release. Later calls return ErrClosed.
func (*Object) Direct ¶ added in v0.9.0
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.
type 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.
Source Files
¶
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. |