Skip to content

Commit

Permalink
error instead of panic on wit parse failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Dec 17, 2024
1 parent 277ab71 commit c248d38
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/wasmex/components/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Wasmex.Components.Component do
end

defmacro __using__(opts) do
genserver_stuff =
genserver_setup =
quote do
use GenServer

Expand Down Expand Up @@ -65,6 +65,6 @@ defmodule Wasmex.Components.Component do
[]
end

[genserver_stuff, functions]
[genserver_setup, functions]
end
end
2 changes: 1 addition & 1 deletion lib/wasmex/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions native/wasmex/src/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Term> {
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()
Expand Down
17 changes: 13 additions & 4 deletions test/components/wit_parser_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c248d38

Please sign in to comment.