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

Features/wasm components #630

Merged
merged 45 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
807f313
WIP
Sep 10, 2024
4cfdb5a
added test
Sep 10, 2024
a993e65
test passes
Sep 10, 2024
f26b4e5
pass a store instead of an engine
Sep 10, 2024
2a7ae28
WIP tests pass for passing component
Sep 10, 2024
34a37e3
i can haz instance ref
Sep 10, 2024
6be5bc6
oops
Sep 10, 2024
481560b
make stuff public
Sep 16, 2024
58566c5
WIP failing test
Sep 17, 2024
b771ea7
fix local wasmex path
willus10245 Sep 17, 2024
154a0a1
got a bindgen test to pass
Sep 19, 2024
c88af96
WIP we have a component making http calls yall
Oct 1, 2024
a45f802
more stuffs
Oct 2, 2024
c07761b
exec fun works again
Oct 18, 2024
d8e26f7
passing test for dynamic invocation of component function
Nov 11, 2024
8e5d484
slightly better interface
Nov 11, 2024
94fa630
woot converting lists
Nov 11, 2024
6caad86
code is a little gross, but maps and lists are working
Nov 12, 2024
a2b551c
some refactorings to gradually clean things up
Nov 16, 2024
a657d6d
steal component_types.wasm from wasmtime-rb
Nov 16, 2024
274b8a4
tuple is working
Nov 17, 2024
ed1c972
more cleanup
Nov 17, 2024
bbe99c4
refactor test, add floats
Nov 17, 2024
a5853f3
Merge remote-tracking branch 'upstream/main' into features/wasm-compo…
Nov 17, 2024
b5c3913
rename exec_func
Nov 17, 2024
d3fe26e
remove dev junk
Nov 17, 2024
9a64bf8
beginning error handling
Nov 17, 2024
6fe2c8b
better error handling
Nov 18, 2024
e858dad
move things around, sketch out the beginning of genserver
Nov 23, 2024
86d0ab7
handle errors correctly
Nov 23, 2024
7a71235
wasi is optional for components, turns out :)
Nov 26, 2024
b2d4109
oops
Nov 26, 2024
dc158e7
4 matt
Nov 26, 2024
709e05d
rust 4matt
Nov 26, 2024
f05e1ab
refactor tests a bit
Nov 26, 2024
35fc5f1
clean up some garbage
Nov 27, 2024
0a84175
more cleanup
Nov 27, 2024
de5616c
consistency is good
Nov 27, 2024
0849dd7
support option type
Nov 29, 2024
d516cce
format rust
Nov 30, 2024
7374c02
fix the rust lint issues
Nov 30, 2024
807ae94
missed one
Nov 30, 2024
0579e3f
just enough docs to make credo happy. Definitely needs improvment
Nov 30, 2024
44e830f
fix test failure, need to decide on specific errors later
Nov 30, 2024
4acd201
fix some warnings and tests
Nov 30, 2024
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
39 changes: 39 additions & 0 deletions lib/wasmex/components.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Wasmex.Components do
tessi marked this conversation as resolved.
Show resolved Hide resolved
use GenServer

def start_link(%{bytes: component_bytes}) do
with {:ok, store} <- Wasmex.Components.Store.new(%Wasmex.Wasi.WasiP2Options{}),
{:ok, component} <- Wasmex.Components.Component.new(store, component_bytes) do
GenServer.start_link(__MODULE__, %{store: store, component: component})
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
GenServer.call(pid, {:call_function, stringify(name), params}, timeout)
end

@impl true
def init(%{store: store, component: component} = state) do
case Wasmex.Components.Instance.new(store, component) do
{:ok, instance} -> {:ok, Map.merge(state, %{instance: instance})}
{:error, reason} -> {:error, reason}
end
end

@impl true
def handle_call(
{:call_function, name, params},
_from,
%{instance: instance} = state
) do
case Wasmex.Components.Instance.call_function(instance, name, params) do
{:ok, result} -> {:reply, {:ok, result}, state}
{:error, error} -> {:error, error}
end
end

