Skip to content

Commit

Permalink
More informative error message
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Apr 3, 2024
1 parent f651ad4 commit 42f4e3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions native/wasmex/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ pub fn set_global_value(

let global_type = global.ty(&store_or_caller).content().clone();

let new_value = decode_term_as_wasm_value(global_type, new_value)
.ok_or_else(|| rustler::Error::Term(Box::new("Cannot convert to a WebAssembly value.")))?;
let new_value = decode_term_as_wasm_value(global_type.clone(), new_value).ok_or_else(|| {
rustler::Error::Term(Box::new(format!(
"Cannot convert to a WebAssembly {:?} value. Given `{:?}`.",
global_type,
PrintableTermType::PrintTerm(new_value.get_type())
)))
})?;

let val: Val = match new_value {
WasmValue::I32(value) => value.into(),
Expand Down
3 changes: 3 additions & 0 deletions test/wasmex/instance_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ 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 {:error, "Cannot convert to a WebAssembly I32 value. Given `Atom`."} =
Wasmex.Instance.set_global_value(store, instance, "count_32", :abc)

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

Expand Down

0 comments on commit 42f4e3d

Please sign in to comment.