wasmexec

package module
v0.0.0-...-3f336d6 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: MIT Imports: 5 Imported by: 0

README

wasmexec

  • wasmexec is a utility to execute WebAssembly in Go with interface similar to WebAssembly.instantiateStreaming().
  • This package is using wagon as an interpreter of WebAssembly.

Usage

1. Create Wasm binary
  • multiply.wat
(module
  (import "go" "imultiply" (func $imultiply (param $a i32) (param $b i32) (result i32)))
  (func (export "multiply") (param $a i32) (param $b i32) (result i32)
    local.get $a
    local.get $b
    call $imultiply)
)
$ wat2wasm multiply.wat
2. Read Wasm binary and execute exported function
  • main.go
// 1. Open Wasm binary
f, _ := os.Open("exmaple.wasm")
defer f.Close()

// 2. Create ImportObject
importObject := wasmexec.ImportObject{
    "go": {
        "imultiply": func(a int, b int) int{
            return a * b
        },
    },
}

// 3. Initialize WebAssembly instance
inst, _ := wasmexec.InstantiateStreaming(f, importObject)

// 4. Call exported function
result, _ := inst.Call("multiply", uint64(2), uint64(3))
fmt.Println(result) // #=> 6

For details, please see examples/multiply.

Supported Features

  • import func
  • import global
  • import memory

License

MIT

Author

syumai

Documentation

Index

Constants

View Source
const MemoryPageSize = 65536 // 64KiB

Variables

This section is empty.

Functions

This section is empty.

Types

type ImportObject

type ImportObject map[string]map[string]interface{}

type Instance

type Instance struct {
	Module *wasm.Module
	VM     *exec.VM
}

func InstantiateStreaming

func InstantiateStreaming(src io.Reader, importObject ImportObject) (*Instance, error)

func (*Instance) Call

func (i *Instance) Call(name string, args ...uint64) (uint64, error)

type Memory

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

func NewMemory

func NewMemory(initial uint32, maximum uint32) *Memory

func (*Memory) Buffer

func (m *Memory) Buffer() []byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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