Documentation
¶
Overview ¶
Package binary decodes a core WebAssembly module -- a wasip1 program, say -- into its sections without a wazy.Runtime: decoding compiles and validates nothing.
m, err := binary.Decode(wasmBytes, 0) // e.g. from os.ReadFile("app.wasm")
if err != nil {
return err
}
for _, imp := range m.ImportSection {
fmt.Println("import", imp.Module, imp.Name)
}
for _, exp := range m.ExportSection {
fmt.Println("export", exp.Name)
}
An import's or export's Type is an api.ExternType (api.ExternTypeFunc, ...). A func import's DescFunc indexes TypeSection.
Every name here is an alias of the decoder wazy itself compiles from, so what this package reports is exactly what CompileModule reads. A component is not a core module: Decode rejects one with ErrInvalidVersion. Use package github.com/samyfodil/wazy/component/binary for those.
Example ¶
Decode a wasip1 program and list what it imports and exports.
package main
import (
"fmt"
"github.com/samyfodil/wazy/binary"
"github.com/samyfodil/wazy/internal/testing/dwarftestdata"
)
func main() {
wasm := dwarftestdata.ZigCCWasm // the bytes of a .wasm file
m, err := binary.Decode(wasm, 0)
if err != nil {
panic(err)
}
for _, imp := range m.ImportSection {
fmt.Println("import", imp.Module, imp.Name)
}
for _, exp := range m.ExportSection {
fmt.Println("export", exp.Name)
}
}
Output: import wasi_snapshot_preview1 proc_exit export memory export _start
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidMagicNumber = binary.ErrInvalidMagicNumber ErrInvalidVersion = binary.ErrInvalidVersion ErrInvalidSectionID = binary.ErrInvalidSectionID ErrInvalidByte = binary.ErrInvalidByte )
The errors Decode returns for a malformed preamble or section.
var Magic = binary.Magic
Magic is the 4-byte preamble, "\0asm", that core modules and components share; the version that follows it is what tells them apart.
Functions ¶
This section is empty.
Types ¶
type ConstantExpression ¶
type ConstantExpression = wasm.ConstantExpression
A decoded module and its parts.
type Module ¶
A decoded module and its parts.
func Decode ¶
func Decode(b []byte, features api.CoreFeatures) (*Module, error)
Decode decodes a core module, checking its encoding against the given features; 0 means api.CoreFeaturesV2, a runtime's default. Function bodies are decoded but not validated.
It keeps custom sections (Module.CustomSections) and skips DWARF, and accepts any memory size the format can declare: a runtime's own memory limit is applied when it compiles the module, not here.