defp stringify(s) when is_binary(s), do: s
defp stringify(s) when is_atom(s), do: Atom.to_string(s)
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Wasmex.Component do
defmodule Wasmex.Components.Component do
@type t :: %__MODULE__{
resource: binary(),
reference: reference()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Wasmex.Component.Instance do
defmodule Wasmex.Components.Instance do
defstruct store_resource: nil,
instance_resource: nil,
# The actual NIF store resource.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Wasmex.ComponentStore do
defmodule Wasmex.Components.Store do
alias Wasmex.Wasi.WasiP2Options
alias Wasmex.Engine

Expand Down
2 changes: 1 addition & 1 deletion lib/wasmex/wasi/wasi_p2_options.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Wasmex.Wasi.WasiP2Options do
@moduledoc ~S"""
Configures WASI P2 support for a Wasmex.ComponentStore.
Configures WASI P2 support for a Wasmex.Components.Store.

"""

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Wasmex.MixProject do
end

# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(:test), do: ["lib", "test/component_fixtures"]
defp elixirc_paths(_), do: ["lib"]

defp package() do
Expand Down
1 change: 0 additions & 1 deletion native/wasmex/src/component_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ fn val_to_term<'a>(val: &Val, env: rustler::Env<'a>) -> Term<'a> {
.map(|(key, val)| (key, val_to_term(val, env)))
.collect::<Vec<(&String, Term<'a>)>>();
Term::map_from_pairs(env, converted_pairs.as_slice()).unwrap()
// String::from("wut").encode(env)
}
Val::Tuple(tuple) => {
let tuple_terms = tuple
Expand Down
66 changes: 66 additions & 0 deletions test/components/component_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule Wasmex.WasmComponentsTest do
use ExUnit.Case, async: true

alias Wasmex.Engine
alias Wasmex.EngineConfig

test "invoke component func" do
{:ok, store} = Wasmex.Components.Store.new(%Wasmex.Wasi.WasiP2Options{})
component_bytes = File.read!("test/component_fixtures/hello_world/hello_world.wasm")
{:ok, component} = Wasmex.Components.Component.new(store, component_bytes)
IO.inspect("building instance")
{:ok, instance} = Wasmex.Components.Instance.new(store, component)
IO.inspect("executing component function")

assert {:ok, "Hello, Elixir!"} =
Wasmex.Components.Instance.call_function(instance, "greet", ["Elixir"])

assert {:ok, ["Hello, Elixir!", "Hello, Elixir!"]} =
Wasmex.Components.Instance.call_function(instance, "multi-greet", ["Elixir", 2])
end

test "functions with maps and lists" do
{:ok, store} = Wasmex.Components.Store.new(%Wasmex.Wasi.WasiP2Options{inherit_stdout: true})

component_bytes = File.read!("test/component_fixtures/live_state/live-state.wasm")
{:ok, component} = Wasmex.Components.Component.new(store, component_bytes)
{:ok, instance} = Wasmex.Components.Instance.new(store, component)

assert {:ok, %{"customers" => [customer]} = state} =
Wasmex.Components.Instance.call_function(instance, "init", [])

assert customer["email"] == "[email protected]"

customer2 = Map.put(customer, "last-name", "Smith")

assert {:ok, shown} =
Wasmex.Components.Instance.call_function(instance, "show-customer", [customer2])

assert {:ok, %{"customers" => customers} = state} =
Wasmex.Components.Instance.call_function(instance, "add-customer", [customer2, state])

assert Enum.count(customers) == 2
end

describe "error handling" do
setup do
{:ok, store} = Wasmex.Components.Store.new(%Wasmex.Wasi.WasiP2Options{inherit_stdout: true})

component_bytes = File.read!("test/component_fixtures/hello_world/hello_world.wasm")
{:ok, component} = Wasmex.Components.Component.new(store, component_bytes)
{:ok, instance} = Wasmex.Components.Instance.new(store, component)
%{instance: instance}
end

test "function not exported", %{instance: instance} do

assert {:error, error} = Wasmex.Components.Instance.call_function(instance, "garbage", [:wut])
assert error =~ "garbage not exported"
end

test "invalid arguments", %{instance: instance} do
assert {:error, error} = Wasmex.Components.Instance.call_function(instance, "greet", [1])
assert error =~ "type mismatch"
end
end
end
53 changes: 53 additions & 0 deletions test/components/component_types_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Wasm.Components.ComponentTypesTest do
use ExUnit.Case, async: true

alias Wasmex.Engine
alias Wasmex.EngineConfig

setup do
{:ok, store} = Wasmex.Components.Store.new(%Wasmex.Wasi.WasiP2Options{})
component_bytes = File.read!("test/component_fixtures/component_types/component_types.wasm")
{:ok, component} = Wasmex.Components.Component.new(store, component_bytes)
{:ok, instance} = Wasmex.Components.Instance.new(store, component)
[instance: instance]
end

test "strings", %{instance: instance} do
assert {:ok, "mom"} = Wasmex.Components.Instance.call_function(instance, "id-string", ["mom"])
end

test "boolean", %{instance: instance} do
assert {:ok, true} = Wasmex.Components.Instance.call_function(instance, "id-bool", [true])
end

test "integers", %{instance: instance} do
# all the integer types
for type <- ["u8", "u16", "u32", "u64", "s8", "s16", "s32", "s64"] do
assert {:ok, 7} = Wasmex.Components.Instance.call_function(instance, "id-#{type}", [7])
end
end

test "floats", %{instance: instance} do
pi = 3.14592
assert {:ok, pi} = Wasmex.Components.Instance.call_function(instance, "id-f32", [pi])
assert {:ok, pi} = Wasmex.Components.Instance.call_function(instance, "id-f64", [pi])
end

test "records", %{instance: instance} do
# don't love this yet, be nicer to support atom keys
assert {:ok, %{"x" => 1, "y" => 2}} =
Wasmex.Components.Instance.call_function(instance, "id-record", [
%{"x" => 1, "y" => 2}
])
end

test "lists", %{instance: instance} do
assert {:ok, [1, 2, 3]} =
Wasmex.Components.Instance.call_function(instance, "id-list", [[1, 2, 3]])
end

test "tuples", %{instance: instance} do
assert {:ok, {1, "two"}} =
Wasmex.Components.Instance.call_function(instance, "id-tuple", [{1, "two"}])
end
end
9 changes: 9 additions & 0 deletions test/components/components_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Wasmex.ComponentsTest do
use ExUnit.Case, async: true

test "interacting with a component server" do
component_bytes = File.read!("test/component_fixtures/component_types/component_types.wasm")
component = start_supervised!({Wasmex.Components, %{bytes: component_bytes}})
assert {:ok, "mom"} = Wasmex.Components.call_function(component, "id-string", ["mom"])
end
end
53 changes: 0 additions & 53 deletions test/wasm_component_types_test.exs

This file was deleted.

66 changes: 0 additions & 66 deletions test/wasm_components_test.exs

This file was deleted.