wasm

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: MIT Imports: 13 Imported by: 0

README

wasm-go

WebAssembly + Go: a match made in heaven

Documentation

Overview

Package wasm provides the generic components used to build wazero plugins.

Index

Constants

View Source
const PageSize = 64 * 1024

PageSize is the size of memory pages in WebAssembly programs (64 KiB).

Variables

View Source
var (
	// ErrNoRuntime is an error returned when attempting to compile a host
	// module in a context which has no wazero runtime.
	ErrNoRuntime = errors.New("compilation context contains no wazero runtime")
)

Functions

func Build

func Build[T Module](runtime wazero.Runtime, mod HostModule[T], decorators ...Decorator[T]) wazero.HostModuleBuilder

Build builds the host module p in the wazero runtime r, returning the instance of HostModuleBuilder that was created. This is a low level function which is only exposed for certain advanced use cases where a program might not be able to leverage Compile/Instantiate, most application should not need to use this function.

func Configure

func Configure[T any](value T, options ...Option[T])

Configure applies the list of options to the value passed as first argument.

func Instantiate

func Instantiate[T Module](ctx *InstantiationContext, compiled *CompiledModule[T], opts ...Option[T]) (api.Module, error)

Instantiate creates an module instance for the given compiled wazero host module. The list of options is used to pass configuration to the module instance.

The function returns the wazero module instance that was created from the underlying compiled module. The returned module is bound to the instantiation context. If the module is closed, its state is automatically removed from the parent context, as well as removed from the parent wazero runtime like any other module instance closed by the application.

func NewCallContext

func NewCallContext(ctx context.Context, ins *InstantiationContext) context.Context

NewCallContext returns a Go context inheriting from ctx and containing the state needed for module instantiated from wazero host module to properly bind their methods to their receiver (e.g. the module instance).

Use this function when calling methods of an instantiated WebAssenbly module which may invoke exported functions of a wazero host module, for example:

// The program first creates the instantiation context and uses it to
// instantiate compiled host module (not shown here).
instiation := wasm.NewInstantiationContext(...)

...

// In this example the parent is the background context, but it might be any
// other Go context relevant to the application.
ctx = wasm.NewCallContext(context.Background(), instantiation)

start := module.ExportedFunction("_start")
r, err := start.Call(ctx)
if err != nil {
	...
}

func Read

func Read(memory api.Memory, offset, length uint32) []byte

Read returns a byte slice from a module memory. The function calls Read on the given memory and panics if offset/length are beyond the range of memory.

func WithCallContext

func WithCallContext[T Module](ctx context.Context, mod HostModule[T], opts ...Option[T]) (context.Context, func())

WithCallContext returns a Go context inheriting from ctx and containig the necessary state to be used in calls to exported functions of the given wazero host modul. This function is rarely used by applications, it is often more useful in tests to setup the test state without constructing the entire compilation and instantiation contexts (see NewCallContext instead).

Types

type Array

type Array[T primitive] []T

Array is a type representing a sequence of contiguous items in memory. Array values are composed of a pair of pointer and number of items. The item size is determined by the size of the type T.

Arrays only satisfy the Param interface, they cannot be used as return value of functions.

At this time, arrays may only be composed of primitive Go types, but this restriction may be relaxed in a future version. Use List to laod sequences of complex types.

func (Array[T]) FormatObject

func (arg Array[T]) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Array[T]) FormatValue

func (arg Array[T]) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Array[T]) LoadObject

func (arg Array[T]) LoadObject(memory api.Memory, object []byte) Array[T]

func (Array[T]) LoadValue

func (arg Array[T]) LoadValue(memory api.Memory, stack []uint64) Array[T]

func (Array[T]) ObjectSize

func (arg Array[T]) ObjectSize() int

func (Array[T]) StoreObject

func (arg Array[T]) StoreObject(memory api.Memory, object []byte)

func (Array[T]) ValueTypes

func (arg Array[T]) ValueTypes() []api.ValueType

type Bytes

type Bytes Array[byte]

Bytes is a type alias for arrays of bytes, which is a common use case (e.g. I/O functions working on a byte buffer).

func (Bytes) Format

func (arg Bytes) Format(w io.Writer)

