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 ¶
- Variables
- func Export(m *Module, v any, ...) uint64
- func Flush()
- func Forget(ids ...uint64)
- func Func(fn func(Args) (any, 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 StreamMap[D, 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 Args
- type Buffer
- type Error
- type Flags
- type Module
- type Object
- func (o *Object) Call(sym string, flags Flags, args []any, result any) error
- func (o *Object) CallContext(ctx context.Context, sym string, flags Flags, args []any, result any) error
- func (o *Object) Close() error
- func (o *Object) Handle() uint64
- func (o *Object) Module() *Module
- func (o *Object) Subscribe(sym string, flags Flags, args []any, 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, 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 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 ¶
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 macOS.
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() ([]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 ¶
WindowHandle returns the native window of w, NSWindow* on macOS, or 0.
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.
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 (*Buffer) Bytes ¶
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) MarshalJSON ¶
MarshalJSON gives the platform the buffer's address and size.
func (*Buffer) UnmarshalJSON ¶
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.
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.
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, 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 ¶
Close closes the object: the platform's close(), then its release. Later calls return ErrClosed.
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.