wasm

package
v0.0.0-...-4e1a221 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpcodeUnreachableName       = "unreachable"
	OpcodeNopName               = "nop"
	OpcodeBlockName             = "block"
	OpcodeLoopName              = "loop"
	OpcodeIfName                = "if"
	OpcodeElseName              = "else"
	OpcodeEndName               = "end"
	OpcodeBrName                = "br"
	OpcodeBrIfName              = "br_if"
	OpcodeBrTableName           = "br_table"
	OpcodeReturnName            = "return"
	OpcodeCallName              = "call"
	OpcodeCallIndirectName      = "call_indirect"
	OpcodeDropName              = "drop"
	OpcodeSelectName            = "select"
	OpcodeLocalGetName          = "local.get"
	OpcodeLocalSetName          = "local.set"
	OpcodeLocalTeeName          = "local.tee"
	OpcodeGlobalGetName         = "global.get"
	OpcodeGlobalSetName         = "global.set"
	OpcodeI32LoadName           = "i32.load"
	OpcodeI64LoadName           = "i64.load"
	OpcodeF32LoadName           = "f32.load"
	OpcodeF64LoadName           = "f64.load"
	OpcodeI32Load8SName         = "i32.load8_s"
	OpcodeI32Load8UName         = "i32.load8_u"
	OpcodeI32Load16SName        = "i32.load16_s"
	OpcodeI32Load16UName        = "i32.load16_u"
	OpcodeI64Load8SName         = "i64.load8_s"
	OpcodeI64Load8UName         = "i64.load8_u"
	OpcodeI64Load16SName        = "i64.load16_s"
	OpcodeI64Load16UName        = "i64.load16_u"
	OpcodeI64Load32SName        = "i64.load32_s"
	OpcodeI64Load32UName        = "i64.load32_u"
	OpcodeI32StoreName          = "i32.store"
	OpcodeI64StoreName          = "i64.store"
	OpcodeF32StoreName          = "f32.store"
	OpcodeF64StoreName          = "f64.store"
	OpcodeI32Store8Name         = "i32.store8"
	OpcodeI32Store16Name        = "i32.store16"
	OpcodeI64Store8Name         = "i64.store8"
	OpcodeI64Store16Name        = "i64.store16"
	OpcodeI64Store32Name        = "i64.store32"
	OpcodeMemorySizeName        = "memory.size"
	OpcodeMemoryGrowName        = "memory.grow"
	OpcodeI32ConstName          = "i32.const"
	OpcodeI64ConstName          = "i64.const"
	OpcodeF32ConstName          = "f32.const"
	OpcodeF64ConstName          = "f64.const"
	OpcodeI32EqzName            = "i32.eqz"
	OpcodeI32EqName             = "i32.eq"
	OpcodeI32NeName             = "i32.ne"
	OpcodeI32LtSName            = "i32.lt_s"
	OpcodeI32LtUName            = "i32.lt_u"
	OpcodeI32GtSName            = "i32.gt_s"
	OpcodeI32GtUName            = "i32.gt_u"
	OpcodeI32LeSName            = "i32.le_s"
	OpcodeI32LeUName            = "i32.le_u"
	OpcodeI32GeSName            = "i32.ge_s"
	OpcodeI32GeUName            = "i32.ge_u"
	OpcodeI64EqzName            = "i64.eqz"
	OpcodeI64EqName             = "i64.eq"
	OpcodeI64NeName             = "i64.ne"
	OpcodeI64LtSName            = "i64.lt_s"
	OpcodeI64LtUName            = "i64.lt_u"
	OpcodeI64GtSName            = "i64.gt_s"
	OpcodeI64GtUName            = "i64.gt_u"
	OpcodeI64LeSName            = "i64.le_s"
	OpcodeI64LeUName            = "i64.le_u"
	OpcodeI64GeSName            = "i64.ge_s"
	OpcodeI64GeUName            = "i64.ge_u"
	OpcodeF32EqName             = "f32.eq"
	OpcodeF32NeName             = "f32.ne"
	OpcodeF32LtName             = "f32.lt"
	OpcodeF32GtName             = "f32.gt"
	OpcodeF32LeName             = "f32.le"
	OpcodeF32GeName             = "f32.ge"
	OpcodeF64EqName             = "f64.eq"
	OpcodeF64NeName             = "f64.ne"
	OpcodeF64LtName             = "f64.lt"
	OpcodeF64GtName             = "f64.gt"
	OpcodeF64LeName             = "f64.le"
	OpcodeF64GeName             = "f64.ge"
	OpcodeI32ClzName            = "i32.clz"
	OpcodeI32CtzName            = "i32.ctz"
	OpcodeI32PopcntName         = "i32.popcnt"
	OpcodeI32AddName            = "i32.add"
	OpcodeI32SubName            = "i32.sub"
	OpcodeI32MulName            = "i32.mul"
	OpcodeI32DivSName           = "i32.div_s"
	OpcodeI32DivUName           = "i32.div_u"
	OpcodeI32RemSName           = "i32.rem_s"
	OpcodeI32RemUName           = "i32.rem_u"
	OpcodeI32AndName            = "i32.and"
	OpcodeI32OrName             = "i32.or"
	OpcodeI32XorName            = "i32.xor"
	OpcodeI32ShlName            = "i32.shl"
	OpcodeI32ShrSName           = "i32.shr_s"
	OpcodeI32ShrUName           = "i32.shr_u"
	OpcodeI32RotlName           = "i32.rotl"
	OpcodeI32RotrName           = "i32.rotr"
	OpcodeI64ClzName            = "i64.clz"
	OpcodeI64CtzName            = "i64.ctz"
	OpcodeI64PopcntName         = "i64.popcnt"
	OpcodeI64AddName            = "i64.add"
	OpcodeI64SubName            = "i64.sub"
	OpcodeI64MulName            = "i64.mul"
	OpcodeI64DivSName           = "i64.div_s"
	OpcodeI64DivUName           = "i64.div_u"
	OpcodeI64RemSName           = "i64.rem_s"
	OpcodeI64RemUName           = "i64.rem_u"
	OpcodeI64AndName            = "i64.and"
	OpcodeI64OrName             = "i64.or"
	OpcodeI64XorName            = "i64.xor"
	OpcodeI64ShlName            = "i64.shl"
	OpcodeI64ShrSName           = "i64.shr_s"
	OpcodeI64ShrUName           = "i64.shr_u"
	OpcodeI64RotlName           = "i64.rotl"
	OpcodeI64RotrName           = "i64.rotr"
	OpcodeF32AbsName            = "f32.abs"
	OpcodeF32NegName            = "f32.neg"
	OpcodeF32CeilName           = "f32.ceil"
	OpcodeF32FloorName          = "f32.floor"
	OpcodeF32TruncName          = "f32.trunc"
	OpcodeF32NearestName        = "f32.nearest"
	OpcodeF32SqrtName           = "f32.sqrt"
	OpcodeF32AddName            = "f32.add"
	OpcodeF32SubName            = "f32.sub"
	OpcodeF32MulName            = "f32.mul"
	OpcodeF32DivName            = "f32.div"
	OpcodeF32MinName            = "f32.min"
	OpcodeF32MaxName            = "f32.max"
	OpcodeF32CopysignName       = "f32.copysign"
	OpcodeF64AbsName            = "f64.abs"
	OpcodeF64NegName            = "f64.neg"
	OpcodeF64CeilName           = "f64.ceil"
	OpcodeF64FloorName          = "f64.floor"
	OpcodeF64TruncName          = "f64.trunc"
	OpcodeF64NearestName        = "f64.nearest"
	OpcodeF64SqrtName           = "f64.sqrt"
	OpcodeF64AddName            = "f64.add"
	OpcodeF64SubName            = "f64.sub"
	OpcodeF64MulName            = "f64.mul"
	OpcodeF64DivName            = "f64.div"
	OpcodeF64MinName            = "f64.min"
	OpcodeF64MaxName            = "f64.max"
	OpcodeF64CopysignName       = "f64.copysign"
	OpcodeI32WrapI64Name        = "i32.wrap_i64"
	OpcodeI32TruncF32SName      = "i32.trunc_f32_s"
	OpcodeI32TruncF32UName      = "i32.trunc_f32_u"
	OpcodeI32TruncF64SName      = "i32.trunc_f64_s"
	OpcodeI32TruncF64UName      = "i32.trunc_f64_u"
	OpcodeI64ExtendI32SName     = "i64.extend_i32_s"
	OpcodeI64ExtendI32UName     = "i64.extend_i32_u"
	OpcodeI64TruncF32SName      = "i64.trunc_f32_s"
	OpcodeI64TruncF32UName      = "i64.trunc_f32_u"
	OpcodeI64TruncF64SName      = "i64.trunc_f64_s"
	OpcodeI64TruncF64UName      = "i64.trunc_f64_u"
	OpcodeF32ConvertI32sName    = "f32.convert_i32_s"
	OpcodeF32ConvertI32UName    = "f32.convert_i32_u"
	OpcodeF32ConvertI64SName    = "f32.convert_i64_s"
	OpcodeF32ConvertI64UName    = "f32.convert_i64u"
	OpcodeF32DemoteF64Name      = "f32.demote_f64"
	OpcodeF64ConvertI32SName    = "f64.convert_i32_s"
	OpcodeF64ConvertI32UName    = "f64.convert_i32_u"
	OpcodeF64ConvertI64SName    = "f64.convert_i64_s"
	OpcodeF64ConvertI64UName    = "f64.convert_i64_u"
	OpcodeF64PromoteF32Name     = "f64.promote_f32"
	OpcodeI32ReinterpretF32Name = "i32.reinterpret_f32"
	OpcodeI64ReinterpretF64Name = "i64.reinterpret_f64"
	OpcodeF32ReinterpretI32Name = "f32.reinterpret_i32"
	OpcodeF64ReinterpretI64Name = "f64.reinterpret_i64"

	OpcodeI32Extend8SName  = "i32.extend8_s"
	OpcodeI32Extend16SName = "i32.extend16_s"
	OpcodeI64Extend8SName  = "i64.extend8_s"
	OpcodeI64Extend16SName = "i64.extend16_s"
	OpcodeI64Extend32SName = "i64.extend32_s"
)
View Source
const (
	// MemoryPageSize is the unit of memory length in WebAssembly,
	// and is defined as 2^16 = 65536.
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-instances%E2%91%A0
	MemoryPageSize = uint32(65536)
	// MemoryMaxPages is maximum number of pages defined (2^16).
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#grow-mem
	MemoryMaxPages = uint32(65536)
	// MemoryPageSizeInBits satisfies the relation: "1 << MemoryPageSizeInBits == MemoryPageSize".
	MemoryPageSizeInBits = 16
)
View Source
const (
	MaximumGlobals       = uint32(1 << 27)
	MaximumFunctionIndex = uint32(1 << 27)
)