func (Bytes) FormatObject

func (arg Bytes) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Bytes) FormatValue

func (arg Bytes) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Bytes) LoadObject

func (arg Bytes) LoadObject(memory api.Memory, object []byte) Bytes

func (Bytes) LoadValue

func (arg Bytes) LoadValue(memory api.Memory, stack []uint64) Bytes

func (Bytes) ObjectSize

func (arg Bytes) ObjectSize() int

func (Bytes) StoreObject

func (arg Bytes) StoreObject(memory api.Memory, object []byte)

func (Bytes) ValueTypes

func (arg Bytes) ValueTypes() []api.ValueType

type CompilationContext

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

CompilationContext is a type carrying the state needed to perform the compilation of wazero host modules.

func NewCompilationContext

func NewCompilationContext(ctx context.Context, rt wazero.Runtime) *CompilationContext

NewCompilationContext constructs a new wazero host module compilation context. The newly created instance captures the context and wazero runtime passed as arguments.

func (*CompilationContext) Close

Close closes the compilation context, making it unusable to the program.

type CompiledModule added in v0.2.0

type CompiledModule[T Module] struct {
	HostModule HostModule[T]
	wazero.CompiledModule
}

CompiledModule represents a compiled version of a wazero host module.

func Compile

func Compile[T Module](ctx *CompilationContext, mod HostModule[T], decorators ...Decorator[T]) (*CompiledModule[T], error)

Compile compiles a wazero host module within the given context.

type Decorator

type Decorator[T Module] interface {
	Decorate(module string, fn Function[T]) Function[T]
}

Decorator is an interface type which applies a transformation to a function.

func DecoratorFunc

func DecoratorFunc[T Module](d func(string, Function[T]) Function[T]) Decorator[T]

DecoratorFunc is a helper used to create decorators from functions using type inference to keep the syntax simple.

func Log

func Log[T Module](logger *log.Logger) Decorator[T]

Log constructs a function decorator which adds logging to function calls.

type Errno

type Errno int32

Errno is an error type representing error codes that are often returned by WebAssembly module functions.

This type is employed when converting Go errors embedded into optional values to WebAssembly error codes. Go errors are converted to Errno values by unwrapping and inspecting the values to determine their error code. If a Go error has an Errno method, it is called to be converted to an Errno value.

The special value 0 always indicates that there were no errors, similarly to how a Go program would treat a nil error.

func (Errno) Errno

func (err Errno) Errno() int32

Errno returns err as an int32 value.

func (Errno) Error

func (err Errno) Error() string

Error returns a human readable representation of err.

type Error

type Error = Optional[None]

Error is a special optional type which either contains an error or no values. It is useful to represent return values of functions that may error but do not return any other values.

func Fail

func Fail(err error) Error

Fail constructs an Error value from the given Go error. The function panics if err is nil.

type Float32

type Float32 float32

func (Float32) FormatObject

func (arg Float32) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Float32) FormatValue

func (arg Float32) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Float32) LoadObject

func (arg Float32) LoadObject(memory api.Memory, object []byte) Float32

func (Float32) LoadValue

func (arg Float32) LoadValue(memory api.Memory, stack []uint64) Float32

func (Float32) ObjectSize

func (arg Float32) ObjectSize() int

func (Float32) StoreObject

func (arg Float32) StoreObject(memory api.Memory, object []byte)

func (Float32) StoreValue

func (arg Float32) StoreValue(memory api.Memory, stack []uint64)

func (Float32) ValueTypes

func (arg Float32) ValueTypes() []api.ValueType

type Float64

type Float64 float64

func (Float64) FormatObject

func (arg Float64) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Float64) FormatValue

func (arg Float64) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Float64) LoadObject

func (arg Float64) LoadObject(memory api.Memory, object []byte) Float64

func (Float64) LoadValue

func (arg Float64) LoadValue(memory api.Memory, stack []uint64) Float64

func (Float64) ObjectSize

func (arg Float64) ObjectSize() int

func (Float64) StoreObject

func (arg Float64) StoreObject(memory api.Memory, object []byte)

func (Float64) StoreValue

func (arg Float64) StoreValue(memory api.Memory, stack []uint64)

