Code Examples

Practical WebAssembly implementations and common use cases in action.

Core Examples

Simple Arithmetic

(module
  (func $add (param $a i32) (param $b i32) (result i32)
    get_local $a
    get_local $b
    i32.add)
)

Basic addition function in WebAssembly text format.

Memory Allocation

(module
  (memory $memory 1)
  (data (i32.const 0) "Hello, WebAssembly!")
)

Demonstrates linear memory and data initialization.

Advanced Examples

Complex Math

(module
  (func $factorial (param $n i32) (result i32)
    get_local $n
    i32.const 1
    i32.eq
    if (result i32)
      i32.const 1
    else
      get_local $n
      i32.const 1
      i32.sub
      call $factorial
      get_local $n
      i32.mul
    end
  )
)

Recursive factorial calculation with WebAssembly.

Host Function Integration

(module
  (import "env" "log" (func $log (param i32)))
  (func $main ()
    i32.const 42
    call $log
  )
)

Shows integration with JavaScript host functions.

Ready to Build?

Start experimenting with these code samples and bring your ideas to life.

📚 View Documentation