Skip to content

Commit

Permalink
Test more global types
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Apr 3, 2024
1 parent de07ced commit 3923979
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 4 additions & 1 deletion test/example_wasm_files/globals.wat
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(module
(global $meaning_of_life (export "meaning_of_life") i32 (i32.const 42))
(global $count (export "count") (mut i64) (i64.const 0))
(global (export "count_32") (mut i32) (i32.const -32))
(global (export "count_64") (mut i64) (i64.const -64))
(global (export "bad_pi_32") (mut f32) (f32.const 0))
(global (export "bad_pi_64") (mut f64) (f64.const 0))
)
16 changes: 13 additions & 3 deletions test/wasmex/instance_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ defmodule Wasmex.InstanceTest do
Wasmex.Instance.get_global_value(store, instance, "unknown_global")

assert 42 = Wasmex.Instance.get_global_value(store, instance, "meaning_of_life")
assert 0 = Wasmex.Instance.get_global_value(store, instance, "count")
assert -32 = Wasmex.Instance.get_global_value(store, instance, "count_32")
assert -64 = Wasmex.Instance.get_global_value(store, instance, "count_64")
end

test t(&Wasmex.Instance.set_global_value/4), context do
Expand All @@ -226,8 +227,17 @@ defmodule Wasmex.InstanceTest do
assert {:error, "Could not set global: immutable global cannot be set"} =
Wasmex.Instance.set_global_value(store, instance, "meaning_of_life", 0)

assert :ok = Wasmex.Instance.set_global_value(store, instance, "count", 99)
assert 99 = Wasmex.Instance.get_global_value(store, instance, "count")
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 = Wasmex.Instance.set_global_value(store, instance, "count_64", 17)
assert 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 :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
end
end
end

0 comments on commit 3923979

Please sign in to comment.