func (Float64) ValueTypes

func (arg Float64) ValueTypes() []api.ValueType

type Function

type Function[T any] struct {
	Name    string
	Params  []Value
	Results []Value
	Func    func(T, context.Context, api.Module, []uint64)
}

Function represents a single function exported by a plugin. Programs may configure the fields individually but it is often preferrable to use one of the Func* constructors instead to let the Go compiler ensure type and memory safety when generating the code to bridge between WebAssembly and Go.

func F0

func F0[T any, R Result](fn func(T, context.Context) R) Function[T]

F0 is the Function constructor for functions accepting no parameters.

func F1

func F1[T any, P Param[P], R Result](fn func(T, context.Context, P) R) Function[T]

F1 is the Function constructor for functions accepting one parameter.

func F2

func F2[
	T any,
	P1 Param[P1],
	P2 Param[P2],
	R Result,
](fn func(T, context.Context, P1, P2) R) Function[T]

F2 is the Function constructor for functions accepting two parameters.

func F3

func F3[
	T any,
	P1 Param[P1],
	P2 Param[P2],
	P3 Param[P3],
	R Result,
](fn func(T, context.Context, P1, P2, P3) R) Function[T]

F3 is the Function constructor for functions accepting three parameters.

func F4

func F4[
	T any,
	P1 Param[P1],
	P2 Param[P2],
	P3 Param[P3],
	P4 Param[P4],
	R Result,
](fn func(T, context.Context, P1, P2, P3, P4) R) Function[T]

F4 is the Function constructor for functions accepting four parameters.

func F5

func F5[
	T any,
	P1 Param[P1],
	P2 Param[P2],
	P3 Param[P3],
	P4 Param[P4],
	P5 Param[P5],
	R Result,
](fn func(T, context.Context, P1, P2, P3, P4, P5) R) Function[T]

F5 is the Function constructor for functions accepting five parameters.

func F6

func F6[
	T any,
	P1 Param[P1],
	P2 Param[P2],
	P3 Param[P3],
	P4 Param[P4],
	P5 Param[P5],
	P6 Param[P6],
	R Result,
](fn func(T, context.Context, P1, P2, P3, P4, P5, P6) R) Function[T]

F6 is the Function constructor for functions accepting six parameters.

func F7

func F7[
	T any,
	P1 Param[P1],
	P2 Param[P2],
	P3 Param[P3],
	P4 Param[P4],
	P5 Param[P5],
	P6 Param[P6],
	P7 Param[P7],
	R Result,
](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7) R) Function[T]

F7 is the Function constructor for functions accepting seven parameters.

func F8

func F8[
	T any,
	P1 Param[P1],
	P2 Param[P2],
	P3 Param[P3],
	P4 Param[P4],
	P5 Param[P5],
	P6 Param[P6],
	P7 Param[P7],
	P8 Param[P8],
	R Result,
](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7, P8) R) Function[T]

F8 is the Function constructor for functions accepting eight parameters.

type Functions

type Functions[T any] map[string]Function[T]

Functions is a map type representing the collection of functions exported by a plugin. The map keys are the names of that each function gets exported as. The function value is the description of the wazero host function to be added when building a plugin. The type parameter T is used to ensure consistency between the plugin definition and the functions that compose it.

type HostModule added in v0.2.0

type HostModule[T Module] interface {
	// Returns the name of the host module (e.g. "wasi_snapshot_preview1").
	Name() string
	// Returns the collection of functions exported by the host module.
	// The method may return the same value across multiple calls to this
	// method, the program is expected to treat it as a read-only value.
	Functions() Functions[T]
	// Creates a new instance of the host module type, using the list of options
	// passed as arguments to configure it. This method is intended to be called
	// automatically when instantiating a module via an instantiation context.
	Instantiate(...Option[T]) T
}

HostModule is an interface representing type-safe wazero host modules. The interface is parametrized on the module type that it instantiates.

HostModule instances are expected to be immutable and therfore safe to use concurrently from multiple goroutines.

type InstantiationContext

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

InstantiationContext is a type carrying the state of instantiated wazero host modules. This context must be used to create call contexts to invoke exported functions of WebAssembly modules (see NewCallContext).

