wasmer

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2021 License: MIT Imports: 6 Imported by: 61

Documentation

Overview

Package wasmer is a Go library to run WebAssembly binaries.

Index

Constants

View Source
const (
	IMMUTABLE = GlobalMutability(C.WASM_CONST)
	MUTABLE   = GlobalMutability(C.WASM_VAR)
)
View Source
const (
	WASI_VERSION_LATEST    = WasiVersion(C.LATEST)
	WASI_VERSION_SNAPSHOT0 = WasiVersion(C.SNAPSHOT0)
	WASI_VERSION_SNAPSHOT1 = WasiVersion(C.SNAPSHOT1)
	WASI_VERSION_INVALID   = WasiVersion(C.INVALID_VERSION)
)
View Source
const WasmMaxPages = uint(0x10000)
View Source
const WasmMinPages = uint(0x100)
View Source
const WasmPageSize = uint(0x10000)

Variables

This section is empty.

Functions

func LimitMaxUnbound

func LimitMaxUnbound() uint32

func ValidateModule

func ValidateModule(store *Store, bytes []byte) error

ValidateModule validates a new Module against the given Store.

It takes two arguments, the Store and the Wasm module as an byte array of WAT code..

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 err := wasmer.ValidateModule(store, wasmBytes)

func Wat2Wasm

func Wat2Wasm(wat string) ([]byte, error)

Wat2Wasm parsers a string as either WAT code or a binary Wasm module.

⚠️ This is not part of the standard Wasm C API. It is Wasmer specific.

  wat := "(module)"
  wasm, _ := Wat2Wasm(wat)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, _ := wasmer.NewModule(store, wasmBytes)

Types

type Config

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

func NewConfig

func NewConfig() *Config

NewConfig instantiates and returns a new Config.

config := NewConfig()

func (*Config) UseCraneliftCompiler

func (self *Config) UseCraneliftCompiler()

UseCraneliftCompiler sets the compiler to Cranelift in the configuration.

config := NewConfig()
config.UseCraneliftCompiler()

func (*Config) UseJITEngine

func (self *Config) UseJITEngine()

UseJITEngine sets the engine to JIT in the configuration.

config := NewConfig()
config.UseJITEngine()

func (*Config) UseLLVMCompiler

func (self *Config) UseLLVMCompiler()

UseLLVMCompiler sets the compiler to LLVM in the configuration.

config := NewConfig()
config.UseLLVMCompiler()

func (*Config) UseNativeEngine

func (self *Config) UseNativeEngine()

UseNativeEngine sets the engine to Native in the configuration.

config := NewConfig()
config.UseNativeEngine()

func (*Config) UseTarget

func (self *Config) UseTarget(target *Target)

type CpuFeatures

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

func NewCpuFeatures

func NewCpuFeatures() *CpuFeatures

func (*CpuFeatures) Add

func (self *CpuFeatures) Add(feature string) error

type Engine

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

func NewEngine

func NewEngine() *Engine

NewEngine instantiates and returns a new Engine with the default configuration.

engine := NewEngine()

func NewEngineWithConfig

func NewEngineWithConfig(config *Config) *Engine

NewEngineWithConfig instantiates and returns a new Engine with the given configuration.

config := NewConfig()
engine := NewEngineWithConfig(config)

func NewJITEngine

func NewJITEngine() *Engine

NewJITEngine instantiates and returns a new JIT engine.

engine := NewJITEngine()

func NewNativeEngine

func NewNativeEngine() *Engine

NewNativeEngine instantiates and returns a new Native engine.

engine := NewNativeEngine()

type Error

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

Error represents a Wasmer runtime error.

func (*Error) Error

func (error *Error) Error() string

Error returns the Error's message.

type ExportType

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

func NewExportType

func NewExportType(name string, ty IntoExternType) *ExportType

NewExportType instantiates a new ExportType with a name and an extern type.

ℹ️ An extern type is anything implementing IntoExternType: FunctionType, GlobalType, MemoryType, TableType.

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, CONST)
exportType := NewExportType("a_global", globalType)

func (*ExportType) Name

func (self *ExportType) Name() string

Name returns the name of the export type.

exportType := NewExportType("a_global", globalType)
exportType.Name() // "global"

func (*ExportType) Type

func (self *ExportType) Type() *ExternType

Type returns the type of the export type.

exportType := NewExportType("a_global", globalType)
exportType.Type() // ExternType

type Exports

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

func (*Exports) Get

func (self *Exports) Get(name string) (*Extern, error)

