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 29 commits
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
163 changes: 163 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "mix test",
"type": "shell",
"command": "mix",
"args": [
"test",
"--color"
],
"options": {
"cwd": "${workspaceRoot}",
"requireFiles": [
"test/**/test_helper.exs",
"test/**/*_test.exs"
]
},
"problemMatcher": "$mixTestFailure"
},
{
"label": "mix test failed",
"type": "shell",
"command": "mix",
"args": [
"test",
"--color",
"--failed",
"--trace"
],
"options": {
"cwd": "${workspaceRoot}",
"requireFiles": [
"test/**/test_helper.exs",
"test/**/*_test.exs"
]
},
"problemMatcher": "$mixTestFailure"
},
{
"label": "mix test file",
"type": "shell",
"command": "mix",
"args": [
"test",
"${relativeFile}",
"--color",
"--trace"
],
"options": {
"cwd": "${workspaceRoot}",
"env": {
"WASMEX_BUILD": "true"
},
"requireFiles": [
"test/**/test_helper.exs",
"test/**/*_test.exs"
]
},
"problemMatcher": "$mixTestFailure"
},
{
"label": "mix test focused",
"type": "shell",
"command": "mix",
"args": [
"test",
"${relativeFile}:${lineNumber}",
"--color",
"--trace"
],
"options": {
"cwd": "${workspaceRoot}",
"requireFiles": [
"test/**/test_helper.exs",
"test/**/*_test.exs"
],
"env": {
"WASMEX_BUILD": "true"
}
},
"problemMatcher": "$mixTestFailure",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "Initial Setup",
"type": "process",
"command": "mix",
"args": [
"setup"
],
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": [
"$mixCompileError"
]
},
{
"label": "mix compile",
"type": "process",
"command": "mix",
"args": [
"test",
"--exclude",
"test",
"--warnings-as-errors"
],
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": [
"$mixCompileWarning",
"$mixCompileError"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "mix format",
"type": "process",
"command": "mix",
"args": [
"format"
],
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": [
"$mixCompileWarning",
"$mixCompileError"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "mix check",
"type": "process",
"command": "mix",
"args": [
"check"
],
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": [
"$mixCompileWarning",
"$mixCompileError"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
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
29 changes: 29 additions & 0 deletions lib/wasmex/components/component.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Wasmex.Components.Component do
@type t :: %__MODULE__{
resource: binary(),
reference: reference()
}

defstruct resource: nil,
# The actual NIF store resource.
# Normally the compiler will happily do stuff like inlining the
# resource in attributes. This will convert the resource into an
# empty binary with no warning. This will make that harder to
# accidentally do.
reference: nil

def __wrap_resource__(resource) do
%__MODULE__{
resource: resource,
reference: make_ref()
}
end

def new(store_or_caller, component_bytes) do
%{resource: store_or_caller_resource} = store_or_caller
case Wasmex.Native.component_new(store_or_caller_resource, component_bytes) do
{:error, err} -> {:error, err}
resource -> {:ok, __wrap_resource__(resource)}
end
end
end
36 changes: 36 additions & 0 deletions lib/wasmex/components/component_instance.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Wasmex.Components.Instance do
defstruct store_resource: nil,
instance_resource: nil,
# The actual NIF store resource.
# Normally the compiler will happily do stuff like inlining the
# resource in attributes. This will convert the resource into an
# empty binary with no warning. This will make that harder to
# accidentally do.
reference: nil

def __wrap_resource__(store_resource, instance_resource) do
%__MODULE__{
store_resource: store_resource,
instance_resource: instance_resource,
reference: make_ref()
}
end

def new(store_or_caller, component) do
%{resource: store_or_caller_resource} = store_or_caller
%{resource: component_resource} = component

case Wasmex.Native.component_instance_new(store_or_caller_resource, component_resource) do
{:error, err} -> {:error, err}
resource -> {:ok, __wrap_resource__(store_or_caller_resource, resource)}
end
end

def call_function(
%__MODULE__{store_resource: store_resource, instance_resource: instance_resource},
function,
args
) do
Wasmex.Native.component_call_function(store_resource, instance_resource, function, args)
end
end
26 changes: 26 additions & 0 deletions lib/wasmex/components/component_store.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Wasmex.Components.Store do
alias Wasmex.Wasi.WasiP2Options
alias Wasmex.Engine

defstruct resource: nil, reference: nil

def __wrap_resource__(resource) do
%__MODULE__{
resource: resource,
reference: make_ref()
}
end

def new(%WasiP2Options{} = options, store_limits \\ nil, engine \\ nil) do
%Engine{resource: engine_resource} = engine || Engine.default()

case Wasmex.Native.store_new_wasi_p2(
options,
store_limits,
engine_resource
) do
{:error, err} -> {:error, err}
resource -> {:ok, __MODULE__.__wrap_resource__(resource)}
end
end
end
3 changes: 2 additions & 1 deletion lib/wasmex/engine_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Wasmex.EngineConfig do
defstruct consume_fuel: false,
cranelift_opt_level: :none,
wasm_backtrace_details: false,
memory64: false
memory64: false,
wasm_component_model: true

@type t :: %__MODULE__{
consume_fuel: boolean(),
Expand Down
7 changes: 7 additions & 0 deletions lib/wasmex/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ defmodule Wasmex.Native do

def store_new(_store_limits, _engine_resource), do: error()
def store_new_wasi(_wasi_options, _store_limits, _engine_resource), do: error()
def store_new_wasi_p2(_wasi_options, _store_limits, _engine_resource), do: error()

def store_or_caller_get_fuel(_store_or_caller_resource), do: error()
def store_or_caller_set_fuel(_store_or_caller_resource, _fuel), do: error()

def component_new(_store, _component_bytes), do: error()

def component_instance_new(_store, _component), do: error()

def component_call_function(_store, _instance, _function_name, _params), do: error()

# When the NIF is loaded, it will override functions in this module.
# Calling error is handles the case when the nif could not be loaded.
defp error, do: :erlang.nif_error(:nif_not_loaded)
Expand Down
22 changes: 22 additions & 0 deletions lib/wasmex/wasi/wasi_p2_options.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Wasmex.Wasi.WasiP2Options do
@moduledoc ~S"""
Configures WASI P2 support for a Wasmex.Components.Store.

"""

defstruct inherit_stdin: true,
inherit_stdout: true,
inherit_stderr: true,
allow_http: false,
args: [],
env: %{}

@type t :: %__MODULE__{
args: [String.t()],
env: %{String.t() => String.t()},
inherit_stdin: boolean(),
inherit_stdout: boolean(),
inherit_stdin: boolean(),
allow_http: boolean()
}
end
5 changes: 5 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Wasmex.MixProject do
version: @version,
elixir: "~> 1.12",
start_permanent: Mix.env() == :prod,
elixirc_paths: elixirc_paths(Mix.env()),
name: "wasmex",
description: description(),
package: package(),
Expand Down Expand Up @@ -37,6 +38,10 @@ defmodule Wasmex.MixProject do
"Wasmex is an Elixir library for executing WebAssembly binaries"
end

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

defp package() do
[
# These are the default files included in the package
Expand Down
Loading