# README
wypes
Go library to define type-safe host functions in wazero and other WebAssembly runtimes.
Features:
- ๐ก Type safe
- ๐ Fast
- ๐จ Works with any WebAssmebly runtime, like wazero or wasman
- ๐ง Handles for you memory operations
- ๐ Manages external references
- ๐งผ Simple and clean API
- ๐ Can be compiled with TinyGo
- ๐ No reflect, no unsafe, only generics and dedication.
๐ฆ Installation
go get github.com/orsinium-labs/wypes
๐ง Usage
Define a function using provided types:
func addI32(a wypes.Int32, b wypes.Int32) wypes.Int32 {
return a + b
}
Define a mapping of module and function names to function definitions:
modules := wypes.Modules{
"env": {
"add_i32": wypes.H2(addI32),
},
}
Link the modules to the runtime. We provide a convenience method to do this for wazero:
err := modules.DefineWazero(r, nil)
That's it! Now the wasm module can call the env.add_i32
function.
๐น Tricks
The library provides lots of useful types that you can use in your functions. Make sure to check the docs. A few highlights:
- Context provides access to the context.Context passed into the guest function call in wazero.
- Store provides access to all the state: memory, stack, references.
- Duration and Time to pass time.Duration and time.Time (as UNIX timestamp).
- HostRef can hold a reference to the Refs store of host objects.
- Void is used as the return type for functions that return no value.
See documentation for more.
# Functions
H0 defines a [HostFunc] that accepts no arguments.
H1 defines a [HostFunc] that accepts 1 high-level argument.
H10 defines a [HostFunc] that accepts 10 high-level arguments.
H11 defines a [HostFunc] that accepts 11 high-level arguments.
H12 defines a [HostFunc] that accepts 12 high-level arguments.
H13 defines a [HostFunc] that accepts 13 high-level arguments.
H14 defines a [HostFunc] that accepts 14 high-level arguments.
H15 defines a [HostFunc] that accepts 15 high-level arguments.
H16 defines a [HostFunc] that accepts 16 high-level arguments.
H17 defines a [HostFunc] that accepts 17 high-level arguments.
H18 defines a [HostFunc] that accepts 18 high-level arguments.
H2 defines a [HostFunc] that accepts 2 high-level arguments.
H3 defines a [HostFunc] that accepts 3 high-level arguments.
H4 defines a [HostFunc] that accepts 4 high-level arguments.
H5 defines a [HostFunc] that accepts 5 high-level arguments.
H6 defines a [HostFunc] that accepts 6 high-level arguments.
H7 defines a [HostFunc] that accepts 7 high-level arguments.
H8 defines a [HostFunc] that accepts 8 high-level arguments.
H9 defines a [HostFunc] that accepts 9 high-level arguments.
No description provided by the author
Create new memory instance that internally stores data in a slice.
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
ValueTypeExternref is an externref type.
ValueTypeF32 is a 32-bit floating point number.
ValueTypeF64 is a 64-bit floating point number.
ValueTypeI32 is a 32-bit integer.
ValueTypeI64 is a 64-bit integer.
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
Bytes wraps a slice of bytes.
Context wraps [context.Context].
HostFunc is a wrapped host-defined function.
HostRef is a reference to a Go object stored on the host side in [Refs].
List wraps a Go slice of any type that implements the [MemoryLiftLower] interface.
ListStrings wraps a Go slice of strings.
MapRefs is a simple [Refs] implementation powered by a map.
Pair wraps two values of arbitrary types.
Result is the implementation required for the host side of component model functions that return a *[cm.Result] type.
ReturnedList wraps a Go slice of any type that supports the [MemoryLiftLower] interface so it can be returned as a List.
Store provides access for host-defined functions to the runtime data.
String wraps [string].
Void is a return type of a function that returns nothing.
# Interfaces
Lift reads values from [Store] into a native Go value.
LiftLower is a type that implements both [Lift] and [Lower].
Lower writes a native Go value into the [Store].
Memory provides access to the linear memory of the wasm runtime.
MemoryLift reads values from [Store.Memory] into a native Go value.
MemoryLiftLower is a type that implements both [MemoryLift] and [MemoryLower].
MemoryLower writes a native Go value into the [Store.Memory].
Refs holds references to Go values that you want to reference from wasm using [HostRef].
No description provided by the author
Value is an interface implemented by all the types in wypes.
# Type aliases
No description provided by the author
Bool wraps [bool].
Byte is an alias for [UInt8].
Complex128 wraps [complex128].
Complex64 wraps [complex64].
Duration wraps [time.Duration].
Float32 wraps [float32].
Float64 wraps [float64].
Int wraps [int], a signed 32-bit integer.
Int16 wraps [int16], a signed 16-bit integer.
Int32 wraps [int32], a signed 32-bit integer.
Int64 wraps [int64], a signed 64-bit integer.
Int8 wraps [int8], a signed 8-bit integer.
Module is a collection of host-defined functions in a module with the same name.
Modules is a collection of host-defined modules.
No description provided by the author
Rune is an alias for [UInt32].
Wraps a slice of bytes to be used as [Memory].
SliceStack adapts a slice of raw values into a [Stack].
Time wraps [time.Time].
UInt wraps uint, 32-bit unsigned integer.
UInt16 wraps uint16, 16-bit unsigned integer.
UInt32 wraps uint32, 32-bit unsigned integer.
UInt64 wraps uint64, 64-bit unsigned integer.
UInt8 wraps uint8, 8-bit unsigned integer.
UIntPtr wraps uintptr, pointer-sized unsigned integer.
No description provided by the author