Crate wasm_encoder
source ·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.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§
- reencode
wasmparser
Structs§
- Represents a type of an array in a WebAssembly module.
- An encoder for the canonical function section of WebAssembly components.
- An encoder for the code section.
- Represents a WebAssembly component that is being encoded.
- An encoder for the alias section of WebAssembly component.
- Convenience type to build a component incrementally and automatically keep track of index spaces.
- Used for encoding component defined types.
- An encoder for the export section of WebAssembly component.
- Used to encode component function types.
- An encoder for the import section of WebAssembly components.
- An encoder for the instance section of WebAssembly components.
- Encoding for the
component-name
custom section which assigns human-readable names to items within a component. - An encoder for the start section of WebAssembly components.
- Represents a component type.
- Used to encode component and instance types.
- An encoder for the type section of WebAssembly components.
- A constant expression.
- The “coreinstances” section for the core dump
- The “coremodules” custom section for coredumps which lists the names of the modules
- The “core” custom section for coredumps, as described in the tool-conventions repository.
- A “corestack” custom section as described in the tool-conventions repository
- Used to encode core types.
- An encoder for the core type section of WebAssembly components.
- A custom section holding arbitrary data.
- An encoder for the data count section.
- An encoder for the data section.
- A segment in the data section.
- The definition of a data symbol within a symbol table.
- An encoder for the element section.
- An element segment in the element section.
- An encoder for the export section of WebAssembly module.
- Field type in composite types (structs, arrays).
- Represents a type of a function in a WebAssembly module.
- An encoder for a function body within the code section.
- An encoder for the function section of WebAssembly modules.
- An encoder for the global section.
- A global’s type.
- An encoder for the import section of WebAssembly modules.
- A map used to describe names with two levels of indirection, as opposed to a
NameMap
which has one level of indirection. - An encoder for the core instance section of WebAssembly components.
- Represents an instance type.
- An encoder for the linking custom section.
- The immediate for a memory instruction.
- An encoder for the memory section.
- A memory’s type.
- Represents a WebAssembly component that is being encoded.
- An encoder for the module section of WebAssembly components.
- Represents the type of a core module.
- A map used to name items in a wasm module, organized by naming each individual index.
- An encoder for the custom
name
section. - An encoder for the component section of WebAssembly components.
- The value of a field in the producers custom section
- An encoder for the producers custom 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.
- A section made up of uninterpreted, raw bytes.
- A reference type.
- An encoder for the start section of WebAssembly modules.
- Represents a type of a struct in a WebAssembly module.
- Represents a subtype of possible other types in a WebAssembly module.
- A subsection of the linking custom section that provides extra information about the symbols present in this Wasm object file.
- An encoder for the table section.
- A table’s type.
- An encoder for the tag section.
- A tag’s type.
- An encoder for the type section of WebAssembly modules.
Enums§
- An abstract heap type.
- Different forms of aliases that can be inserted into a
ComponentAliasSection
. - The type for a
block
/if
/loop
. - Represents options for canonical function definitions.
- Represents the kind of an export from a WebAssembly component.
- Represents the kinds of outer aliasable items in a component.
- Known section identifiers of WebAssembly components.
- Represents a reference to a type.
- Represents a component value type.
- Represents a composite type in a WebAssembly module.
- 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.
- A data segment’s mode.
- An element segment’s mode.
- A sequence of elements in a segment in the element section.
- The type of an entity.
- Represents the kind of an export from a WebAssembly module.
- Part of the function references proposal.
- WebAssembly instructions.
- Represents an argument to a module instantiation.
- The memory ordering for atomic instructions.
- Represents a primitive component value type.
- Known section identifiers of WebAssembly modules.
- Storage type for composite type fields.
- Represents a tag kind.
- Represents the possible type bounds for type references.
- The type of a core WebAssembly value.
Traits§
- A WebAssembly component section.
- Implemented by types that can be encoded into a byte sink.
- A WebAssembly module section.
Type Aliases§
- Describe an unchecked SIMD lane index.