std/imports
std/imports
Section titled “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.
import_func
Section titled “import_func”pub function import_func(): u8---- Import-kind constants -----------------------------------
import_table
Section titled “import_table”pub function import_table(): u8import_memory
Section titled “import_memory”pub function import_memory(): u8import_global
Section titled “import_global”pub function import_global(): u8mut_const
Section titled “mut_const”pub function mut_const(): u8---- Mut constants for globaltype ----------------------------
mut_var
Section titled “mut_var”pub function mut_var(): u8import_desc_func
Section titled “import_desc_func”pub function import_desc_func(typeidx: u32): u8[]Function: just a typeidx.
import_desc_global
Section titled “import_desc_global”pub function import_desc_global(vt: u8, is_mut: u8): u8[]Global: valtype byte + mut byte.
import_desc_memory
Section titled “import_desc_memory”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”.
reftype_funcref
Section titled “reftype_funcref”pub function reftype_funcref(): u8reftype_externref
Section titled “reftype_externref”pub function reftype_externref(): u8import_desc_table
Section titled “import_desc_table”pub function import_desc_table(reftype: u8, min: u32, max: i32): u8[]encode_import_section
Section titled “encode_import_section”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.
encode_global_section
Section titled “encode_global_section”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.