wasm

package
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: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFuncInvalidInputType  = errors.New("invalid func input type")
	ErrFuncInvalidReturnType = errors.New("invalid func return type")
)

errors of func

View Source
var (
	ErrExportedFuncNotFound = errors.New("exported func is not found")
	ErrFuncIndexOutOfRange  = errors.New("function index out of range")
	ErrInvalidArgNum        = errors.New("invalid number of arguments")
)

errors on exec func

View Source
var (
	ErrUnreachable                 = errors.New("unreachable")
	ErrBlockNotInitialized         = errors.New("block not initialized")
	ErrBlockNotFound               = errors.New("block not found")
	ErrFuncSignMismatch            = errors.New("function signature mismatch")
	ErrLabelNotFound               = errors.New("label not found")
	ErrTableIndexOutOfRange        = errors.New("table index out of range")
	ErrTableInstanceNotInitialized = errors.New("table entry not initialized")
)

errors on control instr

View Source
var (
	ErrInvalidMagicNumber = errors.New("invalid magic number")
	ErrInvalidVersion     = errors.New("invalid version header")
)

errors on parsing module

View Source
var ErrInvalidSubcode = errors.New("invalid bulk memory subcode")
View Source
var ErrPtrOutOfBounds = errors.New("pointer is out of bounds")

ErrPtrOutOfBounds will be throw when the pointer visiting a pos out of the range of memory

View Source
var ErrUndefined = errors.New("undefined")

ErrUndefined is a panic error

Functions

func MemoryPagesToBytesNum

func MemoryPagesToBytesNum(pages uint32) (bytesNum uint64)

MemoryPagesToBytesNum converts the given pages into the number of bytes contained in these pages.

Types

type Frame

type Frame struct {
	PC         uint64
	Func       *wasmFunc
	Locals     []uint64
	LabelStack *stacks.Stack[*stacks.Label]
}

Frame is the context data of one instance

type Global

type Global struct {
	*types.GlobalType
	Val interface{}
}

Global is an instance of the global value

type HostFunc

type HostFunc struct {
	Signature *types.FuncType // the shape of func (defined by inputs and outputs)

	// Generator is a func defined by other dev which acts as a Generator to the function
	// (generate when NewInstance's func initializing
	Generator func(ins *Instance) RawHostFunc
	// contains filtered or unexported fields
}

HostFunc is an implement of wasm.Fn, which represents all the functions defined under host(golang) environment

type IndexSpace

type IndexSpace struct {
	Functions []fn
	Globals   []*Global
	Tables    []*Table
	Memories  []*Memory
}

IndexSpace is the indeices to the imports

type Instance

type Instance struct {
	*Module

	Active     *Frame
	FrameStack *stacks.Stack[*Frame]

	Functions []fn
	Memory    *Memory
	Globals   []uint64

	OperandStack *stacks.Stack[uint64]
}

Instance is an instantiated module

func NewInstance

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

NewInstance will instantiate the module with extern modules

func (*Instance) CallExportedFunc

func (ins *Instance) CallExportedFunc(name string, args ...uint64) (returns []uint64, returnTypes []types.ValueType, err error)

CallExportedFunc will call the func `name` with the args TODO: enhance this

type Memory

type Memory struct {
	types.MemoryType
	External bool
	Value    []byte
}

Memory is an instance of the memory value

func (*Memory) Grow

func (mem *Memory) Grow(newPages uint32) (result uint32)

func (*Memory) PageSize

func (m *Memory) PageSize() uint32

PageSize returns the current memory buffer size in pages.

type Module

type Module struct {
	config.ModuleConfig

	// sections
	TypeSection     []*types.FuncType
	ImportSection   []*segments.ImportSegment
	FunctionSection []uint32
	TableSection    []*types.TableType
	MemorySection   []*types.MemoryType
	GlobalSection   []*segments.GlobalSegment
	ExportSection   map[string]*segments.ExportSegment
	StartSection    []uint32
	ElementsSection []*segments.ElemSegment
	CodeSection     []*segments.CodeSegment
	DataSection     []*segments.DataSegment
	DataCount       uint32

	// index spaces
	IndexSpace *IndexSpace
}

Module is a standard wasm module implement according to wasm v1, https://www.w3.org/TR/wasm-core-1/#syntax-module%E2%91%A0

func NewModule

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

NewModule reads bytes from the io.Reader and read all sections, finally return a wasman.Module entity if no error

type RawHostFunc

type RawHostFunc = func([]uint64) []uint64

Host-defined function that accepts and returns raw values.

It is up to the function implementation to interpret bits from the raw values as the expected Go types.

type Table

type Table struct {
	types.TableType
	Value []*uint32 // vec of addr to func
}

Table is an instance of the table value

Jump to

Keyboard shortcuts

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