Get retrieves and returns an Extern by its name.

❗️ If the name does not refer to an existing export, Get will return an Error.

instance, _ := NewInstance(module, NewImportObject())
extern, error := instance.Exports.Get("an_export")

func (*Exports) GetFunction

func (self *Exports) GetFunction(name string) (NativeFunction, error)

GetFunction retrieves a exported function by its name and returns it as a native Go function.

❗️ If the name does not refer to an existing export, GetFunction will return an Error.

⚠️ If the export is not a function, GetFunction will return nil as its result.

instance, _ := NewInstance(module, NewImportObject())
exportedFunc, error := instance.Exports.GetFunction("an_exported_function")

if error != nil && exportedFunc != nil {
    exportedFunc()
}

func (*Exports) GetGlobal

func (self *Exports) GetGlobal(name string) (*Global, error)

GetGlobal retrieves and returns a exported Global by its name.

❗️ If the name does not refer to an existing export, GetGlobal will return an Error.

⚠️ If the export is not a global, GetGlobal will return nil as a result.

instance, _ := NewInstance(module, NewImportObject())
exportedGlobal, error := instance.Exports.GetGlobal("an_exported_global")

func (*Exports) GetMemory

func (self *Exports) GetMemory(name string) (*Memory, error)

GetMemory retrieves and returns a exported Memory by its name.

❗️ If the name does not refer to an existing export, GetMemory will return an Error.

⚠️ If the export is not a memory, GetMemory will return nil as a result.

instance, _ := NewInstance(module, NewImportObject())
exportedMemory, error := instance.Exports.GetMemory("an_exported_memory")

func (*Exports) GetRawFunction

func (self *Exports) GetRawFunction(name string) (*Function, error)

GetRawFunction retrieves and returns a exported Function by its name.

❗️ If the name does not refer to an existing export, GetRawFunction will return an Error.

⚠️ If the export is not a function, GetRawFunction will return nil as its result.

instance, _ := NewInstance(module, NewImportObject())
exportedFunc, error := instance.Exports.GetRawFunction("an_exported_function")

if error != nil && exportedFunc != nil {
    exportedFunc.Call()
}

func (*Exports) GetTable

func (self *Exports) GetTable(name string) (*Table, error)

GetTable retrieves and returns a exported Table by its name.

❗️ If the name does not refer to an existing export, GetTable will return an Error.

⚠️ If the export is not a table, GetTable will return nil as a result.

instance, _ := NewInstance(module, NewImportObject())
exportedTable, error := instance.Exports.GetTable("an_exported_table")

func (*Exports) GetWasiStartFunction

func (self *Exports) GetWasiStartFunction() (NativeFunction, error)

type Extern

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

func (*Extern) IntoExtern

func (self *Extern) IntoExtern() *Extern

func (*Extern) IntoFunction

func (self *Extern) IntoFunction() *Function

IntoFunction converts the Extern into a Function.

⚠️ If the Extern is not a Function, IntoFunction will return nil as its result.

function, _ := instance.Exports.GetFunction("exported_function")
extern = function.IntoExtern()
_ := extern.IntoFunction()

func (*Extern) IntoGlobal

func (self *Extern) IntoGlobal() *Global

IntoGlobal converts the Extern into a Global.

⚠️ If the Extern is not a Global, IntoGlobal will return nil as its result.

global, _ := instance.Exports.GetGlobal("exported_global")
extern = global.IntoExtern()
_ := extern.IntoGlobal()

func (*Extern) IntoMemory

func (self *Extern) IntoMemory() *Memory

IntoMemory converts the Extern into a Memory.

⚠️ If the Extern is not a Memory, IntoMemory will return nil as its result.

memory, _ := instance.Exports.GetMemory("exported_memory")
extern = memory.IntoExtern()
_ := extern.IntoMemory()

func (*Extern) IntoTable

func (self *Extern) IntoTable() *Table

IntoTable converts the Extern into a Table.

⚠️ If the Extern is not a Table, IntoTable will return nil as its result.

table, _ := instance.Exports.GetTable("exported_table")
extern = table.IntoExtern()
_ := extern.IntoTable()

func (*Extern) Kind

func (self *Extern) Kind() ExternKind

Kind returns the Extern's ExternKind.

global, _ := instance.Exports.GetGlobal("exported_global")
_ = global.IntoExtern().Kind()

func (*Extern) Type

func (self *Extern) Type() *ExternType

Type returns the Extern's ExternType.

global, _ := instance.Exports.GetGlobal("exported_global")
_ = global.IntoExtern().Type()

