TExcelTAD
Home › WebAssembly › Your First Module

Your First Module

Let's build a module step by step and learn the playground conventions along the way.

The smallest valid module

(module) on its own is a valid WebAssembly module: it has no functions and does nothing. Its binary is just the 8-byte header. A module that does something needs at least one function, and to be callable from outside, that function must be exported.

wat · try it yourself
(module
  (func (export "main") (result i32)
    i32.const 2026))
2026

Reading it from the inside out:

  • (func …) declares a function.
  • (export "main") inline-exports it under the name main.
  • (result i32) says the function returns one 32-bit integer.
  • i32.const 2026 pushes the constant 2026 onto the stack. When the function ends, whatever is on the stack is its result.

The playground calls the exported main (or _start) after instantiating the module and prints what it returns.

Printing with an import

Return values are handy, but to print several things we import the host's console.log. Imports must come before any functions, memories, tables or globals that the module defines itself.

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))

  (func (export "main")
    (call $log (i32.const 10))
    (call $log (i32.const 20))))
10
20

The host's log accepts any number of numeric parameters and prints them separated by spaces. You choose the signature in the import, so the same host function can be imported more than once with different types:

wat · try it yourself
(module
  (import "console" "log" (func $log1 (param i32)))
  (import "console" "log" (func $log2 (param i32 i32)))
  (import "console" "log" (func $logf (param f64)))

  (func (export "main")
    (call $log1 (i32.const 7))
    (call $log2 (i32.const 3) (i32.const 4))
    (call $logf (f64.const 3.25))))
7
3 4
3.25

Printing text

WebAssembly has no string type. A string is just bytes in linear memory. The playground's console.str import takes a pointer (address) and a length and prints those bytes as UTF-8. A data segment puts the bytes into memory when the module is instantiated:

wat · try it yourself
(module
  (import "console" "str" (func $print (param i32 i32)))
  (memory (export "memory") 1)
  (data (i32.const 0) "Hello, WebAssembly!")

  (func (export "main")
    (call $print (i32.const 0) (i32.const 19))))
Hello, WebAssembly!

The string occupies addresses 0 to 18 (19 bytes). console.str reads from the module's exported memory. You'll learn much more about this in Data Segments & Strings.

A start function

A module can also name a start function: it runs automatically during instantiation, before the host can call any export. It must take no parameters and return nothing.

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (func $init (call $log (i32.const 1)))
  (start $init)
  (func (export "main") (call $log (i32.const 2))))
1
2

1 is printed during instantiation, 2 when the playground calls main. Toolchains use the start function (or an exported _initialize) for setup work such as constructing global objects.

Common beginner mistakes

Mistake What you'll see
Forgetting (export "main") Nothing runs: the host can't see the function
Declaring an import after a func error: imports must occur before all non-import definitions
Leaving the wrong number of values on the stack error: type mismatch at end of function, expected [] but got [i32]
Calling $log with an i64 when the import says i32 a type mismatch error at validation

Try the third one now: this main promises no result but leaves a value behind.

wat · try it yourself
(module
  (func (export "main")
    i32.const 1))
main.wat:3:5: error: type mismatch at end of function, expected [] but got [i32]
    i32.const 1))
    ^^^^^^^^^

Test yourself

What does (func (export "main") (result i32) i32.const 5) return?

  1. Nothing
  2. 5
  3. An error: return is missing
  4. The address 5

Where must imports appear in a module?

  1. Anywhere
  2. After all functions
  3. Before any functions, memories, tables or globals the module defines
  4. Only inside the start function

Which signature is allowed for a start function?

  1. Any signature
  2. No parameters and no results
  3. One i32 parameter
  4. It must return i32