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

leb128

std › wasm › leb128

LEB128, the variable-length integer encoding WebAssembly uses for every index, count and constant: 7 bits per byte, low bits first, with the top bit set on every byte but the last.

from std.wasm.leb128 import *

macro leb(value: int) {
    @emit uleb128(value)
}

macro sleb(value: int) {
    @emit sleb128(value)
}

leb 624485
sleb -123456
e5 8e 26
c0 bb 78

Macros

uleb128_length

How many bytes value, which can’t be negative, takes as unsigned LEB128.

SyntaxParametersResultDescription
uleb128_length(value)value: intreturns int

uleb128_padded

value as unsigned LEB128 in exactly n bytes. A larger n than uleb128_length(value) pads with zero groups, which WebAssembly allows, to give a field a fixed width.

SyntaxParametersResultDescription
uleb128_padded(value, n)value: int, n: intreturns Bytes<...>

uleb128

value, which can’t be negative, as unsigned LEB128 in as few bytes as possible.

SyntaxParametersResultDescription
uleb128(value)value: intreturns Bytes<...>

sleb128_length

How many bytes value takes as signed LEB128.

SyntaxParametersResultDescription
sleb128_length(value)value: intreturns int

sleb128_padded

value as signed LEB128 in exactly n bytes, sign-extended to fill any extra ones.

SyntaxParametersResultDescription
sleb128_padded(value, n)value: int, n: intreturns Bytes<...>

sleb128

value as signed LEB128 in as few bytes as possible.

SyntaxParametersResultDescription
sleb128(value)value: intreturns Bytes<...>

Types

Byte

type Byte = bits<8>

A byte.

Bytes

struct Bytes<const N: int>

N bytes, packed by bitter in order: a LEB128 encoding, or a whole instruction.

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