Expand description
A WebAssembly encoder.
The main builder is the Module
. You can build a section with a
section-specific builder, like TypeSection
or ImportSection
, and
then add it to the module with Module::section
. When you are finished
building the module, call either Module::as_slice
or Module::finish
to get the encoded bytes. The former gives a shared reference to the
underlying bytes as a slice, while the latter gives you ownership of them as
a vector.
§Example
If we wanted to build this module:
(module
(type (func (param i32 i32) (result i32)))
(func (type 0)
local.get 0
local.get 1
i32.add)
(export "f" (func 0)))
then we would do this:
use wasm_encoder::{
CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction,
Module, TypeSection, ValType,
};
let mut module = Module::new();
// Encode the type section.
let mut types = TypeSection::new();
let params = vec![ValType::I32, ValType::I32];
let results = vec![ValType::I32];
types.ty().function(params, results);
module.section(&types);
// Encode the function section.
let mut functions = FunctionSection::new();
let type_index = 0;
functions.function(type_index);
module.section(&functions);
// Encode the export section.
let mut exports = ExportSection::new();
exports.export("f", ExportKind::Func, 0);
module.section(&exports);
// Encode the code section.
let mut codes = CodeSection::new();
let locals = vec![];
let mut f = Function::new(locals);
f.instruction(&Instruction::LocalGet(0));
f.instruction(&Instruction::LocalGet(1));
f.instruction(&Instruction::I32Add);
f.instruction(&Instruction::End);
codes.function(&f);
module.section(&codes);
// Extract the encoded Wasm bytes for this module.
let wasm_bytes = module.finish();
// We generated a valid Wasm module!
assert!(wasmparser::validate(&wasm_bytes).is_ok());
Modules§
Structs§
- Array
Type - Represents a type of an array in a WebAssembly module.
- Branch
Hint - A single branch hint within a function.
- Branch
Hints - Helper structure to encode the
metadata.code.branch_hint
custom section. - Canonical
Function Section component-model
- An encoder for the canonical function section of WebAssembly components.
- Code
Section - An encoder for the code section.
- Component
component-model
- Represents a WebAssembly component that is being encoded.
- Component
Alias Section component-model
- An encoder for the alias section of WebAssembly component.
- Component
Builder component-model
- Convenience type to build a component incrementally and automatically keep track of index spaces.
- Component
Core Type Encoder component-model
- Used to encode core types.
- Component
Defined Type Encoder component-model
- Used for encoding component defined types.
- Component
Export Section component-model
- An encoder for the export section of WebAssembly component.
- Component
Func Type Encoder component-model
- Used to encode component function types.
- Component
Import Section component-model
- An encoder for the import section of WebAssembly components.
- Component
Instance Section component-model
- An encoder for the instance section of WebAssembly components.
- Component
Name Section component-model
- Encoding for the
component-name
custom section which assigns human-readable names to items within a component. - Component
Start Section component-model
- An encoder for the start section of WebAssembly components.
- Component
Type component-model
- Represents a component type.
- Component
Type Encoder component-model
- Used to encode component and instance types.
- Component
Type Section component-model
- An encoder for the type section of WebAssembly components.
- Composite
Type - Represents a composite type in a WebAssembly module.
- Const
Expr - A constant expression.
- Cont
Type - Represents a type of a continuation in a WebAssembly module.
- Core
Dump Instances Section - The “coreinstances” section for the core dump
- Core
Dump Modules Section - The “coremodules” custom section for coredumps which lists the names of the modules
- Core
Dump Section - The “core” custom section for coredumps, as described in the tool-conventions repository.
- Core
Dump Stack Section - A “corestack” custom section as described in the tool-conventions repository
- Core
Type Encoder - A single-use encoder for encoding a type; this forces all encoding for a type to be done in a single shot.
- Core
Type Section component-model
- An encoder for the core type section of WebAssembly components.
- Custom
Section - A custom section holding arbitrary data.
- Data
Count Section - An encoder for the data count section.
- Data
Section - An encoder for the data section.
- Data
Segment - A segment in the data section.
- Data
Symbol Definition - The definition of a data symbol within a symbol table.
- Element
Section - An encoder for the element section.
- Element
Segment - An element segment in the element section.
- Export
Section - An encoder for the export section of WebAssembly module.
- Field
Type - Field type in composite types (structs, arrays).
- Func
Type - Represents a type of a function in a WebAssembly module.
- Function
- An encoder for a function body within the code section.
- Function
Section - An encoder for the function section of WebAssembly modules.
- Global
Section - An encoder for the global section.
- Global
Type - A global’s type.
- Import
Section - An encoder for the import section of WebAssembly modules.
- Indirect
Name Map - A map used to describe names with two levels of indirection, as opposed to a
NameMap
which has one level of indirection. - Instance
Section component-model
- An encoder for the core instance section of WebAssembly components.
- Instance
Type component-model
- Represents an instance type.
- Linking
Section - An encoder for the linking custom section.
- MemArg
- The immediate for a memory instruction.
- Memory
Section - An encoder for the memory section.
- Memory
Type - A memory’s type.
- Module
- Represents a WebAssembly component that is being encoded.
- Module
Section component-model
- An encoder for the module section of WebAssembly components.
- Module
Type component-model
- Represents the type of a core module.
- NameMap
- A map used to name items in a wasm module, organized by naming each individual index.
- Name
Section - An encoder for the custom
name
section. - Nested
Component Section component-model
- An encoder for the component section of WebAssembly components.
- Producers
Field - The value of a field in the producers custom section
- Producers
Section - An encoder for the producers custom section.
- RawCustom
Section - A raw custom section where the bytes specified contain the leb-encoded length of the custom section, the custom section’s name, and the custom section’s data.
- RawSection
- A section made up of uninterpreted, raw bytes.
- RefType
- A reference type.
- Start
Section - An encoder for the start section of WebAssembly modules.
- Struct
Type - Represents a type of a struct in a WebAssembly module.
- SubType
- Represents a subtype of possible other types in a WebAssembly module.
- Symbol
Table - A subsection of the linking custom section that provides extra information about the symbols present in this Wasm object file.
- Table
Section - An encoder for the table section.
- Table
Type - A table’s type.
- TagSection
- An encoder for the tag section.
- TagType
- A tag’s type.
- Type
Section - An encoder for the type section of WebAssembly modules.
Enums§
- Abstract
Heap Type - An abstract heap type.
- Alias
component-model
- Different forms of aliases that can be inserted into a
ComponentAliasSection
. - Block
Type - The type for a
block
/if
/loop
. - Canonical
Option component-model
- Represents options for canonical function definitions.
- Catch
- Component
Export Kind component-model
- Represents the kind of an export from a WebAssembly component.
- Component
Outer Alias Kind component-model
- Represents the kinds of outer aliasable items in a component.
- Component
Section Id component-model
- Known section identifiers of WebAssembly components.
- Component
Type Ref component-model
- Represents a reference to a type.
- Component
ValType component-model
- Represents a component value type.
- Composite
Inner Type - A
CompositeType
can contain one of these types. - Core
Dump Value - Local and stack values are encoded using one byte for the type (similar to Wasm’s Number Types) followed by bytes representing the actual value See the tool-conventions repo for more details.
- Data
Segment Mode - A data segment’s mode.
- Element
Mode - An element segment’s mode.
- Elements
- A sequence of elements in a segment in the element section.
- Entity
Type - The type of an entity.
- Export
Kind - Represents the kind of an export from a WebAssembly module.
- Handle
- Heap
Type - Part of the function references proposal.
- Instruction
- WebAssembly instructions.
- Module
Arg component-model
- Represents an argument to a module instantiation.
- Ordering
- The memory ordering for atomic instructions.
- Primitive
ValType component-model
- Represents a primitive component value type.
- Section
Id - Known section identifiers of WebAssembly modules.
- Storage
Type - Storage type for composite type fields.
- TagKind
- Represents a tag kind.
- Type
Bounds component-model
- Represents the possible type bounds for type references.
- ValType
- The type of a core WebAssembly value.
Traits§
- Component
Section component-model
- A WebAssembly component section.
- Encode
- Implemented by types that can be encoded into a byte sink.
- Section
- A WebAssembly module section.
Type Aliases§
- Lane
- Describe an unchecked SIMD lane index.