The wazero specific limitation described at RATIONALE.md. TL;DR; We multiply by 8 (to get offsets in bytes) and the multiplication result must be less than 32bit max

View Source
const (
	ValueTypeI32 = api.ValueTypeI32
	ValueTypeI64 = api.ValueTypeI64
	ValueTypeF32 = api.ValueTypeF32
	ValueTypeF64 = api.ValueTypeF64
)
View Source
const (
	// ExternTypeFuncName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeFunc.
	ExternTypeFuncName = "func"
	// ExternTypeTableName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeTable.
	ExternTypeTableName = "table"
	// ExternTypeMemoryName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeMemory.
	ExternTypeMemoryName = "memory"
	// ExternTypeGlobalName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeGlobal.
	ExternTypeGlobalName = "global"
)

The below are exported to consolidate parsing behavior for external types.

View Source
const Features20191205 = FeatureMutableGlobal

Features20191205 include those finished in WebAssembly 1.0 (20191205).

See https://github.com/WebAssembly/proposals/blob/main/finished-proposals.md See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205

View Source
const FeaturesFinished = 0xffffffffffffffff

FeaturesFinished include all supported finished features, regardless of W3C status.

See https://github.com/WebAssembly/proposals/blob/main/finished-proposals.md

View Source
const SectionIDHostFunction = SectionID(0xff)

SectionIDHostFunction is a pseudo-section ID for host functions.

Note: This is not defined in the WebAssembly 1.0 (20191205) Binary Format.

Variables

This section is empty.

Functions

func CallGoFunc

func CallGoFunc(ctx context.Context, callCtx *CallContext, f *FunctionInstance, params []uint64) []uint64

CallGoFunc executes the FunctionInstance.GoFunc by converting params to Go types. The results of the function call are converted back to api.ValueType.

* callCtx is passed to the host function as a first argument.

Note: ctx must use the caller's memory, which might be different from the defining module on an imported function.

func ExternTypeName

func ExternTypeName(et ExternType) string

ExternTypeName returns the name of the WebAssembly 1.0 (20191205) Text Format field of the given type.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#imports⑤ See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#exports%E2%91%A4

func InstructionName

func InstructionName(oc Opcode) string

InstructionName returns the instruction corresponding to this binary Opcode. See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#a7-index-of-instructions

func MemoryPagesToBytesNum

func MemoryPagesToBytesNum(pages uint32) (bytesNum uint64)

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

func PagesToUnitOfBytes

func PagesToUnitOfBytes(pages uint32) string

PagesToUnitOfBytes converts the pages to a human-readable form similar to what's specified. Ex. 1 -> "64Ki"

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-instances%E2%91%A0

func PopGoFuncParams

func PopGoFuncParams(f *FunctionInstance, popParam func() uint64) []uint64

PopGoFuncParams pops the correct number of parameters off the stack into a parameter slice for use in CallGoFunc

For example, if the host function F requires the (x1 uint32, x2 float32) parameters, and the stack is [..., A, B], then the function is called as F(A, B) where A and B are interpreted as uint32 and float32 respectively.

func PopValues

func PopValues(count int, popper func() uint64) []uint64

PopValues pops api.ValueType values from the stack and returns them in reverse order.

Note: the popper intentionally doesn't return bool or error because the caller's stack depth is trusted.

func SectionIDName

func SectionIDName(sectionID SectionID) string

SectionIDName returns the canonical name of a module section. https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#sections%E2%91%A0

func ValueTypeName

func ValueTypeName(t ValueType) string

ValueTypeName is an alias of api.ValueTypeName defined to simplify imports.

Types

type CallContext

type CallContext struct {

	// Note: This is a part of CallContext so that scope is correct and Close is coherent.
	// Sys is exposed only for WASI
	Sys *SysContext
	// contains filtered or unexported fields
}

CallContext is a function call context bound to a module. This is important as one module's functions can call imported functions, but all need to effect the same memory.

Note: This does not include the context.Context because doing so risks caching the wrong context which can break functionality like trace propagation. Note: this also implements api.Module in order to simplify usage as a host function parameter.

func NewCallContext

func NewCallContext(store *Store, instance *ModuleInstance, Sys *SysContext) *CallContext

func (*CallContext) Close

func (m *CallContext) Close() (err error)

Close implements the same method as documented on api.Module.

