Skip to content

Latest commit

 

History

History
43 lines (35 loc) · 771 Bytes

01-intro.livemd

File metadata and controls

43 lines (35 loc) · 771 Bytes

Introduction to Orb

Mix.install([
  {:jason, "~> 1.0"},
  :orb,
  {:wasmex, "~> 0.8.3"}
])

Define WebAssembly module using Orb

defmodule CalculateMean do
  use Orb

  global do
    @tally 0
    @count 0
  end

  defw insert(n: I32) do
    @tally = @tally + n
    @count = @count + 1
  end

  defw calculate_mean(), I32 do
    @tally / @count
  end
end

Run WebAssembly module using Wasmex

{:ok, pid} = Wasmex.start_link(%{bytes: Orb.to_wat(CalculateMean)})
Wasmex.call_function(pid, :insert, [3])
Wasmex.call_function(pid, :insert, [4])
Wasmex.call_function(pid, :insert, [5])
Wasmex.call_function(pid, :insert, [6])
Wasmex.call_function(pid, :insert, [7])
{:ok, [5]} = Wasmex.call_function(pid, :calculate_mean, [])