Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 ยท Modules, Memory and Tables

Data Segments and Strings

Learn how WebAssembly stores strings and constant data. Use active and passive data segments, UTF-8 with TextEncoder and TextDecoder, and memory.init with data.drop.

WebAssembly has no string type. Text is just bytes in linear memory, nearly always encoded as UTF-8, and a "string" is a pair of numbers: where it starts (a pointer) and how long it is (a length). Constant text and tables of numbers that a program needs from the very beginning are stored in the module as data segments, which the engine copies into memory.

This lesson shows the two kinds of segment, and how JavaScript converts between its own strings and the bytes Wasm sees.

Active segments

An active data segment is copied into a memory automatically when the module is instantiated. It names the memory, an offset (a constant expression such as i32.const 16) and the bytes. Compilers produce them for string literals, constant arrays and initialized global variables.

In binary form: a flag byte (0x00 for memory 0), the offset expression ending in 0x0b, then a vector of bytes. The helper str already writes a vector of bytes, so a segment is [0x00, 0x41, offset, 0x0b, ...str(text)].

The number after i32.const is a signed LEB128 value. A plain byte only works for numbers from 0 to 63 (and -64 to -1). The number 64 needs two bytes (0xc0 0x00), and writing the single byte 0x40 would mean -64. The examples here keep addresses below 64; lesson 12 adds a sleb helper for bigger constants.

This module stores two strings at fixed addresses, exports its memory, and exports four tiny functions that return each string's pointer and byte length.

Node.js Strings from data segments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The accented string has a JavaScript length of 10 (the emoji counts as two UTF-16 units) but 15 UTF-8 bytes. Always pass byte lengths between JavaScript and Wasm, never string.length. When the length is wrong and cuts through a multi-byte character, TextDecoder shows the replacement character instead of failing.

Strings: pointer and length, or null-terminated

Two conventions exist, and the compiler decides which one you meet.

Style Used by How the end is found
Pointer + length Rust (&str), Go, Zig slices, wasm-bindgen A separate length value
Null-terminated C and C++ (char *) A 0 byte after the last character

With C strings you need a strlen to learn the size. With pointer and length, a string can contain zero bytes and no scan is needed. Either way, the module's memory only holds bytes: JavaScript is responsible for encoding on the way in and decoding on the way out.

Passive segments and bulk memory

An active segment is applied once, at instantiation. A passive segment (flag 0x01) is not copied anywhere automatically. It sits in the module until code runs memory.init to copy part of it into memory, which can be done many times and at any address. When you are finished, data.drop releases the segment, and any later memory.init of it traps. This is how large blocks of data are loaded lazily and then freed.

Instruction Binary Stack inputs
memory.init $seg 0xFC 0x08 seg 0x00 destination, offset in segment, length
data.drop $seg 0xFC 0x09 seg none

A module that uses memory.init must also include a Data Count section (id 12), which is just the number of segments as one number, placed before the Code section. This lets the engine validate code without having seen the data yet.

Node.js A passive segment, memory.init and data.drop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The first print shows a run of zero bytes (rendered as \u0000), since nothing has been copied yet. Both loads work because a passive segment can be used repeatedly; after release() the same call traps.

Putting text into memory from JavaScript

For text that is not known when the module is built (user input, a file's contents), JavaScript has to place it into memory itself. The recipe has three steps, which later lessons turn into a reusable helper:

  1. Encode the string with TextEncoder to find its byte length.
  2. Ask the module for that many bytes (an allocation function) and receive a pointer.
  3. Copy the bytes in with Uint8Array.set or encodeInto, then call the Wasm function with the pointer and length.

Passing Strings and Arrays builds this with a real allocator. Remember that the allocation can grow memory, so create the typed array after the allocation, never before.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does an active data segment do?

    Active segments name a memory and an offset and are applied automatically at instantiation. Passive ones wait for memory.init.
  2. 2. Why should you pass the UTF-8 byte length rather than string.length?

    string.length counts UTF-16 code units. A string's size in memory is its UTF-8 byte count, which can be larger or smaller.
  3. 3. What happens when memory.init uses a segment that was dropped?

    After data.drop the segment has length zero, so copying a non-empty range traps as out of bounds.
  4. 4. Which section must be present when code uses memory.init?

    The Data Count section tells the engine how many data segments exist so code can be validated before the Data section.

Summary

  • Strings are bytes in memory, usually UTF-8, referenced by a pointer and a length (or a terminating zero in C).
  • Active data segments initialize memory at instantiation; passive segments are copied with memory.init and released with data.drop.
  • JavaScript uses TextEncoder and TextDecoder to convert; always use byte lengths.
  • memory.init requires the Data Count section.
  • A wrong length can split a multi-byte character.

Next: Tables and Indirect Calls