func (*CallContext) CloseWithExitCode

func (m *CallContext) CloseWithExitCode(exitCode uint32) (err error)

CloseWithExitCode implements the same method as documented on api.Module.

func (*CallContext) ExportedFunction

func (m *CallContext) ExportedFunction(name string) api.Function

ExportedFunction implements api.Module ExportedFunction

func (*CallContext) ExportedGlobal

func (m *CallContext) ExportedGlobal(name string) api.Global

ExportedGlobal implements api.Module ExportedGlobal

func (*CallContext) ExportedMemory

func (m *CallContext) ExportedMemory(name string) api.Memory

ExportedMemory implements api.Module ExportedMemory

func (*CallContext) FailIfClosed

func (m *CallContext) FailIfClosed() error

FailIfClosed returns a sys.ExitError if CloseWithExitCode was called.

func (*CallContext) GetIncCounter

func (m *CallContext) GetIncCounter() uint64

GetIncCounter Increases opcounter on 1 and retruns new value

func (*CallContext) Memory

func (m *CallContext) Memory() api.Memory

Memory implements api.Module Memory

func (*CallContext) Name

func (m *CallContext) Name() string

Name implements the same method as documented on api.Module

func (*CallContext) String

func (m *CallContext) String() string

String implements the same method as documented on api.Module

func (*CallContext) WithMemory

func (m *CallContext) WithMemory(memory *MemoryInstance) *CallContext

WithMemory allows overriding memory without re-allocation when the result would be the same.

type Code

type Code struct {
	// LocalTypes are any function-scoped variables in insertion order.
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-local
	LocalTypes []ValueType

	// Body is a sequence of expressions ending in OpcodeEnd
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-expr
	Body []byte
}

Code is an entry in the Module.CodeSection containing the locals and body of the function. See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-code

type ConstantExpression

type ConstantExpression struct {
	Opcode Opcode
	Data   []byte
}

type DataSegment

type DataSegment struct {
	OffsetExpression *ConstantExpression
	Init             []byte
}

type DecodeModule

type DecodeModule func(source []byte, enabledFeatures Features, memoryMaxPages uint32) (result *Module, err error)

DecodeModule parses the configured source into a Module. This function returns when the source is exhausted or an error occurs. The result can be initialized for use via Store.Instantiate.

Here's a description of the return values: * result is the module parsed or nil on error * err is a FormatError invoking the parser, dangling block comments or unexpected characters. See binary.DecodeModule and text.DecodeModule

type ElemType

type ElemType = byte

ElemType is fixed to ElemTypeFuncref until post 20191205 reference type is implemented.

const (
	ElemTypeFuncref ElemType = 0x70
)

type ElementSegment

type ElementSegment struct {
	// OffsetExpr returns the table element offset to apply to Init indices.
	// Note: This can be validated prior to instantiation unless it includes OpcodeGlobalGet (an imported global).
	OffsetExpr *ConstantExpression

	// Init indices are table elements relative to the result of OffsetExpr. The values are positions in the function
	// index namespace that initialize the corresponding element.
	Init []Index
}

ElementSegment are initialization instructions for a TableInstance

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#syntax-elem

type EncodeModule

type EncodeModule func(m *Module) (bytes []byte)

EncodeModule encodes the given module into a byte slice depending on the format of the implementation. See binary.EncodeModule

type Engine

type Engine interface {
	// CompileModule implements the same method as documented on wasm.Engine.
	CompileModule(ctx context.Context, module *Module) error

	// NewModuleEngine compiles down the function instances in a module, and returns ModuleEngine for the module.
	//
	// * name is the name the module was instantiated with used for error handling.
	// * module is the source module from which moduleFunctions are instantiated. This is used for caching.
	// * importedFunctions: functions this module imports, already compiled in this engine.
	// * moduleFunctions: functions declared in this module that must be compiled.
	// * table: a possibly shared table used by this module. When nil tableInit will be nil.
	// * tableInit: a mapping of TableInstance.Table index to the function index it should point to.
	//
	// Note: Input parameters must be pre-validated with wasm.Module Validate, to ensure no fields are invalid
	// due to reasons such as out-of-bounds.
	NewModuleEngine(
		name string,
		module *Module,
		importedFunctions, moduleFunctions []*FunctionInstance,
		table *TableInstance,
		tableInit map[Index]Index,
	) (ModuleEngine, error)

	// DeleteCompiledModule releases compilation caches for the given module (source).
	// Note: it is safe to call this function for a module from which module instances are instantiated even when these module instances
	// are having outstanding calls.
	DeleteCompiledModule(module *Module)
}

Engine is a Store-scoped mechanism to compile functions declared or imported by a module. This is a top-level type implemented by an interpreter or JIT compiler.

type Export

type Export struct {
	Type ExternType

	// Name is what the host refers to this definition as.
	Name string

	// Index is the index of the definition to export, the index namespace is by Type
	// Ex. If ExternTypeFunc, this is a position in the function index namespace.
	Index Index
}

Export is the binary representation of an export indicated by Type See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-export

type ExportInstance

type ExportInstance struct {
	Type     ExternType
	Function *FunctionInstance
	Global   *GlobalInstance
	Memory   *MemoryInstance
	Table    *TableInstance
}

ExportInstance represents an exported instance in a Store. The difference from the spec is that in wazero, a ExportInstance holds pointers to the instances, rather than "addresses" (i.e. index to Store.Functions, Globals, etc) for convenience.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#syntax-exportinst

type ExternType

type ExternType = byte

ExternType classifies imports and exports with their respective types.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#import-section%E2%91%A0 See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#export-section%E2%91%A0 See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#external-types%E2%91%A0

const (
	ExternTypeFunc   ExternType = 0x00
	ExternTypeTable  ExternType = 0x01
	ExternTypeMemory ExternType = 0x02
	ExternTypeGlobal ExternType = 0x03
)

type Features

type Features uint64

Features are the currently enabled features.

Note: This is a bit flag until we have too many (>63). Flags are simpler to manage in multiple places than a map.

const (
	// FeatureMutableGlobal decides if global vars are allowed to be imported or exported (ExternTypeGlobal)
	// See https://github.com/WebAssembly/mutable-global
	FeatureMutableGlobal Features = 1 << iota

	// FeatureSignExtensionOps decides if parsing should succeed on the following instructions:
	//
	// * OpcodeI32Extend8S
	// * OpcodeI32Extend16S
	// * OpcodeI64Extend8S
	// * OpcodeI64Extend16S
	// * OpcodeI64Extend32S
	//
	// See https://github.com/WebAssembly/spec/blob/main/proposals/sign-extension-ops/Overview.md
	FeatureSignExtensionOps

	// FeatureMultiValue decides if parsing should succeed on the following:
	//
	// * FunctionType.Results length greater than one.
	// * `block`, `loop` and `if` can be arbitrary function types.
	//
	// See https://github.com/WebAssembly/spec/blob/main/proposals/multi-value/Overview.md
	FeatureMultiValue
)

func (Features) Get

func (f Features) Get(feature Features) bool

Get returns the value of the given feature.

func (Features) Require

func (f Features) Require(feature Features) error

Require fails with a configuration error if the given feature is not enabled

func (Features) Set

func (f Features) Set(feature Features, val bool) Features

Set assigns the value for the given feature.

func (Features) String

func (f Features) String() string

String implements fmt.Stringer by returning each enabled feature.

type FileEntry

type FileEntry struct {
	Path string
	FS   fs.FS
	// File when nil this is a mount like "." or "/".
	File fs.File
}

FileEntry maps a path to an open file in a file system.