type ExternKind

type ExternKind C.wasm_externkind_t

func (ExternKind) String

func (self ExternKind) String() string

String returns the ExternKind as a string.

FUNCTION.String() // "func"
GLOBAL.String()   // "global"
TABLE.String()    // "table"
MEMORY.String()   // "memory"

type ExternType

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

ExternType classifies imports and external values with their respective types.

See also

Specification: https://webassembly.github.io/spec/core/syntax/types.html#external-types

func (*ExternType) IntoFunctionType

func (self *ExternType) IntoFunctionType() *FunctionType

IntoFunctionType converts the ExternType into a FunctionType.

⚠️ If the ExternType is not a FunctionType, IntoFunctionType will return nil as its result.

function, _ := instance.Exports.GetFunction("exported_function")
externType = function.IntoExtern().Type()
_ := externType.IntoFunctionType()

func (*ExternType) IntoGlobalType

func (self *ExternType) IntoGlobalType() *GlobalType

IntoGlobalType converts the ExternType into a GlobalType.

⚠️ If the ExternType is not a GlobalType, IntoGlobalType will return nil as its result.

global, _ := instance.Exports.GetGlobal("exported_global")
externType = global.IntoExtern().Type()
_ := externType.IntoGlobalType()

func (*ExternType) IntoMemoryType

func (self *ExternType) IntoMemoryType() *MemoryType

IntoMemoryType converts the ExternType into a MemoryType.

⚠️ If the ExternType is not a MemoryType, IntoMemoryType will return nil as its result.

memory, _ := instance.Exports.GetMemory("exported_memory")
externType = memory.IntoExtern().Type()
_ := externType.IntoMemoryType()

func (*ExternType) IntoTableType

func (self *ExternType) IntoTableType() *TableType

IntoTableType converts the ExternType into a TableType.

⚠️ If the ExternType is not a TableType, IntoTableType will return nil as its result.

table, _ := instance.Exports.GetTable("exported_table")
externType = table.IntoExtern().Type()
_ := externType.IntoTableType()

func (*ExternType) Kind

func (self *ExternType) Kind() ExternKind

Kind returns the ExternType's ExternKind

global, _ := instance.Exports.GetGlobal("exported_global")
extern = global.IntoExtern()
_ = extern.Kind()

type Frame

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

func (*Frame) FunctionIndex

func (self *Frame) FunctionIndex() uint32

func (*Frame) FunctionOffset

func (self *Frame) FunctionOffset() uint

func (*Frame) Instance

func (self *Frame) Instance()

func (*Frame) ModuleOffset

func (self *Frame) ModuleOffset() uint

type Function

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

func NewFunction

func NewFunction(store *Store, ty *FunctionType, function func([]Value) ([]Value, error)) *Function

NewGlobal instantiates a new Global in the given Store.

It takes three arguments, the Store, the FunctionType and the definition for the Function.

The function definition must be a native Go function with a Value array as its single argument. The function must return a Value array or an error.

⚠️ Even if the function does not take any argument (or use any argument) it must receive a Value array

   as its single argument. At runtime, this array will be empty.
   The same applies to the result.

  hostFunction := wasmer.NewFunction(
		store,
		wasmer.NewFunctionType(wasmer.NewValueTypes(), wasmer.NewValueTypes(wasmer.I32)),
		func(args []wasmer.Value) ([]wasmer.Value, error) {
			return []wasmer.Value{wasmer.NewI32(42)}, nil
		},
	 )

func NewFunctionWithEnvironment

func NewFunctionWithEnvironment(store *Store, ty *FunctionType, userEnvironment interface{}, functionWithEnv func(interface{}, []Value) ([]Value, error)) *Function

func (*Function) Call

func (self *Function) Call(parameters ...interface{}) (interface{}, error)

Call will call the Function and return its results as native Go values.

function, _ := instance.Exports.GetFunction("exported_function")
_ = function.Call(1, 2, 3)

func (*Function) IntoExtern

func (self *Function) IntoExtern() *Extern

IntoExtern converts the Function into an Extern.

function, _ := instance.Exports.GetFunction("exported_function")
extern := function.IntoExtern()

func (*Function) Native

func (self *Function) Native() NativeFunction

Native will turn the Function into a native Go function that can be then called.

function, _ := instance.Exports.GetFunction("exported_function")
nativeFunction = function.Native()
_ = nativeFunction(1, 2, 3)

func (*Function) ParameterArity

func (self *Function) ParameterArity() uint

