build-a-wasm-runtime / lesson-10.md
Lesson 10 · Decoding a module

The export section

A module keeps its functions private unless it explicitly exports them by name. Today you decode the export section so a host can later ask for a function by the name the module published.

The goal

Decode the export section into a list of named exports, each with its kind and index.

Start here - the target
TO DO
Scenario: Decoding a named function export
Givenan export section whose payload is 01 03 61 64 64 00 00 (one export)
Whenthe export section is decoded
Thenthe result is one export with name "add", kind function, and index 0
Andthe name is a length-prefixed UTF-8 byte string: 03 61 64 64 is the three bytes for "add"
Background

The export section (id 7) is a module’s public interface: a vector of exports, each pairing a name with something inside the module. An export record is a name, then a one-byte kind (0x00 function, 0x01 table, 0x02 memory, 0x03 global), then an index into that kind’s space. So 01 03 61 64 64 00 00 is one export named add (three bytes 61 64 64), kind function, index 0.

The name itself is worth a close look: it is a vec of bytes, a u32 length followed by that many UTF-8 bytes - the same vector pattern again, now producing a string. This is the surface a host reaches through: when your runtime later runs an exported function, it will look the name up here to find which function index to invoke. Decode all four kinds even though you will mostly use functions; a real module exports memories and globals too, and skipping the kind byte wrong would desynchronize the whole section.

Make it work
// An export is: name (a vec of UTF-8 bytes), a kind byte, and an index.
// Kind 0x00 = func, 0x01 = table, 0x02 = mem, 0x03 = global.
type Export struct{ Name string; Kind byte; Index uint32 }
func readExport(c *Cursor) (Export, error) {
// name = readVec(readByte) turned into a string; then kind byte; then index
}
CheckpointDONE
You can read a module's public names and what they point to. Commit and stop here.