Note: This does not introduce cycles because the types here are in the package "wasi" not "internalwasi".

type FunctionInstance

type FunctionInstance struct {
	// DebugName is for debugging purpose, and is used to augment stack traces.
	DebugName string

	// Kind describes how this function should be called.
	Kind FunctionKind

	// Type is the signature of this function.
	Type *FunctionType

	// LocalTypes holds types of locals, set when Kind == FunctionKindWasm
	LocalTypes []ValueType

	// Body is the function body in WebAssembly Binary Format, set when Kind == FunctionKindWasm
	Body []byte

	// GoFunc holds the runtime representation of host functions.
	// This is nil when Kind == FunctionKindWasm. Otherwise, all the above fields are ignored as they are
	// specific to Wasm functions.
	GoFunc *reflect.Value

	// ModuleInstance holds the pointer to the module instance to which this function belongs.
	Module *ModuleInstance

	// TypeID is assigned by a store for FunctionType.
	TypeID FunctionTypeID

	// Index holds the index of this function instance in the function index namespace (beginning with imports).
	Index Index
}

FunctionInstance represents a function instance in a Store. See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#function-instances%E2%91%A0

func (*FunctionInstance) Call

func (f *FunctionInstance) Call(ctx context.Context, params ...uint64) (ret []uint64, err error)

Call implements the same method as documented on api.Function

func (*FunctionInstance) ParamTypes

func (f *FunctionInstance) ParamTypes() []api.ValueType

ParamTypes implements the same method as documented on api.Function

func (*FunctionInstance) ResultTypes

func (f *FunctionInstance) ResultTypes() []api.ValueType

ResultTypes implements the same method as documented on api.Function

type FunctionKind

type FunctionKind byte

FunctionKind identifies the type of function that can be called.

const (
	// FunctionKindWasm is not a Go function: it is implemented in Wasm.
	FunctionKindWasm FunctionKind = iota
	// FunctionKindGoNoContext is a function implemented in Go, with a signature matching FunctionType.
	FunctionKindGoNoContext
	// FunctionKindGoContext is a function implemented in Go, with a signature matching FunctionType, except arg zero is
	// a context.Context.
	FunctionKindGoContext
	// FunctionKindGoModule is a function implemented in Go, with a signature matching FunctionType, except arg
	// zero is an api.Module.
	FunctionKindGoModule
	// FunctionKindGoContextModule is a function implemented in Go, with a signature matching FunctionType, except arg
	// zero is a context.Context and arg one is an api.Module.
	FunctionKindGoContextModule
)

type FunctionType

type FunctionType struct {
	// Params are the possibly empty sequence of value types accepted by a function with this signature.
	Params []ValueType

	// Results are the possibly empty sequence of value types returned by a function with this signature.
	//
	// Note: In WebAssembly 1.0 (20191205), there can be at most one result.
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#result-types%E2%91%A0
	Results []ValueType
	// contains filtered or unexported fields
}

FunctionType is a possibly empty function signature.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#function-types%E2%91%A0

func DecodeBlockType

func DecodeBlockType(types []*FunctionType, r *bytes.Reader, enabledFeatures Features) (*FunctionType, uint64, error)

func (*FunctionType) EqualsSignature

func (t *FunctionType) EqualsSignature(params []ValueType, results []ValueType) bool

EqualsSignature returns true if the function type has the same parameters and results.

func (*FunctionType) String

func (t *FunctionType) String() string

String implements fmt.Stringer.

type FunctionTypeID

type FunctionTypeID uint32

FunctionTypeID is a uniquely assigned integer for a function type. This is wazero specific runtime object and specific to a store, and used at runtime to do type-checks on indirect function calls.

type Global

type Global struct {
	Type *GlobalType
	Init *ConstantExpression
}

type GlobalInstance

type GlobalInstance struct {
	Type *GlobalType
	// Val holds a 64-bit representation of the actual value.
	Val uint64
}

GlobalInstance represents a global instance in a store. See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#global-instances%E2%91%A0

type GlobalType

type GlobalType struct {
	ValType ValueType
	Mutable bool
}

type Import

type Import struct {
	Type ExternType
	// Module is the possibly empty primary namespace of this import
	Module string
	// Module is the possibly empty secondary namespace of this import
	Name string
	// DescFunc is the index in Module.TypeSection when Type equals ExternTypeFunc
	DescFunc Index
	// DescTable is the inlined Table when Type equals ExternTypeTable
	DescTable *Table
	// DescMem is the inlined Memory when Type equals ExternTypeMemory
	DescMem *Memory
	// DescGlobal is the inlined GlobalType when Type equals ExternTypeGlobal
	DescGlobal *GlobalType
}

Import is the binary representation of an import indicated by Type See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-import

type Index

type Index = uint32

Index is the offset in an index namespace, not necessarily an absolute position in a Module section. This is because index namespaces are often preceded by a corresponding type in the Module.ImportSection.

For example, the function index namespace starts with any ExternTypeFunc in the Module.ImportSection followed by the Module.FunctionSection

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-index

type IndirectNameMap

type IndirectNameMap []*NameMapAssoc

IndirectNameMap associates an index with an association of names.

Note: IndirectNameMap is unique by NameMapAssoc.Index, but NameMapAssoc.NameMap needn't be unique. Note: When encoding in the Binary format, this must be ordered by NameMapAssoc.Index https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-indirectnamemap

type Memory

type Memory struct {
	Min, Max uint32
	// IsMaxEncoded true if the Max is encoded in the orignial source (binary or text).
	IsMaxEncoded bool
}

Memory describes the limits of pages (64KB) in a memory.

type MemoryInstance

type MemoryInstance struct {
	Buffer   []byte
	Min, Max uint32
}

MemoryInstance represents a memory instance in a store, and implements api.Memory.

Note: In WebAssembly 1.0 (20191205), there may be up to one Memory per store, which means the precise memory is always wasm.Store Memories index zero: `store.Memories[0]` See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-instances%E2%91%A0.

func (*MemoryInstance) Grow

func (m *MemoryInstance) Grow(newPages uint32) (result uint32)

Grow extends the memory buffer by "newPages" * memoryPageSize. The logic here is described in https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#grow-mem.

Returns -1 if the operation resulted in exceeding the maximum memory pages. Otherwise, returns the prior memory size after growing the memory buffer.

func (*MemoryInstance) PageSize

func (m *MemoryInstance) PageSize() (result uint32)

PageSize returns the current memory buffer size in pages.

func (*MemoryInstance) Read

func (m *MemoryInstance) Read(offset, byteCount uint32) ([]byte, bool)

Read implements api.Memory Read

func (*MemoryInstance) ReadByte

func (m *MemoryInstance) ReadByte(offset uint32) (byte, bool)

ReadByte implements api.Memory ReadByte

func (*MemoryInstance) ReadFloat32Le

func (m *MemoryInstance) ReadFloat32Le(offset uint32) (float32, bool)

ReadFloat32Le implements api.Memory ReadFloat32Le

func (*MemoryInstance) ReadFloat64Le

func (m *MemoryInstance) ReadFloat64Le(offset uint32) (float64, bool)

ReadFloat64Le implements api.Memory ReadFloat64Le

func (*MemoryInstance) ReadUint32Le

func (m *MemoryInstance) ReadUint32Le(offset uint32) (uint32, bool)

ReadUint32Le implements api.Memory ReadUint32Le

func (*MemoryInstance) ReadUint64Le

func (m *MemoryInstance) ReadUint64Le(offset uint32) (uint64, bool)

ReadUint64Le implements api.Memory ReadUint64Le

func (*MemoryInstance) Size

func (m *MemoryInstance) Size() uint32

