Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow options to be a keyword list #673

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading