Skip to content

Commit

Permalink
allow options to be a keyword list
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Dec 2, 2024
1 parent a8a3bc5 commit 904fd1f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/wasmex/components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ defmodule Wasmex.Components do
end
end

def start_link(opts) when is_list(opts) do
with {:ok, store} <- build_store(opts),
component_bytes <- Keyword.get(opts, :bytes),
{:ok, component} <- Wasmex.Components.Component.new(store, component_bytes) do
GenServer.start_link(__MODULE__, %{store: store, component: component}, opts)
end
end

defp build_store(opts) do
if wasi_options = Keyword.get(opts, :wasi) do
Wasmex.Components.Store.new_wasi(wasi_options)
else
Wasmex.Components.Store.new()
end
end

@spec call_function(pid(), String.t() | atom(), list(number()), pos_integer()) ::
{:ok, list(number())} | {:error, any()}
def call_function(pid, name, params, timeout \\ 5000) do
Expand Down
11 changes: 10 additions & 1 deletion test/components/components_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Wasmex.ComponentsTest do
use ExUnit.Case, async: true
alias Wasmex.Wasi.WasiP2Options

test "interacting with a component server" do
test "interacting with a component GenServer" do
component_bytes = File.read!("test/component_fixtures/component_types/component_types.wasm")
component_pid = start_supervised!({Wasmex.Components, %{bytes: component_bytes}})
assert {:ok, "mom"} = Wasmex.Components.call_function(component_pid, "id-string", ["mom"])
Expand All @@ -21,4 +21,13 @@ defmodule Wasmex.ComponentsTest do

assert time =~ Date.utc_today() |> Date.to_iso8601()
end

test "register by name" do
component_bytes = File.read!("test/component_fixtures/component_types/component_types.wasm")

{:ok, _pid} =
start_supervised({Wasmex.Components, bytes: component_bytes, name: ComponentTypes})

assert {:ok, "mom"} = Wasmex.Components.call_function(ComponentTypes, "id-string", ["mom"])
end
end

0 comments on commit 904fd1f

Please sign in to comment.