Documentation
¶
Overview ¶
Package ffi loads native libraries, such as a Rust crate or a Zig package built as a dynamic library, and binds their functions to Go functions, without cgo.
It is experimental, as native modules are: its API and golet-plugin.json may change in breaking ways in any release.
A package names the libraries it binds in golet-plugin.json, next to its sources, with how to build them:
{"libraries": [{"name": "adder", "build": {"cargo": "./rust"}}]}
`golet dev` and `golet build` build them for the app's platform (cargo build or zig build) and put them among the app's resources; `golet native build` builds them for every platform, ready to publish, so that the apps of others download them instead of building them. The package embeds the manifest and binds the library's functions:
//go:embed golet-plugin.json
var manifest []byte
var adder = ffi.New("adder", manifest)
var fns struct {
Add func(a, b int32) int32
Greeting func(name string) string `ffi:"adder_greeting"`
}
func Add(a, b int32) (int32, error) {
if err := adder.Bind(&fns); err != nil {
return 0, err
}
return fns.Add(a, b), nil
}
Bind binds the functions once; calling it again, from any goroutine, only returns the first call's result, cheaply, so functions call it before using fns. Its success orders the binding before what follows it.
Functions are called through purego (github.com/ebitengine/purego), which converts their arguments and results as the C ABI of the platform passes them: integers, floats, pointers, bools, strings (to and from NUL-terminated C strings) and, on some platforms, structs of those. Libraries are loaded once and never unloaded. ffi works on macOS, Linux and Windows on amd64 and arm64; elsewhere Load reports errors.ErrUnsupported.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnvVar ¶
EnvVar returns the environment variable that overrides where a library is found: GOLET_NATIVE_ and its name in upper case, with characters other than letters and digits as underscores ("adder" is GOLET_NATIVE_ADDER).
Types ¶
type Library ¶
type Library struct {
// contains filtered or unexported fields
}
Library is a native library, loaded on first use. Its methods are safe for concurrent use.
func New ¶
New returns the library named name in manifest, the content of the package's golet-plugin.json. A nil manifest leaves the library to $GOLET_NATIVE_<NAME>, the app's resources, the executable's directory and the CLI's builds (see Path). New only reads the manifest: the library is loaded on first use, and an invalid manifest, or one that does not name the library, is reported then.
name is the library's name without its prefix and extension: "adder" is libadder.dylib on macOS, libadder.so on Linux and adder.dll on Windows, unless the manifest's files name others.
func (*Library) Bind ¶
Bind sets the function fields of the struct fns points to to call the library's functions, loading the library. A field's tag names its function, which is otherwise its name in snake case (AddNumbers calls add_numbers):
var fns struct {
Add func(a, b int32) int32
Version func() string `ffi:"adder_version"`
Extra func() `ffi:"adder_extra,optional"` // nil when missing
Cache map[string]int // not a function: left alone
Hidden func() `ffi:"-"` // left alone
}
It binds them once: later calls with the same pointer, from any goroutine, return the first one's result without binding again, so functions can call Bind before each use of fns. Fields bound before the first call are left alone.
func (*Library) BindFunc ¶
BindFunc sets the function fptr points to to call the library's function named name, loading the library, once: later calls with the same pointer return the first one's result.
func (*Library) From ¶
From tells l the directory of the package that binds it, when the program knows it, as from runtime.Caller: the CLI's builds in that package's module are found too, whatever the working directory. It returns l, and is called before the library loads.
func (*Library) Path ¶
Path returns the file Load loads, the first of:
- the file $GOLET_NATIVE_<NAME> names (see EnvVar);
- the one among the app's resources (golet.PathResources), where `golet build` and `golet dev` put it, signed with the app on macOS;
- on macOS, the one in the app's Contents/Frameworks, where Swift libraries go;
- the one next to the executable;
and, in programs that are not packaged apps, as under `go run` and `go test`:
- the one the CLI built from sources (`golet native build -target host`, or `golet dev`), in .golet/native/<goos>-<goarch> of the module of the package's directory (see From) or of the working directory;
- the published one in the user's cache (<cache>/golet/natives/<sha256>/), downloaded there when it is missing, checking its SHA-256.
Packaged apps never download libraries: build them with the CLI.