Skip to content

Commit

Permalink
Change get_global_value to return {:ok, result}
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Apr 3, 2024
1 parent 3923979 commit fbc8b06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/wasmex/instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ defmodule Wasmex.Instance do
instance_resource,
global_name
)
|> case do
{:error, _reason} = term -> term
result when is_number(result) -> {:ok, result}
end
end

@doc ~S"""
Expand Down
20 changes: 13 additions & 7 deletions test/wasmex/instance_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ defmodule Wasmex.InstanceTest do
assert {:error, "exported global `unknown_global` not found"} =
Wasmex.Instance.get_global_value(store, instance, "unknown_global")

assert 42 = Wasmex.Instance.get_global_value(store, instance, "meaning_of_life")
assert -32 = Wasmex.Instance.get_global_value(store, instance, "count_32")
assert -64 = Wasmex.Instance.get_global_value(store, instance, "count_64")
assert {:ok, 42} = Wasmex.Instance.get_global_value(store, instance, "meaning_of_life")
assert {:ok, -32} = Wasmex.Instance.get_global_value(store, instance, "count_32")
assert {:ok, -64} = Wasmex.Instance.get_global_value(store, instance, "count_64")
end

test t(&Wasmex.Instance.set_global_value/4), context do
Expand All @@ -228,16 +228,22 @@ defmodule Wasmex.InstanceTest do
Wasmex.Instance.set_global_value(store, instance, "meaning_of_life", 0)

assert :ok = Wasmex.Instance.set_global_value(store, instance, "count_32", 99)
assert 99 = Wasmex.Instance.get_global_value(store, instance, "count_32")
assert {:ok, 99} = Wasmex.Instance.get_global_value(store, instance, "count_32")

assert :ok = Wasmex.Instance.set_global_value(store, instance, "count_64", 17)
assert 17 = Wasmex.Instance.get_global_value(store, instance, "count_64")
assert {:ok, 17} = Wasmex.Instance.get_global_value(store, instance, "count_64")

assert :ok = Wasmex.Instance.set_global_value(store, instance, "bad_pi_32", 3.14)
assert_in_delta 3.14, Wasmex.Instance.get_global_value(store, instance, "bad_pi_32"), 0.01

assert_in_delta 3.14,
elem(Wasmex.Instance.get_global_value(store, instance, "bad_pi_32"), 1),
0.01

assert :ok = Wasmex.Instance.set_global_value(store, instance, "bad_pi_64", 3.14)
assert_in_delta 3.14, Wasmex.Instance.get_global_value(store, instance, "bad_pi_64"), 0.01

assert_in_delta 3.14,
elem(Wasmex.Instance.get_global_value(store, instance, "bad_pi_64"), 1),
0.01
end
end
end

0 comments on commit fbc8b06

Please sign in to comment.