Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

module

std › wasm › module

The WebAssembly module container: the header, sections, and their length prefixes. A section starts with its id byte and its length in bytes, which deferred_uleb128 fills in once bitter knows it.

from std.wasm.module import *
from std.wasm.impl import *
from std.bitter.deferred import *

header

# The type section: one function type, () -> i32.
byte(0x01)
deferred_uleb128 span(types_start, types_end), 1
types_start:
byte(0x01)
func_type_0_to_1(I32)
types_end:
00 61 73 6d 01 00 00 00
01 05 01 60 00 01 7f

section_header writes a section’s id and size, and the helpers below write the entries of the type, import, function, memory, export, code and data sections: enough for a WASI program like examples/wasm/hello.basm. The table, global, start and element sections have no helpers yet.

Macros

deferred_uleb128

value, usually span(start, end), as unsigned LEB128 in exactly n bytes, padded if it turns out to need fewer. n has to be enough for the final value: one byte holds up to 127.

SyntaxParametersResultDescription
deferred_uleb128 value, nvalue: Deferred, n: intemits DeferredLeb128<...>

byte

One byte, such as a section id.

SyntaxParametersResultDescription
byte valuevalue: intemits Byte

The 8 bytes every module starts with: \0asm, then version 1.

SyntaxParametersResultDescription
headeremits Bytes<8>

func_type_0_to_1

A function type with no parameters and one result of type result, such as I32.

SyntaxParametersResultDescription
func_type_0_to_1 resultresult: Byteemits Bytes<4>

u32

value, which can’t be negative, as unsigned LEB128: the spec’s u32, which every count and index is.

SyntaxParametersResultDescription
u32 valuevalue: intemits Bytes<...>

size

The number of bytes from label start to label end, as unsigned LEB128: the size before a section or a function body. It always takes 5 bytes, since it isn’t known until layout, and 5 hold any size.

SyntaxParametersResultDescription
size start, endstart: int, end: intemits DeferredLeb128<5>

section_header

A section’s id, such as TYPE_SECTION, and its size. Put the label start right after it and end after the section’s contents.

SyntaxParametersResultDescription
section_header id, start, endid: int, start: int, end: int

params

SyntaxParametersResultDescription
params()returns Bytes<1>A function’s parameter types: params(I32, I32). Up to four.
params(a)a: Bytereturns Bytes<2>
params(a, b)a: Byte, b: Bytereturns Bytes<3>
params(a, b, c)a: Byte, b: Byte, c: Bytereturns Bytes<4>
params(a, b, c, d)a: Byte, b: Byte, c: Byte, d: Bytereturns Bytes<5>

results

SyntaxParametersResultDescription
results()returns Bytes<1>A function’s result types: results(I32), or results() for none.
results(a)a: Bytereturns Bytes<2>

func_type

A function type: func_type params(I32), results(I32).

SyntaxParametersResultDescription
func_type parameters, returnedparameters: Bytes<...>, returned: Bytes<...>

name

A name: its length in bytes, then its UTF-8 bytes.

SyntaxParametersResultDescription
name source<S>, source: S

import_func

Imports the function item_name from the module module_name, with the type numbered type_index in the type section. Imported functions are numbered before the module’s own.

SyntaxParametersResultDescription
import_func module_name, item_name, type_index<S, T>, module_name: S, item_name: T, type_index: int

memory

A memory of pages 64 KiB pages, with no maximum.

SyntaxParametersResultDescription
memory pagespages: int

export_func

Exports function number index as export_name.

SyntaxParametersResultDescription
export_func export_name, index<S>, export_name: S, index: int

export_memory

Exports memory number index as export_name.

SyntaxParametersResultDescription
export_memory export_name, index<S>, export_name: S, index: int

data_segment

A data segment that copies the string source’s UTF-8 bytes into memory 0 at address offset when the module starts.

SyntaxParametersResultDescription
data_segment offset, source<S>, offset: int, source: S

Types

Leb128Group

struct Leb128Group

One byte of a LEB128 value bitter works out later.

FieldTypeDescription
continuationboolSet on every byte but the last.
payloadPositioned<7>Seven bits of the value.

DeferredLeb128

struct DeferredLeb128<const N: int>

N bytes of a LEB128 value bitter works out later.

Some of its fields are generated by @for or @if.

Constants

ConstantTypeValueDescription
CUSTOM_SECTION0The id of the custom section.
TYPE_SECTION1The id of the type section: function signatures.
IMPORT_SECTION2The id of the import section.
FUNCTION_SECTION3The id of the function section: each defined function’s type.
TABLE_SECTION4The id of the table section.
MEMORY_SECTION5The id of the memory section.
GLOBAL_SECTION6The id of the global section.
EXPORT_SECTION7The id of the export section.
START_SECTION8The id of the start section.
ELEMENT_SECTION9The id of the element section.
CODE_SECTION10The id of the code section: each defined function’s body.
DATA_SECTION11The id of the data section.