Elixir - Wasmtime

## 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

# Using WebAssembly from Elixir

Wasmtime is available on Hex and can be used programmatically to interact with Wasm modules. This guide will go over installing the wasmex package and running a simple Wasm module from Elixir.

## Getting started and simple example

First, copy this example WebAssembly text module into the current directory. It exports a function for calculating the greatest common denominator of two numbers.

    (module
      (func $gcd (param i32 i32) (result i32)
        (local i32)
        block  ;; label = @1
          block  ;; label = @2
            local.get 0
            br_if 0 (;@2;)
            local.get 1
            local.set 2
            br 1 (;@1;)
          end
          loop  ;; label = @2
            local.get 1
            local.get 0
            local.tee 2
            i32.rem_u
            local.set 0
            local.get 2
            local.set 1
            local.get 0
            br_if 0 (;@2;)
          end
        end
        local.get 2
      )
      (export "gcd" (func $gcd))
    )

The library has a Rust-based native extension, but thanks to `rustler_precompiled`, you should not have to compile anything. It'll just work!

This WAT file can be executed in `iex`:

    Mix.install([:wasmex])
    bytes = File.read!("gcd.wat")
    {:ok, pid} = Wasmex.start_link(%{bytes: bytes}) # starts a GenServer running a WASM instance
    Wasmex.call_function(pid, "gcd", [27, 6])

The last command should output:

    iex(5)> Wasmex.call_function(pid, "gcd", [27, 6])
    {:ok, [3]}

If this is the output you see, congrats! You've successfully ran your first WebAssembly code in Elixir!

## More examples and contributing

To learn more, check out an another example and the API documentation. If you have any questions, do not hesitate to open an issue on the GitHub repository.