Sponsored by AppSignal
Would you like to see your link here? Contact us
Notesclub

Orb Introduction

guides/01-intro.livemd

Orb Introduction

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

Section

alias OrbWasmtime.Instance, as: Wasmtime
defmodule Mean do
  use Orb

  I32.global(sum: 0, count: 0)

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

  defw calculate_mean(), I32 do
    @sum / @count
  end
end
a = Wasmtime.run(Mean)
Wasmtime.call(a, :insert, 3)
Wasmtime.call(a, :insert, 4)
Wasmtime.call(a, :insert, 5)
Wasmtime.call(a, :insert, 6)
Wasmtime.call(a, :insert, 7)
Wasmtime.call(a, :calculate_mean)