ParameterArity returns the number of arguments the Function expects as per its definition.

function, _ := instance.Exports.GetFunction("exported_function")
arity := function.ParameterArity()

func (*Function) ResultArity

func (self *Function) ResultArity() uint

ParameterArity returns the number of results the Function will return.

function, _ := instance.Exports.GetFunction("exported_function")
arity := function.ResultArity()

func (*Function) Type

func (self *Function) Type() *FunctionType

Type returns the Function's FunctionType.

function, _ := instance.Exports.GetFunction("exported_function")
ty := function.Type()

type FunctionEnvironment

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

type FunctionType

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

FunctionType classifies the signature of functions, mapping a vector of parameters to a vector of results. They are also used to classify the inputs and outputs of instructions.

See also

Specification: https://webassembly.github.io/spec/core/syntax/types.html#function-types

func NewFunctionType

func NewFunctionType(params []*ValueType, results []*ValueType) *FunctionType

NewFunctionType instantiates a new FunctionType from two ValueType arrays: the parameters and the results.

params := wasmer.NewValueTypes()
results := wasmer.NewValueTypes(wasmer.I32)
functionType := wasmer.NewFunctionType(params, results)

func (*FunctionType) IntoExternType

func (self *FunctionType) IntoExternType() *ExternType

IntoExternType converts the FunctionType into an ExternType.

function, _ := instance.Exports.GetFunction("exported_function")
functionType := function.Type()
externType = functionType.IntoExternType()

func (*FunctionType) Params

func (self *FunctionType) Params() []*ValueType

Params returns the parameters definitions from the FunctionType as a ValueType array

params := wasmer.NewValueTypes()
results := wasmer.NewValueTypes(wasmer.I32)
functionType := wasmer.NewFunctionType(params, results)
paramsValueTypes = functionType.Params()

func (*FunctionType) Results

func (self *FunctionType) Results() []*ValueType

Results returns the results definitions from the FunctionType as a ValueType array

params := wasmer.NewValueTypes()
results := wasmer.NewValueTypes(wasmer.I32)
functionType := wasmer.NewFunctionType(params, results)
resultsValueTypes = functionType.Results()

type Global

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

Global stores a single value of the given GlobalType.

See also

https://webassembly.github.io/spec/core/syntax/modules.html#globals

func NewGlobal

func NewGlobal(store *Store, ty *GlobalType, value Value) *Global

NewGlobal instantiates a new Global in the given Store.

It takes three arguments, the Store, the GlobalType and the Value for the Global.

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, CONST)
global := NewGlobal(store, globalType, NewValue(42, I32))

func (*Global) Get

func (self *Global) Get() (interface{}, error)

Get returns the Global's value as a native Go value.

global, _ := instance.Exports.GetGlobal("exported_global")
value, _ := global.Get()

func (*Global) IntoExtern

func (self *Global) IntoExtern() *Extern

IntoExtern converts the Global into an Extern.

global, _ := instance.Exports.GetGlobal("exported_global")
extern := global.IntoExtern()

func (*Global) Set

func (self *Global) Set(value interface{}, kind ValueKind) error

Set sets the Global's value.

It takes two arguments, the Global's value as a native Go value and the value's ValueKind.

global, _ := instance.Exports.GetGlobal("exported_global")
_ = global.Set(1, I32)

func (*Global) Type

func (self *Global) Type() *GlobalType

Type returns the Global's GlobalType.

global, _ := instance.Exports.GetGlobal("exported_global")
ty := global.Type()

type GlobalMutability

type GlobalMutability C.wasm_mutability_t

func (GlobalMutability) String

func (self GlobalMutability) String() string

String returns the GlobalMutability as a string.

IMMUTABLE.String() // "const"
MUTABLE.String()   // "var"

type GlobalType

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

GlobalType classifies global variables, which hold a value and can either be mutable or immutable.

See also

Specification: https://webassembly.github.io/spec/core/syntax/types.html#global-types

func NewGlobalType

func NewGlobalType(valueType *ValueType, mutability GlobalMutability) *GlobalType

NewGlobalType instantiates a new GlobalType from a ValueType and a GlobalMutability

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, IMMUTABLE)

func (*GlobalType) IntoExternType

func (self *GlobalType) IntoExternType() *ExternType

IntoExternType converts the GlobalType into an ExternType.

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, IMMUTABLE)
externType = globalType.IntoExternType()

func (*GlobalType) Mutability

func (self *GlobalType) Mutability() GlobalMutability

