Skip to content

Commit

Permalink
Improve the error messages you get when memory creation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Oct 2, 2023
1 parent 7d8a71d commit 4b38f69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,16 @@ impl Memory {
js_sys::Reflect::set(&descriptor, &"shared".into(), &ty.shared.into()).unwrap();
}

let js_memory = js_sys::WebAssembly::Memory::new(&descriptor)
.map_err(|_e| MemoryError::Generic("Error while creating the memory".to_owned()))?;
let js_memory = js_sys::WebAssembly::Memory::new(&descriptor).map_err(|e| {
let error_message = if let Some(s) = e.as_string() {
s
} else if let Some(obj) = e.dyn_ref::<js_sys::Object>() {
obj.to_string().into()
} else {
"Error while creating the memory".to_string()
};
MemoryError::Generic(error_message)
})?;

Ok(js_memory)
}
Expand Down
16 changes: 13 additions & 3 deletions lib/wasix/src/runtime/task_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,31 @@ pub trait VirtualTaskManager: std::fmt::Debug + Send + Sync + 'static {
SpawnMemoryType::CreateMemoryOfType(mut ty) => {
ty.shared = true;
let mem = Memory::new(&mut store, ty).map_err(|err| {
tracing::error!("could not create memory: {err}");
tracing::error!(
error = &err as &dyn std::error::Error,
memory_type=?ty,
"could not create memory",
);
WasiThreadError::MemoryCreateFailed(err)
})?;
Ok(Some(mem))
}
SpawnMemoryType::ShareMemory(mem, old_store) => {
let mem = mem.share_in_store(&old_store, store).map_err(|err| {
tracing::warn!("could not clone memory: {err}");
tracing::warn!(
error = &err as &dyn std::error::Error,
"could not clone memory",
);
WasiThreadError::MemoryCreateFailed(err)
})?;
Ok(Some(mem))
}
SpawnMemoryType::CopyMemory(mem, old_store) => {
let mem = mem.copy_to_store(&old_store, store).map_err(|err| {
tracing::warn!("could not copy memory: {err}");
tracing::warn!(
error = &err as &dyn std::error::Error,
"could not copy memory",
);
WasiThreadError::MemoryCreateFailed(err)
})?;
Ok(Some(mem))
Expand Down

0 comments on commit 4b38f69

Please sign in to comment.