Use anyhow::Error in instantiation errors.

This commit updates the error enums used in instantiation errors to encapsulate
an `anyhow::Error` rather than a string.
This commit is contained in:
Peter Huene
2021-03-08 09:30:13 -08:00
parent 5fa0f8d469
commit 623290d42e
4 changed files with 14 additions and 14 deletions

View File

@@ -133,9 +133,9 @@ impl CompilationArtifacts {
}
let obj = obj.write().map_err(|_| {
SetupError::Instantiate(InstantiationError::Resource(
"failed to create image memory".to_string(),
))
SetupError::Instantiate(InstantiationError::Resource(anyhow::anyhow!(
"failed to create image memory"
)))
})?;
Ok(CompilationArtifacts {
@@ -236,7 +236,7 @@ impl CompiledModule {
&artifacts.unwind_info,
)
.map_err(|message| {
SetupError::Instantiate(InstantiationError::Resource(format!(
SetupError::Instantiate(InstantiationError::Resource(anyhow::anyhow!(
"failed to build code memory for functions: {}",
message
)))

View File

@@ -38,7 +38,9 @@ pub fn make_trampoline(
assert!(compiled_function.relocations.is_empty());
let ptr = code_memory
.allocate_for_function(&compiled_function)
.map_err(|message| SetupError::Instantiate(InstantiationError::Resource(message)))?
.map_err(|message| {
SetupError::Instantiate(InstantiationError::Resource(anyhow::anyhow!(message)))
})?
.as_ptr();
Ok(unsafe { std::mem::transmute::<*const VMFunctionBody, VMTrampoline>(ptr) })
}

View File

@@ -71,7 +71,7 @@ pub struct LinkError(pub String);
pub enum InstantiationError {
/// Insufficient resources available for execution.
#[error("Insufficient resources: {0}")]
Resource(String),
Resource(anyhow::Error),
/// A wasm link error occured.
#[error("Failed to link module")]
@@ -91,7 +91,7 @@ pub enum InstantiationError {
pub enum FiberStackError {
/// Insufficient resources available for the request.
#[error("Insufficient resources: {0}")]
Resource(String),
Resource(anyhow::Error),
/// An error for when the allocator doesn't support custom fiber stacks.
#[error("Custom fiber stacks are not supported by the allocator")]
NotSupported,
@@ -569,10 +569,8 @@ impl OnDemandInstanceAllocator {
let mut memories: PrimaryMap<DefinedMemoryIndex, _> =
PrimaryMap::with_capacity(module.memory_plans.len() - num_imports);
for plan in &module.memory_plans.values().as_slice()[num_imports..] {
memories.push(
Memory::new_dynamic(plan, creator)
.map_err(|e| InstantiationError::Resource(e.to_string()))?,
);
memories
.push(Memory::new_dynamic(plan, creator).map_err(InstantiationError::Resource)?);
}
Ok(memories)
}

View File

@@ -485,7 +485,7 @@ impl InstancePool {
max_pages,
commit_memory_pages,
)
.map_err(|e| InstantiationError::Resource(e.to_string()))?,
.map_err(InstantiationError::Resource)?,
);
}
@@ -509,7 +509,7 @@ impl InstancePool {
let base = tables.next().unwrap();
commit_table_pages(base, max_elements as usize * mem::size_of::<*mut u8>())
.map_err(|e| InstantiationError::Resource(e.to_string()))?;
.map_err(InstantiationError::Resource)?;
instance
.tables
@@ -785,7 +785,7 @@ impl StackPool {
.add((index * self.stack_size) + self.page_size);
commit_stack_pages(bottom_of_stack, size_without_guard)
.map_err(|e| FiberStackError::Resource(e.to_string()))?;
.map_err(FiberStackError::Resource)?;
// The top of the stack should be returned
Ok(bottom_of_stack.add(size_without_guard))