Documentation ¶
Overview ¶
Package wasm provides functions for reading and parsing WebAssembly modules.
Index ¶
- Constants
- Variables
- func EncodeModule(w io.Writer, m *Module) error
- func ReadByte(r io.Reader) (byte, error)
- func SetDebugMode(dbg bool)
- type BlockType
- type DataSegment
- type DataSegmentNamesSubsection
- type DuplicateExportError
- type ElemSegmentNamesSubsection
- type ElemType
- type ElementSegment
- type ExportEntry
- type External
- type FuncImport
- type Function
- type FunctionBody
- type FunctionNamesSubsection
- type FunctionSig
- type GlobalEntry
- type GlobalNamesSubsection
- type GlobalVar
- type GlobalVarImport
- type Import
- type ImportEntry
- type InvalidCodeIndexError
- type InvalidExternalError
- type InvalidInitExprOpError
- type InvalidSectionIDError
- type InvalidTypeConstructorError
- type LabelNames
- type LabelsNamesSubsection
- type LocalEntry
- type LocalNames
- type LocalNamesSubsection
- type Marshaler
- type Memory
- type MemoryImport
- type MemoryNamesSubsection
- type MissingSectionError
- type Module
- type ModuleNameSubsection
- type NameSection
- type NameSubsection
- type NameType
- type Naming
- type RawSection
- type ResizableLimits
- type ResolveFunc
- type Section
- type SectionCode
- type SectionCustom
- type SectionData
- type SectionElements
- type SectionExports
- type SectionFunctions
- type SectionGlobals
- type SectionID
- type SectionImports
- type SectionMemories
- type SectionStartFunction
- type SectionTables
- type SectionTypes
- type Table
- type TableEntry
- type TableImport
- type TableNamesSubsection
- type TypeNamesSubsection
- type Unmarshaler
- type ValidationError
- type ValueType
Constants ¶
const ( Magic uint32 = 0x6d736100 Version uint32 = 0x1 )
const ( NameModule = NameType(0) NameFunction = NameType(1) NameLocal = NameType(2) // Extended name subsections from https://github.com/WebAssembly/extended-name-section/blob/main/proposals/extended-name-section/Overview.md NameLabels = NameType(3) NameTypes = NameType(4) NameTable = NameType(5) NameMemory = NameType(6) NameGlobal = NameType(7) NameElemSegment = NameType(8) NameDataSegment = NameType(9) )
const (
CustomSectionName = "name"
)
A list of well-known custom sections
const TypeFunc uint8 = 0x60
TypeFunc represents the value type of a function
Variables ¶
var ErrEmptyInitExpr = errors.New("wasm: Initializer expression produces no value")
var ErrFunctionNoEnd = errors.New("Function body does not end with 0x0b (end)")
var ErrInvalidMagic = errors.New("magic header not detected")
var ErrUnsupportedSection = errors.New("wasm: unsupported section")
Functions ¶
func EncodeModule ¶
EncodeModule writes a provided module to w using WASM binary encoding.
func SetDebugMode ¶
func SetDebugMode(dbg bool)
Types ¶
type BlockType ¶
type BlockType ValueType // varint7
BlockType represents the signature of a structured block
const BlockTypeEmpty BlockType = 0x40
type DataSegment ¶
type DataSegment struct { Index uint32 // The index into the global linear memory space, should always be 0 in the MVP. Offset []byte // initializer expression for computing the offset for placing elements, should return an i32 value Data []byte }
DataSegment describes a group of repeated elements that begin at a specified offset in the linear memory
func (*DataSegment) MarshalWASM ¶
func (s *DataSegment) MarshalWASM(w io.Writer) error
func (*DataSegment) UnmarshalWASM ¶
func (s *DataSegment) UnmarshalWASM(r io.Reader) error
type DataSegmentNamesSubsection ¶
type DataSegmentNamesSubsection struct {
Names []Naming
}
func (*DataSegmentNamesSubsection) MarshalWASM ¶
func (s *DataSegmentNamesSubsection) MarshalWASM(w io.Writer) error
func (*DataSegmentNamesSubsection) Type ¶
func (s *DataSegmentNamesSubsection) Type() NameType
func (*DataSegmentNamesSubsection) UnmarshalWASM ¶
func (s *DataSegmentNamesSubsection) UnmarshalWASM(r io.Reader) error
type DuplicateExportError ¶
type DuplicateExportError string
func (DuplicateExportError) Error ¶
func (e DuplicateExportError) Error() string
type ElemSegmentNamesSubsection ¶
type ElemSegmentNamesSubsection struct {
Names []Naming
}
func (*ElemSegmentNamesSubsection) MarshalWASM ¶
func (s *ElemSegmentNamesSubsection) MarshalWASM(w io.Writer) error
func (*ElemSegmentNamesSubsection) Type ¶
func (s *ElemSegmentNamesSubsection) Type() NameType
func (*ElemSegmentNamesSubsection) UnmarshalWASM ¶
func (s *ElemSegmentNamesSubsection) UnmarshalWASM(r io.Reader) error
type ElemType ¶
type ElemType uint8 // varint7
ElemType describes the type of a table's elements
const ElemTypeAnyFunc ElemType = 0x70
ElemTypeAnyFunc descibres an any_func value
type ElementSegment ¶
type ElementSegment struct { Index uint32 // The index into the global table space, should always be 0 in the MVP. Offset []byte // initializer expression for computing the offset for placing elements, should return an i32 value Elems []uint32 }
ElementSegment describes a group of repeated elements that begin at a specified offset
func (*ElementSegment) MarshalWASM ¶
func (s *ElementSegment) MarshalWASM(w io.Writer) error
func (*ElementSegment) UnmarshalWASM ¶
func (s *ElementSegment) UnmarshalWASM(r io.Reader) error
type ExportEntry ¶
ExportEntry represents an exported entry by the module
func (*ExportEntry) MarshalWASM ¶
func (e *ExportEntry) MarshalWASM(w io.Writer) error
func (*ExportEntry) UnmarshalWASM ¶
func (e *ExportEntry) UnmarshalWASM(r io.Reader) error
type External ¶
type External uint8
External describes the kind of the entry being imported or exported.
type FuncImport ¶
type FuncImport struct {
Type uint32
}
func (FuncImport) Kind ¶
func (FuncImport) Kind() External
func (FuncImport) MarshalWASM ¶
func (f FuncImport) MarshalWASM(w io.Writer) error
type Function ¶
type Function struct { Sig *FunctionSig Body *FunctionBody Host reflect.Value Name string }
Function represents an entry in the function index space of a module.
type FunctionBody ¶
type FunctionBody struct { Offset int64 // The offset of the first byte of this function body relative to the start of the code section. Module *Module // The parent module containing this function body, for execution purposes Locals []LocalEntry Code []byte }
func (*FunctionBody) MarshalWASM ¶
func (f *FunctionBody) MarshalWASM(w io.Writer) error
func (*FunctionBody) UnmarshalWASM ¶
func (f *FunctionBody) UnmarshalWASM(r io.Reader) error
type FunctionNamesSubsection ¶
type FunctionNamesSubsection struct {
Names []Naming
}
func (*FunctionNamesSubsection) MarshalWASM ¶
func (s *FunctionNamesSubsection) MarshalWASM(w io.Writer) error
func (*FunctionNamesSubsection) Type ¶
func (s *FunctionNamesSubsection) Type() NameType
func (*FunctionNamesSubsection) UnmarshalWASM ¶
func (s *FunctionNamesSubsection) UnmarshalWASM(r io.Reader) error
type FunctionSig ¶
type FunctionSig struct { // value for the 'func` type constructor Form uint8 // must be 0x60 // The parameter types of the function ParamTypes []ValueType ReturnTypes []ValueType }
FunctionSig describes the signature of a declared function in a WASM module
func (FunctionSig) Equals ¶
func (f FunctionSig) Equals(other FunctionSig) bool
func (*FunctionSig) MarshalWASM ¶
func (f *FunctionSig) MarshalWASM(w io.Writer) error
func (FunctionSig) String ¶
func (f FunctionSig) String() string
func (*FunctionSig) UnmarshalWASM ¶
func (f *FunctionSig) UnmarshalWASM(r io.Reader) error
type GlobalEntry ¶
type GlobalEntry struct { Type GlobalVar // Type holds information about the value type and mutability of the variable Init []byte // Init is an initializer expression that computes the initial value of the variable }
GlobalEntry declares a global variable.
func (*GlobalEntry) MarshalWASM ¶
func (g *GlobalEntry) MarshalWASM(w io.Writer) error
func (*GlobalEntry) UnmarshalWASM ¶
func (g *GlobalEntry) UnmarshalWASM(r io.Reader) error
type GlobalNamesSubsection ¶
type GlobalNamesSubsection struct {
Names []Naming
}
func (*GlobalNamesSubsection) MarshalWASM ¶
func (s *GlobalNamesSubsection) MarshalWASM(w io.Writer) error
func (*GlobalNamesSubsection) Type ¶
func (s *GlobalNamesSubsection) Type() NameType
func (*GlobalNamesSubsection) UnmarshalWASM ¶
func (s *GlobalNamesSubsection) UnmarshalWASM(r io.Reader) error
type GlobalVar ¶
type GlobalVar struct { Type ValueType // Type of the value stored by the variable Mutable bool // Whether the value of the variable can be changed by the set_global operator }
GlobalVar describes the type and mutability of a declared global variable
type GlobalVarImport ¶
type GlobalVarImport struct {
Type GlobalVar
}
func (GlobalVarImport) Kind ¶
func (GlobalVarImport) Kind() External
func (GlobalVarImport) MarshalWASM ¶
func (t GlobalVarImport) MarshalWASM(w io.Writer) error
type Import ¶
Import is an interface implemented by types that can be imported by a WebAssembly module.
type ImportEntry ¶
type ImportEntry struct { ModuleName string // module name string FieldName string // field name string // If Kind is Function, Type is a FuncImport containing the type index of the function signature // If Kind is Table, Type is a TableImport containing the type of the imported table // If Kind is Memory, Type is a MemoryImport containing the type of the imported memory // If the Kind is Global, Type is a GlobalVarImport Type Import }
ImportEntry describes an import statement in a Wasm module.
func (*ImportEntry) UnmarshalWASM ¶
func (i *ImportEntry) UnmarshalWASM(r io.Reader) error
type InvalidCodeIndexError ¶
type InvalidCodeIndexError int
func (InvalidCodeIndexError) Error ¶
func (e InvalidCodeIndexError) Error() string
type InvalidExternalError ¶
type InvalidExternalError uint8
func (InvalidExternalError) Error ¶
func (e InvalidExternalError) Error() string
type InvalidInitExprOpError ¶
type InvalidInitExprOpError byte
func (InvalidInitExprOpError) Error ¶
func (e InvalidInitExprOpError) Error() string
type InvalidSectionIDError ¶
type InvalidSectionIDError SectionID
func (InvalidSectionIDError) Error ¶
func (e InvalidSectionIDError) Error() string
type InvalidTypeConstructorError ¶
func (InvalidTypeConstructorError) Error ¶
func (e InvalidTypeConstructorError) Error() string
type LabelNames ¶
type LabelsNamesSubsection ¶
type LabelsNamesSubsection struct {
Funcs []LabelNames
}
func (*LabelsNamesSubsection) MarshalWASM ¶
func (s *LabelsNamesSubsection) MarshalWASM(w io.Writer) error
func (*LabelsNamesSubsection) Type ¶
func (s *LabelsNamesSubsection) Type() NameType
func (*LabelsNamesSubsection) UnmarshalWASM ¶
func (s *LabelsNamesSubsection) UnmarshalWASM(r io.Reader) error
type LocalEntry ¶
type LocalEntry struct { Count uint32 // The total number of local variables of the given Type used in the function body Type ValueType // The type of value stored by the variable }
func (*LocalEntry) MarshalWASM ¶
func (l *LocalEntry) MarshalWASM(w io.Writer) error
func (*LocalEntry) UnmarshalWASM ¶
func (l *LocalEntry) UnmarshalWASM(r io.Reader) error
type LocalNames ¶
type LocalNamesSubsection ¶
type LocalNamesSubsection struct {
Funcs []LocalNames
}
func (*LocalNamesSubsection) MarshalWASM ¶
func (s *LocalNamesSubsection) MarshalWASM(w io.Writer) error
func (*LocalNamesSubsection) Type ¶
func (s *LocalNamesSubsection) Type() NameType
func (*LocalNamesSubsection) UnmarshalWASM ¶
func (s *LocalNamesSubsection) UnmarshalWASM(r io.Reader) error
type Memory ¶
type Memory struct {
Limits ResizableLimits
}
type MemoryImport ¶
type MemoryImport struct {
Type Memory
}
func (MemoryImport) Kind ¶
func (MemoryImport) Kind() External
func (MemoryImport) MarshalWASM ¶
func (t MemoryImport) MarshalWASM(w io.Writer) error
type MemoryNamesSubsection ¶
type MemoryNamesSubsection struct {
Names []Naming
}
func (*MemoryNamesSubsection) MarshalWASM ¶
func (s *MemoryNamesSubsection) MarshalWASM(w io.Writer) error
func (*MemoryNamesSubsection) Type ¶
func (s *MemoryNamesSubsection) Type() NameType
func (*MemoryNamesSubsection) UnmarshalWASM ¶
func (s *MemoryNamesSubsection) UnmarshalWASM(r io.Reader) error
type MissingSectionError ¶
type MissingSectionError SectionID
func (MissingSectionError) Error ¶
func (e MissingSectionError) Error() string
type Module ¶
type Module struct { Version uint32 Sections []Section Types *SectionTypes Import *SectionImports Function *SectionFunctions Table *SectionTables Memory *SectionMemories Global *SectionGlobals Export *SectionExports Start *SectionStartFunction Elements *SectionElements Code *SectionCode Data *SectionData Customs []*SectionCustom }
Module represents a parsed WebAssembly module: http://webassembly.org/docs/modules/
func DecodeModule ¶
DecodeModule decodes a WASM module.
func MustDecode ¶
MustDecode decodes a WASM module and panics on failure.
func (*Module) Custom ¶
func (m *Module) Custom(name string) *SectionCustom
Custom returns a custom section with a specific name, if it exists.
func (*Module) Names ¶
func (m *Module) Names() (*NameSection, error)
Names returns the names section. If no names section exists, this function returns a MissingSectionError.
type ModuleNameSubsection ¶
type ModuleNameSubsection struct {
Name string
}
func (*ModuleNameSubsection) MarshalWASM ¶
func (s *ModuleNameSubsection) MarshalWASM(w io.Writer) error
func (*ModuleNameSubsection) Type ¶
func (s *ModuleNameSubsection) Type() NameType
func (*ModuleNameSubsection) UnmarshalWASM ¶
func (s *ModuleNameSubsection) UnmarshalWASM(r io.Reader) error
type NameSection ¶
type NameSection struct {
Entries []NameSubsection
}
NameSection is a custom section that stores names of modules, functions and locals for debugging purposes. See https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#name-section for more details.
func (*NameSection) MarshalWASM ¶
func (s *NameSection) MarshalWASM(w io.Writer) error
func (*NameSection) UnmarshalWASM ¶
func (s *NameSection) UnmarshalWASM(r io.Reader) error
type NameSubsection ¶
type NameSubsection interface { Marshaler Unmarshaler Type() NameType }
type RawSection ¶
RawSection is a declared section in a WASM module.
func (*RawSection) GetRawSection ¶
func (s *RawSection) GetRawSection() *RawSection
func (*RawSection) SectionID ¶
func (s *RawSection) SectionID() SectionID
type ResizableLimits ¶
type ResizableLimits struct { Flags uint8 // 1 if the Maximum field is valid, 0 otherwise Initial uint32 // initial length (in units of table elements or wasm pages) Maximum uint32 // If flags is 1, it describes the maximum size of the table or memory }
ResizableLimits describe the limit of a table or linear memory.
func (*ResizableLimits) MarshalWASM ¶
func (lim *ResizableLimits) MarshalWASM(w io.Writer) error
func (*ResizableLimits) UnmarshalWASM ¶
func (lim *ResizableLimits) UnmarshalWASM(r io.Reader) error
type ResolveFunc ¶
ResolveFunc is a function that takes a module name and returns a valid resolved module.
type Section ¶
type Section interface { // SectionID returns a section ID for WASM encoding. Should be unique across types. SectionID() SectionID // GetRawSection Returns an embedded RawSection pointer to populate generic fields. GetRawSection() *RawSection // ReadPayload reads a section payload, assuming the size was already read, and reader is limited to it. ReadPayload(r io.Reader) error // WritePayload writes a section payload without the size. // Caller should calculate written size and add it before the payload. WritePayload(w io.Writer) error }
Section is a generic WASM section interface.
type SectionCode ¶
type SectionCode struct { RawSection Bodies []FunctionBody }
SectionCode describes the body for every function declared inside a module.
func (*SectionCode) ReadPayload ¶
func (s *SectionCode) ReadPayload(r io.Reader) error
func (*SectionCode) SectionID ¶
func (*SectionCode) SectionID() SectionID
func (*SectionCode) WritePayload ¶
func (s *SectionCode) WritePayload(w io.Writer) error
type SectionCustom ¶
type SectionCustom struct { RawSection Name string Data []byte }
func (*SectionCustom) ReadPayload ¶
func (s *SectionCustom) ReadPayload(r io.Reader) error
func (*SectionCustom) SectionID ¶
func (s *SectionCustom) SectionID() SectionID
func (*SectionCustom) WritePayload ¶
func (s *SectionCustom) WritePayload(w io.Writer) error
type SectionData ¶
type SectionData struct { RawSection Entries []DataSegment }
SectionData describes the initial values of a module's linear memory
func (*SectionData) ReadPayload ¶
func (s *SectionData) ReadPayload(r io.Reader) error
func (*SectionData) SectionID ¶
func (*SectionData) SectionID() SectionID
func (*SectionData) WritePayload ¶
func (s *SectionData) WritePayload(w io.Writer) error
type SectionElements ¶
type SectionElements struct { RawSection Entries []ElementSegment }
SectionElements describes the initial contents of a table's elements.
func (*SectionElements) ReadPayload ¶
func (s *SectionElements) ReadPayload(r io.Reader) error
func (*SectionElements) SectionID ¶
func (*SectionElements) SectionID() SectionID
func (*SectionElements) WritePayload ¶
func (s *SectionElements) WritePayload(w io.Writer) error
type SectionExports ¶
type SectionExports struct { RawSection Entries []ExportEntry }
SectionExports declares the export section of a module
func (*SectionExports) ReadPayload ¶
func (s *SectionExports) ReadPayload(r io.Reader) error
func (*SectionExports) SectionID ¶
func (*SectionExports) SectionID() SectionID
func (*SectionExports) WritePayload ¶
func (s *SectionExports) WritePayload(w io.Writer) error
type SectionFunctions ¶
type SectionFunctions struct { RawSection // Sequences of indices into (FunctionSignatues).Entries Types []uint32 }
SectionFunction declares the signature of all functions defined in the module (in the code section)
func (*SectionFunctions) ReadPayload ¶
func (s *SectionFunctions) ReadPayload(r io.Reader) error
func (*SectionFunctions) SectionID ¶
func (*SectionFunctions) SectionID() SectionID
func (*SectionFunctions) WritePayload ¶
func (s *SectionFunctions) WritePayload(w io.Writer) error
type SectionGlobals ¶
type SectionGlobals struct { RawSection Globals []GlobalEntry }
SectionGlobals defines the value of all global variables declared in a module.
func (*SectionGlobals) ReadPayload ¶
func (s *SectionGlobals) ReadPayload(r io.Reader) error
func (*SectionGlobals) SectionID ¶
func (*SectionGlobals) SectionID() SectionID
func (*SectionGlobals) WritePayload ¶
func (s *SectionGlobals) WritePayload(w io.Writer) error
type SectionID ¶
type SectionID uint8
SectionID is a 1-byte code that encodes the section code of both known and custom sections.
const ( SectionIDCustom SectionID = 0 SectionIDType SectionID = 1 SectionIDImport SectionID = 2 SectionIDFunction SectionID = 3 SectionIDTable SectionID = 4 SectionIDMemory SectionID = 5 SectionIDGlobal SectionID = 6 SectionIDExport SectionID = 7 SectionIDStart SectionID = 8 SectionIDElement SectionID = 9 SectionIDCode SectionID = 10 SectionIDData SectionID = 11 )
type SectionImports ¶
type SectionImports struct { RawSection Entries []ImportEntry }
SectionImports declares all imports that will be used in the module.
func (*SectionImports) ReadPayload ¶
func (s *SectionImports) ReadPayload(r io.Reader) error
func (*SectionImports) SectionID ¶
func (*SectionImports) SectionID() SectionID
func (*SectionImports) WritePayload ¶
func (s *SectionImports) WritePayload(w io.Writer) error
type SectionMemories ¶
type SectionMemories struct { RawSection Entries []Memory }
SectionMemories describes all linear memories used by a module.
func (*SectionMemories) ReadPayload ¶
func (s *SectionMemories) ReadPayload(r io.Reader) error
func (*SectionMemories) SectionID ¶
func (*SectionMemories) SectionID() SectionID
func (*SectionMemories) WritePayload ¶
func (s *SectionMemories) WritePayload(w io.Writer) error
type SectionStartFunction ¶
type SectionStartFunction struct { RawSection Index uint32 // The index of the start function into the global index space. }
SectionStartFunction represents the start function section.
func (*SectionStartFunction) ReadPayload ¶
func (s *SectionStartFunction) ReadPayload(r io.Reader) error
func (*SectionStartFunction) SectionID ¶
func (*SectionStartFunction) SectionID() SectionID
func (*SectionStartFunction) WritePayload ¶
func (s *SectionStartFunction) WritePayload(w io.Writer) error
type SectionTables ¶
type SectionTables struct { RawSection Entries []Table }
SectionTables describes all tables declared by a module.
func (*SectionTables) ReadPayload ¶
func (s *SectionTables) ReadPayload(r io.Reader) error
func (*SectionTables) SectionID ¶
func (*SectionTables) SectionID() SectionID
func (*SectionTables) WritePayload ¶
func (s *SectionTables) WritePayload(w io.Writer) error
type SectionTypes ¶
type SectionTypes struct { RawSection Entries []FunctionSig }
SectionTypes declares all function signatures that will be used in a module.
func (*SectionTypes) ReadPayload ¶
func (s *SectionTypes) ReadPayload(r io.Reader) error
func (*SectionTypes) SectionID ¶
func (*SectionTypes) SectionID() SectionID
func (*SectionTypes) WritePayload ¶
func (s *SectionTypes) WritePayload(w io.Writer) error
type Table ¶
type Table struct { // The type of elements ElementType ElemType Limits ResizableLimits }
Table describes a table in a Wasm module.
type TableEntry ¶
TableEntry represents a table index and tracks its initialized state.
type TableImport ¶
type TableImport struct {
Type Table
}
func (TableImport) Kind ¶
func (TableImport) Kind() External
func (TableImport) MarshalWASM ¶
func (t TableImport) MarshalWASM(w io.Writer) error
type TableNamesSubsection ¶
type TableNamesSubsection struct {
Names []Naming
}
func (*TableNamesSubsection) MarshalWASM ¶
func (s *TableNamesSubsection) MarshalWASM(w io.Writer) error
func (*TableNamesSubsection) Type ¶
func (s *TableNamesSubsection) Type() NameType
func (*TableNamesSubsection) UnmarshalWASM ¶
func (s *TableNamesSubsection) UnmarshalWASM(r io.Reader) error
type TypeNamesSubsection ¶
type TypeNamesSubsection struct {
Names []Naming
}
func (*TypeNamesSubsection) MarshalWASM ¶
func (s *TypeNamesSubsection) MarshalWASM(w io.Writer) error
func (*TypeNamesSubsection) Type ¶
func (s *TypeNamesSubsection) Type() NameType
func (*TypeNamesSubsection) UnmarshalWASM ¶
func (s *TypeNamesSubsection) UnmarshalWASM(r io.Reader) error
type Unmarshaler ¶
type ValidationError ¶
type ValidationError string
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
Package leb128 provides functions for reading integer values encoded in the Little Endian Base 128 (LEB128) format: https://en.wikipedia.org/wiki/LEB128
|
Package leb128 provides functions for reading integer values encoded in the Little Endian Base 128 (LEB128) format: https://en.wikipedia.org/wiki/LEB128 |