diff --git a/lib/wasmex/components.ex b/lib/wasmex/components.ex index 518c58a..3433f17 100644 --- a/lib/wasmex/components.ex +++ b/lib/wasmex/components.ex @@ -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 diff --git a/test/components/components_test.exs b/test/components/components_test.exs index db87d82..7063df1 100644 --- a/test/components/components_test.exs +++ b/test/components/components_test.exs @@ -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"]) @@ -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