func NewInstantiationContext

func NewInstantiationContext(ctx context.Context, rt wazero.Runtime) *InstantiationContext

NewInstantiationContext creates a new wazero host module instantiation context.

func (*InstantiationContext) Close

func (ins *InstantiationContext) Close(ctx context.Context) error

Close closes the instantiation context, making it unusable to the program.

Closing the context alos closes all modules that were instantiated from it and implement the io.Closer interface.

type Int16

type Int16 int16

func (Int16) FormatObject

func (arg Int16) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Int16) FormatValue

func (arg Int16) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Int16) LoadObject

func (arg Int16) LoadObject(memory api.Memory, object []byte) Int16

func (Int16) LoadValue

func (arg Int16) LoadValue(memory api.Memory, stack []uint64) Int16

func (Int16) ObjectSize

func (arg Int16) ObjectSize() int

func (Int16) StoreObject

func (arg Int16) StoreObject(memory api.Memory, object []byte)

func (Int16) StoreValue

func (arg Int16) StoreValue(memory api.Memory, stack []uint64)

func (Int16) ValueTypes

func (arg Int16) ValueTypes() []api.ValueType

type Int32

type Int32 int32

func (Int32) FormatObject

func (arg Int32) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Int32) FormatValue

func (arg Int32) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Int32) LoadObject

func (arg Int32) LoadObject(memory api.Memory, object []byte) Int32

func (Int32) LoadValue

func (arg Int32) LoadValue(memory api.Memory, stack []uint64) Int32

func (Int32) ObjectSize

func (arg Int32) ObjectSize() int

func (Int32) StoreObject

func (arg Int32) StoreObject(memory api.Memory, object []byte)

func (Int32) StoreValue

func (arg Int32) StoreValue(memory api.Memory, stack []uint64)

func (Int32) ValueTypes

func (arg Int32) ValueTypes() []api.ValueType

type Int64

type Int64 int64

func (Int64) FormatObject

func (arg Int64) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Int64) FormatValue

func (arg Int64) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Int64) LoadObject

func (arg Int64) LoadObject(memory api.Memory, object []byte) Int64

func (Int64) LoadValue

func (arg Int64) LoadValue(memory api.Memory, stack []uint64) Int64

func (Int64) ObjectSize

func (arg Int64) ObjectSize() int

func (Int64) StoreObject

func (arg Int64) StoreObject(memory api.Memory, object []byte)

func (Int64) StoreValue

func (arg Int64) StoreValue(memory api.Memory, stack []uint64)

func (Int64) ValueTypes

func (arg Int64) ValueTypes() []api.ValueType

type Int8

type Int8 int8

func (Int8) FormatObject

func (arg Int8) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Int8) FormatValue

func (arg Int8) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Int8) LoadObject

func (arg Int8) LoadObject(memory api.Memory, object []byte) Int8

func (Int8) LoadValue

func (arg Int8) LoadValue(memory api.Memory, stack []uint64) Int8

func (Int8) ObjectSize

func (arg Int8) ObjectSize() int

func (Int8) StoreObject

func (arg Int8) StoreObject(memory api.Memory, object []byte)

func (Int8) StoreValue

func (arg Int8) StoreValue(memory api.Memory, stack []uint64)

func (Int8) ValueTypes

func (arg Int8) ValueTypes() []api.ValueType

type List

type List[T Object[T]] struct {
	// contains filtered or unexported fields
}

List represents a sequence of objects held in module memory.

func (List[T]) FormatValue

func (arg List[T]) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (List[T]) Index

func (arg List[T]) Index(index int) T

func (List[T]) Len

func (arg List[T]) Len() int

func (List[T]) LoadValue

func (arg List[T]) LoadValue(memory api.Memory, stack []uint64) List[T]

func (List[T]) Range

func (arg List[T]) Range(fn func(int, T) bool)

func (List[T]) ValueTypes

func (arg List[T]) ValueTypes() []api.ValueType

type Memory

type Memory []byte

Memory is an implementation of the api.Memory interface of wazero backed by a Go byte slice. The memory has a fixed size and cannot grow nor shrink.

This type is mostly useful in tests to construct memory areas where output parameters can be stored.

