Skip to content

std/imports

std/wasm/imports — import + global section composers for the WebAssembly Core 1.0 binary format. Spec: https://webassembly.github.io/spec/core/binary/modules.html#import-section https://webassembly.github.io/spec/core/binary/modules.html#global-section Eighth slice of Phase 1 of docs/TOOLCHAIN-SELF-HOSTING.md. The sections module covered every section that doesn’t need a “descriptor union” — type / function / memory / export / start / code / data. Imports and globals are the two outliers: - An import entry is module : name + name : name + kind : u8 + desc, where desc shape depends on kind (typeidx for funcs, tabletype for tables, memtype for memories, globaltype for globals). - A global entry is globaltype + init_expr, where globaltype is valtype + mut : u8 and the init_expr is a constant expression terminated by 0x0B. The shape used here: each import-descriptor variant has a helper that returns the descriptor’s body bytes, the caller composes a parallel-array layout (modules / names / kinds / desc_bodies), and encode_import_section stitches the section together. This keeps the section composer flat without forcing a union type.

pub function import_func(): u8

---- Import-kind constants -----------------------------------

pub function import_table(): u8
pub function import_memory(): u8
pub function import_global(): u8
pub function mut_const(): u8

---- Mut constants for globaltype ----------------------------

pub function mut_var(): u8
pub function import_desc_func(typeidx: u32): u8[]

Function: just a typeidx.

pub function import_desc_global(vt: u8, is_mut: u8): u8[]

Global: valtype byte + mut byte.

pub function import_desc_memory(min_pages: u32, max_pages: i32): u8[]

Memory: a limits record (same shape memtype uses in the memory section). max_pages = -1 means “no maximum”.

pub function reftype_funcref(): u8
pub function reftype_externref(): u8
pub function import_desc_table(reftype: u8, min: u32, max: i32): u8[]
pub function encode_import_section(buf: u8[], modules: string[], names: string[], kinds: u8[], desc_bodies: u8[][]): u8[]

---- Import section ------------------------------------------

Body: vec(import). Four parallel arrays — modules[i] and names[i] form the import’s namespaced identity, kinds[i] picks the descriptor variant (use the import_func / table / memory / global constants above), and desc_bodies[i] carries the descriptor body that the matching import_desc_* helper produced.

pub function encode_global_section(buf: u8[], valtypes: u8[], muts: u8[], init_exprs: u8[][]): u8[]

---- Global section ------------------------------------------

Body: vec(global). Each entry: globaltype + init_expr. The caller supplies init_exprs[i] as a complete constant- expression byte sequence including the terminating 0x0B end — typical shape is i32.const N ; end (3 bytes for small N), produced by inst.inst_i32_const followed by inst.inst_end.