wasmer

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package wasmer is a Go library to run WebAssembly binaries.

Example
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func simpleWasmFile() string {
	_, filename, _, _ := runtime.Caller(0)
	return path.Join(path.Dir(filename), "test", "testdata", "examples", "simple.wasm")
}

func main() {
	// Reads the WebAssembly module as bytes.
	bytes, _ := wasm.ReadBytes(simpleWasmFile())

	// Instantiates the WebAssembly module.
	instance, _ := wasm.NewInstance(bytes)

	// Close the WebAssembly instance later.
	defer instance.Close()

	// Gets the `sum` exported function from the WebAssembly instance.
	sum := instance.Exports["sum"]

	// Calls that exported function with Go standard values. The
	// WebAssembly types are inferred and values are casted
	// automatically.
	result, _ := sum(1, 2)

	fmt.Print("Result of `sum(1, 2)` is: ")
	fmt.Println(result)

}
Output:

Result of `sum(1, 2)` is: 3
Example (Greet)
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
	"strings"
)

func greetWasmFile() string {
	_, filename, _, _ := runtime.Caller(0)
	return path.Join(path.Dir(filename), "test", "testdata", "examples", "greet.wasm")
}

func main() {
	// Instantiate the module.
	bytes, _ := wasm.ReadBytes(greetWasmFile())
	instance, _ := wasm.NewInstance(bytes)
	defer instance.Close()

	// Set the subject to greet.
	subject := "Wasmer 🐹"
	lengthOfSubject := len(subject)

	// Allocate memory for the subject, and get a pointer to it.
	allocateResult, _ := instance.Exports["allocate"](lengthOfSubject)
	inputPointer := allocateResult.ToI32()

	// Write the subject into the memory.
	memory := instance.Memory.Data()[inputPointer:]

	for nth := 0; nth < lengthOfSubject; nth++ {
		memory[nth] = subject[nth]
	}

	// C-string terminates by NULL.
	memory[lengthOfSubject] = 0

	// Run the `greet` function. Given the pointer to the subject.
	greetResult, _ := instance.Exports["greet"](inputPointer)
	outputPointer := greetResult.ToI32()

	// Read the result of the `greet` function.
	memory = instance.Memory.Data()[outputPointer:]
	nth := 0
	var output strings.Builder

	for {
		if memory[nth] == 0 {
			break
		}

		output.WriteByte(memory[nth])
		nth++
	}

	lengthOfOutput := nth

	fmt.Println(output.String())

	// Deallocate the subject, and the output.
	deallocate := instance.Exports["deallocate"]
	deallocate(inputPointer, lengthOfSubject)
	deallocate(outputPointer, lengthOfOutput)

}
Output:

Hello, Wasmer 🐹!
Example (Memory)
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func memoryWasmFile() string {
	_, filename, _, _ := runtime.Caller(0)
	return path.Join(path.Dir(filename), "test", "testdata", "examples", "memory.wasm")
}

func main() {
	// Reads the WebAssembly module as bytes.
	bytes, _ := wasm.ReadBytes(memoryWasmFile())

	// Instantiates the WebAssembly mdule.
	instance, _ := wasm.NewInstance(bytes)
	defer instance.Close()

	// Calls the `return_hello` exported function. This function
	// returns a pointer to a string.
	result, _ := instance.Exports["return_hello"]()

	// Gets the pointer value as an integer.
	pointer := result.ToI32()

	// Reads the memory.
	memory := instance.Memory.Data()

	fmt.Println(string(memory[pointer : pointer+13]))

}
Output:

Hello, World!

Index

Examples

Constants