func NewFixedSizeMemory

func NewFixedSizeMemory(size uint32) Memory

NewFixedSizeMemory constructs a Memory instance of size bytes aligned on the WebAssembly page size.

func (Memory) Definition

func (mem Memory) Definition() api.MemoryDefinition

func (Memory) Grow

func (mem Memory) Grow(uint32) (uint32, bool)

func (Memory) Read

func (mem Memory) Read(offset, length uint32) ([]byte, bool)

func (Memory) ReadByte

func (mem Memory) ReadByte(offset uint32) (byte, bool)

func (Memory) ReadFloat32Le

func (mem Memory) ReadFloat32Le(offset uint32) (float32, bool)

func (Memory) ReadFloat64Le

func (mem Memory) ReadFloat64Le(offset uint32) (float64, bool)

func (Memory) ReadUint16Le

func (mem Memory) ReadUint16Le(offset uint32) (uint16, bool)

func (Memory) ReadUint32Le

func (mem Memory) ReadUint32Le(offset uint32) (uint32, bool)

func (Memory) ReadUint64Le

func (mem Memory) ReadUint64Le(offset uint32) (uint64, bool)

func (Memory) Size

func (mem Memory) Size() uint32

func (Memory) Write

func (mem Memory) Write(offset uint32, value []byte) bool

func (Memory) WriteByte

func (mem Memory) WriteByte(offset uint32, value byte) bool

func (Memory) WriteFloat32Le

func (mem Memory) WriteFloat32Le(offset uint32, value float32) bool

func (Memory) WriteFloat64Le

func (mem Memory) WriteFloat64Le(offset uint32, value float64) bool

func (Memory) WriteString

func (mem Memory) WriteString(offset uint32, value string) bool

func (Memory) WriteUint16Le

func (mem Memory) WriteUint16Le(offset uint32, value uint16) bool

func (Memory) WriteUint32Le

func (mem Memory) WriteUint32Le(offset uint32, value uint32) bool

func (Memory) WriteUint64Le

func (mem Memory) WriteUint64Le(offset uint32, value uint64) bool

type Module

type Module interface{ api.Closer }

Module is a type constraint used to validate that all module instances created from wazero host modules abide to the same set of requirements.

type None

type None struct{}

None is a special type of size zero bytes.

func (None) FormatObject

func (None) FormatObject(w io.Writer, _ api.Memory, _ []byte)

func (None) FormatValue

func (None) FormatValue(w io.Writer, _ api.Memory, _ []uint64)

func (None) LoadObject

func (None) LoadObject(api.Memory, []byte) (none None)

func (None) LoadValue

func (None) LoadValue(api.Memory, []uint64) (none None)

func (None) ObjectSize

func (None) ObjectSize() int

func (None) StoreObject

func (None) StoreObject(api.Memory, []byte)

func (None) StoreValue

func (None) StoreValue(api.Memory, []uint64)

func (None) ValueTypes

func (None) ValueTypes() []api.ValueType

type Object

type Object[T any] interface {
	// Writes a human-readable representation of the object to w.
	FormatObject(w io.Writer, memory api.Memory, object []byte)
	// Loads and returns the object from the given byte slice. If the object
	// contains pointers it migh read them from the module memory passed as
	// first argument.
	LoadObject(memory api.Memory, object []byte) T
	// Stores the object to the given tye slice. If the object contains pointers
	// it might write them to the module memory passed as first argument (doing
	// so without writing to arbitrary location is difficult so this use case is
	// rather rare).
	StoreObject(memory api.Memory, object []byte)
	// Returns the size of the the object in bytes. The byte slices passed to
	// LoadObject and StoreObjects are guaranteed to be at least of the length
	// returned by this method.
	ObjectSize() int
}

Object is an interface which represents values that can be loaded or stored in memory.

The interface is mainly used as a constraint for generic type parameters.

type Option

type Option[T any] interface {
	// Configure is called to apply the configuration option to the value passed
	// as argument.
	Configure(T)
}

Option is a generic interface used to represent options that apply configuration to a value.

func OptionFunc

func OptionFunc[T any](opt func(T)) Option[T]

