Help us shape the future of Go! Take the 2026 Go Developer Survey

wasi

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package wasi is a WASI preview 1 (wasi_snapshot_preview1) host written against a byte-addressed guest memory and the engine's rootfs.FS.

It implements every function Go's wasip1 port imports (runtime, syscall, internal/syscall/unix), with the layouts and errno values of the snapshot-01 specification (docs/REFERENCES.md). It knows nothing of a WebAssembly runtime: a host calls Proc.Call with the raw arguments and a Memory, so the same code serves the native executor (as a wazero host module) and the browser executor (from a worker, over shared memory).

Index

Constants

View Source
const (
	ESUCCESS     = 0
	E2BIG        = 1
	EACCES       = 2
	EAGAIN       = 6
	EBADF        = 8
	EEXIST       = 20
	EFAULT       = 21
	EINVAL       = 28
	EIO          = 29
	EISDIR       = 31
	ELOOP        = 32
	ENAMETOOLONG = 37
	ENOENT       = 44
	ENOSPC       = 51
	ENOSYS       = 52
	ENOTDIR      = 54
	ENOTEMPTY    = 55
	ENOTSUP      = 58
	EPERM        = 63
	EROFS        = 69
	ESPIPE       = 70
	ENOTCAPABLE  = 76
)

Errno values of wasi_snapshot_preview1 (enum order of the spec).

View Source
const (
	I32 = 'i'
	I64 = 'I'
)

Param types of a function's core-wasm signature.

View Source
const MaxFDs = 1024

MaxFDs bounds the descriptors a process may hold.

Variables

View Source
var FuncIndex = func() map[string]int {
	m := make(map[string]int, len(Funcs))
	for i, f := range Funcs {
		m[f.Name] = i
	}
	return m
}()

FuncIndex maps a name to its index in Funcs.

View Source
var Funcs = []Func{
	{"args_get", "ii", false},
	{"args_sizes_get", "ii", false},
	{"environ_get", "ii", false},
	{"environ_sizes_get", "ii", false},
	{"clock_res_get", "ii", false},
	{"clock_time_get", "iIi", false},
	{"fd_advise", "iIIi", false},
	{"fd_allocate", "iII", false},
	{"fd_close", "i", false},
	{"fd_datasync", "i", false},
	{"fd_fdstat_get", "ii", false},
	{"fd_fdstat_set_flags", "ii", false},
	{"fd_fdstat_set_rights", "iII", false},
	{"fd_filestat_get", "ii", false},
	{"fd_filestat_set_size", "iI", false},
	{"fd_filestat_set_times", "iIIi", false},
	{"fd_pread", "iiiIi", false},
	{"fd_prestat_get", "ii", false},
	{"fd_prestat_dir_name", "iii", false},
	{"fd_pwrite", "iiiIi", false},
	{"fd_read", "iiii", false},
	{"fd_readdir", "iiiIi", false},
	{"fd_renumber", "ii", false},
	{"fd_seek", "iIii", false},
	{"fd_sync", "i", false},
	{"fd_tell", "ii", false},
	{"fd_write", "iiii", false},
	{"path_create_directory", "iii", false},
	{"path_filestat_get", "iiiii", false},
	{"path_filestat_set_times", "iiiiIIi", false},
	{"path_link", "iiiiiii", false},
	{"path_open", "iiiiiIIii", false},
	{"path_readlink", "iiiiii", false},
	{"path_remove_directory", "iii", false},
	{"path_rename", "iiiiii", false},
	{"path_symlink", "iiiii", false},
	{"path_unlink_file", "iii", false},
	{"poll_oneoff", "iiii", false},
	{"proc_exit", "i", true},
	{"proc_raise", "i", false},
	{"sched_yield", "", false},
	{"random_get", "ii", false},
	{"sock_accept", "iii", false},
	{"sock_recv", "iiiiii", false},
	{"sock_send", "iiiii", false},
	{"sock_shutdown", "ii", false},
}

Funcs lists the imports, in a fixed order a host may index by.

Functions

This section is empty.

Types

type Config

type Config struct {
	Args, Env      []string
	FS             *rootfs.FS
	Stdin          io.Reader
	Stdout, Stderr io.Writer
	// Now is the realtime clock (default time.Now).
	Now func() time.Time
	// Rand is the randomness source (default crypto/rand).
	Rand io.Reader
}

Config is a process's world.

type Func

type Func struct {
	Name   string
	Params string // one of I32/I64 per parameter
	// NoResult is set for proc_exit; every other function returns an errno.
	NoResult bool
}

Func describes one import.

type Memory

type Memory interface {
	Read(off, n uint32) ([]byte, bool)
	Write(off uint32, b []byte) bool
}

Memory is the guest's linear memory. Read returns a slice valid until the next call on the Memory; both return false out of bounds.

type Proc

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

Proc is one guest process's WASI state.

func New

func New(cfg Config) *Proc

New returns a process with stdin, stdout, stderr and "/" preopened as 3.

func (*Proc) Call

func (p *Proc) Call(fn int, args []uint64, m Memory) Result

Call runs import number fn of Funcs with args (one per parameter, i32 values in the low 32 bits).

type Result

type Result struct {
	Errno uint32
	// Sleep asks the host to block the guest this long before returning
	// (poll_oneoff on clocks). The host may cut it short on a kill.
	Sleep time.Duration
	// Exit is set by proc_exit, with Code.
	Exit bool
	Code uint32
}

Result is what a call returned.

Jump to

Keyboard shortcuts

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