View Source
const (
	// ImportExportKindFunction represents an import/export descriptor of kind function.
	ImportExportKindFunction = ImportExportKind(cWasmFunction)

	// ImportExportKindGlobal represents an import/export descriptor of kind global.
	ImportExportKindGlobal = ImportExportKind(cWasmGlobal)

	// ImportExportKindMemory represents an import/export descriptor of kind memory.
	ImportExportKindMemory = ImportExportKind(cWasmMemory)

	// ImportExportKindTable represents an import/export descriptor of kind table.
	ImportExportKindTable = ImportExportKind(cWasmTable)
)
View Source
const (
	// Unknown represents an unknown WASI version.
	Unknown = WasiVersion(cVersionUnknown)

	// Latest represents the latest WASI version.
	Latest = WasiVersion(cVersionSnapshot0)

	// Snapshot0 represents the `wasi_unstable` WASI version.
	Snapshot0 = WasiVersion(cVersionSnapshot0)

	// Snapshot1 represents the `wasi_snapshot1_preview` WASI version.
	Snapshot1 = WasiVersion(cVersionSnapshot1)
)

Variables

This section is empty.

Functions

func GetLastError

func GetLastError() (string, error)

GetLastError returns the last error message if any, otherwise returns an error.

func ReadBytes

func ReadBytes(filename string) ([]byte, error)

ReadBytes reads a `.wasm` file and returns its content as an array of bytes.

Example
package main

import (
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
)

func main() {
	bytes, err := wasm.ReadBytes("my_program.wasm")

	_ = bytes
	_ = err

	// `bytes` now contains the WebAssembly module. They can be
	// used by `NewInstance` or `Validate`.
}
Output:

func Validate

func Validate(bytes []byte) bool

Validate validates a sequence of bytes that is supposed to represent a valid WebAssembly module.

Example
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	fmt.Println(wasm.Validate(GetBytes()))

}
Output:

true

Types

type ExportDescriptor

type ExportDescriptor struct {
	// The export name.
	Name string

	// The export kind/type.
	Kind ImportExportKind
}

ExportDescriptor represents an export descriptor of a WebAssembly module. It is different of an export of a WebAssembly instance. An export descriptor only has a name and a kind/type.

type ExportedFunctionError

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

ExportedFunctionError represents any kind of errors related to a WebAssembly exported function. It is returned by `Instance` functions only.

func NewExportedFunctionError

func NewExportedFunctionError(functionName string, message string) *ExportedFunctionError

NewExportedFunctionError constructs a new `ExportedFunctionError`, where `functionName` is the name of the exported function, and `message` is the error message. If the error message contains `%s`, then this parameter will be replaced by `functionName`.

func (*ExportedFunctionError) Error

func (error *ExportedFunctionError) Error() string

ExportedFunctionError is an actual error. The `Error` function returns the error message.

type Import

type Import interface{}

Import represents a WebAssembly instance imported function or memory. Imagine it is an union of `ImportFunction` and `ImportMemory`.

type ImportDescriptor

type ImportDescriptor struct {
	// The import name.
	Name string

	// The import namespace.
	Namespace string

	// The import kind/type.
	Kind ImportExportKind
}

ImportDescriptor represents an import descriptor of a WebAssembly module. It is different of an import of a WebAssembly instance. An import descriptor only has a name, a namespace, and a kind/type.

type ImportExportKind

type ImportExportKind int

ImportExportKind represents an import/export descriptor kind/type.

type ImportFunction

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

ImportFunction represents a WebAssembly instance imported function.

type ImportMemory

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

ImportMemory represents a WebAssembly instance imported memory.

type ImportObject

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

ImportObject owns a set of imports. It can be combined with a `Module` to create an `Instance`.

func NewDefaultWasiImportObject

func NewDefaultWasiImportObject() *ImportObject

NewDefaultWasiImportObject constructs a new `ImportObject` with WASI host imports.

To specify WASI program arguments, environment variables, preopened directories, and more, see `NewWasiImportObject`

func NewDefaultWasiImportObjectForVersion

func NewDefaultWasiImportObjectForVersion(version WasiVersion) *ImportObject

NewDefaultWasiImportObjectForVersion is similar to `NewDefaultWasiImportObject` but it specifies the WASI version.

