binary

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
var (
	ErrInvalidMagicNumber = binary.ErrInvalidMagicNumber
	ErrInvalidVersion     = binary.ErrInvalidVersion
	ErrInvalidSectionID   = binary.ErrInvalidSectionID
	ErrInvalidByte        = binary.ErrInvalidByte
)

The errors Decode returns for a malformed preamble or section.

View Source
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 Code

type Code = wasm.Code

A decoded module and its parts.

type ConstantExpression

type ConstantExpression = wasm.ConstantExpression

A decoded module and its parts.

type CustomSection

type CustomSection = wasm.CustomSection

A decoded module and its parts.

type DataSegment

type DataSegment = wasm.DataSegment

A decoded module and its parts.

type ElementSegment

type ElementSegment = wasm.ElementSegment

A decoded module and its parts.

type Export

type Export = wasm.Export

A decoded module and its parts.

type FunctionType

type FunctionType = wasm.FunctionType

A decoded module and its parts.

type Global

type Global = wasm.Global

A decoded module and its parts.

type GlobalType

type GlobalType = wasm.GlobalType

A decoded module and its parts.

type Import

type Import = wasm.Import

A decoded module and its parts.

type Index

type Index = wasm.Index

A decoded module and its parts.

type Memory

type Memory = wasm.Memory

A decoded module and its parts.

type Module

type Module = wasm.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.

type NameSection

type NameSection = wasm.NameSection

A decoded module and its parts.

type Table

type Table = wasm.Table

A decoded module and its parts.

type Tag

type Tag = wasm.Tag

A decoded module and its parts.

type ValueType

type ValueType = wasm.ValueType

A decoded module and its parts.

Jump to

Keyboard shortcuts

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