Skip to content

std/module

std/wasm/module — top-level module composer for the WebAssembly Core 1.0 binary format. Spec: https://webassembly.github.io/spec/core/binary/modules.html Ninth slice of Phase 1 of docs/TOOLCHAIN-SELF-HOSTING.md. Bundles the section composers (sections.fern + imports.fern) behind a single build entry point: a caller fills in a Module struct field-by-field, then calls build(m) to get the complete module bytes back. Section ordering matters — the wasm spec requires sections in the order: type, import, function, table, memory, global, export, start, element, code, data. build emits them in that order, skipping any section whose input is empty.

pub struct Module { type_params: u8[][], type_results: u8[][], import_modules: string[], import_names: string[], import_kinds: u8[], import_descs: u8[][], function_typeidxs: u32[], table_present: boolean, table_reftype: u8, table_min: u32, table_max: i32, memory_present: boolean, memory_min: u32, memory_max: i32, global_valtypes: u8[], global_muts: u8[], global_inits: u8[][], export_names: string[], export_kinds: u8[], export_idxs: u32[], has_start: boolean, start_funcidx: u32, elem_offsets: i32[], elem_funcidxs: u32[][], code_bodies: u8[][], data_offsets: i32[], data_inits: u8[][] }

Module — the per-section inputs the existing section composers each take. Vector sections (type_*, import_*, function_*, global_*, export_*, code_*, data_*) skip emitting their section header when empty. Singleton sections (memory, start) gate on the matching boolean.

pub function module_new(): Module

module_new — a Module with every section empty. Callers populate the fields they need before calling build().

pub function (m: Module) with_types(params: u8[][], results: u8[][]): Module

with_types — type section: one functype per (params, results) pair (parallel arrays).

pub function (m: Module) with_imports(modules: string[], names: string[], kinds: u8[], descs: u8[][]): Module

with_imports — import section: parallel (module, name, kind, desc) arrays, one per import.

pub function (m: Module) with_functions(typeidxs: u32[]): Module

with_functions — function section: one typeidx per declared (non-imported) function, in declaration order.

pub function (m: Module) with_table(reftype: u8, min: u32, max: i32): Module

with_table — table section (sets table_present). max = -1 means “no maximum”.

pub function (m: Module) with_memory(min: u32, max: i32): Module

with_memory — memory section (sets memory_present). max = -1 means “no maximum”.

pub function (m: Module) with_globals(valtypes: u8[], muts: u8[], inits: u8[][]): Module

with_globals — global section: parallel (valtype, mut, init) arrays, one per global.

pub function (m: Module) with_exports(names: string[], kinds: u8[], idxs: u32[]): Module

with_exports — export section: parallel (name, kind, idx) arrays, one per export.

pub function (m: Module) with_start(funcidx: u32): Module

with_start — start section (sets has_start).

pub function (m: Module) with_elements(offsets: i32[], funcidxs: u32[][]): Module

with_elements — element section: parallel (offset, funcidx-list) arrays, one active funcref segment per pair.

pub function (m: Module) with_code(bodies: u8[][]): Module

with_code — code section: one pre-wrapped function body per typeidx in function_typeidxs.

pub function (m: Module) with_data(offsets: i32[], inits: u8[][]): Module

with_data — data section: parallel (offset, init-bytes) arrays, one active data segment per pair.

pub function build(m: Module): u8[]

build — serialise the module to the complete wasm binary: preamble (\0asm + version 1) followed by every populated section in spec-required order.