func NewImportObject

func NewImportObject() *ImportObject

NewImportObject creates an empty `ImportObject`.

func NewWasiImportObject

func NewWasiImportObject(
	arguments []string,
	environmentVariables []string,
	preopenedDirs []string,
	mappedDirs []MapDirEntry,
) *ImportObject

NewWasiImportObject creates an `ImportObject` with the default WASI imports. Specify arguments (the first is the program name), environment variables ("envvar=value"), preopened directories (host file paths), and mapped directories (host file paths with an alias, see `MapDirEntry`)

func NewWasiImportObjectForVersion

func NewWasiImportObjectForVersion(
	version WasiVersion,
	arguments []string,
	environmentVariables []string,
	preopenedDirs []string,
	mappedDirs []MapDirEntry,
) *ImportObject

NewWasiImportObjectForVersion is similar to `NewWasiImportObject` but it specifies the WASI version.

func (*ImportObject) Close

func (importObject *ImportObject) Close()

Close frees the `ImportObject`

func (*ImportObject) Extend

func (importObject *ImportObject) Extend(imports Imports) error

Extend adds the given imports to the existing import object

func (*ImportObject) Imports

func (importObject *ImportObject) Imports() (*Imports, error)

Imports returns `*Imports` for a given `ImportObject`

type ImportObjectError

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

ImportObjectError represents errors related to `ImportObject`s.

func NewImportObjectError

func NewImportObjectError(message string) *ImportObjectError

NewImportObjectError constructs a new `ImportObjectError`

func (*ImportObjectError) Error

func (error *ImportObjectError) Error() string

ImportObjectError is an actual error. The `Error` function returns the error message.

type ImportedFunctionError

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

ImportedFunctionError represents any kind of errors related to a WebAssembly imported function. It is returned by `Import` or `Imports` functions only.

func NewImportedFunctionError

func NewImportedFunctionError(functionName string, message string) *ImportedFunctionError

NewImportedFunctionError constructs a new `ImportedFunctionError`, where `functionName` is the name of the imported function, and `message` is the error message. If the error message contains `%s`, then this parameter will be replaced by `functionName`.

func (*ImportedFunctionError) Error

func (error *ImportedFunctionError) Error() string

ImportedFunctionError is an actual error. The `Error` function returns the error message.

type Imports

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

Imports represents a set of imported functions for a WebAssembly instance.

func NewImports

func NewImports() *Imports

NewImports constructs a new empty `Imports`.

func (*Imports) Append

func (imports *Imports) Append(importName string, implementation interface{}, cgoPointer unsafe.Pointer) (*Imports, error)

Append adds a new imported function to the current set. Deprecated, please use AppendFunction instead.

func (*Imports) AppendFunction

func (imports *Imports) AppendFunction(importName string, implementation interface{}, cgoPointer unsafe.Pointer) (*Imports, error)

AppendFunction adds a new imported function to the current set.

func (*Imports) AppendMemory

func (imports *Imports) AppendMemory(importName string, memory *Memory) (*Imports, error)

AppendMemory adds a new imported memory to the current set.

func (*Imports) Close

func (imports *Imports) Close()

Close closes/frees all imports. For the moment, only imported functions must be freed. Imported memory must be freed manually by the owner.

func (*Imports) Namespace

func (imports *Imports) Namespace(namespace string) *Imports

Namespace changes the current namespace of the next imported functions.

type Instance

type Instance struct {

	// All functions exported by the WebAssembly instance, indexed
	// by their name as a string. An exported function is a
	// regular variadic Go closure. Arguments are untyped. Since
	// WebAssembly only supports: `i32`, `i64`, `f32` and `f64`
	// types, the accepted Go types are: `int8`, `uint8`, `int16`,
	// `uint16`, `int32`, `uint32`, `int64`, `int`, `uint`, `float32`
	// and `float64`. In addition to those types, the `Value` type
	// (from this project) is accepted. The conversion from a Go
	// value to a WebAssembly value is done automatically except for
	// the `Value` type (where type is coerced, that's the intent
	// here). The WebAssembly type is automatically inferred. Note
	// that the returned value is of kind `Value`, and not a
	// standard Go type.
	Exports map[string]func(...interface{}) (Value, error)

	// The exported memory of a WebAssembly instance.
	Memory *Memory
	// contains filtered or unexported fields
}