Size implements api.Memory Size

func (*MemoryInstance) Write

func (m *MemoryInstance) Write(offset uint32, val []byte) bool

Write implements api.Memory Write

func (*MemoryInstance) WriteByte

func (m *MemoryInstance) WriteByte(offset uint32, v byte) bool

WriteByte implements api.Memory WriteByte

func (*MemoryInstance) WriteFloat32Le

func (m *MemoryInstance) WriteFloat32Le(offset uint32, v float32) bool

WriteFloat32Le implements api.Memory WriteFloat32Le

func (*MemoryInstance) WriteFloat64Le

func (m *MemoryInstance) WriteFloat64Le(offset uint32, v float64) bool

WriteFloat64Le implements api.Memory WriteFloat64Le

func (*MemoryInstance) WriteUint32Le

func (m *MemoryInstance) WriteUint32Le(offset, v uint32) bool

WriteUint32Le implements api.Memory WriteUint32Le

func (*MemoryInstance) WriteUint64Le

func (m *MemoryInstance) WriteUint64Le(offset uint32, v uint64) bool

WriteUint64Le implements api.Memory WriteUint64Le

type Module

type Module struct {
	// TypeSection contains the unique FunctionType of functions imported or defined in this module.
	//
	// Note: Currently, there is no type ambiguity in the index as WebAssembly 1.0 only defines function type.
	// In the future, other types may be introduced to support Features such as module linking.
	//
	// Note: In the Binary Format, this is SectionIDType.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#types%E2%91%A0%E2%91%A0
	TypeSection []*FunctionType

	// ImportSection contains imported functions, tables, memories or globals required for instantiation
	// (Store.Instantiate).
	//
	// Note: there are no unique constraints relating to the two-level namespace of Import.Module and Import.Name.
	//
	// Note: In the Binary Format, this is SectionIDImport.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#import-section%E2%91%A0
	ImportSection []*Import

	// FunctionSection contains the index in TypeSection of each function defined in this module.
	//
	// Note: The function Index namespace begins with imported functions and ends with those defined in this module.
	// For example, if there are two imported functions and one defined in this module, the function Index 3 is defined
	// in this module at FunctionSection[0].
	//
	// Note: FunctionSection is index correlated with the CodeSection. If given the same position, ex. 2, a function
	// type is at TypeSection[FunctionSection[2]], while its locals and body are at CodeSection[2].
	//
	// Note: In the Binary Format, this is SectionIDFunction.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#function-section%E2%91%A0
	FunctionSection []Index

	// TableSection contains each table defined in this module.
	//
	// Note: The table Index namespace begins with imported tables and ends with those defined in this module.
	// For example, if there are two imported tables and one defined in this module, the table Index 3 is defined in
	// this module at TableSection[0].
	//
	// Note: Version 1.0 (20191205) of the WebAssembly spec allows at most one table definition per module, so the
	// length of the TableSection can be zero or one, and can only be one if there is no imported table.
	//
	// Note: In the Binary Format, this is SectionIDTable.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#table-section%E2%91%A0
	TableSection *Table

	// MemorySection contains each memory defined in this module.
	//
	// Note: The memory Index namespace begins with imported memories and ends with those defined in this module.
	// For example, if there are two imported memories and one defined in this module, the memory Index 3 is defined in
	// this module at TableSection[0].
	//
	// Note: Version 1.0 (20191205) of the WebAssembly spec allows at most one memory definition per module, so the
	// length of the MemorySection can be zero or one, and can only be one if there is no imported memory.
	//
	// Note: In the Binary Format, this is SectionIDMemory.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-section%E2%91%A0
	MemorySection *Memory

	// GlobalSection contains each global defined in this module.
	//
	// Global indexes are offset by any imported globals because the global index space begins with imports, followed by
	// ones defined in this module. For example, if there are two imported globals and three defined in this module, the
	// global at index 3 is defined in this module at GlobalSection[0].
	//
	// Note: In the Binary Format, this is SectionIDGlobal.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#global-section%E2%91%A0
	GlobalSection []*Global

	// ExportSection contains each export defined in this module.
	//
	// Note: In the Binary Format, this is SectionIDExport.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#exports%E2%91%A0
	ExportSection []*Export

	// StartSection is the index of a function to call before returning from Store.Instantiate.
	//
	// Note: The index here is not the position in the FunctionSection, rather in the function index namespace, which
	// begins with imported functions.
	//
	// Note: In the Binary Format, this is SectionIDStart.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#start-section%E2%91%A0
	StartSection *Index

	// Note: In the Binary Format, this is SectionIDElement.
	ElementSection []*ElementSegment

	// CodeSection is index-correlated with FunctionSection and contains each function's locals and body.
	// When present, the HostFunctionSection must be nil.
	//
	// Note: In the Binary Format, this is SectionIDCode.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#code-section%E2%91%A0
	CodeSection []*Code

	// Note: In the Binary Format, this is SectionIDData.
	DataSection []*DataSegment

	// NameSection is set when the SectionIDCustom "name" was successfully decoded from the binary format.
	//
	// Note: This is the only SectionIDCustom defined in the WebAssembly 1.0 (20191205) Binary Format.
	// Others are skipped as they are not used in wazero.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#name-section%E2%91%A0
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#custom-section%E2%91%A0
	NameSection *NameSection

	// HostFunctionSection is index-correlated with FunctionSection and contains a host function defined in Go.
	// When present, the CodeSection must be nil.
	//
	// Note: This section currently has no serialization format, so is not encodable.
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#host-functions%E2%91%A2
	HostFunctionSection []*reflect.Value

	// ID is the sha256 value of the source code (text/binary) and is used for caching.
	ID ModuleID
	// contains filtered or unexported fields
}

Module is a WebAssembly binary representation. See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#modules%E2%91%A8

Differences from the specification: * NameSection is the only key ("name") decoded from the SectionIDCustom. * ExportSection is represented as a map for lookup convenience. * HostFunctionSection is a custom section that contains any go `func`s. It may be present when CodeSection is not.

func NewHostModule

func NewHostModule(
	moduleName string,
	nameToGoFunc map[string]interface{},
	nameToMemory map[string]*Memory,
	nameToGlobal map[string]*Global,
	enabledFeatures Features,
) (m *Module, err error)

NewHostModule is defined internally for use in WASI tests and to keep the code size in the root directory small.

func (*Module) AllDeclarations

func (m *Module) AllDeclarations() (functions []Index, globals []*GlobalType, memory *Memory, table *Table, err error)

AllDeclarations returns all declarations for functions, globals, memories and tables in a module including imported ones.

func (*Module) AssignModuleID

func (m *Module) AssignModuleID(source []byte)

AssignModuleID calculates a sha256 checksum on `source` and set Module.ID to the result.

func (*Module) ImportFuncCount

func (m *Module) ImportFuncCount() uint32

ImportFuncCount returns the possibly empty count of imported functions. This plus SectionElementCount of SectionIDFunction is the size of the function index namespace.

func (*Module) ImportGlobalCount

func (m *Module) ImportGlobalCount() uint32

ImportGlobalCount returns the possibly empty count of imported globals. This plus SectionElementCount of SectionIDGlobal is the size of the global index namespace.

func (*Module) ImportMemoryCount

func (m *Module) ImportMemoryCount() uint32

ImportMemoryCount returns the possibly empty count of imported memories. This plus SectionElementCount of SectionIDMemory is the size of the memory index namespace.

func (*Module) ImportTableCount

func (m *Module) ImportTableCount() uint32

ImportTableCount returns the possibly empty count of imported tables. This plus SectionElementCount of SectionIDTable is the size of the table index namespace.

