Mix.install([
{:jason, "~> 1.0"},
{:orb, "~> 0.0.48"},
{: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, [])