Instance represents a WebAssembly instance.

Example (Basic)
package main

import (
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Instantiates a WebAssembly instance from bytes.
	instance, err := wasm.NewInstance(GetBytes())
	defer instance.Close()

	_ = err
}
Output:

Example (ExportedFunctions)
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Instantiates a WebAssembly instance from bytes.
	instance, _ := wasm.NewInstance(GetBytes())
	defer instance.Close()

	// Gets an exported function.
	sum, functionExists := instance.Exports["sum"]

	fmt.Println(functionExists)

	// Calls the `sum` exported function with Go values.
	result, _ := sum(1, 2)

	fmt.Println(result)

	result, _ = sum(wasm.I32(3), wasm.I32(4))

	// Calls the `sum` exported function with WebAssembly values.
	fmt.Println(result)

}
Output:

true
3
7
Example (Memory)
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Instantiates a WebAssembly instance from bytes.
	instance, _ := wasm.NewInstance(GetBytes())
	defer instance.Close()

	// Calls an exported function.
	result, _ := instance.Exports["string"]()

	// Gets the pointer value as an integer.
	pointer := result.ToI32()

	// Reads 13 bytes as a string from the memory.
	memory := instance.Memory.Data()

	fmt.Println(
		string(memory[pointer : pointer+13]),
	)

}
Output:

Hello, World!

func NewInstance

func NewInstance(bytes []byte) (Instance, error)

NewInstance constructs a new `Instance` with no imports.

func NewInstanceWithImports

func NewInstanceWithImports(bytes []byte, imports *Imports) (Instance, error)

NewInstanceWithImports constructs a new `Instance` with imports.

func (*Instance) Close

func (instance *Instance) Close()

Close closes/frees an `Instance`.

Example
package main

import (
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Instantiates a WebAssembly instance from bytes.
	instance, _ := wasm.NewInstance(GetBytes())

	// Closes/frees the instance (usually used with `defer`).
	defer instance.Close()

}
Output:

func (*Instance) HasMemory

func (instance *Instance) HasMemory() bool

HasMemory checks whether the instance has at least one exported memory.

func (*Instance) SetContextData

func (instance *Instance) SetContextData(data interface{})

SetContextData assigns a data that can be used by all imported functions. Each imported function receives as its first argument an instance context (see `InstanceContext`). An instance context can hold any kind of data, including data that contain Go references such as slices, maps, or structs with reference types or pointers. It is important to understand that data is global to the instance, and thus is shared by all imported functions.

type InstanceContext

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

InstanceContext represents a way to access instance API from within an imported context.

func IntoInstanceContext

func IntoInstanceContext(instanceContext unsafe.Pointer) InstanceContext

IntoInstanceContext casts the first `context unsafe.Pointer` argument of an imported function into an `InstanceContext`.

func (*InstanceContext) Data

func (instanceContext *InstanceContext) Data() interface{}

Data returns the instance context data as an `interface{}`. It's up to the user to assert the proper type.

func (*InstanceContext) Memory

func (instanceContext *InstanceContext) Memory() *Memory

Memory returns the current instance memory.

type InstanceError

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

InstanceError represents any kind of errors related to a WebAssembly instance. It is returned by `Instance` functions only.

func NewInstanceError

func NewInstanceError(message string) *InstanceError

NewInstanceError constructs a new `InstanceError`.

func (*InstanceError) Error

func (error *InstanceError) Error() string

`InstanceError` is an actual error. The `Error` function returns the error message.