func (*Module) IsHostModule

func (m *Module) IsHostModule() bool

func (*Module) SectionElementCount

func (m *Module) SectionElementCount(sectionID SectionID) uint32

SectionElementCount returns the count of elements in a given section ID

For example... * SectionIDType returns the count of FunctionType * SectionIDCustom returns one if the NameSection is present * SectionIDHostFunction returns the count of HostFunctionSection * SectionIDExport returns the count of unique export names

func (*Module) TypeOfFunction

func (m *Module) TypeOfFunction(funcIdx Index) *FunctionType

TypeOfFunction returns the wasm.SectionIDType index for the given function namespace index or nil. Note: The function index namespace is preceded by imported functions. TODO: Returning nil should be impossible when decode results are validated. Validate decode before back-filling tests.

func (*Module) Validate

func (m *Module) Validate(enabledFeatures Features) error

type ModuleEngine

type ModuleEngine interface {
	// Name returns the name of the module this engine was compiled for.
	Name() string

	// Call invokes a function instance f with given parameters.
	Call(ctx context.Context, m *CallContext, f *FunctionInstance, params ...uint64) (results []uint64, err error)
}

ModuleEngine implements function calls for a given module.

type ModuleID

type ModuleID = [sha256.Size]byte

ModuleID represents sha256 hash value uniquely assigned to Module.

type ModuleInstance

type ModuleInstance struct {
	Name      string
	Exports   map[string]*ExportInstance
	Functions []*FunctionInstance
	Globals   []*GlobalInstance
	// Memory is set when Module.MemorySection had a memory, regardless of whether it was exported.
	Memory *MemoryInstance
	Table  *TableInstance
	Types  []*FunctionType

	// CallCtx holds default function call context from this function instance.
	CallCtx *CallContext

	// Engine implements function calls for this module.
	Engine ModuleEngine

	// TypeIDs is index-correlated with types and holds typeIDs which is uniquely assigned to a type by store.
	// This is necessary to achieve fast runtime type checking for indirect function calls at runtime.
	TypeIDs []FunctionTypeID
}

ModuleInstance represents instantiated wasm module. The difference from the spec is that in wazero, a ModuleInstance holds pointers to the instances, rather than "addresses" (i.e. index to Store.Functions, Globals, etc) for convenience.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#syntax-moduleinst

type NameAssoc

type NameAssoc struct {
	Index Index
	Name  string
}

type NameMap

type NameMap []*NameAssoc

NameMap associates an index with any associated names.

Note: Often the index namespace bridges multiple sections. For example, the function index namespace starts with any ExternTypeFunc in the Module.ImportSection followed by the Module.FunctionSection

Note: NameMap is unique by NameAssoc.Index, but NameAssoc.Name needn't be unique. Note: When encoding in the Binary format, this must be ordered by NameAssoc.Index See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-namemap

type NameMapAssoc

type NameMapAssoc struct {
	Index   Index
	NameMap NameMap
}

type NameSection

type NameSection struct {
	// ModuleName is the symbolic identifier for a module. Ex. math
	//
	// Note: This can be empty for any reason including configuration.
	ModuleName string

	// FunctionNames is an association of a function index to its symbolic identifier. Ex. add
	//
	// * the key (idx) is in the function namespace, where module defined functions are preceded by imported ones.
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#functions%E2%91%A7
	//
	// Ex. Assuming the below text format is the second import, you would expect FunctionNames[1] = "mul"
	//	(import "Math" "Mul" (func $mul (param $x f32) (param $y f32) (result f32)))
	//
	// Note: FunctionNames are only used for debugging. At runtime, functions are called based on raw numeric index.
	// Note: This can be nil for any reason including configuration.
	FunctionNames NameMap

	// LocalNames contains symbolic names for function parameters or locals that have one.
	//
	// Note: In the Text Format, function local names can inherit parameter names from their type. Ex.
	//  * (module (import (func (param $x i32) (param i32))) (func (type 0))) = [{0, {x,0}}]
	//  * (module (import (func (param i32) (param $y i32))) (func (type 0) (local $z i32))) = [0, [{y,1},{z,2}]]
	//  * (module (func (param $x i32) (local $y i32) (local $z i32))) = [{x,0},{y,1},{z,2}]
	//
	// Note: LocalNames are only used for debugging. At runtime, locals are called based on raw numeric index.
	// Note: This can be nil for any reason including configuration.
	LocalNames IndirectNameMap
}

NameSection represent the known custom name subsections defined in the WebAssembly Binary Format

Note: This can be nil if no names were decoded for any reason including configuration. See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#name-section%E2%91%A0

type Opcode

type Opcode = byte

Opcode is the binary Opcode of an instruction. See also InstructionName

