Documentation
¶
Overview ¶
Copyright 2026 Michal Vyskocil. All rights reserved. Use of this source code is governed by a MIT license that can be found in the LICENSE file.
Package wasm provides gio helpers for "github.com/tetratelabs/wazero" providing a compatibility layer for WebAssembly binaries. Integration of WebAssembly is a bit complicated, however this module provides helpers making wazero easier to use.
- NewBuilder returns a Builder that holds mount and compiled cache configurations.
- Builder.Build creates a Runtime backed by a wazero runtime instance and the builder configuration.
- Runtime.Compile compiles a wasm binary and return a FilterBuilder.
- FilterBuilder.BuilderFunc returns a unix.FilterBuilderFunc suitable for any gonix consumer expecting one (sh, xargs, …).
Multicall binaries ¶
Multi-call binaries (one wasm file dispatching multiple subcommands, such as uutils/coreutils) use FilterBuilder.WithMulticall to prepend a fixed argv[0] on every invocation.
codeberg.org/gonix/wasm/coreutils is the reference consumer: it embeds the rust/uutils coreutils.wasm blob and exposes one unix.FilterBuilderFunc per tool.
Mount layering ¶
Filesystem visibility inside the wasm sandbox comes from
- Static mounts on Builder via Builder.WithDirMounts and Builder.WithFSMounts: applied to every Run on every Runtime built from that Builder. Use for read-only system overlays or shared directories.
CWD ¶
The unix.GetDirFunc(ctx) is used to pass the cwd from a shell to the filter. This is passed as PWD environment variable into wasm sandbox.
Implicit compilation cache ¶
wasm creates an implicit wazero.CompilationCache by default. This is shared across all Runtime in a single process. The Builder.WithCacheDir can change the cache to be directory baked
Index ¶
Constants ¶
const ( // ExitCodeTimeout is returned when a command context deadline expires // This is the same exit code as GNU timeout(1) returns ExitCodeTimeout = 124 // ExitCodeCanceled is returned if the parent context is canceled ExitCodeCanceled = 130 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder builds a wazero runtime a main shared context for wasm execution - it passes a configuration needed to create one The individual calls Set changes the state of the Builder
func NewBuilder ¶
func NewBuilder() Builder
func (Builder) Build ¶
Build creates new wazero runtime and returns an wasm abstraction It captures the existing state of the builder and further calls to With functions won't affect the created Runtime.
func (*Builder) Close ¶
Close invalidates the on-disk wasm cache if was configured. This uses a pointer receiver, so modify the Builder itself. Calling it twice is safe.
func (Builder) WithCacheDir ¶
WithCacheDir setups dir as a new cache dir. It's an error to call this twice. Return an error if the cache directory can't be setup.
Due the dependency on a fallible resource this can't be chained like other With calls.
func (Builder) WithDirMounts ¶
WithDirMounts sets the parameters for wazero.FSConfig
func (Builder) WithFSMounts ¶
WithFSMounts sets the parameters for wazero.FSConfig
type DirMount ¶
DirMount is a parameter for wazero.WithDirMount or wazero.WithReadOnlyDirMount
type ExitedError ¶ added in v0.1.1
func (*ExitedError) Error ¶ added in v0.1.1
func (e *ExitedError) Error() string
func (*ExitedError) ExitCode ¶ added in v0.1.1
func (e *ExitedError) ExitCode() int
ExitCode reads an exit code, which can be propagated
func (*ExitedError) Unwrap ¶ added in v0.1.1
func (e *ExitedError) Unwrap() error
type FSMount ¶
FSMount is a wrapper for wazero.WithFSMount
type FilterBuilder ¶
type FilterBuilder struct {
// contains filtered or unexported fields
}
FilterBuilder captures the Runtime and wazero.CompiledModule and exposes FilterBuilder.BuilderFunc which returns new unix.Filter implemented by wazero.
func (FilterBuilder) BuilderFunc ¶
func (b FilterBuilder) BuilderFunc(name string) unix.FilterBuilderFunc
BuilderFunc returns a unix.FilterBuilderFunc - it is intentionally incompatible with unix.FilterLookupFunc pattern as this base layer does not know which commands are exported
func (FilterBuilder) WithMulticall ¶
func (b FilterBuilder) WithMulticall(prefix string) FilterBuilder
WithMulticall sets the binary as a multicall
eg WithMulticall("coreutils") will change argv from echo "hello" to coreutils echo "hello"
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime connects the builder with a wazero runtime - all builder parameters are passed to the wazero Runtime when compiling the module
func (*Runtime) Close ¶
Close closes wasm runtime - all compiled modules will be freed, so calling any web assembly code after this is an error
func (*Runtime) Compile ¶
Compile returns the FilterBuilder - which can be used to build individual wasm based filters. Safe to call before or after Runtime.Close
Directories
¶
| Path | Synopsis |
|---|---|
|
coreutils
module
|
|
|
internal
|
|
|
compile
command
Compile and store wasm compilation cache to speedup tests - wasm compile under race detector takes ages - repeated test runs are much faster because of that
|
Compile and store wasm compilation cache to speedup tests - wasm compile under race detector takes ages - repeated test runs are much faster because of that |