type MapDirEntry

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

MapDirEntry is an entry that can be passed to `NewWasiImportObject`. Preopens a file for the WASI module but renames it to the given name

type Memory

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

Memory represents a WebAssembly memory. To read and write data, please see the `Data` function. The memory can be owned or borrowed. It is only possible to create an owned memory from the user-land.

func NewMemory

func NewMemory(min, max uint32) (*Memory, error)

NewMemory instantiates a new owned WebAssembly memory, bound for imported memory.

func (*Memory) Close

func (memory *Memory) Close()

Close closes/frees memory allocated at the NewMemory at time.

func (*Memory) Data

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

Data returns a slice of bytes over the WebAssembly memory.

Example
package main

import (
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Instantiates the WebAssembly module.
	instance, _ := wasm.NewInstance(GetBytes())
	defer instance.Close()

	// Gets memory data as a slice of bytes.
	memory := instance.Memory.Data()

	// Then, regular slice operations are available.
	_ = memory[1:3]
}
Output:

func (*Memory) Grow

func (memory *Memory) Grow(numberOfPages uint32) error

Grow the memory by a number of pages (65kb each).

func (*Memory) IsOwned

func (memory *Memory) IsOwned() bool

IsOwned checks whether the memory is owned, or borrowed.

func (*Memory) Length

func (memory *Memory) Length() uint32

Length calculates the memory length (in bytes).

Example
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Instantiates the WebAssembly module.
	instance, _ := wasm.NewInstance(GetBytes())
	defer instance.Close()

	// Gets memory data length.
	length := instance.Memory.Length()

	fmt.Println(length)

}
Output:

1114112

type MemoryError

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

MemoryError represents any kind of errors related to a WebAssembly memory. It is returned by `Memory` functions only.

func NewMemoryError

func NewMemoryError(message string) *MemoryError

NewMemoryError constructs a new `MemoryError`.

func (*MemoryError) Error

func (error *MemoryError) Error() string

`MemoryError` is an actual error. The `Error` function returns the error message.

type Module

type Module struct {
	Exports []ExportDescriptor
	Imports []ImportDescriptor
	// contains filtered or unexported fields
}

Module represents a WebAssembly module.

func Compile

func Compile(bytes []byte) (Module, error)

Compile compiles a WebAssembly module from bytes.

Example
package main

import (
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Compiles the bytes into a WebAssembly module.
	module, err := wasm.Compile(GetBytes())
	defer module.Close()

	_ = err
}
Output:

func DeserializeModule

func DeserializeModule(serializedModuleBytes []byte) (Module, error)

DeserializeModule deserializes a sequence of bytes into a module. Ideally, those bytes must come from `Module.Serialize`.

func (*Module) Close

func (module *Module) Close()

Close closes/frees a `Module`.

func (*Module) Instantiate

func (module *Module) Instantiate() (Instance, error)

Instantiate creates a new instance of the WebAssembly module.

Example
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Compiles the bytes into a WebAssembly module.
	module, _ := wasm.Compile(GetBytes())
	defer module.Close()

	// Instantiates the WebAssembly module.
	instance, _ := module.Instantiate()
	defer instance.Close()

	// Gets an exported function.
	sum, functionExists := instance.Exports["sum"]

	fmt.Println(functionExists)

	// Calls the `sum` exported function with Go values.
	result, _ := sum(1, 2)

	fmt.Println(result)

}
Output:

true
3

func (*Module) InstantiateWithImportObject

func (module *Module) InstantiateWithImportObject(importObject *ImportObject) (Instance, error)

InstantiateWithImportObject creates a new instance of a WebAssembly module with an `ImportObject`

func (*Module) InstantiateWithImports

func (module *Module) InstantiateWithImports(imports *Imports) (Instance, error)

InstantiateWithImports creates a new instance with imports of the WebAssembly module.

func (*Module) Serialize

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

