wasman

package module
v0.0.0-...-4c44063 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 9 Imported by: 1

README

WASMan (WebAssembly Manager)

Go Report Card Go

Another wasm interpreter engine for gophers.

This is a substantially modified fork of https://github.com/c0mm4nd/wasman by way of https://github.com/orsinium-forks/wasman for the purpose of major bugfixing.

Usage

Executable

Install

go install github.com/hybridgroup/wasman/cmd/wasman
$ wasman -h
Usage of ./wasman:
  -extern-files string
        external modules files
  -func string
        main func (default "main")
  -main string
        main module (default "module.wasm")
  -max-toll uint
        the maximum toll in simple toll station

Example: numeric.wasm

$ wasman -main numeric.wasm -func fib 20 # calc the fibonacci number
{
  type: i32
  result: 6765
  toll: 315822
}

If we limit the max toll, it will panic when overflow.

$ wasman -main numeric.wasm -max-toll 300000 -func fib 20
panic: toll overflow

goroutine 1 [running]:
main.main()
        /home/ubuntu/Desktop/wasman/cmd/wasman/main.go:85 +0x87d
Go Embedding

PkgGoDev

Example

Look for examples?

They are in examples folder

TODOs

  • add more complex examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSign = errors.New("invalid signature")
)

errors on linking modules

Functions

func DefineFunc

func DefineFunc(l *Linker, modName, funcName string, f func()) error

func DefineFunc01

func DefineFunc01[Z Primitive](l *Linker, modName, funcName string, f func() Z) error

func DefineFunc02

func DefineFunc02[Y, Z Primitive](l *Linker, modName, funcName string, f func() (Y, Z)) error

func DefineFunc10

func DefineFunc10[A Primitive](l *Linker, modName, funcName string, f func(A)) error

func DefineFunc11

func DefineFunc11[A, Z Primitive](l *Linker, modName, funcName string, f func(A) Z) error

func DefineFunc12

func DefineFunc12[A, Y, Z Primitive](l *Linker, modName, funcName string, f func(A) (Y, Z)) error

func DefineFunc20

func DefineFunc20[A, B Primitive](l *Linker, modName, funcName string, f func(A, B)) error

func DefineFunc21

func DefineFunc21[A, B, Z Primitive](l *Linker, modName, funcName string, f func(A, B) Z) error

func DefineFunc22

func DefineFunc22[A, B, Y, Z Primitive](l *Linker, modName, funcName string, f func(A, B) (Y, Z)) error

func DefineFunc31

func DefineFunc31[A, B, C, Z Primitive](l *Linker, modName, funcName string, f func(A, B, C) Z) error

func DefineFunc32

func DefineFunc32[A, B, C, Y, Z Primitive](l *Linker, modName, funcName string, f func(A, B, C) (Y, Z)) error

func DefineGlobal

func DefineGlobal[T any](l *Linker, modName, globalName string, global T) error

Types

type Instance

type Instance = wasm.Instance

Instance is same to wasm.Instance

func NewInstance

func NewInstance(module *Module, externModules map[string]*Module) (*Instance, error)

NewInstance is a wrapper to the wasm.NewInstance

type Linker

type Linker struct {
	config.LinkerConfig

	Modules map[string]*Module // the built-in modules which acts as externs when instantiating coming main module
}

Linker is a helper to instantiate new modules

func NewLinker

func NewLinker(config config.LinkerConfig) *Linker

NewLinker creates a new Linker

func NewLinkerWithModuleMap

func NewLinkerWithModuleMap(config config.LinkerConfig, in map[string]*Module) *Linker

NewLinkerWithModuleMap creates a new Linker with the built-in modules

func (*Linker) Define

func (l *Linker) Define(modName string, mod *Module)

Define put the module on its namespace

func (*Linker) DefineMemory

func (l *Linker) DefineMemory(modName, memName string, mem []byte) error

DefineMemory will defined an external memory for the main module

func (*Linker) DefineRawHostFunc

func (l *Linker) DefineRawHostFunc(
	modName, funcName string, f wasm.RawHostFunc, ins []any, outs []any,
) error

DefineRawHostFunc puts a simple raw func into Linker's modules.

func (*Linker) DefineTable

func (l *Linker) DefineTable(modName, tableName string, table []*uint32) error

DefineTable will defined an external table for the main module

func (*Linker) Instantiate

func (l *Linker) Instantiate(mainModule *Module) (*Instance, error)

Instantiate will instantiate a Module into an runnable Instance

type Module

type Module = wasm.Module

Module is same to wasm.Module

func NewModule

func NewModule(config config.ModuleConfig, r utils.Reader) (*Module, error)

NewModule is a wrapper to the wasm.NewModule

func NewModuleFromBytes

func NewModuleFromBytes(config config.ModuleConfig, b []byte) (*Module, error)

NewModuleFromBytes is a wrapper to the wasm.NewModule that avoids having to make a copy of bytes that are already in memory.

type Primitive

type Primitive interface {
	int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 | uintptr |
		float32 | float64
}

Primitive is a type constraint for arguments and results of host-defined functions that can be used with the linker.

Directories

Path Synopsis
cmd
log

Jump to

Keyboard shortcuts

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