Mutability returns the GlobalType's GlobalMutability

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, IMMUTABLE)
globalType.Mutability().String() // "const"

func (*GlobalType) ValueType

func (self *GlobalType) ValueType() *ValueType

ValueType returns the GlobalType's ValueType

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, IMMUTABLE)
globalType.ValueType().Kind().String() // "i32"

type ImportObject

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

func NewImportObject

func NewImportObject() *ImportObject

NewImportObject instantiates a new empty ImportObject.

imports := NewImportObject()

func (*ImportObject) ContainsNamespace

func (self *ImportObject) ContainsNamespace(name string) bool

ContainsNamespace returns true if the ImportObject contains the given namespace (or module name)

imports := NewImportObject()
_ = imports.ContainsNamespace("env") // false

func (*ImportObject) Register

func (self *ImportObject) Register(namespaceName string, namespace map[string]IntoExtern)

Register registers a namespace (or module name) in the ImportObject.

It takes two arguments: the namespace name and a map with imports names as key and externs as values.

ℹ️ An extern is anything implementing IntoExtern: Function, Global, Memory, Table.

  imports := NewImportObject()
  importObject.Register(
		"env",
		map[string]wasmer.IntoExtern{
			"host_function": hostFunction,
			"host_global": hostGlobal,
		},
	)

ℹ️ The namespace (or module name) may be empty:

  imports := NewImportObject()
  importObject.Register(
		"",
		map[string]wasmer.IntoExtern{
			"host_function": hostFunction,
			"host_global": hostGlobal,
		},
	)

type ImportType

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

func NewImportType

func NewImportType(module string, name string, ty IntoExternType) *ImportType

NewImportType instantiates a new ImportType with a module name (or namespace), a name and an extern type.

ℹ️ An extern type is anything implementing IntoExternType: FunctionType, GlobalType, MemoryType, TableType.

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, CONST)
importType := NewImportType("ns", "host_global", globalType)

func (*ImportType) Module

func (self *ImportType) Module() string

Module returns the ImportType's module name (or namespace).

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, CONST)
importType := NewImportType("ns", "host_global", globalType)
_ = importType.Module()

func (*ImportType) Name

func (self *ImportType) Name() string

Name returns the ImportType's name.

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, CONST)
importType := NewImportType("ns", "host_global", globalType)
_ = importType.Name()

func (*ImportType) Type

func (self *ImportType) Type() *ExternType

Type returns the ImportType's type as an ExternType.

valueType := NewValueType(I32)
globalType := NewGlobalType(valueType, CONST)
importType := NewImportType("ns", "host_global", globalType)
_ = importType.Type()

type Instance

type Instance struct {
	Exports *Exports
	// contains filtered or unexported fields
}

func NewInstance

func NewInstance(module *Module, imports *ImportObject) (*Instance, error)

NewInstance instantiates a new Instance.

It takes two arguments, the Module and an ImportObject.

⚠️ Instantiating a module may return TrapError if the module's start function traps.

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, err := wasmer.NewModule(store, wasmBytes)
  importObject := wasmer.NewImportObject()
  instance, err := wasmer.NewInstance(module, importObject)

type IntoExtern

type IntoExtern interface {
	IntoExtern() *Extern
}

type IntoExternType

type IntoExternType interface {
	IntoExternType() *ExternType
}

type Limits

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

Limits classify the size range of resizeable storage associated with memory types and table types.

See also

Specification: https://webassembly.github.io/spec/core/syntax/types.html#limits

func NewLimits

func NewLimits(minimum uint32, maximum uint32) (*Limits, error)

NewLimits instantiates a new Limits which describes the Memory used. The minimum and maximum parameters are "number of memory pages".

ℹ️ Each page is 64 KiB in size.

⚠️ You cannot Memory.Grow the Memory beyond the maximum defined here.

func (*Limits) Maximum

func (self *Limits) Maximum() uint32

Maximum returns the maximum size of the Memory allocated in "number of pages".

Each page is 64 KiB in size.

⚠️ You cannot Memory.Grow beyond this defined maximum size.

func (*Limits) Minimum

func (self *Limits) Minimum() uint32

Minimum returns the minimum size of the Memory allocated in "number of pages".

ℹ️ Each page is 64 KiB in size.

type Memory

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

Memory is a vector of raw uninterpreted bytes.

See also

Specification: https://webassembly.github.io/spec/core/syntax/modules.html#memories

func NewMemory

func NewMemory(store *Store, ty *MemoryType) *Memory

NewMemory instantiates a new Memory in the given Store.

