Crate wasm_encoder[−][src]
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, Export, 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", Export::Function(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
encoders | Low-level encoders. |
Structs
AliasSection | An encoder for the alias section. |
CodeSection | An encoder for the code section. |
CustomSection | A custom section holding arbitrary data. |
DataCountSection | An encoder for the data count section. |
DataSection | An encoder for the data section. |
DataSegment | A segment in the data section. |
DataSymbolDefinition | The definition of a data symbol within a symbol table. |
ElementSection | An encoder for the element section. |
ElementSegment | An element segment in the element section. |
ExportSection | An encoder for the export section. |
Function | An encoder for a function body within the code section. |
FunctionSection | An encoder for the function section. |
GlobalSection | An encoder for the global section. |
GlobalType | A global’s type. |
ImportSection | An encoder for the import section. |
InstanceSection | An encoder for the instance section. |
Limits | Limits for a table or memory. |
LinkingSection | An encoder for the linking custom section. |
MemArg | The immediate for a memory instruction. |
MemorySection | An encoder for the memory section. |
MemoryType | A memory’s type. |
Module | A Wasm module that is being encoded. |
ModuleSection | An encoder for the module section. |
RawSection | A section made up of uninterpreted, raw bytes. |
StartSection | An encoder for the start section. |
SymbolTable | A subsection of the linking custom section that provides extra information about the symbols present in this Wasm object file. |
TableSection | An encoder for the table section. |
TableType | A table’s type. |
TypeSection | An encoder for the type section. |
Enums
BlockType | The type for a |
DataSegmentMode | A data segment’s mode. |
Element | An element in a segment in the element section. |
ElementMode | An element segment’s mode. |
Elements | A sequence of elements in a segment in the element section. |
EntityType | The type of an entity. |
Export | A WebAssembly export. |
Instruction | WebAssembly instructions. |
ItemKind | Kinds of WebAssembly items |
SectionId | Known section IDs. |
ValType | The type of a value. |
Traits
Section | A WebAssembly section. |
Type Definitions
Lane | Describe an unchecked SIMD lane index. |