const (
	// OpcodeUnreachable causes an unconditional trap.
	OpcodeUnreachable Opcode = 0x00
	// OpcodeNop does nothing
	OpcodeNop Opcode = 0x01
	// OpcodeBlock brackets a sequence of instructions. A branch instruction on an if label breaks out to after its
	// OpcodeEnd.
	OpcodeBlock Opcode = 0x02
	// OpcodeLoop brackets a sequence of instructions. A branch instruction on a loop label will jump back to the
	// beginning of its block.
	OpcodeLoop Opcode = 0x03
	// OpcodeIf brackets a sequence of instructions. When the top of the stack evaluates to 1, the block is executed.
	// Zero jumps to the optional OpcodeElse. A branch instruction on an if label breaks out to after its OpcodeEnd.
	OpcodeIf Opcode = 0x04
	// OpcodeElse brackets a sequence of instructions enclosed by an OpcodeIf. A branch instruction on a then label
	// breaks out to after the OpcodeEnd on the enclosing OpcodeIf.
	OpcodeElse Opcode = 0x05
	// OpcodeEnd terminates a control instruction OpcodeBlock, OpcodeLoop or OpcodeIf.
	OpcodeEnd Opcode = 0x0b

	// OpcodeBr is a stack-polymorphic opcode that performs an unconditional branch. How the stack is modified depends
	// on whether the "br" is enclosed by a loop, and if FeatureMultiValue is enabled.
	//
	// Here are the rules in pseudocode about how the stack is modified based on the "br" operand L (label):
	//	if L is loop: append(L.originalStackWithoutInputs, N-values popped from the stack) where N == L.inputs
	//	else: append(L.originalStackWithoutInputs, N-values popped from the stack) where N == L.results
	//
	// In WebAssembly 1.0 (20191205), N can be zero or one. When FeatureMultiValue is enabled, N can be more than one,
	// depending on the type use of the label L.
	//
	// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#-hrefsyntax-instr-controlmathsfbrl
	OpcodeBr Opcode = 0x0c

	OpcodeBrIf         Opcode = 0x0d
	OpcodeBrTable      Opcode = 0x0e
	OpcodeReturn       Opcode = 0x0f
	OpcodeCall         Opcode = 0x10
	OpcodeCallIndirect Opcode = 0x11

	OpcodeDrop   Opcode = 0x1a
	OpcodeSelect Opcode = 0x1b

	OpcodeLocalGet  Opcode = 0x20
	OpcodeLocalSet  Opcode = 0x21
	OpcodeLocalTee  Opcode = 0x22
	OpcodeGlobalGet Opcode = 0x23
	OpcodeGlobalSet Opcode = 0x24

	OpcodeI32Load    Opcode = 0x28
	OpcodeI64Load    Opcode = 0x29
	OpcodeF32Load    Opcode = 0x2a
	OpcodeF64Load    Opcode = 0x2b
	OpcodeI32Load8S  Opcode = 0x2c
	OpcodeI32Load8U  Opcode = 0x2d
	OpcodeI32Load16S Opcode = 0x2e
	OpcodeI32Load16U Opcode = 0x2f
	OpcodeI64Load8S  Opcode = 0x30
	OpcodeI64Load8U  Opcode = 0x31
	OpcodeI64Load16S Opcode = 0x32
	OpcodeI64Load16U Opcode = 0x33
	OpcodeI64Load32S Opcode = 0x34
	OpcodeI64Load32U Opcode = 0x35
	OpcodeI32Store   Opcode = 0x36
	OpcodeI64Store   Opcode = 0x37
	OpcodeF32Store   Opcode = 0x38
	OpcodeF64Store   Opcode = 0x39
	OpcodeI32Store8  Opcode = 0x3a
	OpcodeI32Store16 Opcode = 0x3b
	OpcodeI64Store8  Opcode = 0x3c
	OpcodeI64Store16 Opcode = 0x3d
	OpcodeI64Store32 Opcode = 0x3e
	OpcodeMemorySize Opcode = 0x3f
	OpcodeMemoryGrow Opcode = 0x40

	OpcodeI32Const Opcode = 0x41
	OpcodeI64Const Opcode = 0x42
	OpcodeF32Const Opcode = 0x43
	OpcodeF64Const Opcode = 0x44

	OpcodeI32Eqz Opcode = 0x45
	OpcodeI32Eq  Opcode = 0x46
	OpcodeI32Ne  Opcode = 0x47
	OpcodeI32LtS Opcode = 0x48
	OpcodeI32LtU Opcode = 0x49
	OpcodeI32GtS Opcode = 0x4a
	OpcodeI32GtU Opcode = 0x4b
	OpcodeI32LeS Opcode = 0x4c
	OpcodeI32LeU Opcode = 0x4d
	OpcodeI32GeS Opcode = 0x4e
	OpcodeI32GeU Opcode = 0x4f

	OpcodeI64Eqz Opcode = 0x50
	OpcodeI64Eq  Opcode = 0x51
	OpcodeI64Ne  Opcode = 0x52
	OpcodeI64LtS Opcode = 0x53
	OpcodeI64LtU Opcode = 0x54
	OpcodeI64GtS Opcode = 0x55
	OpcodeI64GtU Opcode = 0x56
	OpcodeI64LeS Opcode = 0x57
	OpcodeI64LeU Opcode = 0x58
	OpcodeI64GeS Opcode = 0x59
	OpcodeI64GeU Opcode = 0x5a

	OpcodeF32Eq Opcode = 0x5b
	OpcodeF32Ne Opcode = 0x5c
	OpcodeF32Lt Opcode = 0x5d
	OpcodeF32Gt Opcode = 0x5e
	OpcodeF32Le Opcode = 0x5f
	OpcodeF32Ge Opcode = 0x60

	OpcodeF64Eq Opcode = 0x61
	OpcodeF64Ne Opcode = 0x62
	OpcodeF64Lt Opcode = 0x63
	OpcodeF64Gt Opcode = 0x64
	OpcodeF64Le Opcode = 0x65
	OpcodeF64Ge Opcode = 0x66

	OpcodeI32Clz    Opcode = 0x67
	OpcodeI32Ctz    Opcode = 0x68
	OpcodeI32Popcnt Opcode = 0x69
	OpcodeI32Add    Opcode = 0x6a
	OpcodeI32Sub    Opcode = 0x6b
	OpcodeI32Mul    Opcode = 0x6c
	OpcodeI32DivS   Opcode = 0x6d
	OpcodeI32DivU   Opcode = 0x6e
	OpcodeI32RemS   Opcode = 0x6f
	OpcodeI32RemU   Opcode = 0x70
	OpcodeI32And    Opcode = 0x71
	OpcodeI32Or     Opcode = 0x72
	OpcodeI32Xor    Opcode = 0x73
	OpcodeI32Shl    Opcode = 0x74
	OpcodeI32ShrS   Opcode = 0x75
	OpcodeI32ShrU   Opcode = 0x76
	OpcodeI32Rotl   Opcode = 0x77
	OpcodeI32Rotr   Opcode = 0x78

	OpcodeI64Clz    Opcode = 0x79
	OpcodeI64Ctz    Opcode = 0x7a
	OpcodeI64Popcnt Opcode = 0x7b
	OpcodeI64Add    Opcode = 0x7c
	OpcodeI64Sub    Opcode = 0x7d
	OpcodeI64Mul    Opcode = 0x7e
	OpcodeI64DivS   Opcode = 0x7f
	OpcodeI64DivU   Opcode = 0x80
	OpcodeI64RemS   Opcode = 0x81
	OpcodeI64RemU   Opcode = 0x82
	OpcodeI64And    Opcode = 0x83
	OpcodeI64Or     Opcode = 0x84
	OpcodeI64Xor    Opcode = 0x85
	OpcodeI64Shl    Opcode = 0x86
	OpcodeI64ShrS   Opcode = 0x87
	OpcodeI64ShrU   Opcode = 0x88
	OpcodeI64Rotl   Opcode = 0x89
	OpcodeI64Rotr   Opcode = 0x8a

	OpcodeF32Abs      Opcode = 0x8b
	OpcodeF32Neg      Opcode = 0x8c
	OpcodeF32Ceil     Opcode = 0x8d
	OpcodeF32Floor    Opcode = 0x8e
	OpcodeF32Trunc    Opcode = 0x8f
	OpcodeF32Nearest  Opcode = 0x90
	OpcodeF32Sqrt     Opcode = 0x91
	OpcodeF32Add      Opcode = 0x92
	OpcodeF32Sub      Opcode = 0x93
	OpcodeF32Mul      Opcode = 0x94
	OpcodeF32Div      Opcode = 0x95
	OpcodeF32Min      Opcode = 0x96
	OpcodeF32Max      Opcode = 0x97
	OpcodeF32Copysign Opcode = 0x98

	OpcodeF64Abs      Opcode = 0x99
	OpcodeF64Neg      Opcode = 0x9a
	OpcodeF64Ceil     Opcode = 0x9b
	OpcodeF64Floor    Opcode = 0x9c
	OpcodeF64Trunc    Opcode = 0x9d
	OpcodeF64Nearest  Opcode = 0x9e
	OpcodeF64Sqrt     Opcode = 0x9f
	OpcodeF64Add      Opcode = 0xa0
	OpcodeF64Sub      Opcode = 0xa1
	OpcodeF64Mul      Opcode = 0xa2
	OpcodeF64Div      Opcode = 0xa3
	OpcodeF64Min      Opcode = 0xa4
	OpcodeF64Max      Opcode = 0xa5
	OpcodeF64Copysign Opcode = 0xa6

	OpcodeI32WrapI64   Opcode = 0xa7
	OpcodeI32TruncF32S Opcode = 0xa8
	OpcodeI32TruncF32U Opcode = 0xa9
	OpcodeI32TruncF64S Opcode = 0xaa
	OpcodeI32TruncF64U Opcode = 0xab

	OpcodeI64ExtendI32S Opcode = 0xac
	OpcodeI64ExtendI32U Opcode = 0xad
	OpcodeI64TruncF32S  Opcode = 0xae
	OpcodeI64TruncF32U  Opcode = 0xaf
	OpcodeI64TruncF64S  Opcode = 0xb0
	OpcodeI64TruncF64U  Opcode = 0xb1

	OpcodeF32ConvertI32s Opcode = 0xb2
	OpcodeF32ConvertI32U Opcode = 0xb3
	OpcodeF32ConvertI64S Opcode = 0xb4
	OpcodeF32ConvertI64U Opcode = 0xb5
	OpcodeF32DemoteF64   Opcode = 0xb6

	OpcodeF64ConvertI32S Opcode = 0xb7
	OpcodeF64ConvertI32U Opcode = 0xb8
	OpcodeF64ConvertI64S Opcode = 0xb9
	OpcodeF64ConvertI64U Opcode = 0xba
	OpcodeF64PromoteF32  Opcode = 0xbb

	OpcodeI32ReinterpretF32 Opcode = 0xbc
	OpcodeI64ReinterpretF64 Opcode = 0xbd
	OpcodeF32ReinterpretI32 Opcode = 0xbe
	OpcodeF64ReinterpretI64 Opcode = 0xbf

	// OpcodeI32Extend8S extends a signed 8-bit integer to a 32-bit integer.
	// Note: This is dependent on the flag FeatureSignExtensionOps
	OpcodeI32Extend8S Opcode = 0xc0

	// OpcodeI32Extend16S extends a signed 16-bit integer to a 32-bit integer.
	// Note: This is dependent on the flag FeatureSignExtensionOps
	OpcodeI32Extend16S Opcode = 0xc1

	// OpcodeI64Extend8S extends a signed 8-bit integer to a 64-bit integer.
	// Note: This is dependent on the flag FeatureSignExtensionOps
	OpcodeI64Extend8S Opcode = 0xc2

	// OpcodeI64Extend16S extends a signed 16-bit integer to a 64-bit integer.
	// Note: This is dependent on the flag FeatureSignExtensionOps
	OpcodeI64Extend16S Opcode = 0xc3

	// OpcodeI64Extend32S extends a signed 32-bit integer to a 64-bit integer.
	// Note: This is dependent on the flag FeatureSignExtensionOps
	OpcodeI64Extend32S Opcode = 0xc4

	LastOpcode = OpcodeI64Extend32S
)