OptionFunc is a constructor which creates an option from a function. This function is useful to leverage type inference and not have to repeat the type T in the type parameter.

type Optional

type Optional[T ParamResult[T]] struct {
	// contains filtered or unexported fields
}

Optional represents a function result which may be missing due to the program encountering an error. The type contains either a value of type T or an error.

var OK Optional[None]

OK is a special value indicating that a function which returns nothing has not errored either.

func Err

func Err[T ParamResult[T]](err error) Optional[T]

Err constructs an optional from an error. The function panics if the error is nil since a nil error indicates that the optional should contain a value.

func Opt

func Opt[T ParamResult[T]](res T, err error) Optional[T]

Opt constructs an optional from a pair of a result and error.

func Res

func Res[T ParamResult[T]](res T) Optional[T]

Res constructs an optional from a value.

func (Optional[T]) Error

func (opt Optional[T]) Error() error

Error returns the error embedded in opt, or nil if opt contains a value.

func (Optional[T]) FormatValue

func (opt Optional[T]) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Optional[T]) LoadValue

func (opt Optional[T]) LoadValue(memory api.Memory, stack []uint64) Optional[T]

func (Optional[T]) Result

func (opt Optional[T]) Result() T

Value returns the underlying value of the optional. The method panics if opt contained an error.

func (Optional[T]) StoreValue

func (opt Optional[T]) StoreValue(memory api.Memory, stack []uint64)

func (Optional[T]) ValueTypes

func (opt Optional[T]) ValueTypes() []api.ValueType

type Param

type Param[T any] interface {
	Value
	// Loads and returns the parameter value from the stack and optionally
	// reading its content from memory.
	LoadValue(memory api.Memory, stack []uint64) T
}

Param is an interface representing parameters of WebAssembly functions which are read form the stack.

The interface is mainly used as a constraint for generic type parameters.

type ParamResult

type ParamResult[T any] interface {
	Param[T]
	Result
}

type Pointer

type Pointer[T Object[T]] struct {
	// contains filtered or unexported fields
}

Pointer is a parameter type used to represent a pointer to an object held in program memory.

func New

func New[T Object[T]]() Pointer[T]

New constructs a pointer to an object of type T backed by Go memory.

This function is mostly useful to construct pointers to pass to module methods in tests, its usage in actual production code should be rare.

func Ptr

func Ptr[T Object[T]](memory api.Memory, offset uint32) Pointer[T]

Ptr constructs a pointer of objects T backed by a memory area at a specified offset.

This function is mostly useful to construct pointers to pass to module methods in tests, its usage in actual production code should be rare.

func (Pointer[T]) FormatValue

func (arg Pointer[T]) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Pointer[T]) Index

func (arg Pointer[T]) Index(index int) Pointer[T]

func (Pointer[T]) Load

func (arg Pointer[T]) Load() (value T)

func (Pointer[T]) LoadValue

func (arg Pointer[T]) LoadValue(memory api.Memory, stack []uint64) Pointer[T]

func (Pointer[T]) Memory

func (arg Pointer[T]) Memory() api.Memory

func (Pointer[T]) Object

func (arg Pointer[T]) Object() []byte

func (Pointer[T]) Offset

func (arg Pointer[T]) Offset() uint32

func (Pointer[T]) Store

func (arg Pointer[T]) Store(value T)

func (Pointer[T]) ValueTypes

func (arg Pointer[T]) ValueTypes() []api.ValueType

type Result

type Result interface {
	Value
	// Stores the result value onto the stack and optionally writing its
	// content to memory.
	StoreValue(memory api.Memory, stack []uint64)
}

Result is an interface reprenting results of WebAssembly functions which are written to the stack.

The interface is mainly used as a constraint for generic type parameters.

type SEGFAULT

type SEGFAULT struct{ Offset, Length uint32 }

SEGFAULT is an error type used as value in panics triggered by reading outside of the addressable memory of a program.

func (SEGFAULT) Error

func (err SEGFAULT) Error() string

type String

type String string

String is similar to Bytes but holds the value as a Go string which is not sharing memory with the WebAssembly program memory anymore.

func (String) FormatValue

func (arg String) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (String) LoadValue

func (arg String) LoadValue(memory api.Memory, stack []uint64) String

