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 13 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test/wasm_source/target/*
test/wasm_link_test/target/*
test/wasm_link_dep_test/target/*
test/wasm_link_import_test/target/*
test/support/todo_list/target/*

.mix_tasks
**/.DS_Store
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
erlang 27.0
elixir 1.17.0-otp-27
rust 1.81.0
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./native/wasmex/Cargo.toml"
]
}
tessi marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions lib/wasmex/component.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Wasmex.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
30 changes: 30 additions & 0 deletions lib/wasmex/component_instance.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Wasmex.Component.Instance 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) 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__(resource)}
end
end
end
26 changes: 26 additions & 0 deletions lib/wasmex/component_store.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Wasmex.ComponentStore do
alias Wasmex.Engine
alias Wasmex.Wasi.WasiOptions

defstruct resource: nil, reference: nil

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

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

case Wasmex.Native.store_new_wasi_p2(
tessi marked this conversation as resolved.
Show resolved Hide resolved
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
31 changes: 14 additions & 17 deletions lib/wasmex/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,8 @@ defmodule Wasmex.Native do
version = mix_config[:version]
github_url = mix_config[:package][:links]["GitHub"]

use RustlerPrecompiled,
otp_app: :wasmex,
base_url: "#{github_url}/releases/download/v#{version}",
version: version,
targets: ~w(
aarch64-apple-darwin
aarch64-unknown-linux-gnu
aarch64-unknown-linux-musl
riscv64gc-unknown-linux-gnu
x86_64-apple-darwin
x86_64-pc-windows-gnu
x86_64-pc-windows-msvc
x86_64-unknown-freebsd
x86_64-unknown-linux-gnu
x86_64-unknown-linux-musl
),
force_build: System.get_env("WASMEX_BUILD") in ["1", "true"]
use Rustler,
otp_app: :wasmex

def engine_new(_engine_config), do: error()
def engine_precompile_module(_engine_resource, _bytes), do: error()
Expand Down Expand Up @@ -80,10 +65,22 @@ 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 exec_func(_store, _instance, _function_name), do: error()

def todo_instantiate(_store, _component), do: error()
def todo_init(_store, _instance), do: error()
def todo_add(_store, _instance, _todo, _list), 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
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/support"]
defp elixirc_paths(_), do: ["lib"]

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