Serialize serializes the current module into a sequence of bytes. Those bytes can be deserialized into a module with `DeserializeModule`.

Example
package main

import (
	"fmt"
	wasm "github.com/wasmerio/go-ext-wasm/wasmer"
	"path"
	"runtime"
)

func GetBytes() []byte {
	_, filename, _, _ := runtime.Caller(0)
	modulePath := path.Join(path.Dir(filename), "test", "testdata", "tests.wasm")

	bytes, _ := wasm.ReadBytes(modulePath)

	return bytes
}

func main() {
	// Compiles the bytes into a WebAssembly module.
	module1, _ := wasm.Compile(GetBytes())
	defer module1.Close()

	// Serializes the module into a sequence of bytes.
	serialization, _ := module1.Serialize()

	// Do something with `serialization`.
	// Then later…

	// Deserializes the module.
	module2, _ := wasm.DeserializeModule(serialization)
	defer module2.Close()
	// And enjoy!

	// Instantiates the WebAssembly module.
	instance, _ := module2.Instantiate()
	defer instance.Close()

	// Gets an exported function.
	sum, functionExists := instance.Exports["sum"]

	fmt.Println(functionExists)

	// Calls the `sum` exported function with Go values.
	result, _ := sum(1, 2)

	fmt.Println(result)

}
Output:

true
3

type ModuleError

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

ModuleError represents any kind of errors related to a WebAssembly module.

func NewModuleError

func NewModuleError(message string) *ModuleError

NewModuleError constructs a new `ModuleError`.

func (*ModuleError) Error

func (error *ModuleError) Error() string

`ModuleError` is an actual error. The `Error` function returns the error message.

type Value

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

Value represents a WebAssembly value of a particular type.

func F32

func F32(value float32) Value

F32 constructs a WebAssembly value of type `f32`.

func F64

func F64(value float64) Value

F64 constructs a WebAssembly value of type `f64`.

func I32

func I32(value int32) Value

I32 constructs a WebAssembly value of type `i32`.

func I64

func I64(value int64) Value

I64 constructs a WebAssembly value of type `i64`.

func (Value) GetType

func (value Value) GetType() ValueType

GetType gets the type of the WebAssembly value.

func (Value) String

func (value Value) String() string

String formats the WebAssembly value as a Go string.

func (Value) ToF32

func (value Value) ToF32() float32

ToF32 reads the WebAssembly value bits as a `float32`. The WebAssembly value type is ignored.

func (Value) ToF64

func (value Value) ToF64() float64

ToF64 reads the WebAssembly value bits as a `float64`. The WebAssembly value type is ignored.

func (Value) ToI32

func (value Value) ToI32() int32

ToI32 reads the WebAssembly value bits as an `int32`. The WebAssembly value type is ignored.

func (Value) ToI64

func (value Value) ToI64() int64

ToI64 reads the WebAssembly value bits as an `int64`. The WebAssembly value type is ignored.

func (Value) ToVoid

func (value Value) ToVoid() interface{}

ToVoid reads the WebAssembly value bits as a `nil`. The WebAssembly value type is ignored.

type ValueType

type ValueType int

ValueType represents the `Value` type.

const (
	// TypeI32 represents the WebAssembly `i32` type.
	TypeI32 ValueType = iota

	// TypeI64 represents the WebAssembly `i64` type.
	TypeI64

	// TypeF32 represents the WebAssembly `f32` type.
	TypeF32

	// TypeF64 represents the WebAssembly `f64` type.
	TypeF64

	// TypeVoid represents nothing.
	// WebAssembly doesn't have “void” type, but it is introduced
	// here to represent the returned value of a WebAssembly exported
	// function that returns nothing.
	TypeVoid
)

type WasiVersion

type WasiVersion uint

WasiVersion represents the WASI version.

func WasiGetVersion

func WasiGetVersion(module Module) WasiVersion

WasiGetVersion returns the WASI version of a module if any, other `Unknown` is returned.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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