Documentation
¶
Overview ¶
Package binary decodes a WebAssembly component binary -- its imports, exports, types, embedded core modules and instantiation graph -- without a wazy.Runtime: decoding is pure and compiles nothing.
c, err := binary.Decode(wasmBytes) // e.g. from os.ReadFile("app.wasm")
if err != nil {
return err
}
for _, imp := range c.Imports {
fmt.Println("import", imp.Name)
}
for _, exp := range c.Exports {
fmt.Println("export", exp.Name)
}
An import's or export's ExternType is the sort byte of the binary format: 0x00 core module, 0x01 func, 0x02 value, 0x03 type, 0x04 component, 0x05 instance. For a func, Component.ResolveType(ExternIndex) gives its component.FuncDesc; for an instance, its InstanceDesc.
Every name here is an alias of the decoder wazy itself instantiates from, so what this package reports is exactly what Instantiate sees. The value type descriptors (component.FuncDesc, component.RecordDesc, ...) live in package component.
A core wasm module is not a component: Decode rejects one with ErrInvalidVersion. Use package github.com/samyfodil/wazy/binary for those.
Example ¶
Decode a component and list what it imports and exports.
package main
import (
"fmt"
"github.com/samyfodil/wazy/component/binary"
"github.com/samyfodil/wazy/internal/component/testfixtures"
)
func main() {
wasm := testfixtures.RealHello // the bytes of a .wasm component
c, err := binary.Decode(wasm)
if err != nil {
panic(err)
}
for _, imp := range c.Imports {
fmt.Println("import", imp.Name)
}
for _, exp := range c.Exports {
fmt.Println("export", exp.Name)
}
}
Output: import wasi:cli/environment@0.2.3 import wasi:cli/exit@0.2.3 import wasi:io/error@0.2.3 import wasi:io/streams@0.2.3 import wasi:cli/stdin@0.2.3 import wasi:cli/stdout@0.2.3 import wasi:cli/stderr@0.2.3 import wasi:clocks/wall-clock@0.2.3 import wasi:filesystem/types@0.2.3 import wasi:filesystem/preopens@0.2.3 export wasi:cli/run@0.2.3
Index ¶
- Constants
- Variables
- type AliasDef
- type Canon
- type CanonOpt
- type Component
- type ComponentDesc
- type ComponentFuncSpaceEntry
- type ComponentFuncSpaceEntryKind
- type ComponentInstanceSpaceEntry
- type ComponentInstanceSpaceEntryKind
- type ComponentSpaceEntry
- type ComponentSpaceEntryKind
- type CoreFuncSpaceEntry
- type CoreFuncSpaceEntryKind
- type CoreInlineExport
- type CoreInstance
- type CoreInstantiateArg
- type CoreModule
- type CoreModuleSpaceEntry
- type CoreModuleSpaceEntryKind
- type Export
- type ExternDesc
- type Import
- type InlineExport
- type Instance
- type InstanceDesc
- type InstantiateArg
- type RawSection
- type ResourceDesc
- type Start
- type Type
- type TypeDecl
- type TypeDeclKind
- type TypeSpaceEntry
- type TypeSpaceEntryKind
Examples ¶
Constants ¶
const ( TypeDeclCoreType = binary.TypeDeclCoreType TypeDeclType = binary.TypeDeclType TypeDeclAlias = binary.TypeDeclAlias TypeDeclImport = binary.TypeDeclImport TypeDeclExport = binary.TypeDeclExport )
The declaration kinds, TypeDecl.Kind.
const ( TypeSpaceDef = binary.TypeSpaceDef TypeSpaceAlias = binary.TypeSpaceAlias TypeSpaceImport = binary.TypeSpaceImport TypeSpaceExport = binary.TypeSpaceExport ComponentFuncFromImport = binary.ComponentFuncFromImport ComponentFuncFromAlias = binary.ComponentFuncFromAlias ComponentFuncFromCanonLift = binary.ComponentFuncFromCanonLift ComponentFuncFromExport = binary.ComponentFuncFromExport ComponentInstanceFromImport = binary.ComponentInstanceFromImport ComponentInstanceFromDefinition = binary.ComponentInstanceFromDefinition ComponentInstanceFromAlias = binary.ComponentInstanceFromAlias ComponentInstanceFromExport = binary.ComponentInstanceFromExport ComponentFromImport = binary.ComponentFromImport ComponentFromDefinition = binary.ComponentFromDefinition ComponentFromAlias = binary.ComponentFromAlias ComponentFromExport = binary.ComponentFromExport CoreFuncFromAlias = binary.CoreFuncFromAlias CoreFuncFromCanon = binary.CoreFuncFromCanon CoreModuleFromDefinition = binary.CoreModuleFromDefinition CoreModuleFromAlias = binary.CoreModuleFromAlias )
The index-space entry kinds.
const ( CanonKindLift = binary.CanonKindLift CanonKindLower = binary.CanonKindLower CanonKindResourceNew = binary.CanonKindResourceNew CanonKindResourceDrop = binary.CanonKindResourceDrop CanonKindResourceRep = binary.CanonKindResourceRep CanonKindResourceDropAsync = binary.CanonKindResourceDropAsync CanonKindTaskCancel = binary.CanonKindTaskCancel CanonKindSubtaskCancel = binary.CanonKindSubtaskCancel CanonKindTaskReturn = binary.CanonKindTaskReturn CanonKindContextGet = binary.CanonKindContextGet CanonKindContextSet = binary.CanonKindContextSet CanonKindThreadYield = binary.CanonKindThreadYield CanonKindSubtaskDrop = binary.CanonKindSubtaskDrop CanonKindStreamNew = binary.CanonKindStreamNew CanonKindStreamRead = binary.CanonKindStreamRead CanonKindStreamWrite = binary.CanonKindStreamWrite CanonKindStreamCancelRead = binary.CanonKindStreamCancelRead CanonKindStreamCancelWrite = binary.CanonKindStreamCancelWrite CanonKindStreamDropReadable = binary.CanonKindStreamDropReadable CanonKindStreamDropWritable = binary.CanonKindStreamDropWritable CanonKindFutureNew = binary.CanonKindFutureNew CanonKindFutureRead = binary.CanonKindFutureRead CanonKindFutureWrite = binary.CanonKindFutureWrite CanonKindFutureCancelRead = binary.CanonKindFutureCancelRead CanonKindFutureCancelWrite = binary.CanonKindFutureCancelWrite CanonKindFutureDropReadable = binary.CanonKindFutureDropReadable CanonKindFutureDropWritable = binary.CanonKindFutureDropWritable CanonKindErrorContextNew = binary.CanonKindErrorContextNew CanonKindErrorContextDebugMessage = binary.CanonKindErrorContextDebugMessage CanonKindErrorContextDrop = binary.CanonKindErrorContextDrop CanonKindWaitableSetNew = binary.CanonKindWaitableSetNew CanonKindWaitableSetWait = binary.CanonKindWaitableSetWait CanonKindWaitableSetPoll = binary.CanonKindWaitableSetPoll CanonKindWaitableSetDrop = binary.CanonKindWaitableSetDrop CanonKindWaitableJoin = binary.CanonKindWaitableJoin CanonKindBackpressureInc = binary.CanonKindBackpressureInc CanonKindBackpressureDec = binary.CanonKindBackpressureDec CanonKindThreadIndex = binary.CanonKindThreadIndex CanonKindThreadNewIndirect = binary.CanonKindThreadNewIndirect CanonKindThreadSuspend = binary.CanonKindThreadSuspend CanonKindThreadYieldThenResume = binary.CanonKindThreadYieldThenResume )
The canon definition kinds, Canon.Kind.
Variables ¶
var ( ErrInvalidMagicNumber = binary.ErrInvalidMagicNumber ErrInvalidVersion = binary.ErrInvalidVersion ErrInvalidLayer = binary.ErrInvalidLayer ErrInvalidSectionID = binary.ErrInvalidSectionID ErrTruncatedBinary = binary.ErrTruncatedBinary )
The errors Decode returns for a malformed preamble or section.
Functions ¶
This section is empty.
Types ¶
type ComponentDesc ¶
type ComponentDesc = binary.ComponentDesc
The type descriptors package component does not already name.
type ComponentFuncSpaceEntry ¶
type ComponentFuncSpaceEntry = binary.ComponentFuncSpaceEntry
The index spaces: each entry says what produced the item at that index.
type ComponentFuncSpaceEntryKind ¶
type ComponentFuncSpaceEntryKind = binary.ComponentFuncSpaceEntryKind
The index spaces: each entry says what produced the item at that index.
type ComponentInstanceSpaceEntry ¶
type ComponentInstanceSpaceEntry = binary.ComponentInstanceSpaceEntry
The index spaces: each entry says what produced the item at that index.
type ComponentInstanceSpaceEntryKind ¶
type ComponentInstanceSpaceEntryKind = binary.ComponentInstanceSpaceEntryKind
The index spaces: each entry says what produced the item at that index.
type ComponentSpaceEntry ¶
type ComponentSpaceEntry = binary.ComponentSpaceEntry
The index spaces: each entry says what produced the item at that index.
type ComponentSpaceEntryKind ¶
type ComponentSpaceEntryKind = binary.ComponentSpaceEntryKind
The index spaces: each entry says what produced the item at that index.
type CoreFuncSpaceEntry ¶
type CoreFuncSpaceEntry = binary.CoreFuncSpaceEntry
The index spaces: each entry says what produced the item at that index.
type CoreFuncSpaceEntryKind ¶
type CoreFuncSpaceEntryKind = binary.CoreFuncSpaceEntryKind
The index spaces: each entry says what produced the item at that index.
type CoreInlineExport ¶
type CoreInlineExport = binary.CoreInlineExport
A decoded component and its parts.
type CoreInstantiateArg ¶
type CoreInstantiateArg = binary.CoreInstantiateArg
A decoded component and its parts.
type CoreModuleSpaceEntry ¶
type CoreModuleSpaceEntry = binary.CoreModuleSpaceEntry
The index spaces: each entry says what produced the item at that index.
type CoreModuleSpaceEntryKind ¶
type CoreModuleSpaceEntryKind = binary.CoreModuleSpaceEntryKind
The index spaces: each entry says what produced the item at that index.
type ExternDesc ¶
type ExternDesc = binary.ExternDesc
The declarations of an instance or component type, its Decls field, and what an import, an export's ascription, or a declaration says an item is.
type InstanceDesc ¶
type InstanceDesc = binary.InstanceDesc
The type descriptors package component does not already name.
type InstantiateArg ¶
type InstantiateArg = binary.InstantiateArg
A decoded component and its parts.
type ResourceDesc ¶
type ResourceDesc = binary.ResourceDesc
The type descriptors package component does not already name.
type TypeDecl ¶
The declarations of an instance or component type, its Decls field, and what an import, an export's ascription, or a declaration says an item is.
type TypeDeclKind ¶
type TypeDeclKind = binary.TypeDeclKind
The declarations of an instance or component type, its Decls field, and what an import, an export's ascription, or a declaration says an item is.
type TypeSpaceEntry ¶
type TypeSpaceEntry = binary.TypeSpaceEntry
The index spaces: each entry says what produced the item at that index.
type TypeSpaceEntryKind ¶
type TypeSpaceEntryKind = binary.TypeSpaceEntryKind
The index spaces: each entry says what produced the item at that index.