It takes two arguments, the Store and the MemoryType for the Memory.

  memory := wasmer.NewFunction(
		store,
		wasmer.NewMemoryType(wasmer.NewLimits(1, 4)),
	 )

func (*Memory) Data

func (self *Memory) Data() []byte

Data returns the Memory's contents as an byte array.

memory, _ := instance.Exports.GetMemory("exported_memory")
data := memory.Data()

func (*Memory) DataSize

func (self *Memory) DataSize() uint

Size returns the Memory's size as a number of bytes.

memory, _ := instance.Exports.GetMemory("exported_memory")
size := memory.DataSize()

func (*Memory) Grow

func (self *Memory) Grow(delta Pages) bool

Grow grows the Memory's size by a given number of Pages (the delta).

memory, _ := instance.Exports.GetMemory("exported_memory")
grown := memory.Grow(2)

func (*Memory) IntoExtern

func (self *Memory) IntoExtern() *Extern

IntoExtern converts the Memory into an Extern.

memory, _ := instance.Exports.GetMemory("exported_memory")
extern := memory.IntoExtern()

func (*Memory) Size

func (self *Memory) Size() Pages

Size returns the Memory's size as Pages.

memory, _ := instance.Exports.GetMemory("exported_memory")
size := memory.Size()

func (*Memory) Type

func (self *Memory) Type() *MemoryType

Type returns the Memory's MemoryType.

memory, _ := instance.Exports.GetMemory("exported_memory")
ty := memory.Type()

type MemoryType

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

MemoryType classifies linear memories and their size range.

See also

Specification: https://webassembly.github.io/spec/core/syntax/types.html#memory-types

func NewMemoryType

func NewMemoryType(limits *Limits) *MemoryType

NewMemoryType instantiates a new MemoryType given some Limits.

limits := NewLimits(1, 4)
memoryType := NewMemoryType(limits)

func (*MemoryType) IntoExternType

func (self *MemoryType) IntoExternType() *ExternType

IntoExternType converts the MemoryType into an ExternType.

limits := NewLimits(1, 4)
memoryType := NewMemoryType(limits)
externType = memoryType.IntoExternType()

func (*MemoryType) Limits

func (self *MemoryType) Limits() *Limits

Limits returns the MemoryType's Limits.

limits := NewLimits(1, 4)
memoryType := NewMemoryType(limits)
_ = memoryType.Limits()

type Module

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

Module contains stateless WebAssembly code that has already been compiled and can be instantiated multiple times.

WebAssembly programs are organized into modules, which are the unit of deployment, loading, and compilation. A module collects definitions for types, functions, tables, memories, and globals. In addition, it can declare imports and exports and provide initialization logic in the form of data and element segments or a start function.

See also

Specification: https://webassembly.github.io/spec/core/syntax/modules.html#modules

func DeserializeModule

func DeserializeModule(store *Store, bytes []byte) (*Module, error)

DeserializeModule deserializes an byte array to a Module.

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, _ := wasmer.NewModule(store, wasmBytes)
  bytes := module.Serialize()
  //...
  deserializedModule := wasmer.DeserializeModule(store, bytes)

func NewModule

func NewModule(store *Store, bytes []byte) (*Module, error)

NewModule instantiates a new Module with the given Store.

It takes two arguments, the Store and the Wasm module as a byte array of WAT code.

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, err := wasmer.NewModule(store, wasmBytes)

func (*Module) Exports

func (self *Module) Exports() []*ExportType

Exports returns the Module's exports as an ExportType array.

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, _ := wasmer.NewModule(store, wasmBytes)
  exports := module.Exports()

func (*Module) Imports

func (self *Module) Imports() []*ImportType

Imports returns the Module's imports as an ImportType array.

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, _ := wasmer.NewModule(store, wasmBytes)
  imports := module.Imports()

func (*Module) Name

func (self *Module) Name() string

Name returns the Module's name.

⚠️ This is not part of the standard Wasm C API. It is Wasmer specific.

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, _ := wasmer.NewModule(store, wasmBytes)
  name := module.Name()

func (*Module) Serialize

func (self *Module) Serialize() ([]byte, error)

Serialize serializes the module and returns the Wasm code as an byte array.

  wasmBytes := []byte(`...`)
  engine := wasmer.NewEngine()
	 store := wasmer.NewStore(engine)
	 module, _ := wasmer.NewModule(store, wasmBytes)
  bytes := module.Serialize()

type NativeFunction

type NativeFunction = func(...interface{}) (interface{}, error)

