diff --git a/lib/wasmex/components/component.ex b/lib/wasmex/components/component.ex index 39361b9..77c2fa5 100644 --- a/lib/wasmex/components/component.ex +++ b/lib/wasmex/components/component.ex @@ -33,7 +33,7 @@ defmodule Wasmex.Components.Component do end defmacro __using__(opts) do - genserver_stuff = + genserver_setup = quote do use GenServer @@ -65,6 +65,6 @@ defmodule Wasmex.Components.Component do [] end - [genserver_stuff, functions] + [genserver_setup, functions] end end diff --git a/lib/wasmex/native.ex b/lib/wasmex/native.ex index b287d6f..da171c2 100644 --- a/lib/wasmex/native.ex +++ b/lib/wasmex/native.ex @@ -93,7 +93,7 @@ defmodule Wasmex.Native do def component_call_function(_store, _instance, _function_name, _params), do: error() - def wit_exported_functions(path, wit), do: error() + def wit_exported_functions(_path, _wit), 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. diff --git a/native/wasmex/src/wit.rs b/native/wasmex/src/wit.rs index 4183eb0..a1e2457 100644 --- a/native/wasmex/src/wit.rs +++ b/native/wasmex/src/wit.rs @@ -4,8 +4,10 @@ use wit_parser::{Resolve, WorldItem}; #[rustler::nif(name = "wit_exported_functions")] pub fn exported_functions(env: rustler::Env, path: String, wit: String) -> NifResult { let mut resolve = Resolve::new(); - let id = resolve.push_str(path, &wit).unwrap(); - let world_id = resolve.select_world(id, None).unwrap(); + let id = resolve.push_str(path, &wit) + .map_err(|e| rustler::Error::Term(Box::new(format!("Failed to parse WIT: {}", e))))?; + let world_id = resolve.select_world(id, None) + .map_err(|e| rustler::Error::Term(Box::new(format!("Failed to select world: {}", e))))?; let exports = &resolve.worlds[world_id].exports; let exported_functions = exports .iter() diff --git a/test/components/wit_parser_test.exs b/test/components/wit_parser_test.exs index c391804..85d944c 100644 --- a/test/components/wit_parser_test.exs +++ b/test/components/wit_parser_test.exs @@ -1,10 +1,19 @@ defmodule Components.WitParserTest do use ExUnit.Case - test "exported_functions" do - wit = File.read!("test/component_fixtures/hello_world/hello-world.wit") + describe "exports" do + test "exported_functions" do + wit = File.read!("test/component_fixtures/hello_world/hello-world.wit") - assert %{"greet" => 1, "greet-many" => 1, "multi-greet" => 2} = - Wasmex.Native.wit_exported_functions("hello-world.wit", wit) + assert %{"greet" => 1, "greet-many" => 1, "multi-greet" => 2} = + Wasmex.Native.wit_exported_functions("hello-world.wit", wit) + end + + test "wit parse errors" do + wit = "goo goo" + assert {:error, error} = Wasmex.Native.wit_exported_functions("hello-world.wit", wit) + assert error =~ "Failed to parse WIT" + end end + end