WebAssembly has no string type. A Wasm function can only take and return numbers, so a string crosses the boundary as two numbers: a pointer (where the text starts in the module's linear memory) and a length (how many bytes or code units it has). To pass a string to Wasm, the caller encodes the text, usually as UTF-8, copies the bytes into the module's memory, and calls the function with the pointer and the length. Every toolchain does a version of this. The details that differ are the encoding, who allocates the memory, and who frees it.
Those details are where most string bugs come from. I build Clean Language, which compiles to WebAssembly, so I had to choose an answer for each one. Below is how the boundary works in general, how the main toolchains handle it, what is changing in the standard, and then exactly what Clean does, including where its choices are weaker.
Why WebAssembly has no string type
The WebAssembly specification defines four number types: i32, i64, f32 and f64, plus one 128-bit vector type and reference types (WebAssembly spec, Types). There is no string among them.
This is on purpose. WebAssembly is a compile target for many languages, and those languages disagree on what a string is. Rust strings are UTF-8 bytes. JavaScript strings are, in MDN's words, "sequences of UTF-16 code units" (MDN, String). Java, C# and Dart also use 16-bit code units. C strings end with a zero byte. A single built-in string type would be wrong for most of them, so the core standard leaves strings to each language.
What a module does have is linear memory: one big array of bytes that both the module and its host can read and write. A string lives there as bytes, and a number points at it.
How a string crosses the boundary
Here is the usual path when JavaScript calls a Wasm function with the text "café":
JavaScript string "café" (UTF-16, .length is 4)
|
| 1. encode to UTF-8 -> 63 61 66 C3 A9 (5 bytes)
| 2. ask the module for 5 bytes -> malloc(5) returns ptr
| 3. copy the bytes into memory.buffer at ptr
v
wasm_function(ptr, 5) (two i32 numbers, nothing else)
Going the other way, the module returns a pointer (and a length, or a length stored next to the bytes), and JavaScript reads the bytes out of memory.buffer and decodes them with TextDecoder.
UTF-8 versus UTF-16: the length is not the length
The encoding matters because the "length" changes with it:
| Text | JavaScript .length (UTF-16 code units) | UTF-8 bytes |
|---|---|---|
"cafe" | 4 | 4 |
"café" | 4 | 5 |
"😄" | 2 | 4 |
For plain English text the numbers match, which is why a length bug can survive testing and break on the first accented name. MDN's page on TextEncoder.encodeInto() gives the safe rule: converting a JavaScript string s to UTF-8 needs "never less than s.length bytes and never greater than s.length * 3 bytes" (MDN, encodeInto). So you allocate for the worst case, encode, and then pass the number of bytes actually written, not s.length.
Who allocates, and who frees
JavaScript cannot create memory inside the module on its own terms; it has to ask. So the module exports an allocation function, often called malloc, and the caller uses it to reserve space before copying. The harder question is the end of the string's life. Someone must free those bytes, and both sides must agree on when. If the module keeps the pointer after the caller frees it, the module reads garbage. If nobody frees it, memory grows with every call.
How the main toolchains handle it
Rust with wasm-bindgen
wasm-bindgen generates the glue for you. For a &str parameter, the docs say it "copies the string's contents back and forth between the JavaScript garbage-collected heap and the Wasm linear memory with TextDecoder and TextEncoder" (wasm-bindgen guide, str). The same page warns about one edge case: a JavaScript string can hold an unpaired surrogate, which is not valid Unicode, and the encoder replaces it with U+FFFD. The text Rust receives can be slightly different from what JavaScript sent.
AssemblyScript
AssemblyScript keeps strings in JavaScript's format. Its runtime docs say strings use "16-bit char codes (UTF-16 code units, allowing isolated surrogates like JS)" with a header before the data, and that between Wasm and the host "it is not its data that is passed but a pointer to the string's data in linear memory" (AssemblyScript runtime). Because AssemblyScript has a garbage collector, a host holding a string pointer must __pin it so it is not collected, and __unpin it later. The generated bindings do this for you.
C and C++ with Emscripten
Emscripten follows C: strings are UTF-8 and end with a zero byte. UTF8ToString reads a null-terminated string out of memory, and stringToUTF8 copies a JavaScript string in. When you call C through ccall with a string argument, Emscripten puts it on the stack, and the docs are clear that it "is only 'alive' until the call is complete" (Emscripten preamble.js). If C code keeps that pointer, it later points at invalid data.
What is changing: components, WIT and JavaScript strings
Three efforts try to make strings less manual.
The Component Model and WIT. WIT, the interface language of the Component Model, has a real string type, defined as "a Unicode string: that is, a finite sequence of characters" (Component Model book, WIT). Under the hood, the Canonical ABI still passes a pointer and a length, but each component declares its encoding: UTF-8, UTF-16 or a mixed latin1+utf16, and the runtime transcodes between components, using a realloc function the receiving component exports (Canonical ABI). The pointer and length do not go away; you just stop writing that code by hand. The Component Model is still listed as a phase 1 proposal for core WebAssembly (WebAssembly proposals), though it is already used outside the browser.
JS string builtins. This one is finished. It shipped as part of WebAssembly 3.0, whose announcement explains that JavaScript strings "can already be passed to Wasm as externrefs" and that the new builtins let a module "directly access and manipulate such external string values inside Wasm" (Wasm 3.0 announcement). The functions are imported from a reserved wasm:js-string namespace and include length, concat, substring and equals (MDN, JavaScript builtins). No copying into linear memory at all, but it only helps in a JavaScript host, and it fits best for languages that use Wasm garbage collection.
stringref. This proposal would add a real string reference type to WebAssembly, with goals such as "zero-copy passing of strings from JavaScript to WebAssembly & back" (stringref overview). It is still at phase 1, so do not plan around it.
Common bugs when you pass a string to Wasm
- Wrong length. Passing JavaScript's
s.lengthas the byte length. It works for "cafe" and cuts "café" short by one byte, which may also leave invalid UTF-8. - Empty strings. An empty string may arrive as pointer 0 with length 0, or any pointer with length 0. Code that treats pointer 0 as "error" or "no value", or allocates 0 bytes and gets 0 back as a failure signal, breaks on the most ordinary input there is.
- Freed or moved memory. Keeping a pointer to a temporary string, like the Emscripten
ccallcase above. A quieter version lives on the JavaScript side: MDN notes that every call tomemory.growdetaches the oldbuffer, so aUint8Arrayyou created earlier becomes empty (MDN, Memory.grow). If the allocation you just made grew memory, recreate the view before you copy. - Mixed conventions. One function expects a pointer and a length, another expects a pointer to a length-prefixed block. Pass the wrong shape and the host reads an address as a length.
- Zero bytes inside text. With null-terminated strings, any zero byte in the data ends the string early.
How Clean handles strings
When I designed Clean's memory model, I chose one layout for every string and wrote it into the platform spec. A Clean string in memory is a 4-byte length followed by the UTF-8 bytes. It is length-prefixed and not null-terminated. Here is "café" in a compiled module:
address bytes meaning
p + 0 05 00 00 00 length = 5 (i32, little-endian)
p + 4 63 61 66 C3 A9 "c" "a" "f" "é" (é is 2 bytes in UTF-8)
A string value is just the address p. String literals sit in a pool starting at address 4096, each on a 4-byte boundary. Address 0 is never used for data and stays zero, so a null pointer reads as a length-0 string. So Clean code never has to treat pointer 0 as a special case.
At the boundary with the host (the Clean server, or the small loader that Frame ships for the browser), the rules are:
- Strings going out are passed as a
(ptr, len)pair ofi32s, whereptrpoints at the bytes, not the prefix. The compiler generates that unpacking.print, for example, is imported as a function taking(i32, i32). - Strings coming back from the host are one
i32: a pointer to a new length-prefixed block. - The host allocates by calling the module's exported
malloc, then writes the 4-byte length and the UTF-8 bytes.
Freeing is where Clean is simplest, and also least flexible. Allocation is a bump allocator: each malloc moves a heap pointer forward, aligned to 8 bytes. The module exports free, but it does nothing. Memory is reclaimed by scope instead: the host calls scope_push at the start of a unit of work, such as a web request, and scope_pop at the end, which moves the heap pointer back and releases everything from that request at once. There is no garbage collector and no reference counting.
Here is what this looks like from the language side. Both snippets pass the Clean type checker:
start:
string word = "café"
string empty = ""
print("bytes in word: {word.length()}")
print("bytes in empty: {empty.length()}")
This prints bytes in word: 5 and bytes in empty: 0. .length() counts UTF-8 bytes, the same number stored in the prefix.
start:
print(greet("Ana"))
functions:
string greet(string name)
return "Hello, " + name
You never see a pointer here. The compiler builds the length-prefixed string for "Hello, " + name, and when print runs, it hands the host the pointer to the bytes and the length.
Where Clean's approach is weaker
- Length counts bytes, not characters. "café" has length 5, and iterating a string gives one piece per byte. That is predictable and matches the memory, but it surprises people who expect 4, and it is not what JavaScript reports.
- No freeing of a single string. In a long loop that builds many strings, memory only comes back at the end of the scope. Clean has a small reserved area for temporary strings inside loops, but a program that keeps building large strings without a scope boundary will use more memory than a garbage-collected language.
- Two conventions inside the host bridge. Most host functions take
(ptr, len), but a few string helpers, such as concatenation, take a pointer to the whole length-prefixed block. That is exactly the "mixed conventions" bug listed above, and anyone writing a new Clean host has to get it right. - No Component Model or JS string builtins. Clean modules talk to Clean's own hosts. They do not export WIT interfaces, so you cannot plug them into a component runtime the way you can with Rust.
Clean is also in Beta, built by a solo developer, and a new compiler with different syntax is in development. If you need strings that move cleanly between many Wasm modules and languages today, Rust with the Component Model tooling is the stronger choice.
Next step
If you are new to WebAssembly, start with You don't need Rust or JavaScript to start with WebAssembly. To try Clean and see these strings in a real program, follow the Get Started guide.
Sources
- WebAssembly Core Specification: Types, WebAssembly Community Group, 2026
- String, MDN Web Docs, Mozilla, 2026
- TextEncoder: encodeInto() method, MDN Web Docs, Mozilla, 2026
- WebAssembly.Memory.prototype.grow(), MDN Web Docs, Mozilla, 2026
- str, The wasm-bindgen Guide, 2026
- Runtime, AssemblyScript documentation, 2026
- preamble.js, Emscripten documentation, 2026
- An Overview of WIT, The WebAssembly Component Model, Bytecode Alliance, 2026
- Canonical ABI Explainer, WebAssembly Component Model repository, 2026
- Wasm 3.0 Completed, WebAssembly Community Group, 2025
- WebAssembly JavaScript builtins, MDN Web Docs, Mozilla, 2026
- Reference-Typed Strings (stringref) overview, WebAssembly stringref repository
- WebAssembly Proposals, WebAssembly Community Group, 2026