type SectionID

type SectionID = byte

SectionID identifies the sections of a Module in the WebAssembly 1.0 (20191205) Binary Format.

Note: these are defined in the wasm package, instead of the binary package, as a key per section is needed regardless of format, and deferring to the binary type avoids confusion.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#sections%E2%91%A0

const (
	// SectionIDCustom includes the standard defined NameSection and possibly others not defined in the standard.
	SectionIDCustom SectionID = iota // don't add anything not in https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#sections%E2%91%A0
	SectionIDType
	SectionIDImport
	SectionIDFunction
	SectionIDTable
	SectionIDMemory
	SectionIDGlobal
	SectionIDExport
	SectionIDStart
	SectionIDElement
	SectionIDCode
	SectionIDData
)

type Store

type Store struct {
	// EnabledFeatures are read-only to allow optimizations.
	EnabledFeatures Features

	// Engine is a global context for a Store which is in responsible for compilation and execution of Wasm modules.
	Engine Engine
	// contains filtered or unexported fields
}

Store is the runtime representation of "instantiated" Wasm module and objects. Multiple modules can be instantiated within a single store, and each instance, (e.g. function instance) can be referenced by other module instances in a Store via Module.ImportSection.

Every type whose name ends with "Instance" suffix belongs to exactly one store.

Note that store is not thread (concurrency) safe, meaning that using single Store via multiple goroutines might result in race conditions. In that case, the invocation and access to any methods and field of Store must be guarded by mutex.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#store%E2%91%A0

func NewStore

func NewStore(enabledFeatures Features, engine Engine) *Store

func (*Store) Instantiate

func (s *Store) Instantiate(ctx context.Context, module *Module, name string, sys *SysContext) (*CallContext, error)

Instantiate uses name instead of the Module.NameSection ModuleName as it allows instantiating the same module under different names safely and concurrently.

* ctx: the default context used for function calls. * name: the name of the module. * sys: the system context, which will be closed (SysContext.Close) on CallContext.Close.

Note: Module.Validate must be called prior to instantiation.

func (*Store) Module

func (s *Store) Module(moduleName string) api.Module

Module implements wazero.Runtime Module

type SysContext

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

SysContext holds module-scoped system resources currently only used by internalwasi.

func DefaultSysContext

func DefaultSysContext() *SysContext

DefaultSysContext returns SysContext with no values set.

Note: This isn't a constant because SysContext.openedFiles is currently mutable even when empty. TODO: Make it an error to open or close files when no FS was assigned.

func NewSysContext

func NewSysContext(max uint32, args, environ []string, stdin io.Reader, stdout, stderr io.Writer, openedFiles map[uint32]*FileEntry) (sys *SysContext, err error)

NewSysContext is a factory function which helps avoid needing to know defaults or exporting all fields. Note: max is exposed for testing. max is only used for env/args validation.

func (*SysContext) Args

func (c *SysContext) Args() []string

Args is like os.Args and defaults to nil.

Note: The count will never be more than math.MaxUint32. See wazero.SysConfig WithArgs

func (*SysContext) ArgsSize

func (c *SysContext) ArgsSize() uint32

ArgsSize is the size to encode Args as Null-terminated strings.

Note: To get the size without null-terminators, subtract the length of Args from this value. See wazero.SysConfig WithArgs See https://en.wikipedia.org/wiki/Null-terminated_string

func (*SysContext) Close

func (c *SysContext) Close() (err error)

Close implements io.Closer

func (*SysContext) CloseFile

func (c *SysContext) CloseFile(fd uint32) (bool, error)

CloseFile returns true if a file was opened and closed without error, or false if not.

func (*SysContext) Environ

func (c *SysContext) Environ() []string

Environ are "key=value" entries like os.Environ and default to nil.

Note: The count will never be more than math.MaxUint32. See wazero.SysConfig WithEnviron

func (*SysContext) EnvironSize

func (c *SysContext) EnvironSize() uint32

EnvironSize is the size to encode Environ as Null-terminated strings.

Note: To get the size without null-terminators, subtract the length of Environ from this value. See wazero.SysConfig WithEnviron See https://en.wikipedia.org/wiki/Null-terminated_string

func (*SysContext) OpenFile

func (c *SysContext) OpenFile(f *FileEntry) (uint32, bool)

OpenFile returns the file descriptor of the new file or false if we ran out of file descriptors

func (*SysContext) OpenedFile

func (c *SysContext) OpenedFile(fd uint32) (*FileEntry, bool)

OpenedFile returns a file and true if it was opened or nil and false, if not.

func (*SysContext) Stderr

func (c *SysContext) Stderr() io.Writer

Stderr is like exec.Cmd Stderr and defaults to io.Discard. See wazero.SysConfig WithStderr

func (*SysContext) Stdin

func (c *SysContext) Stdin() io.Reader

Stdin is like exec.Cmd Stdin and defaults to a reader of os.DevNull. See wazero.SysConfig WithStdin

func (*SysContext) Stdout

func (c *SysContext) Stdout() io.Writer

Stdout is like exec.Cmd Stdout and defaults to io.Discard. See wazero.SysConfig WithStdout

type Table

type Table = limitsType

Table describes the limits of (function) elements in a table.

type TableInstance

type TableInstance struct {
	// Table holds the Engine-specific compiled functions mapped by element index.
	Table []interface{}

	// Min is the minimum (function) elements in this table and cannot grow to accommodate ElementSegment.
	Min uint32

	// Max if present is the maximum (function) elements in this table, or nil if unbounded.
	Max *uint32
}

TableInstance represents a table of (ElemTypeFuncref) elements in a module.

See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#table-instances%E2%91%A0

type ValueType

type ValueType = api.ValueType

ValueType is an alias of api.ValueType defined to simplify imports.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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