Skip to content

Commit

Permalink
rust: return error message in instantiate()
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Mar 31, 2021
1 parent 7f900a7 commit 328192b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl Module {
// TODO: support imported functions
pub fn instantiate(self) -> Result<Instance, String> {
debug_assert!(!self.0.is_null());
let mut err = CErrorContainer::new();
let ptr = unsafe {
sys::fizzy_instantiate(
self.0,
Expand All @@ -164,14 +165,16 @@ impl Module {
std::ptr::null(),
std::ptr::null(),
0,
std::ptr::null_mut(),
err.as_mut_ptr(),
)
};
// Forget Module (and avoid calling drop) because it has been consumed by instantiate (even if it failed).
core::mem::forget(self);
if ptr.is_null() {
return Err("instantiation failure".to_string());
debug_assert!(err.code() != 0);
return Err(err.message());
}
debug_assert!(err.code() == 0);
Ok(Instance {
0: unsafe { NonNull::new_unchecked(ptr) },
})
Expand Down Expand Up @@ -723,7 +726,10 @@ mod tests {
let module = parse(&input);
assert!(module.is_ok());
let instance = module.unwrap().instantiate();
assert_eq!(instance.err().unwrap(), "instantiation failure");
assert_eq!(
instance.err().unwrap(),
"module defines an imported memory but none was provided"
);
}

#[test]
Expand Down

0 comments on commit 328192b

Please sign in to comment.