type Pages

type Pages C.wasm_memory_pages_t

func (*Pages) ToBytes

func (self *Pages) ToBytes() uint

ToBytes converts a Pages to a native Go uint which is the Pages' size in bytes.

memory, _ := instance.Exports.GetMemory("exported_memory")
size := memory.Size().ToBytes()

func (*Pages) ToUint32

func (self *Pages) ToUint32() uint32

ToUint32 converts a Pages to a native Go uint32 which is the Pages' size.

memory, _ := instance.Exports.GetMemory("exported_memory")
size := memory.Size().ToUint32()

type Store

type Store struct {
	Engine *Engine
	// contains filtered or unexported fields
}

Store represents all global state that can be manipulated by WebAssembly programs. It consists of the runtime representation of all instances of functions, tables, memories, and globals that have been allocated during the life time of the abstract machine.

The Store holds the Engine (that is — amongst many things — used to compile the Wasm bytes into a valid module artifact).

See also

Specification: https://webassembly.github.io/spec/core/exec/runtime.html#store

func NewStore

func NewStore(engine *Engine) *Store

NewStore instantiates a new Store with an Engine.

engine := NewEngine()
store := NewStore(engine)

type Table

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

A table instance is the runtime representation of a table. It holds a vector of function elements and an optional maximum size, if one was specified in the table type at the table’s definition site.

See also

Specification: https://webassembly.github.io/spec/core/exec/runtime.html#table-instances

func (*Table) IntoExtern

func (self *Table) IntoExtern() *Extern

IntoExtern converts the Table into an Extern.

table, _ := instance.Exports.GetTable("exported_table")
extern := table.IntoExtern()

func (*Table) Size

func (self *Table) Size() TableSize

Size returns the Table's size.

table, _ := instance.Exports.GetTable("exported_table")
size := table.Size()

type TableSize

type TableSize C.wasm_table_size_t

func (*TableSize) ToUint32

func (self *TableSize) ToUint32() uint32

ToUint32 converts a TableSize to a native Go uint32.

table, _ := instance.Exports.GetTable("exported_table")
size := table.Size().ToUint32()

type TableType

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

TableType classifies tables over elements of element types within a size range.

See also

Specification: https://webassembly.github.io/spec/core/syntax/types.html#table-types

func NewTableType

func NewTableType(valueType *ValueType, limits *Limits) *TableType

NewTableType instantiates a new TableType given a ValueType and some Limits.

valueType := NewValueType(I32)
limits := NewLimits(1, 4)
tableType := NewTableType(valueType, limits)
_ = tableType.IntoExternType()

func (*TableType) IntoExternType

func (self *TableType) IntoExternType() *ExternType

IntoExternType converts the TableType into an ExternType.

valueType := NewValueType(I32)
limits := NewLimits(1, 4)
tableType := NewTableType(valueType, limits)
_ = tableType.IntoExternType()

func (*TableType) Limits

func (self *TableType) Limits() *Limits

Limits returns the TableType's Limits.

valueType := NewValueType(I32)
limits := NewLimits(1, 4)
tableType := NewTableType(valueType, limits)
_ = tableType.Limits()

func (*TableType) ValueType

func (self *TableType) ValueType() *ValueType

ValueType returns the TableType's ValueType.

valueType := NewValueType(I32)
limits := NewLimits(1, 4)
tableType := NewTableType(valueType, limits)
_ = tableType.ValueType()

type Target

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

func NewTarget

func NewTarget(triple *Triple, cpuFeatures *CpuFeatures) *Target

type Trap

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

func NewTrap

func NewTrap(store *Store, message string) *Trap

func (*Trap) Message

func (self *Trap) Message() string

func (*Trap) Origin

func (self *Trap) Origin() *Frame

func (*Trap) Trace

func (self *Trap) Trace() *trace

type TrapError

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

TrapError represents a trap produced during Wasm execution.

See also

Specification: https://webassembly.github.io/spec/core/intro/overview.html#trap

func (*TrapError) Error

func (self *TrapError) Error() string

Error returns the TrapError's message.

func (*TrapError) Origin

func (self *TrapError) Origin() *Frame

Origin returns the TrapError's origin as a Frame.

func (*TrapError) Trace

func (self *TrapError) Trace() []*Frame

Trace returns the TrapError's trace as a Frame array.

type Triple

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

func NewTriple

func NewTriple(triple string) (*Triple, error)

func NewTripleFromHost

func NewTripleFromHost() *Triple

type Value

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

func NewF32