func (String) ValueTypes

func (arg String) ValueTypes() []api.ValueType

type Uint16

type Uint16 uint16

func (Uint16) FormatObject

func (arg Uint16) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Uint16) FormatValue

func (arg Uint16) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Uint16) LoadObject

func (arg Uint16) LoadObject(memory api.Memory, object []byte) Uint16

func (Uint16) LoadValue

func (arg Uint16) LoadValue(memory api.Memory, stack []uint64) Uint16

func (Uint16) ObjectSize

func (arg Uint16) ObjectSize() int

func (Uint16) StoreObject

func (arg Uint16) StoreObject(memory api.Memory, object []byte)

func (Uint16) StoreValue

func (arg Uint16) StoreValue(memory api.Memory, stack []uint64)

func (Uint16) ValueTypes

func (arg Uint16) ValueTypes() []api.ValueType

type Uint32

type Uint32 uint32

func (Uint32) FormatObject

func (arg Uint32) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Uint32) FormatValue

func (arg Uint32) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Uint32) LoadObject

func (arg Uint32) LoadObject(memory api.Memory, object []byte) Uint32

func (Uint32) LoadValue

func (arg Uint32) LoadValue(memory api.Memory, stack []uint64) Uint32

func (Uint32) ObjectSize

func (arg Uint32) ObjectSize() int

func (Uint32) StoreObject

func (arg Uint32) StoreObject(memory api.Memory, object []byte)

func (Uint32) StoreValue

func (arg Uint32) StoreValue(memory api.Memory, stack []uint64)

func (Uint32) ValueTypes

func (arg Uint32) ValueTypes() []api.ValueType

type Uint64

type Uint64 uint64

func (Uint64) FormatObject

func (arg Uint64) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Uint64) FormatValue

func (arg Uint64) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Uint64) LoadObject

func (arg Uint64) LoadObject(memory api.Memory, object []byte) Uint64

func (Uint64) LoadValue

func (arg Uint64) LoadValue(memory api.Memory, stack []uint64) Uint64

func (Uint64) ObjectSize

func (arg Uint64) ObjectSize() int

func (Uint64) StoreObject

func (arg Uint64) StoreObject(memory api.Memory, object []byte)

func (Uint64) StoreValue

func (arg Uint64) StoreValue(memory api.Memory, stack []uint64)

func (Uint64) ValueTypes

func (arg Uint64) ValueTypes() []api.ValueType

type Uint8

type Uint8 uint8

func (Uint8) FormatObject

func (arg Uint8) FormatObject(w io.Writer, memory api.Memory, object []byte)

func (Uint8) FormatValue

func (arg Uint8) FormatValue(w io.Writer, memory api.Memory, stack []uint64)

func (Uint8) LoadObject

func (arg Uint8) LoadObject(memory api.Memory, object []byte) Uint8

func (Uint8) LoadValue

func (arg Uint8) LoadValue(memory api.Memory, stack []uint64) Uint8

func (Uint8) ObjectSize

func (arg Uint8) ObjectSize() int

func (Uint8) StoreObject

func (arg Uint8) StoreObject(memory api.Memory, object []byte)

func (Uint8) StoreValue

func (arg Uint8) StoreValue(memory api.Memory, stack []uint64)

func (Uint8) ValueTypes

func (arg Uint8) ValueTypes() []api.ValueType

type Value

type Value interface {
	// Writes a human-readable representation of the field to w, using the
	// memory and stack of a program to locate the field value.
	FormatValue(w io.Writer, memory api.Memory, stack []uint64)
	// Returns the sequence of primitive types that the result value is
	// composed of. Values of primitive types will have a single type,
	// but more complex types will have more (e.g. a buffer may hold two
	// 32 bits integers for the pointer and length). The number of types
	// indicates the number of words that are consumed from the stack when
	// loading the parameter.
	ValueTypes() []api.ValueType
}

Value represents a field in a function signature, which may be a parameter or a result.

Directories

Path Synopsis
Package wasmtest provides building blocks useful to write tests for wazero host modules.
Package wasmtest provides building blocks useful to write tests for wazero host modules.

Jump to

Keyboard shortcuts

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