func NewF32(value interface{}) Value

NewF32 instantiates a new F32 Value with the given value.

⚠️ If a Wasm value cannot be created from the given value, NewF32 will panic.

value := NewF32(4.2)

func NewF64

func NewF64(value interface{}) Value

NewF64 instantiates a new F64 Value with the given value.

⚠️ If a Wasm value cannot be created from the given value, NewF64 will panic.

value := NewF64(4.2)

func NewI32

func NewI32(value interface{}) Value

NewI32 instantiates a new I32 Value with the given value.

⚠️ If a Wasm value cannot be created from the given value, NewI32 will panic.

value := NewI32(42)

func NewI64

func NewI64(value interface{}) Value

NewI64 instantiates a new I64 Value with the given value.

⚠️ If a Wasm value cannot be created from the given value, NewI64 will panic.

value := NewI64(42)

func NewValue

func NewValue(value interface{}, kind ValueKind) Value

NewValue instantiates a new Value with the given value and ValueKind.

⚠️ If a Wasm value cannot be created from the given value, NewValue will panic.

value := NewValue(42, I32)

func (*Value) F32

func (self *Value) F32() float32

F32 returns the Value's value as a native Go float32.

value := NewF32(4.2)
_ = value.F32()

func (*Value) F64

func (self *Value) F64() float64

F64 returns the Value's value as a native Go float64.

value := NewF64(4.2)
_ = value.F64()

func (*Value) I32

func (self *Value) I32() int32

I32 returns the Value's value as a native Go int32.

value := NewI32(42)
_ = value.I32()

func (*Value) I64

func (self *Value) I64() int64

I64 returns the Value's value as a native Go int64.

value := NewI64(42)
_ = value.I64()

func (*Value) Kind

func (self *Value) Kind() ValueKind

Kind returns the Value's ValueKind.

value := NewF64(4.2)
_ = value.Kind()

func (*Value) Unwrap

func (self *Value) Unwrap() interface{}

Unwrap returns the Value's value as a native Go value.

value := NewF64(4.2)
_ = value.Unwrap()

type ValueKind

type ValueKind C.wasm_valkind_t

func (ValueKind) IsNumber

func (self ValueKind) IsNumber() bool

IsNumber returns true if the ValueKind is a number type.

I32.IsNumber()     // true
I64.IsNumber()     // true
F32.IsNumber()     // true
F64.IsNumber()     // true
AnyRef.IsNumber()  // false
FuncRef.IsNumber() // false

func (ValueKind) IsReference

func (self ValueKind) IsReference() bool

IsReference returns true if the ValueKind is a reference.

I32.IsReference()     // false
I64.IsReference()     // false
F32.IsReference()     // false
F64.IsReference()     // false
AnyRef.IsReference()  // true
FuncRef.IsReference() // true

func (ValueKind) String

func (self ValueKind) String() string

String returns the ValueKind as a string.

I32.String()     // "i32"
I64.String()     // "i64"
F32.String()     // "f32"
F64.String()     // "f64"
AnyRef.String()  // "anyref"
FuncRef.String() // "funcref"

type ValueType

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

ValueType classifies the individual values that WebAssembly code can compute with and the values that a variable accepts.

func NewValueType

func NewValueType(kind ValueKind) *ValueType

NewValueType instantiates a new ValueType given a ValueKind.

valueType := NewValueType(I32)

func NewValueTypes

func NewValueTypes(kinds ...ValueKind) []*ValueType

NewValueTypes instantiates a new ValueType array from a list of ValueKind. Not that the list may be empty.

valueTypes := NewValueTypes(I32, I64, F32)

ℹ️ NewValueTypes is specifically designed to help you declare function types:

functionType := NewFunctionType(NewValueTypes(), NewValueTypes(I32))

func (*ValueType) Kind

func (self *ValueType) Kind() ValueKind

Kind returns the ValueType's ValueKind

valueType := NewValueType(I32)
_ = valueType.Kind()

type WasiEnvironment

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

func (*WasiEnvironment) GenerateImportObject

func (self *WasiEnvironment) GenerateImportObject(store *Store, module *Module) (*ImportObject, error)

type WasiStateBuilder

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

func NewWasiStateBuilder

func NewWasiStateBuilder(programName string) *WasiStateBuilder

type WasiVersion

type WasiVersion C.wasi_version_t

func GetWasiVersion

func GetWasiVersion(module *Module) WasiVersion

func (WasiVersion) String

func (self WasiVersion) String() string

Jump to

Keyboard shortcuts

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